diff --git a/README.md b/README.md index f8439ca..d6eef64 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # msgrpc -消息服务RPC \ No newline at end of file +分站消息发送的RPC包 \ No newline at end of file diff --git a/common.go b/common.go new file mode 100644 index 0000000..7bae0a3 --- /dev/null +++ b/common.go @@ -0,0 +1,37 @@ +package msgrpc + +import ( + "git.tetele.net/tgo/conf" +) + +const DES_KEY = "msgsirpc" + +type Req struct { + SiteId string + Dbname string +} + +type MsgBody struct { + Req + Platform string + BusinessId string + Domainname string + Type string + Data string + OrderSn string + Memo string +} + +func rpc_server_conn(url ...string) (*MsgRpcServiceClient, error) { + + var wx_rpc_url string = "127.0.0.1:" + conf.MSG_RPC_PORT + if len(url) > 0 && url[0] != "" { + wx_rpc_url = url[0] + } + conn, _, err := DialMsgRpcService("tcp", wx_rpc_url) + if err != nil { + return nil, err + } + + return conn, nil +} diff --git a/msg.pb.go b/msg.pb.go new file mode 100644 index 0000000..a3fdb1a --- /dev/null +++ b/msg.pb.go @@ -0,0 +1,194 @@ +// Code generated by protoc-gen-go. +// source: msg.proto +// DO NOT EDIT! + +/* +Package msgrpc is a generated protocol buffer package. + +It is generated from these files: + msg.proto + +It has these top-level messages: + Request + Response +*/ +package msgrpc + +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 MsgRpcService interface { + SendMsg(in *Request, out *Response) error +} + +// AcceptMsgRpcServiceClient accepts connections on the listener and serves requests +// for each incoming connection. Accept blocks; the caller typically +// invokes it in a go statement. +func AcceptMsgRpcServiceClient(lis net.Listener, x MsgRpcService) { + srv := rpc.NewServer() + if err := srv.RegisterName("MsgRpcService", 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)) + } +} + +// RegisterMsgRpcService publish the given MsgRpcService implementation on the server. +func RegisterMsgRpcService(srv *rpc.Server, x MsgRpcService) error { + if err := srv.RegisterName("MsgRpcService", x); err != nil { + return err + } + return nil +} + +// NewMsgRpcServiceServer returns a new MsgRpcService Server. +func NewMsgRpcServiceServer(x MsgRpcService) *rpc.Server { + srv := rpc.NewServer() + if err := srv.RegisterName("MsgRpcService", x); err != nil { + log.Fatal(err) + } + return srv +} + +// ListenAndServeMsgRpcService listen announces on the local network address laddr +// and serves the given MsgRpcService implementation. +func ListenAndServeMsgRpcService(network, addr string, x MsgRpcService) error { + lis, err := net.Listen(network, addr) + if err != nil { + return err + } + defer lis.Close() + + srv := rpc.NewServer() + if err := srv.RegisterName("MsgRpcService", 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 MsgRpcServiceClient struct { + *rpc.Client +} + +// NewMsgRpcServiceClient returns a MsgRpcService rpc.Client and stub to handle +// requests to the set of MsgRpcService at the other end of the connection. +func NewMsgRpcServiceClient(conn io.ReadWriteCloser) (*MsgRpcServiceClient, *rpc.Client) { + c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) + return &MsgRpcServiceClient{c}, c +} + +func (c *MsgRpcServiceClient) SendMsg(in *Request, out *Response) error { + return c.Call("MsgRpcService.SendMsg", in, out) +} + +// DialMsgRpcService connects to an MsgRpcService at the specified network address. +func DialMsgRpcService(network, addr string) (*MsgRpcServiceClient, *rpc.Client, error) { + c, err := protorpc.Dial(network, addr) + if err != nil { + return nil, nil, err + } + return &MsgRpcServiceClient{c}, c, nil +} + +// DialMsgRpcServiceTimeout connects to an MsgRpcService at the specified network address. +func DialMsgRpcServiceTimeout(network, addr string, + timeout time.Duration) (*MsgRpcServiceClient, *rpc.Client, error) { + c, err := protorpc.DialTimeout(network, addr, timeout) + if err != nil { + return nil, nil, err + } + return &MsgRpcServiceClient{c}, c, nil +} diff --git a/msg.proto b/msg.proto new file mode 100644 index 0000000..5be2d19 --- /dev/null +++ b/msg.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package msgrpc; + +// 配置信息请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 配置信息响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + + +// rpc方法 +service MsgRpcService { + rpc sendMsg (Request) returns (Response); // 发送消息 +} \ No newline at end of file diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..892a7e9 --- /dev/null +++ b/sign.go @@ -0,0 +1,79 @@ +package msgrpc + +import ( + "crypto/md5" + "encoding/hex" + "encoding/json" + "errors" + "log" + "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(DES_KEY) + + 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 +}