From fbadc1db3ef2ed02e9e883ccdd3d1d996b057301 Mon Sep 17 00:00:00 2001 From: guzeng Date: Sat, 21 Aug 2021 13:52:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.info.go | 49 +++++++++++++++++++++++++++++ client.info_test.go | 16 ++++++++++ conn.go | 1 + data.go | 76 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 11 +++++++ go.sum | 20 ++++++++++++ supplier.pb.go | 63 +++++++++---------------------------- supplier.proto | 19 ++++-------- 8 files changed, 193 insertions(+), 62 deletions(-) create mode 100644 client.info_test.go create mode 100644 data.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/client.info.go b/client.info.go index d834d93..3242191 100644 --- a/client.info.go +++ b/client.info.go @@ -1,5 +1,10 @@ package supplierrpc +import ( + "encoding/json" + "errors" +) + type GetParam struct { SiteId string `json:"site_id"` Dbname string `json:"database"` @@ -11,3 +16,47 @@ type GetUuidParam struct { Dbname string `json:"database"` Uuid string `json:"uuid"` } + +func Get(site_id, dbname, id string, url ...string) (map[string]string, error) { + + if dbname == "" || id == "" { + return nil, errors.New("参数错误") + } + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + arg := GetParam{site_id, dbname, id} + + req, err := SetReqData(arg) + if err != nil { + return nil, err + } + res := &Params{} + + err = conn.Get(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + return res_arr, nil + +} diff --git a/client.info_test.go b/client.info_test.go new file mode 100644 index 0000000..8f8910d --- /dev/null +++ b/client.info_test.go @@ -0,0 +1,16 @@ +package supplierrpc + +import ( + "testing" +) + +func Test_Get(t *testing.T) { + siteid := "1056475" + dbname := "shop_v2" + key := "1" + + res, err := Get(siteid, dbname, key) + t.Log(res) + t.Log(err) + +} diff --git a/conn.go b/conn.go index 7adb553..868bfb6 100644 --- a/conn.go +++ b/conn.go @@ -23,4 +23,5 @@ func rpc_server_conn(url ...string) (*SupplierServiceClient, error) { } return conn, nil + } diff --git a/data.go b/data.go new file mode 100644 index 0000000..d17071f --- /dev/null +++ b/data.go @@ -0,0 +1,76 @@ +package supplierrpc + +import ( + "encoding/json" + "errors" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + "github.com/golang/protobuf/proto" +) + +func SetResData(data interface{}, res *Params) { + res_data_json, err := json.Marshal(data) + if err == nil { + + encryData := crypter.DesEn(string(res_data_json), supplierrpc.DES_KEY) + + now_str := strconv.FormatInt(time.Now().Unix(), 10) + + res_sign := supplierrpc.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{}) (*Params, 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 &Params{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil + +} + +func GetData(param *Params) (string, error) { + res_data := param.GetData() + + if res_data != "" { + + time_int64, err := strconv.ParseInt(param.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(param.GetSign(), res_data, param.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + return crypter.DesDe(res_data, DES_KEY), nil + + } + + return "", nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2c59307 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.tetele.net/tgo/supplierrpc + +go 1.14 + +require ( + git.tetele.net/tgo/conf v0.34.1 // indirect + git.tetele.net/tgo/crypter v0.2.2 // indirect + github.com/chai2010/protorpc v1.1.3 // indirect + golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d50ff30 --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +git.tetele.net/tgo/conf v0.34.1 h1:oljFv8stYgfEcC04lf5BPtVSVQNMBoLScI+EXfq7mVI= +git.tetele.net/tgo/conf v0.34.1/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= +git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= +git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= +github.com/chai2010/protorpc v1.1.3 h1:VJK5hIoZn0XCGol0GmbxZkUG6FbTI5LP2Lam6RVd15w= +github.com/chai2010/protorpc v1.1.3/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= +github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ= +github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/supplier.pb.go b/supplier.pb.go index c3f89c8..746ec69 100644 --- a/supplier.pb.go +++ b/supplier.pb.go @@ -9,8 +9,7 @@ It is generated from these files: supplier.proto It has these top-level messages: - Request - Response + Params */ package supplierrpc @@ -28,66 +27,32 @@ import protorpc "github.com/chai2010/protorpc" var _ = proto.Marshal var _ = math.Inf -// 配置信息请求结构 -type Request struct { +type Params 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 *Params) Reset() { *m = Params{} } +func (m *Params) String() string { return proto.CompactTextString(m) } +func (*Params) ProtoMessage() {} -func (m *Request) GetData() string { +func (m *Params) GetData() string { if m != nil && m.Data != nil { return *m.Data } return "" } -func (m *Request) GetTime() string { +func (m *Params) 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 { +func (m *Params) GetSign() string { if m != nil && m.Sign != nil { return *m.Sign } @@ -98,9 +63,9 @@ func init() { } type SupplierService interface { - Get(in *Request, out *Response) error - GetByUuid(in *Request, out *Response) error - IsOpen(in *Request, out *Response) error + Get(in *Params, out *Params) error + GetByUuid(in *Params, out *Params) error + IsOpen(in *Params, out *Params) error } // AcceptSupplierServiceClient accepts connections on the listener and serves requests @@ -172,13 +137,13 @@ func NewSupplierServiceClient(conn io.ReadWriteCloser) (*SupplierServiceClient, return &SupplierServiceClient{c}, c } -func (c *SupplierServiceClient) Get(in *Request, out *Response) error { +func (c *SupplierServiceClient) Get(in *Params, out *Params) error { return c.Call("SupplierService.Get", in, out) } -func (c *SupplierServiceClient) GetByUuid(in *Request, out *Response) error { +func (c *SupplierServiceClient) GetByUuid(in *Params, out *Params) error { return c.Call("SupplierService.GetByUuid", in, out) } -func (c *SupplierServiceClient) IsOpen(in *Request, out *Response) error { +func (c *SupplierServiceClient) IsOpen(in *Params, out *Params) error { return c.Call("SupplierService.IsOpen", in, out) } diff --git a/supplier.proto b/supplier.proto index 6714836..fae1b9c 100644 --- a/supplier.proto +++ b/supplier.proto @@ -1,23 +1,16 @@ syntax = "proto3"; package supplierrpc; -// 配置信息请求结构 -message Request { - string data = 1; - string time = 2; - string sign = 3; -} -// 配置信息响应结构 -message Response { + +message Params { string data = 1; - string time = 2; + string time = 2; string sign = 3; } - // rpc方法 service SupplierService { - rpc Get (Request) returns (Response); // 使用id查询 - rpc GetByUuid (Request) returns (Response); // 使用uuid查询 - rpc IsOpen (Request) returns (Response); // 是否营业中 + rpc Get (Params) returns (Params); // 使用id查询 + rpc GetByUuid (Params) returns (Params); // 使用uuid查询 + rpc IsOpen (Params) returns (Params); // 是否营业中 } \ No newline at end of file