2020-07-22 22:19:39 +08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"errors"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
|
|
|
|
|
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/encrypt"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
2020-09-13 20:37:07 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-07-22 22:19:39 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
"github.com/iwind/TeaGo/rands"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RPCClient struct {
|
2020-09-20 11:56:49 +08:00
|
|
|
apiConfig *configs.APIConfig
|
|
|
|
|
adminClients []pb.AdminServiceClient
|
|
|
|
|
nodeClients []pb.NodeServiceClient
|
|
|
|
|
nodeGrantClients []pb.NodeGrantServiceClient
|
|
|
|
|
nodeClusterClients []pb.NodeClusterServiceClient
|
|
|
|
|
nodeIPAddressClients []pb.NodeIPAddressServiceClient
|
|
|
|
|
serverClients []pb.ServerServiceClient
|
|
|
|
|
apiNodeClients []pb.APINodeServiceClient
|
2020-09-22 11:36:51 +08:00
|
|
|
originClients []pb.OriginServiceClient
|
2020-09-20 11:56:49 +08:00
|
|
|
httpWebClients []pb.HTTPWebServiceClient
|
|
|
|
|
reverseProxyClients []pb.ReverseProxyServiceClient
|
|
|
|
|
httpGzipClients []pb.HTTPGzipServiceClient
|
|
|
|
|
httpHeaderPolicyClients []pb.HTTPHeaderPolicyServiceClient
|
|
|
|
|
httpHeaderClients []pb.HTTPHeaderServiceClient
|
|
|
|
|
httpPageClients []pb.HTTPPageServiceClient
|
|
|
|
|
httpAccessLogPolicyClients []pb.HTTPAccessLogPolicyServiceClient
|
2020-09-20 16:28:16 +08:00
|
|
|
httpCachePolicyClients []pb.HTTPCachePolicyServiceClient
|
2020-09-20 20:12:43 +08:00
|
|
|
httpFirewallPolicyClients []pb.HTTPFirewallPolicyServiceClient
|
2020-09-21 19:51:50 +08:00
|
|
|
httpLocationClients []pb.HTTPLocationServiceClient
|
2020-07-22 22:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
|
|
|
|
|
if apiConfig == nil {
|
|
|
|
|
return nil, errors.New("api config should not be nil")
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
adminClients := []pb.AdminServiceClient{}
|
|
|
|
|
nodeClients := []pb.NodeServiceClient{}
|
|
|
|
|
nodeGrantClients := []pb.NodeGrantServiceClient{}
|
|
|
|
|
nodeClusterClients := []pb.NodeClusterServiceClient{}
|
2020-08-21 21:09:42 +08:00
|
|
|
nodeIPAddressClients := []pb.NodeIPAddressServiceClient{}
|
2020-07-29 19:34:54 +08:00
|
|
|
serverClients := []pb.ServerServiceClient{}
|
2020-09-06 16:19:34 +08:00
|
|
|
apiNodeClients := []pb.APINodeServiceClient{}
|
2020-09-22 11:36:51 +08:00
|
|
|
originClients := []pb.OriginServiceClient{}
|
2020-09-15 14:44:52 +08:00
|
|
|
httpWebClients := []pb.HTTPWebServiceClient{}
|
|
|
|
|
reverseProxyClients := []pb.ReverseProxyServiceClient{}
|
2020-09-16 09:09:10 +08:00
|
|
|
httpGzipClients := []pb.HTTPGzipServiceClient{}
|
2020-09-16 20:29:13 +08:00
|
|
|
httpHeaderPolicyClients := []pb.HTTPHeaderPolicyServiceClient{}
|
|
|
|
|
httpHeaderClients := []pb.HTTPHeaderServiceClient{}
|
2020-09-20 08:58:48 +08:00
|
|
|
httpPageClients := []pb.HTTPPageServiceClient{}
|
2020-09-20 11:56:49 +08:00
|
|
|
httpAccessLogPolicyClients := []pb.HTTPAccessLogPolicyServiceClient{}
|
2020-09-20 16:28:16 +08:00
|
|
|
httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{}
|
2020-09-20 20:12:43 +08:00
|
|
|
httpFirewallPolicyClients := []pb.HTTPFirewallPolicyServiceClient{}
|
2020-09-21 19:51:50 +08:00
|
|
|
httpLocationClients := []pb.HTTPLocationServiceClient{}
|
2020-07-22 22:19:39 +08:00
|
|
|
|
|
|
|
|
conns := []*grpc.ClientConn{}
|
|
|
|
|
for _, endpoint := range apiConfig.RPC.Endpoints {
|
|
|
|
|
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
conns = append(conns, conn)
|
|
|
|
|
}
|
|
|
|
|
if len(conns) == 0 {
|
|
|
|
|
return nil, errors.New("[RPC]no available endpoints")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// node clients
|
|
|
|
|
for _, conn := range conns {
|
2020-07-29 19:34:54 +08:00
|
|
|
adminClients = append(adminClients, pb.NewAdminServiceClient(conn))
|
|
|
|
|
nodeClients = append(nodeClients, pb.NewNodeServiceClient(conn))
|
|
|
|
|
nodeGrantClients = append(nodeGrantClients, pb.NewNodeGrantServiceClient(conn))
|
|
|
|
|
nodeClusterClients = append(nodeClusterClients, pb.NewNodeClusterServiceClient(conn))
|
2020-08-21 21:09:42 +08:00
|
|
|
nodeIPAddressClients = append(nodeIPAddressClients, pb.NewNodeIPAddressServiceClient(conn))
|
2020-07-29 19:34:54 +08:00
|
|
|
serverClients = append(serverClients, pb.NewServerServiceClient(conn))
|
2020-09-06 16:19:34 +08:00
|
|
|
apiNodeClients = append(apiNodeClients, pb.NewAPINodeServiceClient(conn))
|
2020-09-22 11:36:51 +08:00
|
|
|
originClients = append(originClients, pb.NewOriginServiceClient(conn))
|
2020-09-15 14:44:52 +08:00
|
|
|
httpWebClients = append(httpWebClients, pb.NewHTTPWebServiceClient(conn))
|
|
|
|
|
reverseProxyClients = append(reverseProxyClients, pb.NewReverseProxyServiceClient(conn))
|
2020-09-16 09:09:10 +08:00
|
|
|
httpGzipClients = append(httpGzipClients, pb.NewHTTPGzipServiceClient(conn))
|
2020-09-16 20:29:13 +08:00
|
|
|
httpHeaderPolicyClients = append(httpHeaderPolicyClients, pb.NewHTTPHeaderPolicyServiceClient(conn))
|
|
|
|
|
httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn))
|
2020-09-20 08:58:48 +08:00
|
|
|
httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn))
|
2020-09-20 11:56:49 +08:00
|
|
|
httpAccessLogPolicyClients = append(httpAccessLogPolicyClients, pb.NewHTTPAccessLogPolicyServiceClient(conn))
|
2020-09-20 16:28:16 +08:00
|
|
|
httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn))
|
2020-09-20 20:12:43 +08:00
|
|
|
httpFirewallPolicyClients = append(httpFirewallPolicyClients, pb.NewHTTPFirewallPolicyServiceClient(conn))
|
2020-09-21 19:51:50 +08:00
|
|
|
httpLocationClients = append(httpLocationClients, pb.NewHTTPLocationServiceClient(conn))
|
2020-07-22 22:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &RPCClient{
|
2020-09-20 11:56:49 +08:00
|
|
|
apiConfig: apiConfig,
|
|
|
|
|
adminClients: adminClients,
|
|
|
|
|
nodeClients: nodeClients,
|
|
|
|
|
nodeGrantClients: nodeGrantClients,
|
|
|
|
|
nodeClusterClients: nodeClusterClients,
|
|
|
|
|
nodeIPAddressClients: nodeIPAddressClients,
|
|
|
|
|
serverClients: serverClients,
|
|
|
|
|
apiNodeClients: apiNodeClients,
|
2020-09-22 11:36:51 +08:00
|
|
|
originClients: originClients,
|
2020-09-20 11:56:49 +08:00
|
|
|
httpWebClients: httpWebClients,
|
|
|
|
|
reverseProxyClients: reverseProxyClients,
|
|
|
|
|
httpGzipClients: httpGzipClients,
|
|
|
|
|
httpHeaderPolicyClients: httpHeaderPolicyClients,
|
|
|
|
|
httpHeaderClients: httpHeaderClients,
|
|
|
|
|
httpPageClients: httpPageClients,
|
|
|
|
|
httpAccessLogPolicyClients: httpAccessLogPolicyClients,
|
2020-09-20 16:28:16 +08:00
|
|
|
httpCachePolicyClients: httpCachePolicyClients,
|
2020-09-20 20:12:43 +08:00
|
|
|
httpFirewallPolicyClients: httpFirewallPolicyClients,
|
2020-09-21 19:51:50 +08:00
|
|
|
httpLocationClients: httpLocationClients,
|
2020-07-22 22:19:39 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
func (this *RPCClient) AdminRPC() pb.AdminServiceClient {
|
2020-07-22 22:19:39 +08:00
|
|
|
if len(this.adminClients) > 0 {
|
|
|
|
|
return this.adminClients[rands.Int(0, len(this.adminClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
func (this *RPCClient) NodeRPC() pb.NodeServiceClient {
|
|
|
|
|
if len(this.nodeClients) > 0 {
|
|
|
|
|
return this.nodeClients[rands.Int(0, len(this.nodeClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) NodeGrantRPC() pb.NodeGrantServiceClient {
|
|
|
|
|
if len(this.nodeGrantClients) > 0 {
|
|
|
|
|
return this.nodeGrantClients[rands.Int(0, len(this.nodeGrantClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) NodeClusterRPC() pb.NodeClusterServiceClient {
|
|
|
|
|
if len(this.nodeClusterClients) > 0 {
|
|
|
|
|
return this.nodeClusterClients[rands.Int(0, len(this.nodeClusterClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
func (this *RPCClient) NodeIPAddressRPC() pb.NodeIPAddressServiceClient {
|
|
|
|
|
if len(this.nodeIPAddressClients) > 0 {
|
|
|
|
|
return this.nodeIPAddressClients[rands.Int(0, len(this.nodeIPAddressClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
func (this *RPCClient) ServerRPC() pb.ServerServiceClient {
|
|
|
|
|
if len(this.serverClients) > 0 {
|
|
|
|
|
return this.serverClients[rands.Int(0, len(this.serverClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 16:19:34 +08:00
|
|
|
func (this *RPCClient) APINodeRPC() pb.APINodeServiceClient {
|
2020-09-13 20:37:07 +08:00
|
|
|
if len(this.apiNodeClients) > 0 {
|
2020-09-06 16:19:34 +08:00
|
|
|
return this.apiNodeClients[rands.Int(0, len(this.apiNodeClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 11:36:51 +08:00
|
|
|
func (this *RPCClient) OriginRPC() pb.OriginServiceClient {
|
|
|
|
|
if len(this.originClients) > 0 {
|
|
|
|
|
return this.originClients[rands.Int(0, len(this.originClients)-1)]
|
2020-09-13 20:37:07 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-15 14:44:52 +08:00
|
|
|
func (this *RPCClient) HTTPWebRPC() pb.HTTPWebServiceClient {
|
|
|
|
|
if len(this.httpWebClients) > 0 {
|
|
|
|
|
return this.httpWebClients[rands.Int(0, len(this.httpWebClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ReverseProxyRPC() pb.ReverseProxyServiceClient {
|
|
|
|
|
if len(this.reverseProxyClients) > 0 {
|
|
|
|
|
return this.reverseProxyClients[rands.Int(0, len(this.reverseProxyClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
func (this *RPCClient) HTTPGzipRPC() pb.HTTPGzipServiceClient {
|
|
|
|
|
if len(this.httpGzipClients) > 0 {
|
|
|
|
|
return this.httpGzipClients[rands.Int(0, len(this.httpGzipClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
func (this *RPCClient) HTTPHeaderRPC() pb.HTTPHeaderServiceClient {
|
|
|
|
|
if len(this.httpHeaderClients) > 0 {
|
|
|
|
|
return this.httpHeaderClients[rands.Int(0, len(this.httpHeaderClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) HTTPHeaderPolicyRPC() pb.HTTPHeaderPolicyServiceClient {
|
|
|
|
|
if len(this.httpHeaderPolicyClients) > 0 {
|
|
|
|
|
return this.httpHeaderPolicyClients[rands.Int(0, len(this.httpHeaderPolicyClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 08:58:48 +08:00
|
|
|
func (this *RPCClient) HTTPPageRPC() pb.HTTPPageServiceClient {
|
|
|
|
|
if len(this.httpPageClients) > 0 {
|
|
|
|
|
return this.httpPageClients[rands.Int(0, len(this.httpPageClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 11:56:49 +08:00
|
|
|
func (this *RPCClient) HTTPAccessLogPolicyRPC() pb.HTTPAccessLogPolicyServiceClient {
|
|
|
|
|
if len(this.httpAccessLogPolicyClients) > 0 {
|
|
|
|
|
return this.httpAccessLogPolicyClients[rands.Int(0, len(this.httpAccessLogPolicyClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 16:28:16 +08:00
|
|
|
func (this *RPCClient) HTTPCachePolicyRPC() pb.HTTPCachePolicyServiceClient {
|
|
|
|
|
if len(this.httpCachePolicyClients) > 0 {
|
|
|
|
|
return this.httpCachePolicyClients[rands.Int(0, len(this.httpCachePolicyClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 20:12:43 +08:00
|
|
|
func (this *RPCClient) HTTPFirewallPolicyRPC() pb.HTTPFirewallPolicyServiceClient {
|
|
|
|
|
if len(this.httpFirewallPolicyClients) > 0 {
|
|
|
|
|
return this.httpFirewallPolicyClients[rands.Int(0, len(this.httpFirewallPolicyClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
func (this *RPCClient) HTTPLocationRPC() pb.HTTPLocationServiceClient {
|
|
|
|
|
if len(this.httpLocationClients) > 0 {
|
|
|
|
|
return this.httpLocationClients[rands.Int(0, len(this.httpLocationClients)-1)]
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
func (this *RPCClient) Context(adminId int64) context.Context {
|
2020-07-22 22:19:39 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
m := maps.Map{
|
|
|
|
|
"timestamp": time.Now().Unix(),
|
2020-07-29 19:34:54 +08:00
|
|
|
"type": "admin",
|
|
|
|
|
"userId": adminId,
|
2020-07-22 22:19:39 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|