This commit is contained in:
david 2024-02-21 20:58:37 +08:00
commit 7575423cff
7 changed files with 54 additions and 77 deletions

View File

@ -19,6 +19,8 @@ func parseArgs(cmd string) {
switch cmd { switch cmd {
case "-v", "--v", "-version", "--version": case "-v", "--v", "-version", "--version":
versionCmd() versionCmd()
case "--json":
versionCmdJson()
} }
os.Exit(0) 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] 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) 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)
}

View File

@ -28,6 +28,9 @@ func New() {
} }
func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) { 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) expireTime := time.Now().Add(vars.JwtExpireDay)
claims := types.JwtClaims{ claims := types.JwtClaims{
ID: id, ID: id,
@ -51,9 +54,7 @@ func GenerateTokenAes(id uint, identity, client, role string, extend map[string]
} }
func AesEncryptCBC(plan []byte) (string, error) { 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 // NewCipher该函数限制了输入k的长度必须为16, 24或者32
block, _ := aes.NewCipher(JwtSecret) block, _ := aes.NewCipher(JwtSecret)
@ -121,6 +122,10 @@ func PKCS7UnPadding(origData []byte, blocksize int) []byte {
length := len(origData) length := len(origData)
unpadding := int(origData[length-1]) unpadding := int(origData[length-1])
if length-unpadding <= 0 {
return nil
}
return origData[:(length - unpadding)] return origData[:(length - unpadding)]
} }

View File

@ -2,13 +2,13 @@ package exception
// jwt custom error code ,begin:200 // jwt custom error code ,begin:200
var ( var (
ErrAuthPasswd = ErrorJson(201, "Password Error") ErrAuthPasswd = Errorf(201, "Password Error")
ErrAuthNotFound = ErrorJson(202, "Auth Token Not Found") ErrAuthNotFound = Errorf(202, "Auth Token Not Found")
ErrAuthParseFail = ErrorJson(203, "Auth Parse Fail") ErrAuthParseFail = Errorf(203, "Auth Parse Fail")
ErrAuthId = ErrorJson(204, "Auth Id Not Passed") ErrAuthId = Errorf(204, "Auth Id Not Passed")
ErrAuthIdentity = ErrorJson(205, "Auth Identity Not Passed") ErrAuthIdentity = Errorf(205, "Auth Identity Not Passed")
ErrAuthTokenChanged = ErrorJson(206, "Auth Token Changed") ErrAuthTokenChanged = Errorf(206, "Auth Token Changed")
ErrAuthIdType = ErrorJson(207, "Auth Id Type Error") ErrAuthIdType = Errorf(207, "Auth Id Type Error")
ErrAuthExpire = ErrorJson(208, "Auth Token Expire") ErrAuthExpire = Errorf(208, "Auth Token Expire")
ErrAuthClient = ErrorJson(208, "Auth Token Client Not Passed") ErrAuthClient = Errorf(208, "Auth Token Client Not Passed")
) )

View File

@ -2,9 +2,9 @@ package exception
// db custom error code ,begin:300 // db custom error code ,begin:300
var ( var (
ErrDBFatal = ErrorJson(300, "DB Fatal error") ErrDBFatal = Errorf(300, "DB Fatal error")
ErrCacheFatal = ErrorJson(301, "Cache Fatal error") ErrCacheFatal = Errorf(301, "Cache Fatal error")
ErrEtcdFatal = ErrorJson(302, "Etcd Fatal error") ErrEtcdFatal = Errorf(302, "Etcd Fatal error")
ErrElasticFatal = ErrorJson(303, "Elastic Fatal error") ErrElasticFatal = Errorf(303, "Elastic Fatal error")
ErrBlocksMQFatal = ErrorJson(304, "BlocksMQ Fatal error") ErrBlocksMQFatal = Errorf(304, "BlocksMQ Fatal error")
) )

View File

@ -9,27 +9,27 @@ import (
var ( var (
// google grpc error status. // google grpc error status.
OK = ErrorJson(0, "OK") OK = Errorf(0, "OK")
ErrCanceled = ErrorJson(1, "Canceled") ErrCanceled = Errorf(1, "Canceled")
ErrUnknown = ErrorJson(2, "Unknown") ErrUnknown = Errorf(2, "Unknown")
ErrInvalidArgument = ErrorJson(3, "Invalid Argument") ErrInvalidArgument = Errorf(3, "Invalid Argument")
ErrDeadlineExceeded = ErrorJson(4, "Deadline Exceeded") ErrDeadlineExceeded = Errorf(4, "Deadline Exceeded")
ErrNotFound = ErrorJson(5, "Not Found") ErrNotFound = Errorf(5, "Not Found")
ErrAlreadyExists = ErrorJson(6, "Already Exists") ErrAlreadyExists = Errorf(6, "Already Exists")
ErrPermissionDenied = ErrorJson(7, "Permission Denied") ErrPermissionDenied = Errorf(7, "Permission Denied")
ErrResourceExhausted = ErrorJson(8, "Resource Exhausted") ErrResourceExhausted = Errorf(8, "Resource Exhausted")
ErrFailedPrecondition = ErrorJson(9, "Failed Precondition") ErrFailedPrecondition = Errorf(9, "Failed Precondition")
ErrAborted = ErrorJson(10, "Aborted") ErrAborted = Errorf(10, "Aborted")
ErrOutOfRange = ErrorJson(11, "Out Of Range") ErrOutOfRange = Errorf(11, "Out Of Range")
ErrUnimplemented = ErrorJson(12, "Unimplemented") ErrUnimplemented = Errorf(12, "Unimplemented")
ErrInternal = ErrorJson(13, "Internal") ErrInternal = Errorf(13, "Internal")
ErrUnavailable = ErrorJson(14, "Unavailable") ErrUnavailable = Errorf(14, "Unavailable")
ErrDataLoss = ErrorJson(15, "Data Loss") ErrDataLoss = Errorf(15, "Data Loss")
ErrUnauthenticated = ErrorJson(16, "Unauthenticated") ErrUnauthenticated = Errorf(16, "Unauthenticated")
ErrJSONMarshal = ErrorJson(17, "Marshal JSON") ErrJSONMarshal = Errorf(17, "Marshal JSON")
ErrJSONUnmarshal = ErrorJson(18, "Unmarshal JSON") ErrJSONUnmarshal = Errorf(18, "Unmarshal JSON")
ErrSmsCode = ErrorJson(20, "SMS Code Invalid") ErrSmsCode = Errorf(20, "SMS Code Invalid")
// coustom error status // coustom error status
) )
@ -41,7 +41,3 @@ func Error(c uint32, msg string) error {
func Errorf(c uint32, format string, a ...interface{}) error { func Errorf(c uint32, format string, a ...interface{}) error {
return status.New(codes.Code(c), fmt.Sprintf(format, a...)).Err() 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()
}

View File

@ -2,10 +2,10 @@ package exception
// mq custom error code ,begin:900 // mq custom error code ,begin:900
var ( var (
ErrMQClient = ErrorJson(900, "MQ Connect Error") ErrMQClient = Errorf(900, "MQ Connect Error")
ErrMQDispatch = ErrorJson(901, "MQ Dispatch Error") ErrMQDispatch = Errorf(901, "MQ Dispatch Error")
ErrMQProducer = ErrorJson(902, "MQ Producer Error") ErrMQProducer = Errorf(902, "MQ Producer Error")
ErrMQConsumer = ErrorJson(902, "MQ Consumer Error") ErrMQConsumer = Errorf(902, "MQ Consumer Error")
ErrMQInternal = ErrorJson(903, "MQ Internal Error") ErrMQInternal = Errorf(903, "MQ Internal Error")
ErrMQStorage = ErrorJson(904, "MQ Storage Error") ErrMQStorage = Errorf(904, "MQ Storage Error")
) )

View File

@ -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")
)