Browse Source

init

master v0.1.0
guzeng 3 years ago
parent
commit
d1e0308322
5 changed files with 477 additions and 0 deletions
  1. +106
    -0
      client.balance.go
  2. +135
    -0
      client.go
  3. +15
    -0
      client_test.go
  4. +198
    -0
      site.pb.go
  5. +23
    -0
      site.proto

+ 106
- 0
client.balance.go View File

@ -0,0 +1,106 @@
package site
// import (
// "encoding/json"
// "errors"
// "strconv"
// "time"
// "git.tetele.net/tgo/crypter"
// "github.com/golang/protobuf/proto"
// )
// type SiteBalanceTypeReqArg struct {
// SiteId string `json:"site_id"`
// Dbname string `json:"dbname"`
// }
// type SiteBalanceType struct {
// Id string
// Name string
// UseLimit string
// Unit string
// Type string
// CleanRate string
// CleanTime string
// }
// /**
// * 由配置key取对应value
// * 请求及回均加密验签
// * 2021/01/20
// * GZ
// */
// func GetBalanceType(site_id, dbname string, url ...string) (*[]SiteBalanceType, 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)
// if err != nil {
// return nil, err
// }
// defer conn.Close()
// arg := SiteConfigItemReqArg{site_id, dbname, key}
// data_json, err := json.Marshal(arg)
// if err != nil {
// return nil, err
// }
// now_int64 := time.Now().Unix()
// encryData := crypter.DesEn(string(data_json), "confdata")
// now := strconv.FormatInt(now_int64, 10)
// sign := Sign(encryData, now)
// req := &ConfigRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil}
// res := &ConfigResponse{}
// err = conn.GetConfig(req, res)
// if err != nil {
// return nil, err
// }
// res_data := res.GetData()
// if res_data != "" {
// time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
// if err != nil {
// return nil, err
// }
// now_int64 = time.Now().Unix()
// if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
// //时间误差前后10秒,返回
// return nil, errors.New("返回时间错误")
// }
// check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
// if !check_sign {
// return nil, errors.New("返回数据签名错误")
// }
// //解密
// res_data_de := crypter.DesDe(res_data, "confdata")
// var res_arr []SiteBalanceType
// err = json.Unmarshal([]byte(res_data_de), &res_arr)
// if err != nil {
// return nil, err
// }
// return &res_arr, nil
// }
// return nil, nil
// }

+ 135
- 0
client.go View File

@ -0,0 +1,135 @@
package site
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
type SiteConfigItemReqArg struct {
SiteId string `json:"site_id"`
Dbname string `json:"dbname"`
Key string `json:"key"`
}
type SiteConfigItemRes struct {
Type string
Value string
}
/**
* 由配置key取对应value
* 请求及回均加密验签
* 2021/01/20
* GZ
*/
func GetConfigItem(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)
if err != nil {
return nil, err
}
defer conn.Close()
arg := SiteConfigItemReqArg{site_id, dbname, key}
data_json, err := json.Marshal(arg)
if err != nil {
return nil, err
}
now_int64 := time.Now().Unix()
encryData := crypter.DesEn(string(data_json), "confdata")
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
req := &ConfigRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil}
res := &ConfigResponse{}
err = conn.GetConfig(req, res)
if err != nil {
return nil, err
}
res_data := res.GetData()
if res_data != "" {
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
if err != nil {
return nil, err
}
now_int64 = time.Now().Unix()
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
//时间误差前后10秒,返回
return nil, errors.New("返回时间错误")
}
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
if !check_sign {
return nil, errors.New("返回数据签名错误")
}
//解密
res_data_de := crypter.DesDe(res_data, "confdata")
var res_arr SiteConfigItemRes
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}
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
}

+ 15
- 0
client_test.go View File

@ -0,0 +1,15 @@
package site
import (
"testing"
)
func Test_GetConfigItem(t *testing.T) {
siteid := "100065"
dbname := "test1_tetele_com"
key := "percentage"
res, err := GetConfigItem(siteid, dbname, key)
t.Log(res)
t.Log(err)
}

+ 198
- 0
site.pb.go View File

