@ -0,0 +1,52 @@ | |||
package adminrpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
) | |||
func GetByToken(dbname, token string, url ...string) (map[string]string, error) { | |||
if dbname == "" || token == "" { | |||
return nil, errors.New("参数错误") | |||
} | |||
conn, err := rpc_server_conn(url...) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
data := GetByTokenReq{} | |||
data.SiteId = token | |||
data.Dbname = dbname | |||
req, err := SetReqData(data) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res := &Response{} | |||
err = conn.GetByToken(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res_data_de, err := GetResData(res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if res_data_de == "" { | |||
return nil, nil | |||
} | |||
var res_arr map[string]string | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return res_arr, nil | |||
} |
@ -0,0 +1,19 @@ | |||
package adminrpc | |||
import ( | |||
"testing" | |||
) | |||
func Test_GetByToken(t *testing.T) { | |||
dbname := "canyin" | |||
res2, err := GetByToken(dbname, dbname) | |||
t.Log(res2) | |||
// t.Log(res2.GetUserId()) | |||
t.Log(err) | |||
// res3, err := GetUserByUsercode(dbname, "2233") | |||
// t.Log(res3) | |||
// t.Log(*res3.UserId) | |||
// t.Log(err) | |||
} |
@ -0,0 +1,196 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: admin.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package adminrpc is a generated protocol buffer package. | |||
It is generated from these files: | |||
admin.proto | |||
It has these top-level messages: | |||
Request | |||
Response | |||
*/ | |||
package adminrpc | |||
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 AdminService interface { | |||
GetByToken(in *Request, out *Response) error | |||
} | |||
// AcceptAdminServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptAdminServiceClient(lis net.Listener, x AdminService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("AdminService", 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)) | |||
} | |||
} | |||
// RegisterAdminService publish the given AdminService implementation on the server. | |||
func RegisterAdminService(srv *rpc.Server, x AdminService) error { | |||
if err := srv.RegisterName("AdminService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewAdminServiceServer returns a new AdminService Server. | |||
func NewAdminServiceServer(x AdminService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("AdminService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeAdminService listen announces on the local network address laddr | |||
// and serves the given AdminService implementation. | |||
func ListenAndServeAdminService(network, addr string, x AdminService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("AdminService", 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 AdminServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewAdminServiceClient returns a AdminService rpc.Client and stub to handle | |||
// requests to the set of AdminService at the other end of the connection. | |||
func NewAdminServiceClient(conn io.ReadWriteCloser) (*AdminServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &AdminServiceClient{c}, c | |||
} | |||
func (c *AdminServiceClient) GetByToken(in *Request, out *Response) error { | |||
return c.Call("AdminService.GetByToken", in, out) | |||
} | |||
// DialAdminService connects to an AdminService at the specified network address. | |||
func DialAdminService(network, addr string) (*AdminServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &AdminServiceClient{c}, c, nil | |||
} | |||
// DialAdminServiceTimeout connects to an AdminService at the specified network address. | |||
func DialAdminServiceTimeout(network, addr string, | |||
timeout time.Duration) (*AdminServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &AdminServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,21 @@ | |||
syntax = "proto3"; | |||
package adminrpc; | |||
// 信息请求结构 | |||
message Request { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// 信息响应结构 | |||
message Response { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// rpc方法 | |||
service AdminService { | |||
rpc getByToken (Request) returns (Response); // 使用token查询用户 | |||
} |
@ -0,0 +1,40 @@ | |||
package adminrpc | |||
import ( | |||
"git.tetele.net/tgo/conf" | |||
) | |||
const DES_KEY = "adminrpc" | |||
type Req struct { | |||
SiteId string | |||
Dbname string | |||
} | |||
type GetByTokenReq struct { | |||
Req | |||
Token string | |||
} | |||
type Res struct { | |||
Errcode int | |||
Errmsg string | |||
} | |||
func rpc_server_conn(url ...string) (*AdminServiceClient, error) { | |||
var rpc_url string | |||
if len(url) > 0 && url[0] != "" { | |||
rpc_url = url[0] | |||
} else if conf.ADMIN_RPC_URL != "" { | |||
rpc_url = conf.ADMIN_RPC_URL | |||
} else { | |||
rpc_url = "127.0.0.1:" + conf.ADMIN_RPC_PORT | |||
} | |||
conn, _, err := DialAdminService("tcp", rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return conn, nil | |||
} |
@ -0,0 +1,106 @@ | |||
package adminrpc | |||
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 | |||
} |
@ -0,0 +1,12 @@ | |||
module git.tetele.net/tgo/adminrpc | |||
go 1.14 | |||
require ( | |||
git.tetele.net/tgo/conf v0.58.0 | |||
git.tetele.net/tgo/crypter v0.2.2 | |||
github.com/chai2010/protorpc v1.1.3 | |||
github.com/golang/protobuf v1.0.0 | |||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect | |||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect | |||
) |
@ -0,0 +1,19 @@ | |||
git.tetele.net/tgo/conf v0.58.0 h1:I63a6mMQmsuBvKwY6AZnvmWzD8P50hUe/XlVo30DcxI= | |||
git.tetele.net/tgo/conf v0.58.0/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= | |||
git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= | |||
git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= | |||
github.com/chai2010/protorpc v1.1.3 h1:VJK5hIoZn0XCGol0GmbxZkUG6FbTI5LP2Lam6RVd15w= | |||
github.com/chai2010/protorpc v1.1.3/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= | |||
github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ= | |||
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | |||
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= | |||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | |||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= | |||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= | |||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | |||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= | |||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | |||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | |||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | |||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
@ -0,0 +1,129 @@ | |||
package adminrpc | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"encoding/json" | |||
"errors" | |||
"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("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 | |||
} | |||
/** | |||
* 处理返回结果 | |||
*/ | |||
func HandleUserRes(res *Response) (map[string]string, 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 map[string]string | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return res_arr, nil | |||
} |