Browse Source

修改参数结构

master v0.2.0
guzeng 3 years ago
parent
commit
fbadc1db3e
8 changed files with 193 additions and 62 deletions
  1. +49
    -0
      client.info.go
  2. +16
    -0
      client.info_test.go
  3. +1
    -0
      conn.go
  4. +76
    -0
      data.go
  5. +11
    -0
      go.mod
  6. +20
    -0
      go.sum
  7. +14
    -49
      supplier.pb.go
  8. +6
    -13
      supplier.proto

+ 49
- 0
client.info.go View File

@ -1,5 +1,10 @@
package supplierrpc package supplierrpc
import (
"encoding/json"
"errors"
)
type GetParam struct { type GetParam struct {
SiteId string `json:"site_id"` SiteId string `json:"site_id"`
Dbname string `json:"database"` Dbname string `json:"database"`
@ -11,3 +16,47 @@ type GetUuidParam struct {
Dbname string `json:"database"` Dbname string `json:"database"`
Uuid string `json:"uuid"` 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
}

+ 16
- 0
client.info_test.go View File

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

+ 1
- 0
conn.go View File

@ -23,4 +23,5 @@ func rpc_server_conn(url ...string) (*SupplierServiceClient, error) {
} }
return conn, nil return conn, nil
} }

+ 76
- 0
data.go View File

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

+ 11
- 0
go.mod View File

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

+ 20
- 0
go.sum View File

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

+ 14
- 49
supplier.pb.go View File

@ -9,8 +9,7 @@ It is generated from these files:
supplier.proto supplier.proto
It has these top-level messages: It has these top-level messages:
Request
Response
Params
*/ */
package supplierrpc package supplierrpc
@ -28,66 +27,32 @@ import protorpc "github.com/chai2010/protorpc"
var _ = proto.Marshal var _ = proto.Marshal
var _ = math.Inf var _ = math.Inf
// 配置信息请求结构
type Request struct {
type Params struct {
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"`
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"`
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"`
XXX_unrecognized []byte `json:"-"` 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 { if m != nil && m.Data != nil {
return *m.Data return *m.Data
} }
return "" return ""
} }
func (m *Request) GetTime() string {
func (m *Params) GetTime() string {
if m != nil && m.Time != nil { if m != nil && m.Time != nil {
return *m.Time return *m.Time
} }
return "" 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 { if m != nil && m.Sign != nil {
return *m.Sign return *m.Sign
} }
@ -98,9 +63,9 @@ func init() {
} }
type SupplierService interface { 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 // AcceptSupplierServiceClient accepts connections on the listener and serves requests
@ -172,13 +137,13 @@ func NewSupplierServiceClient(conn io.ReadWriteCloser) (*SupplierServiceClient,
return &SupplierServiceClient{c}, c 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) 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) 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) return c.Call("SupplierService.IsOpen", in, out)
} }


+ 6
- 13
supplier.proto View File

@ -1,23 +1,16 @@
syntax = "proto3"; syntax = "proto3";
package supplierrpc; package supplierrpc;
//
message Request {
string data = 1;
string time = 2;
string sign = 3;
}
//
message Response {
message Params {
string data = 1; string data = 1;
string time = 2;
string time = 2;
string sign = 3; string sign = 3;
} }
// rpc方法 // rpc方法
service SupplierService { 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); //
} }

Loading…
Cancel
Save