diff --git a/common.go b/common.go new file mode 100644 index 0000000..3efab3d --- /dev/null +++ b/common.go @@ -0,0 +1,36 @@ +package userrpc + +const DES_KEY = "usersrpc" + +type Req struct { + SiteId string + Dbname string +} + +type BindThirdReq struct { + Req + Userid string + Type string + Openid string + Unionid string +} + +type Res struct { + Errcode string + Errmsg string +} + +func rpc_server_conn(url ...string) (*UserServiceClient, error) { + + var user_rpc_url string = "127.0.0.1:7976" + if len(url) > 0 && url[0] != "" { + user_rpc_url = url[0] + } + + conn, _, err := DialUserService("tcp", user_rpc_url) + if err != nil { + return map[string]string{}, err + } + + return conn, nil +} diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..fb300ec --- /dev/null +++ b/sign.go @@ -0,0 +1,74 @@ +package userrpc + +/** + * 签名 + */ +func Sign(data string, salt string) string { + + var build strings.Builder + + build.WriteString(data) + build.WriteString(salt) + build.WriteString("user&rpc") + + 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 HandleRes(res *Response) (*Res, error) { + + res_data := res.GetData() + + if res_data == "" { + + return nil, errors.New("未收到收据") + } + + 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, DES_KEY) + + var res_arr Res + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil +} diff --git a/third.client.go b/third.client.go new file mode 100644 index 0000000..29d5479 --- /dev/null +++ b/third.client.go @@ -0,0 +1,52 @@ +package userrpc + +/** + * 绑定第三方平台openid + * 2021/04/05 + * gz + */ +func BindThird(site_id, dbname, _type, user_id, openid, unionid, url ...string) (*Res, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := BindThirdReq{} + data.SiteId = site_id + data.Dbname = dbname + data.Openid = openid + data.Type = _type + data.Unionid = unionid + data.Userid = user_id + + data_json, err := json.Marshal(data) + if err != nil { + return nil, err + } + + encryData := crypter.DesEn(string(data_json), DES_KEY) + + now_int64 := time.Now().Unix() + + 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.BindThird(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) +} diff --git a/user.pb.go b/user.pb.go index 514b2a7..b42beec 100644 --- a/user.pb.go +++ b/user.pb.go @@ -15,6 +15,8 @@ It has these top-level messages: UserResponse LoginRequest LoginResponse + Request + Response */ package userrpc @@ -374,6 +376,72 @@ func (m *LoginResponse) GetType() string { return "" } +// 信息请求结构 +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() { } @@ -382,6 +450,7 @@ type UserService interface { Login(in *LoginRequest, out *LoginResponse) error GetByUsername(in *UserInfoByUsername, out *UserResponse) error GetByUsercode(in *UserInfoByUsercode, out *UserResponse) error + BindThird(in *Request, out *Response) error } // AcceptUserServiceClient accepts connections on the listener and serves requests @@ -465,6 +534,9 @@ func (c *UserServiceClient) GetByUsername(in *UserInfoByUsername, out *UserRespo func (c *UserServiceClient) GetByUsercode(in *UserInfoByUsercode, out *UserResponse) error { return c.Call("UserService.GetByUsercode", in, out) } +func (c *UserServiceClient) BindThird(in *Request, out *Response) error { + return c.Call("UserService.BindThird", in, out) +} // DialUserService connects to an UserService at the specified network address. func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) { diff --git a/user.proto b/user.proto index b27c104..9de9437 100644 --- a/user.proto +++ b/user.proto @@ -61,10 +61,25 @@ message LoginResponse { } +// 信息请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 信息响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + // rpc方法 service UserService { rpc getByToken (UserRequest) returns (UserResponse); // 使用token查询用户 rpc login (LoginRequest) returns (LoginResponse); // 登录 rpc getByUsername (UserInfoByUsername) returns (UserResponse); // 使用username查询用户 rpc getByUsercode (UserInfoByUsercode) returns (UserResponse); // 使用usercode查询用户 + rpc bindThird (Request) returns (Response); // 绑定第三方账号 } \ No newline at end of file