From 9028f26d8dcf879c9b380e7e0a11f6054f669b45 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 23 Jun 2021 23:43:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8C=85=E5=90=8D,=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8F=96=E6=89=80=E6=9C=89=E7=AB=99=E7=82=B9=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 39 +-------------------- client.sitelist.go | 62 +++++++++++++++++++++++++++++++++ client.sitelist_test.go | 13 +++++++ client_test.go | 6 +++- common.go | 14 ++++++++ go.mod | 10 ++++++ go.sum | 24 +++++++++++++ sign.go | 77 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 206 insertions(+), 39 deletions(-) create mode 100644 client.sitelist.go create mode 100644 client.sitelist_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 sign.go diff --git a/client.go b/client.go index dea2dbc..461dcfe 100644 --- a/client.go +++ b/client.go @@ -1,12 +1,9 @@ package siterpc import ( - "crypto/md5" - "encoding/hex" "encoding/json" "errors" "strconv" - "strings" "time" "git.tetele.net/tgo/crypter" @@ -48,11 +45,7 @@ func GetConfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, func getconfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) { - var site_rpc_url string = "127.0.0.1:7971" - if len(url) > 0 && url[0] != "" { - site_rpc_url = url[0] - } - conn, _, err := DialSiteService("tcp", site_rpc_url) + conn, err := rpc_server_conn(url...) if err != nil { return nil, err } @@ -118,33 +111,3 @@ func getconfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, return nil, nil } - -/** - * 签名 - */ -func Sign(data string, salt string) string { - - var build strings.Builder - - build.WriteString(data) - build.WriteString(salt) - build.WriteString("site55sign33") - - 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 -} diff --git a/client.sitelist.go b/client.sitelist.go new file mode 100644 index 0000000..8e7ef5b --- /dev/null +++ b/client.sitelist.go @@ -0,0 +1,62 @@ +package siterpc + +import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +/** + * 由配置key取对应value + * 请求及回均加密验签 + * 2021/01/20 + * GZ + */ + +func GetAllSite(url ...string) ([]map[string]string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + now_int64 := time.Now().Unix() + + encryData := crypter.DesEn("get_from_cache", DES_KEY) + + now := strconv.FormatInt(now_int64, 10) + + sign := Sign(encryData, now) + + req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil} + + res := &Response{} + + err = conn.GetAllSite(req, res) + + if err != nil { + return nil, err + } + + res_data, err := GetOrgData(res) + + if err != nil { + return []map[string]string{}, err + } + + var res_arr []map[string]string + + err = json.Unmarshal([]byte(res_data), &res_arr) + + if err != nil { + return []map[string]string{}, err + } + + return res_arr, nil + +} diff --git a/client.sitelist_test.go b/client.sitelist_test.go new file mode 100644 index 0000000..dc92d5f --- /dev/null +++ b/client.sitelist_test.go @@ -0,0 +1,13 @@ +package siterpc + +import ( + "testing" +) + +func Test_GetAllSite(t *testing.T) { + + res, err := GetAllSite() + t.Log(res) + t.Log(err) + +} diff --git a/client_test.go b/client_test.go index e3b8b4a..6d1740f 100644 --- a/client_test.go +++ b/client_test.go @@ -7,9 +7,13 @@ import ( func Test_GetConfigItem(t *testing.T) { siteid := "100065" dbname := "test1_tetele_com" - key := "percentage" + key := "percentages" res, err := GetConfigItem(siteid, dbname, key) t.Log(res) t.Log(err) + res, err = GetConfig(siteid, dbname, key) + t.Log(res) + t.Log(err) + } diff --git a/common.go b/common.go index 07bb76f..4172bdf 100644 --- a/common.go +++ b/common.go @@ -1,3 +1,17 @@ package siterpc var DES_KEY = "confdata" + +func rpc_server_conn(url ...string) (*SiteServiceClient, error) { + + var wx_rpc_url string = "127.0.0.1:7971" + if len(url) > 0 && url[0] != "" { + wx_rpc_url = url[0] + } + conn, _, err := DialSiteService("tcp", wx_rpc_url) + if err != nil { + return nil, err + } + + return conn, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..15eb4e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.tetel.net/tgo/siterpc + +go 1.14 + +require ( + git.tetele.net/tgo/crypter v0.2.2 // indirect + github.com/chai2010/protorpc v1.0.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c06e36b --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +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.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo= +github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54= +github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk= +github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +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= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..4dd2d0a --- /dev/null +++ b/sign.go @@ -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("site55sign33") + + 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 +}