From 5184632ae6974931b454698104a8a2a929c3e048 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 6 Oct 2021 17:43:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0GetActivity=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.go | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ product.pb.go | 72 +++++++++++++++++++++++++++++++++ product.proto | 15 +++++++ sign.go | 37 +++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 data.go create mode 100644 sign.go diff --git a/data.go b/data.go new file mode 100644 index 0000000..10d27ad --- /dev/null +++ b/data.go @@ -0,0 +1,108 @@ +package productrpc + +import ( + "encoding/json" + "errors" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + "github.com/golang/protobuf/proto" +) + +var DES_KEY = "pro%78gd" + +func SetResData(data interface{}, res *Response) { + res_data_json, err := json.Marshal(data) + if err == nil { + + encryData := crypter.DesEn(string(res_data_json), DES_KEY) + + now_str := strconv.FormatInt(time.Now().Unix(), 10) + + res_sign := Sign(encryData, now_str) + + res.Data = proto.String(encryData) + res.Time = proto.String(now_str) + res.Sign = proto.String(res_sign) + } + +} + +func SetReqData(arg interface{}) (*Request, error) { + data_json, err := json.Marshal(arg) + if err != nil { + return nil, err + } + + now_int64 := time.Now().Unix() + + encryData := crypter.DesEn(string(data_json), DES_KEY) + + now := strconv.FormatInt(now_int64, 10) + + sign := Sign(encryData, now) + + return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil + +} + +func GetReqData(req *Request) (string, error) { + res_data := req.GetData() + + if res_data != "" { + + time_int64, err := strconv.ParseInt(req.GetTime(), 10, 64) + if err != nil { + return "", err + } + + now_int64 := time.Now().Unix() + + if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { + //时间误差前后10秒,返回 + return "", errors.New("返回时间错误") + } + + check_sign := CheckSign(req.GetSign(), res_data, req.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + return crypter.DesDe(res_data, DES_KEY), nil + + } + + return "", nil +} + +func GetResData(res *Response) (string, error) { + res_data := res.GetData() + + if res_data != "" { + + time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) + if err != nil { + return "", err + } + + now_int64 := time.Now().Unix() + + if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { + //时间误差前后10秒,返回 + return "", errors.New("返回时间错误") + } + + check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + return crypter.DesDe(res_data, DES_KEY), nil + + } + + return "", nil +} diff --git a/product.pb.go b/product.pb.go index caf5275..4431d3e 100644 --- a/product.pb.go +++ b/product.pb.go @@ -12,6 +12,8 @@ It has these top-level messages: GetRequest GetUuidRequest GetResponse + Request + Response */ package productrpc @@ -127,6 +129,72 @@ func (m *GetResponse) GetValue() []byte { return nil } +// 配置信息请求结构 +type Request struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} + +func (m *Request) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Request) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Request) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +// 配置信息响应结构 +type Response struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} + +func (m *Response) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Response) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Response) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + func init() { } @@ -135,6 +203,7 @@ type ProductService interface { GetByUuid(in *GetUuidRequest, out *GetResponse) error GetSku(in *GetRequest, out *GetResponse) error GetSkuByUuid(in *GetUuidRequest, out *GetResponse) error + GetActivity(in *Request, out *Response) error } // AcceptProductServiceClient accepts connections on the listener and serves requests @@ -218,6 +287,9 @@ func (c *ProductServiceClient) GetSku(in *GetRequest, out *GetResponse) error { func (c *ProductServiceClient) GetSkuByUuid(in *GetUuidRequest, out *GetResponse) error { return c.Call("ProductService.GetSkuByUuid", in, out) } +func (c *ProductServiceClient) GetActivity(in *Request, out *Response) error { + return c.Call("ProductService.GetActivity", in, out) +} // DialProductService connects to an ProductService at the specified network address. func DialProductService(network, addr string) (*ProductServiceClient, *rpc.Client, error) { diff --git a/product.proto b/product.proto index 049e39d..d010609 100644 --- a/product.proto +++ b/product.proto @@ -22,10 +22,25 @@ message GetResponse { } +// 配置信息请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 配置信息响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + // rpc方法 service ProductService { rpc Get (GetRequest) returns (GetResponse); // 使用id查询 rpc GetByUuid (GetUuidRequest) returns (GetResponse); // 使用uuid查询 rpc GetSku (GetRequest) returns (GetResponse); // 使用id查询 rpc GetSkuByUuid (GetUuidRequest) returns (GetResponse); // 使用uuid查询 + rpc GetActivity(Request) returns (Response); //查询商品所在的活动 } \ No newline at end of file diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..396d04e --- /dev/null +++ b/sign.go @@ -0,0 +1,37 @@ +package productrpc + +import ( + "crypto/md5" + "encoding/hex" + "strings" +) + +/** + * 签名 + */ +func Sign(data string, salt string) string { + + var build strings.Builder + + build.WriteString(data) + build.WriteString(salt) + build.WriteString("sup334signlier") + + data_str := build.String() + + h := md5.New() + h.Write([]byte(data_str)) // 需要加密的字符串 + return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 + +} + +/** + * 验证签名 + */ +func CheckSign(sign_str, data, salt string) bool { + sign := Sign(data, salt) + if strings.Compare(sign_str, sign) > -1 { + return true + } + return false +}