2020-09-09 18:53:53 +08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-10-11 11:52:43 +08:00
|
|
|
"crypto/tls"
|
2020-09-09 18:53:53 +08:00
|
|
|
"encoding/base64"
|
|
|
|
|
"errors"
|
2020-09-13 20:37:40 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-09-09 18:53:53 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/configs"
|
|
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/encrypt"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
"github.com/iwind/TeaGo/rands"
|
|
|
|
|
"google.golang.org/grpc"
|
2020-10-11 11:52:43 +08:00
|
|
|
"google.golang.org/grpc/credentials"
|
2020-09-09 18:53:53 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
2020-10-11 11:52:43 +08:00
|
|
|
"net/url"
|
2020-09-09 18:53:53 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RPCClient struct {
|
2020-09-30 17:46:49 +08:00
|
|
|
apiConfig *configs.APIConfig
|
|
|
|
|
conns []*grpc.ClientConn
|
2020-09-09 18:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
|
|
|
|
|
if apiConfig == nil {
|
|
|
|
|
return nil, errors.New("api config should not be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conns := []*grpc.ClientConn{}
|
|
|
|
|
for _, endpoint := range apiConfig.RPC.Endpoints {
|
2020-10-11 11:52:43 +08:00
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("parse endpoint failed: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
var conn *grpc.ClientConn
|
|
|
|
|
if u.Scheme == "http" {
|
|
|
|
|
conn, err = grpc.Dial(u.Host, grpc.WithInsecure())
|
|
|
|
|
} else if u.Scheme == "https" {
|
|
|
|
|
conn, err = grpc.Dial(u.Host, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
|
})))
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errors.New("parse endpoint failed: invalid scheme '" + u.Scheme + "'")
|
|
|
|
|
}
|
2020-09-09 18:53:53 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
conns = append(conns, conn)
|
|
|
|
|
}
|
|
|
|
|
if len(conns) == 0 {
|
|
|
|
|
return nil, errors.New("[RPC]no available endpoints")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &RPCClient{
|
2020-09-30 17:46:49 +08:00
|
|
|
apiConfig: apiConfig,
|
|
|
|
|
conns: conns,
|
2020-09-09 18:53:53 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) NodeRPC() pb.NodeServiceClient {
|
2020-09-30 17:46:49 +08:00
|
|
|
return pb.NewNodeServiceClient(this.pickConn())
|
2020-09-09 18:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
2020-10-09 11:06:43 +08:00
|
|
|
func (this *RPCClient) NodeLogRPC() pb.NodeLogServiceClient {
|
|
|
|
|
return pb.NewNodeLogServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-10 11:47:53 +08:00
|
|
|
func (this *RPCClient) HTTPAccessLogRPC() pb.HTTPAccessLogServiceClient {
|
|
|
|
|
return pb.NewHTTPAccessLogServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 18:53:53 +08:00
|
|
|
func (this *RPCClient) Context() context.Context {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
m := maps.Map{
|
|
|
|
|
"timestamp": time.Now().Unix(),
|
|
|
|
|
"type": "node",
|
|
|
|
|
"userId": 0,
|
|
|
|
|
}
|
|
|
|
|
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, this.apiConfig.Secret, this.apiConfig.NodeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.PrintError(err)
|
|
|
|
|
return context.Background()
|
|
|
|
|
}
|
|
|
|
|
data, err := method.Encrypt(m.AsJSON())
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.PrintError(err)
|
|
|
|
|
return context.Background()
|
|
|
|
|
}
|
|
|
|
|
token := base64.StdEncoding.EncodeToString(data)
|
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", this.apiConfig.NodeId, "token", token)
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
2020-09-30 17:46:49 +08:00
|
|
|
|
|
|
|
|
// 随机选择一个连接
|
|
|
|
|
func (this *RPCClient) pickConn() *grpc.ClientConn {
|
|
|
|
|
if len(this.conns) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return this.conns[rands.Int(0, len(this.conns)-1)]
|
|
|
|
|
}
|