Browse Source

init

master
guzeng 2 years ago
parent
commit
b737a29c78
6 changed files with 402 additions and 0 deletions
  1. +25
    -0
      conn.go
  2. +106
    -0
      data.go
  3. +37
    -0
      sign.go
  4. +198
    -0
      sms.pb.go
  5. +22
    -0
      sms.proto
  6. +14
    -0
      variable.go

+ 25
- 0
conn.go View File

@ -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
}

+ 106
- 0
data.go View File

@ -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
}

+ 37
- 0
sign.go View File

@ -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
}

+ 198
- 0
sms.pb.go View File

@ -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
}

+ 22
- 0
sms.proto View File

@ -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
}

+ 14
- 0
variable.go View File

@ -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 {
}

Loading…
Cancel
Save