15 Commits

Author SHA1 Message Date
  guzeng 639c701dec 修改查询条件 3 years ago
  guzeng 7077ff0fff 查询方法增加查询字段 3 years ago
  guzeng 0cbfce0db4 增加Get380Star方法 3 years ago
  guzeng 1838e087aa 修改调用方法 3 years ago
  guzeng 95ea9b3433 debug 3 years ago
  guzeng dfdf026195 变量集中单个文件 3 years ago
  guzeng bf3dace594 debug 3 years ago
  guzeng 61438902bd 修改调用方法 3 years ago
  guzeng accc30588b 修改调用方法 3 years ago
  guzeng ee60593cbc 修改调用方法 3 years ago
  guzeng fbadc1db3e 修改参数结构 3 years ago
  guzeng e1af7adec7 修改package 3 years ago
  guzeng 47a45bd722 增加结构体 3 years ago
  guzeng 4f833cf133 引入加密 3 years ago
  guzeng a5838b9762 修改方法名 3 years ago
12 changed files with 499 additions and 116 deletions
Split View
  1. +94
    -0
      client.info.go
  2. +16
    -0
      client.info_test.go
  3. +50
    -0
      client.is_open.go
  4. +17
    -0
      client.is_open_test.go
  5. +25
    -0
      conn.go
  6. +106
    -0
      data.go
  7. +12
    -0
      go.mod
  8. +20
    -0
      go.sum
  9. +37
    -0
      sign.go
  10. +80
    -95
      supplier.pb.go
  11. +15
    -21
      supplier.proto
  12. +27
    -0
      variable.go

+ 94
- 0
client.info.go View File

