From b737a29c78948202f4798c59f155a3e7a6fdddcc Mon Sep 17 00:00:00 2001 From: guzeng Date: Sat, 23 Oct 2021 22:09:52 +0800 Subject: [PATCH] init --- conn.go | 25 +++++++ data.go | 106 ++++++++++++++++++++++++++++ sign.go | 37 ++++++++++ sms.pb.go | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++ sms.proto | 22 ++++++ variable.go | 14 ++++ 6 files changed, 402 insertions(+) create mode 100644 conn.go create mode 100644 data.go create mode 100644 sign.go create mode 100644 sms.pb.go create mode 100644 sms.proto create mode 100644 variable.go diff --git a/conn.go b/conn.go new file mode 100644 index 0000000..03b27d5 --- /dev/null +++ b/conn.go @@ -0,0 +1,25 @@ +package smsrpc + +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.SMS_RPC_URL != "" { + rpc_url = conf.SMS_RPC_URL + } else { + rpc_url = "127.0.0.1:" + conf.SMS_RPC_PORT + } + + conn, _, err := DialSmsService("tcp", rpc_url) + if err != nil { + return nil, err + } + + return conn, nil + +} diff --git a/data.go b/data.go new file mode 100644 index 0000000..acd5ec0 --- /dev/null +++ b/data.go @@ -0,0 +1,106 @@ +package smsrpc + +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 +} diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..2e7cecf --- /dev/null +++ b/sign.go @@ -0,0 +1,37 @@ +package smsrpc + +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("smsd$8gnlier") + + 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/sms.pb.go b/sms.pb.go new file mode 100644 index 0000000..362a343 --- /dev/null +++ b/sms.pb.go @@ -0,0 +1,198 @@ +// Code generated by protoc-gen-go. +// source: sms.proto +// DO NOT EDIT! + +/* +Package smsrpc is a generated protocol buffer package. + +It is generated from these files: + sms.proto + +It has these top-level messages: + Request + Response +*/ +package smsrpc + +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 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 *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} + +func (m *Request) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Request) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + 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 { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +func init() { +} + +type SmsService interface { + SendWoMsg(in *Request, out *Response) error + SendTencentMsg(in *Request, out *Response) error +} + +// AcceptSmsServiceClient accepts connections on the listener and serves requests +// for each incoming connection. Accept blocks; the caller typically +// invokes it in a go statement. +func AcceptSmsServiceClient(lis net.Listener, x SmsService) { + srv := rpc.NewServer() + if err := srv.RegisterName("SmsService", 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)) + } +} + +// RegisterSmsService publish the given SmsService implementation on the server. +func RegisterSmsService(srv *rpc.Server, x SmsService) error { + if err := srv.RegisterName("SmsService", x); err != nil { + return err + } + return nil +} + +// NewSmsServiceServer returns a new SmsService Server. +func NewSmsServiceServer(x SmsService) *rpc.Server { + srv := rpc.NewServer() + if err := srv.RegisterName("SmsService", x); err != nil { + log.Fatal(err) + } + return srv +} + +// ListenAndServeSmsService listen announces on the local network address laddr +// and serves the given SmsService implementation. +func ListenAndServeSmsService(network, addr string, x SmsService) error { + lis, err := net.Listen(network, addr) + if err != nil { + return err + } + defer lis.Close() + + srv := rpc.NewServer() + if err := srv.RegisterName("SmsService", 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 SmsServiceClient struct { + *rpc.Client +} + +// NewSmsServiceClient returns a SmsService rpc.Client and stub to handle +// requests to the set of SmsService at the other end of the connection. +func NewSmsServiceClient(conn io.ReadWriteCloser) (*SmsServiceClient, *rpc.Client) { + c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) + return &SmsServiceClient{c}, c +} + +func (c *SmsServiceClient) SendWoMsg(in *Request, out *Response) error { + return c.Call("SmsService.SendWoMsg", in, out) +} +func (c *SmsServiceClient) SendTencentMsg(in *Request, out *Response) error { + return c.Call("SmsService.SendTencentMsg", in, out) +} + +// DialSmsService connects to an SmsService at the specified network address. +func DialSmsService(network, addr string) (*SmsServiceClient, *rpc.Client, error) { + c, err := protorpc.Dial(network, addr) + if err != nil { + return nil, nil, err + } + return &SmsServiceClient{c}, c, nil +} + +// DialSmsServiceTimeout connects to an SmsService at the specified network address. +func DialSmsServiceTimeout(network, addr string, + timeout time.Duration) (*SmsServiceClient, *rpc.Client, error) { + c, err := protorpc.DialTimeout(network, addr, timeout) + if err != nil { + return nil, nil, err + } + return &SmsServiceClient{c}, c, nil +} diff --git a/sms.proto b/sms.proto new file mode 100644 index 0000000..4764e10 --- /dev/null +++ b/sms.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package smsrpc; + +// 信息请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 信息响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + +// rpc方法 +service SmsService { + rpc SendWoMsg (Request) returns (Response); // wo msg + rpc SendTencentMsg (Request) returns (Response); // wo msg +} \ No newline at end of file diff --git a/variable.go b/variable.go new file mode 100644 index 0000000..d87af4b --- /dev/null +++ b/variable.go @@ -0,0 +1,14 @@ +package smsrpc + +var DES_KEY = "smszgi22" + +type WoParam struct { + Cqcode string `json:"cqcode"` + Msg string `json:"msg"` + Mobile string `json:"mobile"` + Excode string `json:"excode"` + TempletId string `json:"templetid"` +} + +type TencentParam struct { +}