Browse Source

增加GetActivity方法

master v0.5.0
guzeng 2 years ago
parent
commit
5184632ae6
4 changed files with 232 additions and 0 deletions
  1. +108
    -0
      data.go
  2. +72
    -0
      product.pb.go
  3. +15
    -0
      product.proto
  4. +37
    -0
      sign.go

+ 108
- 0
data.go View File

@ -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
}

+ 72
- 0
product.pb.go View File

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


+ 15
- 0
product.proto View File

@ -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); //
}

+ 37
- 0
sign.go View File

@ -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
}

Loading…
Cancel
Save