Browse Source

引入加密

master v0.1.0
guzeng 2 years ago
parent
commit
4f833cf133
4 changed files with 163 additions and 86 deletions
  1. +26
    -0
      conn.go
  2. +77
    -0
      sign.go
  3. +47
    -66
      supplier.pb.go
  4. +13
    -20
      supplier.proto

+ 26
- 0
conn.go View File

@ -0,0 +1,26 @@
package supplierrpc
import (
"git.tetele.net/tgo/conf"
)
var DES_KEY = "suppli22"
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
}

+ 77
- 0
sign.go View File

@ -0,0 +1,77 @@
package siterpc
import (
"crypto/md5"
"encoding/hex"
"errors"
"strconv"
"strings"
"time"
"git.tetele.net/tgo/crypter"
)
/**
* 签名
*/
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
}
/**
* 解密
*/
func GetOrgData(res *Response) (string, error) {
res_data := res.GetData()
if res_data == "" {
return "", errors.New("未收到收据")
}
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("返回数据签名错误")
}
//解密
res_data_de := crypter.DesDe(res_data, DES_KEY)
return res_data_de, nil
}

+ 47
- 66
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,96 +28,79 @@ 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 SupplierService interface {
Get(in *GetRequest, out *GetResponse) error
GetByUuid(in *GetUuidRequest, out *GetResponse) error
IsOpen(in *GetRequest, out *BoolResponse) error
Get(in *Request, out *Response) error
GetByUuid(in *Request, out *Response) error
IsOpen(in *Request, out *Response) error
}
// AcceptSupplierServiceClient accepts connections on the listener and serves requests
@ -191,13 +172,13 @@ func NewSupplierServiceClient(conn io.ReadWriteCloser) (*SupplierServiceClient,
return &SupplierServiceClient{c}, c
}
func (c *SupplierServiceClient) Get(in *GetRequest, out *GetResponse) error {
func (c *SupplierServiceClient) Get(in *Request, out *Response) error {
return c.Call("SupplierService.Get", in, out)
}
func (c *SupplierServiceClient) GetByUuid(in *GetUuidRequest, out *GetResponse) error {
func (c *SupplierServiceClient) GetByUuid(in *Request, out *Response) error {
return c.Call("SupplierService.GetByUuid", in, out)
}
func (c *SupplierServiceClient) IsOpen(in *GetRequest, out *BoolResponse) error {
func (c *SupplierServiceClient) IsOpen(in *Request, out *Response) error {
return c.Call("SupplierService.IsOpen", in, out)
}


+ 13
- 20
supplier.proto View File

@ -1,30 +1,23 @@
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 SupplierService {
rpc Get (GetRequest) returns (GetResponse); // 使id查询
rpc GetByUuid (GetUuidRequest) returns (GetResponse); // 使uuid查询
rpc IsOpen (GetRequest) returns (BoolResponse); //
rpc Get (Request) returns (Response); // 使id查询
rpc GetByUuid (Request) returns (Response); // 使uuid查询
rpc IsOpen (Request) returns (Response); //
}

Loading…
Cancel
Save