@ -0,0 +1,198 @@
// Code generated by protoc-gen-go.
// source: site.proto
// DO NOT EDIT!
/*
Package site is a generated protocol buffer package.
It is generated from these files:
site.proto
It has these top-level messages:
ConfigRequest
ConfigResponse
*/
package site
import proto "github.com/chai2010/protorpc/proto"
import math "math"
import "io"
import "log"
import "net"
import "net/rpc"
import "time"
import protorpc "github.com/chai2010/protorpc"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = math.Inf
// 配置信息请求结构
type ConfigRequest 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 *ConfigRequest) Reset() { *m = ConfigRequest{} }
func (m *ConfigRequest) String() string { return proto.CompactTextString(m) }
func (*ConfigRequest) ProtoMessage() {}
func (m *ConfigRequest) GetData() string {
if m != nil && m.Data != nil {
return *m.Data
}
return ""
}
func (m *ConfigRequest) GetTime() string {
if m != nil && m.Time != nil {
return *m.Time
}
return ""
}
func (m *ConfigRequest) GetSign() string {
if m != nil && m.Sign != nil {
return *m.Sign
}
return ""
}
// 配置信息响应结构
type ConfigResponse 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 *ConfigResponse) Reset() { *m = ConfigResponse{} }
func (m *ConfigResponse) String() string { return proto.CompactTextString(m) }
func (*ConfigResponse) ProtoMessage() {}
func (m *ConfigResponse) GetData() string {
if m != nil && m.Data != nil {
return *m.Data
}
return ""
}
func (m *ConfigResponse) GetTime() string {
if m != nil && m.Time != nil {
return *m.Time
}
return ""
}
func (m *ConfigResponse) GetSign() string {
if m != nil && m.Sign != nil {
return *m.Sign
}
return ""
}
func init() {
}
type SiteService interface {
GetConfig(in *ConfigRequest, out *ConfigResponse) error
GetBalanceType(in *ConfigRequest, out *ConfigResponse) error
}
// AcceptSiteServiceClient accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func AcceptSiteServiceClient(lis net.Listener, x SiteService) {
srv := rpc.NewServer()
if err := srv.RegisterName("SiteService", x); err != nil {
log.Fatal(err)
}
for {
conn, err := lis.Accept()
if err != nil {
log.Fatalf("lis.Accept(): %v\n", err)
}
go srv.ServeCodec(protorpc.NewServerCodec(conn))
}
}
// RegisterSiteService publish the given SiteService implementation on the server.
func RegisterSiteService(srv *rpc.Server, x SiteService) error {
if err := srv.RegisterName("SiteService", x); err != nil {
return err
}
return nil
}
// NewSiteServiceServer returns a new SiteService Server.
func NewSiteServiceServer(x SiteService) *rpc.Server {
srv := rpc.NewServer()
if err := srv.RegisterName("SiteService", x); err != nil {
log.Fatal(err)
}
return srv
}
// ListenAndServeSiteService listen announces on the local network address laddr
// and serves the given SiteService implementation.
func ListenAndServeSiteService(network, addr string, x SiteService) error {
lis, err := net.Listen(network, addr)
if err != nil {
return err
}
defer lis.Close()
srv := rpc.NewServer()
if err := srv.RegisterName("SiteService", x); err != nil {
return err
}
for {
conn, err := lis.Accept()
if err != nil {
log.Fatalf("lis.Accept(): %v\n", err)
}
go srv.ServeCodec(protorpc.NewServerCodec(conn))
}
}
type SiteServiceClient struct {
*rpc.Client
}
// NewSiteServiceClient returns a SiteService rpc.Client and stub to handle
// requests to the set of SiteService at the other end of the connection.
func NewSiteServiceClient(conn io.ReadWriteCloser) (*SiteServiceClient, *rpc.Client) {
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn))
return &SiteServiceClient{c}, c
}
func (c *SiteServiceClient) GetConfig(in *ConfigRequest, out *ConfigResponse) error {
return c.Call("SiteService.GetConfig", in, out)
}
func (c *SiteServiceClient) GetBalanceType(in *ConfigRequest, out *ConfigResponse) error {
return c.Call("SiteService.GetBalanceType", in, out)
}
// DialSiteService connects to an SiteService at the specified network address.
func DialSiteService(network, addr string) (*SiteServiceClient, *rpc.Client, error) {
c, err := protorpc.Dial(network, addr)
if err != nil {
return nil, nil, err
}
return &SiteServiceClient{c}, c, nil
}
// DialSiteServiceTimeout connects to an SiteService at the specified network address.
func DialSiteServiceTimeout(network, addr string,
timeout time.Duration) (*SiteServiceClient, *rpc.Client, error) {
c, err := protorpc.DialTimeout(network, addr, timeout)
if err != nil {
return nil, nil, err
}
return &SiteServiceClient{c}, c, nil
}

+ 23
- 0
site.proto View File

@ -0,0 +1,23 @@
syntax = "proto3";
package site;
//
message ConfigRequest {
string data = 1;
string time = 2;
string sign = 3;
}
//
message ConfigResponse {
string data = 1;
string time = 2;
string sign = 3;
}
// rpc方法
service SiteService {
rpc getConfig (ConfigRequest) returns (ConfigResponse); //
rpc getBalanceType (ConfigRequest) returns (ConfigResponse); //
}

Loading…
Cancel
Save