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-11-02 15:49:30 +08:00
|
|
|
"google.golang.org/grpc/connectivity"
|
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-11-02 15:49:30 +08:00
|
|
|
"sync"
|
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-11-02 15:49:30 +08:00
|
|
|
|
|
|
|
|
locker sync.Mutex
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 15:49:30 +08:00
|
|
|
client := &RPCClient{
|
|
|
|
|
apiConfig: apiConfig,
|
2020-09-09 18:53:53 +08:00
|
|
|
}
|
2020-11-02 15:49:30 +08:00
|
|
|
|
|
|
|
|
err := client.init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2020-09-09 18:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-02 15:49:30 +08:00
|
|
|
return client, nil
|
2020-09-09 18:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-10-27 12:33:34 +08:00
|
|
|
func (this *RPCClient) APINodeRPC() pb.APINodeServiceClient {
|
|
|
|
|
return pb.NewAPINodeServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 10:45:44 +08:00
|
|
|
func (this *RPCClient) IPLibraryRPC() pb.IPLibraryServiceClient {
|
|
|
|
|
return pb.NewIPLibraryServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) RegionCountryRPC() pb.RegionCountryServiceClient {
|
|
|
|
|
return pb.NewRegionCountryServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) RegionProvinceRPC() pb.RegionProvinceServiceClient {
|
|
|
|
|
return pb.NewRegionProvinceServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) IPListRPC() pb.IPListServiceClient {
|
|
|
|
|
return pb.NewIPListServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) IPItemRPC() pb.IPItemServiceClient {
|
|
|
|
|
return pb.NewIPItemServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) FileRPC() pb.FileServiceClient {
|
|
|
|
|
return pb.NewFileServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) FileChunkRPC() pb.FileChunkServiceClient {
|
|
|
|
|
return pb.NewFileChunkServiceClient(this.pickConn())
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 11:14:40 +08:00
|
|
|
// 节点上下文信息
|
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
|
|
|
|
2020-10-17 11:14:40 +08:00
|
|
|
// 集群上下文
|
|
|
|
|
func (this *RPCClient) ClusterContext(clusterId string, clusterSecret string) context.Context {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
m := maps.Map{
|
|
|
|
|
"timestamp": time.Now().Unix(),
|
|
|
|
|
"type": "cluster",
|
|
|
|
|
"userId": 0,
|
|
|
|
|
}
|
|
|
|
|
method, err := encrypt.NewMethodInstance(teaconst.EncryptMethod, clusterSecret, clusterId)
|
|
|
|
|
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", clusterId, "token", token)
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 11:19:06 +08:00
|
|
|
// 关闭连接
|
|
|
|
|
func (this *RPCClient) Close() {
|
|
|
|
|
for _, conn := range this.conns {
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 15:49:30 +08:00
|
|
|
// 初始化
|
|
|
|
|
func (this *RPCClient) init() error {
|
|
|
|
|
// 重新连接
|
|
|
|
|
conns := []*grpc.ClientConn{}
|
|
|
|
|
for _, endpoint := range this.apiConfig.RPC.Endpoints {
|
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 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 errors.New("parse endpoint failed: invalid scheme '" + u.Scheme + "'")
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
conns = append(conns, conn)
|
|
|
|
|
}
|
|
|
|
|
if len(conns) == 0 {
|
|
|
|
|
return errors.New("[RPC]no available endpoints")
|
|
|
|
|
}
|
|
|
|
|
this.conns = conns
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 17:46:49 +08:00
|
|
|
// 随机选择一个连接
|
|
|
|
|
func (this *RPCClient) pickConn() *grpc.ClientConn {
|
2020-11-02 15:49:30 +08:00
|
|
|
this.locker.Lock()
|
|
|
|
|
defer this.locker.Unlock()
|
|
|
|
|
|
|
|
|
|
// 检查连接状态
|
|
|
|
|
if len(this.conns) > 0 {
|
|
|
|
|
availableConns := []*grpc.ClientConn{}
|
2020-11-15 11:58:08 +08:00
|
|
|
for _, state := range []connectivity.State{connectivity.Ready, connectivity.Idle, connectivity.Connecting} {
|
|
|
|
|
for _, conn := range this.conns {
|
|
|
|
|
if conn.GetState() == state {
|
|
|
|
|
availableConns = append(availableConns, conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(availableConns) > 0 {
|
|
|
|
|
break
|
2020-11-02 15:49:30 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(availableConns) > 0 {
|
|
|
|
|
return availableConns[rands.Int(0, len(availableConns)-1)]
|
|
|
|
|
}
|
2020-11-13 18:22:56 +08:00
|
|
|
|
|
|
|
|
// 关闭
|
|
|
|
|
for _, conn := range this.conns {
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}
|
2020-11-02 15:49:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重新初始化
|
|
|
|
|
err := this.init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 错误提示已经在构造对象时打印过,所以这里不再重复打印
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 17:46:49 +08:00
|
|
|
if len(this.conns) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-11-02 15:49:30 +08:00
|
|
|
|
2020-09-30 17:46:49 +08:00
|
|
|
return this.conns[rands.Int(0, len(this.conns)-1)]
|
|
|
|
|
}
|