@ -0,0 +1,94 @@
package supplierrpc
import (
"encoding/json"
"errors"
)
func Get(site_id, dbname, id string, field 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 := ReqParam{site_id, dbname, id, field}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.Get(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(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
}
func Get380Star(site_id, dbname string, field string, url ...string) (map[string]string, error) {
if dbname == "" {
return nil, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg := ReqParam{site_id, dbname, "", field}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.Get380Star(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(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 := "*"
res, err := Get380Star(siteid, dbname, key)
t.Log(res)
t.Log(err)
}

+ 50
- 0
client.is_open.go View File

@ -0,0 +1,50 @@
package supplierrpc
import (
"encoding/json"
"errors"
)
func IsOpen(site_id, dbname, id string, url ...string) (*BoolRet, 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 := &Response{}
err = conn.IsOpen(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(res)
if err != nil {
return nil, err
}
if res_data_de == "" {
return nil, nil
}
var res_arr BoolRet
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}

+ 17
- 0
client.is_open_test.go View File

@ -0,0 +1,17 @@
package supplierrpc
import (
"testing"
)
func Test_IsOpen(t *testing.T) {
siteid := "1056475"
dbname := "shop_v2"
key := "1"
res, err := IsOpen(siteid, dbname, key)
t.Log(res)
t.Log(res.Value)
t.Log(err)
}

+ 25
- 0
conn.go View File

@ -0,0 +1,25 @@
package supplierrpc
import (
"git.tetele.net/tgo/conf"
)
func rpc_server_conn(url ...string) (*SupplierServiceClient, error) {
var rpc_url string
if len(url) > 0 && url[0] != "" {
rpc_url = url[0]
} else if conf.SUPPLIER_RPC_URL != "" {
rpc_url = conf.SUPPLIER_RPC_URL
} else {
rpc_url = "127.0.0.1:" + conf.SUPPLIER_RPC_PORT
}
conn, _, err := DialSupplierService("tcp", rpc_url)
if err != nil {
return nil, err
}
return conn, nil
}

+ 106
- 0
data.go View File

@ -0,0 +1,106 @@
package supplierrpc
import (
"encoding/json"
"errors"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
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
}

+ 12
- 0
go.mod View File

@ -0,0 +1,12 @@
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
github.com/chai2010/protorpc v1.1.3
github.com/golang/protobuf v1.0.0
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=

+ 37
- 0
sign.go View File

@ -0,0 +1,37 @@
package supplierrpc
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
}

+ 80
- 95
supplier.pb.go View File

@ -9,10 +9,8 @@ It is generated from these files:
supplier.proto
It has these top-level messages:
GetRequest
GetUuidRequest
GetResponse
BoolResponse
Request
Response
*/
package supplierrpc
@ -30,104 +28,88 @@ import protorpc "github.com/chai2010/protorpc"
var _ = proto.Marshal
var _ = math.Inf
// 使用key查询
type GetRequest struct {
Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"`
Id *string `protobuf:"bytes,2,opt,name=id" json:"id,omitempty"`
// 配置信息请求结构
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 *GetRequest) Reset() { *m = GetRequest{} }
func (m *GetRequest) String() string { return proto.CompactTextString(m) }
func (*GetRequest) ProtoMessage() {}
func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
func (m *GetRequest) GetDbname() string {
if m != nil && m.Dbname != nil {
return *m.Dbname
func (m *Request) GetData() string {
if m != nil && m.Data != nil {
return *m.Data
}
return ""
}
func (m *GetRequest) GetId() string {
if m != nil && m.Id != nil {
return *m.Id
func (m *Request) GetTime() string {
if m != nil && m.Time != nil {
return *m.Time
}
return ""
}
type GetUuidRequest struct {
Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"`
Uuid *string `protobuf:"bytes,2,opt,name=uuid" json:"uuid,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *GetUuidRequest) Reset() { *m = GetUuidRequest{} }
func (m *GetUuidRequest) String() string { return proto.CompactTextString(m) }
func (*GetUuidRequest) ProtoMessage() {}
func (m *GetUuidRequest) GetDbname() string {
if m != nil && m.Dbname != nil {
return *m.Dbname
}
return ""
}
func (m *GetUuidRequest) GetUuid() string {
if m != nil && m.Uuid != nil {
return *m.Uuid
func (m *Request) GetSign() string {
if m != nil && m.Sign != nil {
return *m.Sign
}
return ""
}
// 使用key查询响应结构
type GetResponse struct {
Value []byte `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"`
XXX_unrecognized []byte `json:"-"`
// 配置信息响应结构
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 *GetResponse) Reset() { *m = GetResponse{} }
func (m *GetResponse) String() string { return proto.CompactTextString(m) }
func (*GetResponse) ProtoMessage() {}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func (m *GetResponse) GetValue() []byte {
if m != nil {
return m.Value
func (m *Response) GetData() string {
if m != nil && m.Data != nil {
return *m.Data
}
return nil
return ""
}
// 使用key查询响应结构
type BoolResponse struct {
Value *bool `protobuf:"varint,1,opt,name=value" json:"value,omitempty"`
XXX_unrecognized []byte `json:"-"`
func (m *Response) GetTime() string {
if m != nil && m.Time != nil {
return *m.Time
}
return ""
}
func (m *BoolResponse) Reset() { *m = BoolResponse{} }
func (m *BoolResponse) String() string { return proto.CompactTextString(m) }
func (*BoolResponse) ProtoMessage() {}
func (m *BoolResponse) GetValue() bool {
if m != nil && m.Value != nil {
return *m.Value
func (m *Response) GetSign() string {
if m != nil && m.Sign != nil {
return *m.Sign
}
return false
return ""
}
func init() {
}
type ProductService interface {
Get(in *GetRequest, out *GetResponse) error
GetByUuid(in *GetUuidRequest, out *GetResponse) error
IsOpen(in *GetRequest, out *BoolResponse) error
type SupplierService interface {
Get(in *Request, out *Response) error
GetByUuid(in *Request, out *Response) error
IsOpen(in *Request, out *Response) error
Get380Star(in *Request, out *Response) error
}
// AcceptProductServiceClient accepts connections on the listener and serves requests
// AcceptSupplierServiceClient accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func AcceptProductServiceClient(lis net.Listener, x ProductService) {
func AcceptSupplierServiceClient(lis net.Listener, x SupplierService) {
srv := rpc.NewServer()
if err := srv.RegisterName("ProductService", x); err != nil {
if err := srv.RegisterName("SupplierService", x); err != nil {
log.Fatal(err)
}
@ -140,26 +122,26 @@ func AcceptProductServiceClient(lis net.Listener, x ProductService) {
}
}
// RegisterProductService publish the given ProductService implementation on the server.
func RegisterProductService(srv *rpc.Server, x ProductService) error {
if err := srv.RegisterName("ProductService", x); err != nil {
// RegisterSupplierService publish the given SupplierService implementation on the server.
func RegisterSupplierService(srv *rpc.Server, x SupplierService) error {
if err := srv.RegisterName("SupplierService", x); err != nil {
return err
}
return nil
}
// NewProductServiceServer returns a new ProductService Server.
func NewProductServiceServer(x ProductService) *rpc.Server {
// NewSupplierServiceServer returns a new SupplierService Server.
func NewSupplierServiceServer(x SupplierService) *rpc.Server {
srv := rpc.NewServer()
if err := srv.RegisterName("ProductService", x); err != nil {
if err := srv.RegisterName("SupplierService", x); err != nil {
log.Fatal(err)
}
return srv
}
// ListenAndServeProductService listen announces on the local network address laddr
// and serves the given ProductService implementation.
func ListenAndServeProductService(network, addr string, x ProductService) error {
// ListenAndServeSupplierService listen announces on the local network address laddr
// and serves the given SupplierService implementation.
func ListenAndServeSupplierService(network, addr string, x SupplierService) error {
lis, err := net.Listen(network, addr)
if err != nil {
return err
@ -167,7 +149,7 @@ func ListenAndServeProductService(network, addr string, x ProductService) error
defer lis.Close()
srv := rpc.NewServer()
if err := srv.RegisterName("ProductService", x); err != nil {
if err := srv.RegisterName("SupplierService", x); err != nil {
return err
}
@ -180,42 +162,45 @@ func ListenAndServeProductService(network, addr string, x ProductService) error
}
}
type ProductServiceClient struct {
type SupplierServiceClient struct {
*rpc.Client
}
// NewProductServiceClient returns a ProductService rpc.Client and stub to handle
// requests to the set of ProductService at the other end of the connection.
func NewProductServiceClient(conn io.ReadWriteCloser) (*ProductServiceClient, *rpc.Client) {
// NewSupplierServiceClient returns a SupplierService rpc.Client and stub to handle
// requests to the set of SupplierService at the other end of the connection.
func NewSupplierServiceClient(conn io.ReadWriteCloser) (*SupplierServiceClient, *rpc.Client) {
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn))
return &ProductServiceClient{c}, c
return &SupplierServiceClient{c}, c
}
func (c *ProductServiceClient) Get(in *GetRequest, out *GetResponse) error {
return c.Call("ProductService.Get", in, out)
func (c *SupplierServiceClient) Get(in *Request, out *Response) error {
return c.Call("SupplierService.Get", in, out)
}
func (c *SupplierServiceClient) GetByUuid(in *Request, out *Response) error {
return c.Call("SupplierService.GetByUuid", in, out)
}
func (c *ProductServiceClient) GetByUuid(in *GetUuidRequest, out *GetResponse) error {
return c.Call("ProductService.GetByUuid", in, out)
func (c *SupplierServiceClient) IsOpen(in *Request, out *Response) error {
return c.Call("SupplierService.IsOpen", in, out)
}
func (c *ProductServiceClient) IsOpen(in *GetRequest, out *BoolResponse) error {
return c.Call("ProductService.IsOpen", in, out)
func (c *SupplierServiceClient) Get380Star(in *Request, out *Response) error {
return c.Call("SupplierService.Get380Star", in, out)
}
// DialProductService connects to an ProductService at the specified network address.
func DialProductService(network, addr string) (*ProductServiceClient, *rpc.Client, error) {
// DialSupplierService connects to an SupplierService at the specified network address.
func DialSupplierService(network, addr string) (*SupplierServiceClient, *rpc.Client, error) {
c, err := protorpc.Dial(network, addr)
if err != nil {
return nil, nil, err
}
return &ProductServiceClient{c}, c, nil
return &SupplierServiceClient{c}, c, nil
}
// DialProductServiceTimeout connects to an ProductService at the specified network address.
func DialProductServiceTimeout(network, addr string,
timeout time.Duration) (*ProductServiceClient, *rpc.Client, error) {
// DialSupplierServiceTimeout connects to an SupplierService at the specified network address.
func DialSupplierServiceTimeout(network, addr string,
timeout time.Duration) (*SupplierServiceClient, *rpc.Client, error) {
c, err := protorpc.DialTimeout(network, addr, timeout)
if err != nil {
return nil, nil, err
}
return &ProductServiceClient{c}, c, nil
return &SupplierServiceClient{c}, c, nil
}

+ 15
- 21
supplier.proto View File

@ -1,30 +1,24 @@
syntax = "proto3";
package supplierrpc;
// 使key查询
message GetRequest {
string dbname = 1;
string id = 2;
//
message Request {
string data = 1;
string time = 2;
string sign = 3;
}
message GetUuidRequest {
string dbname = 1;
string uuid = 2;
}
// 使key查询响应结构
message GetResponse {
bytes value = 1;
}
// 使key查询响应结构
message BoolResponse {
bool value = 1;
//
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 IsOpen (GetRequest) returns (BoolResponse); //
service SupplierService {
rpc Get (Request) returns (Response); // 使id查询
rpc GetByUuid (Request) returns (Response); // 使uuid查询
rpc IsOpen (Request) returns (Response); //
rpc Get380Star (Request) returns (Response); // 380star供应商数据
}

+ 27
- 0
variable.go View File

@ -0,0 +1,27 @@
package supplierrpc
var DES_KEY = "suppli22"
type GetParam struct {
SiteId string `json:"site_id"`
Dbname string `json:"database"`
Id string `json:"id"`
}
type GetUuidParam struct {
SiteId string `json:"site_id"`
Dbname string `json:"database"`
Uuid string `json:"uuid"`
Field string `json:"field"`
}
type BoolRet struct {
Value bool `json:"value"`
Msg string `json:"msg"`
}
type ReqParam struct {
SiteId string `json:"site_id"`
Dbname string `json:"database"`
Id string `json:"id"`
Field string `json:"field"`
}

Loading…
Cancel
Save