diff --git a/cmd/service.go b/cmd/service.go index aedbd13..45d08ec 100644 --- a/cmd/service.go +++ b/cmd/service.go @@ -19,6 +19,8 @@ func parseArgs(cmd string) { switch cmd { case "-v", "--v", "-version", "--version": versionCmd() + case "--json": + versionCmdJson() } os.Exit(0) @@ -28,3 +30,7 @@ func versionCmd() { fmt.Printf("[Blocks Service: %s] Version: %s \n", vars.ServiceKey, vars.VERSION) fmt.Printf("[Blocks Service: %s] Compile: %s by %s build.\n", vars.ServiceKey, vars.GO_VERSION, vars.BUILD_TIME) } + +func versionCmdJson() { + fmt.Printf("{\"version\":\"%s\",\"build_time\":\"%s\"}\n", vars.VERSION, vars.BUILD_TIME) +} diff --git a/encipher/aes.go b/encipher/aes.go index e8ff8f5..de87055 100644 --- a/encipher/aes.go +++ b/encipher/aes.go @@ -28,6 +28,9 @@ func New() { } func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) { + if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false { + return "", errors.New("JwtSecret lenght must 16/24/32.") + } expireTime := time.Now().Add(vars.JwtExpireDay) claims := types.JwtClaims{ ID: id, @@ -51,9 +54,7 @@ func GenerateTokenAes(id uint, identity, client, role string, extend map[string] } func AesEncryptCBC(plan []byte) (string, error) { - if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false { - return "", errors.New("JwtSecret lenght must 16/24/32.") - } + // 分组秘钥 // NewCipher该函数限制了输入k的长度必须为16, 24或者32 block, _ := aes.NewCipher(JwtSecret) @@ -121,6 +122,10 @@ func PKCS7UnPadding(origData []byte, blocksize int) []byte { length := len(origData) unpadding := int(origData[length-1]) + + if length-unpadding <= 0 { + return nil + } return origData[:(length - unpadding)] } diff --git a/exception/auth.go b/exception/auth.go index c81a0bc..2e0cfe9 100644 --- a/exception/auth.go +++ b/exception/auth.go @@ -2,13 +2,13 @@ package exception // jwt custom error code ,begin:200 var ( - ErrAuthPasswd = ErrorJson(201, "Password Error") - ErrAuthNotFound = ErrorJson(202, "Auth Token Not Found") - ErrAuthParseFail = ErrorJson(203, "Auth Parse Fail") - ErrAuthId = ErrorJson(204, "Auth Id Not Passed") - ErrAuthIdentity = ErrorJson(205, "Auth Identity Not Passed") - ErrAuthTokenChanged = ErrorJson(206, "Auth Token Changed") - ErrAuthIdType = ErrorJson(207, "Auth Id Type Error") - ErrAuthExpire = ErrorJson(208, "Auth Token Expire") - ErrAuthClient = ErrorJson(208, "Auth Token Client Not Passed") + ErrAuthPasswd = Errorf(201, "Password Error") + ErrAuthNotFound = Errorf(202, "Auth Token Not Found") + ErrAuthParseFail = Errorf(203, "Auth Parse Fail") + ErrAuthId = Errorf(204, "Auth Id Not Passed") + ErrAuthIdentity = Errorf(205, "Auth Identity Not Passed") + ErrAuthTokenChanged = Errorf(206, "Auth Token Changed") + ErrAuthIdType = Errorf(207, "Auth Id Type Error") + ErrAuthExpire = Errorf(208, "Auth Token Expire") + ErrAuthClient = Errorf(208, "Auth Token Client Not Passed") ) diff --git a/exception/db.go b/exception/db.go index b949eb9..f8dcaf1 100644 --- a/exception/db.go +++ b/exception/db.go @@ -2,9 +2,9 @@ package exception // db custom error code ,begin:300 var ( - ErrDBFatal = ErrorJson(300, "DB Fatal error") - ErrCacheFatal = ErrorJson(301, "Cache Fatal error") - ErrEtcdFatal = ErrorJson(302, "Etcd Fatal error") - ErrElasticFatal = ErrorJson(303, "Elastic Fatal error") - ErrBlocksMQFatal = ErrorJson(304, "BlocksMQ Fatal error") + ErrDBFatal = Errorf(300, "DB Fatal error") + ErrCacheFatal = Errorf(301, "Cache Fatal error") + ErrEtcdFatal = Errorf(302, "Etcd Fatal error") + ErrElasticFatal = Errorf(303, "Elastic Fatal error") + ErrBlocksMQFatal = Errorf(304, "BlocksMQ Fatal error") ) diff --git a/exception/errors.go b/exception/errors.go index a73de9a..98ff4b0 100644 --- a/exception/errors.go +++ b/exception/errors.go @@ -9,27 +9,27 @@ import ( var ( // google grpc error status. - OK = ErrorJson(0, "OK") - ErrCanceled = ErrorJson(1, "Canceled") - ErrUnknown = ErrorJson(2, "Unknown") - ErrInvalidArgument = ErrorJson(3, "Invalid Argument") - ErrDeadlineExceeded = ErrorJson(4, "Deadline Exceeded") - ErrNotFound = ErrorJson(5, "Not Found") - ErrAlreadyExists = ErrorJson(6, "Already Exists") - ErrPermissionDenied = ErrorJson(7, "Permission Denied") - ErrResourceExhausted = ErrorJson(8, "Resource Exhausted") - ErrFailedPrecondition = ErrorJson(9, "Failed Precondition") - ErrAborted = ErrorJson(10, "Aborted") - ErrOutOfRange = ErrorJson(11, "Out Of Range") - ErrUnimplemented = ErrorJson(12, "Unimplemented") - ErrInternal = ErrorJson(13, "Internal") - ErrUnavailable = ErrorJson(14, "Unavailable") - ErrDataLoss = ErrorJson(15, "Data Loss") - ErrUnauthenticated = ErrorJson(16, "Unauthenticated") - ErrJSONMarshal = ErrorJson(17, "Marshal JSON") - ErrJSONUnmarshal = ErrorJson(18, "Unmarshal JSON") + OK = Errorf(0, "OK") + ErrCanceled = Errorf(1, "Canceled") + ErrUnknown = Errorf(2, "Unknown") + ErrInvalidArgument = Errorf(3, "Invalid Argument") + ErrDeadlineExceeded = Errorf(4, "Deadline Exceeded") + ErrNotFound = Errorf(5, "Not Found") + ErrAlreadyExists = Errorf(6, "Already Exists") + ErrPermissionDenied = Errorf(7, "Permission Denied") + ErrResourceExhausted = Errorf(8, "Resource Exhausted") + ErrFailedPrecondition = Errorf(9, "Failed Precondition") + ErrAborted = Errorf(10, "Aborted") + ErrOutOfRange = Errorf(11, "Out Of Range") + ErrUnimplemented = Errorf(12, "Unimplemented") + ErrInternal = Errorf(13, "Internal") + ErrUnavailable = Errorf(14, "Unavailable") + ErrDataLoss = Errorf(15, "Data Loss") + ErrUnauthenticated = Errorf(16, "Unauthenticated") + ErrJSONMarshal = Errorf(17, "Marshal JSON") + ErrJSONUnmarshal = Errorf(18, "Unmarshal JSON") - ErrSmsCode = ErrorJson(20, "SMS Code Invalid") + ErrSmsCode = Errorf(20, "SMS Code Invalid") // coustom error status ) @@ -41,7 +41,3 @@ func Error(c uint32, msg string) error { func Errorf(c uint32, format string, a ...interface{}) error { return status.New(codes.Code(c), fmt.Sprintf(format, a...)).Err() } - -func ErrorJson(c uint32, msg string) error { - return status.New(codes.Code(c), fmt.Sprintf(`{ "Code": %d, "Msg": "%s" }`, c, msg)).Err() -} diff --git a/exception/mq.go b/exception/mq.go index 76b87b3..981b2d6 100644 --- a/exception/mq.go +++ b/exception/mq.go @@ -2,10 +2,10 @@ package exception // mq custom error code ,begin:900 var ( - ErrMQClient = ErrorJson(900, "MQ Connect Error") - ErrMQDispatch = ErrorJson(901, "MQ Dispatch Error") - ErrMQProducer = ErrorJson(902, "MQ Producer Error") - ErrMQConsumer = ErrorJson(902, "MQ Consumer Error") - ErrMQInternal = ErrorJson(903, "MQ Internal Error") - ErrMQStorage = ErrorJson(904, "MQ Storage Error") + ErrMQClient = Errorf(900, "MQ Connect Error") + ErrMQDispatch = Errorf(901, "MQ Dispatch Error") + ErrMQProducer = Errorf(902, "MQ Producer Error") + ErrMQConsumer = Errorf(902, "MQ Consumer Error") + ErrMQInternal = Errorf(903, "MQ Internal Error") + ErrMQStorage = Errorf(904, "MQ Storage Error") ) diff --git a/exception/work.go b/exception/work.go deleted file mode 100644 index a784e55..0000000 --- a/exception/work.go +++ /dev/null @@ -1,30 +0,0 @@ -package exception - -// mesh custom error code ,begin:100 -var ( - // proxy - ErrWorkerServiceNotFound = ErrorJson(100, "Service Node Not Started") - ErrWorkerServerIsNil = ErrorJson(101, "Service Is Nil") - ErrWorkerMethodNotFound = ErrorJson(102, "Service Method Not Found") - ErrWorkerRequestContent = ErrorJson(103, "Worker Parse Request Content") - ErrWorkerRequestParams = ErrorJson(104, "Worker Parse Request Params") - - // header - ErrInvalidHeaderParams = ErrorJson(105, "Invalid Header Params") - - // grpc getway - ErrWorkerFailedProxy = ErrorJson(107, "Worker gRPC proxying should never reach this stage") - ErrWorkerGrpcProxyShould = ErrorJson(108, "Worker gRPC proxying should never reach this stage") - ErrWorkerServerStreamNotFound = ErrorJson(109, "Worker lowLevelServerStream not exists in context") - - // http getway - ErrWorkerHttpReadAll = ErrorJson(110, "Worker Http Read All") - ErrWorkerHttpResolveService = ErrorJson(111, "Worker Http Resolve Service") - ErrWorkerHttpMarshalJSON = ErrorJson(112, "Worker Http Parameter Must JSON") - ErrWorkerHttpUnmarshalJSON = ErrorJson(113, "Worker Http Return Not JSON") - ErrWorkerHttpReflectInvokeRpc = ErrorJson(114, "Worker Http Reflect InvokeRpc") - ErrWorkerHttpReflectAsDynamicMessage = ErrorJson(115, "Worker Http Reflect AsDynamicMessage") - - // invok - ErrServiceInvok = ErrorJson(116, "Service Invok") -)