2020-07-22 22:19:39 +08:00
|
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2020-10-11 11:52:50 +08:00
|
|
|
|
"crypto/tls"
|
2020-07-22 22:19:39 +08:00
|
|
|
|
"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-12-23 09:52:31 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
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"
|
2020-11-02 14:37:28 +08:00
|
|
|
|
"google.golang.org/grpc/connectivity"
|
2020-10-11 11:52:50 +08:00
|
|
|
|
"google.golang.org/grpc/credentials"
|
2020-07-22 22:19:39 +08:00
|
|
|
|
"google.golang.org/grpc/metadata"
|
2020-10-04 14:27:05 +08:00
|
|
|
|
"net/url"
|
2020-11-02 14:37:28 +08:00
|
|
|
|
"sync"
|
2020-07-22 22:19:39 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-05-19 19:03:03 +08:00
|
|
|
|
// RPCClient RPC客户端
|
2020-07-22 22:19:39 +08:00
|
|
|
|
type RPCClient struct {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
apiConfig *configs.APIConfig
|
|
|
|
|
|
conns []*grpc.ClientConn
|
2020-11-02 14:37:28 +08:00
|
|
|
|
|
|
|
|
|
|
locker sync.Mutex
|
2020-07-22 22:19:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-19 19:03:03 +08:00
|
|
|
|
// NewRPCClient 构造新的RPC客户端
|
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-11-02 14:37:28 +08:00
|
|
|
|
client := &RPCClient{
|
|
|
|
|
|
apiConfig: apiConfig,
|
2020-07-22 22:19:39 +08:00
|
|
|
|
}
|
2020-11-02 14:37:28 +08:00
|
|
|
|
|
|
|
|
|
|
err := client.init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2020-07-22 22:19:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-23 09:52:31 +08:00
|
|
|
|
// 设置RPC
|
|
|
|
|
|
dao.SetRPC(client)
|
|
|
|
|
|
|
2020-11-02 14:37:28 +08:00
|
|
|
|
return client, nil
|
2020-07-22 22:19:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
|
func (this *RPCClient) AdminRPC() pb.AdminServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewAdminServiceClient(this.pickConn())
|
2020-07-22 22:19:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
|
func (this *RPCClient) NodeRPC() pb.NodeServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewNodeServiceClient(this.pickConn())
|
2020-07-29 19:34:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-09 11:07:01 +08:00
|
|
|
|
func (this *RPCClient) NodeLogRPC() pb.NodeLogServiceClient {
|
|
|
|
|
|
return pb.NewNodeLogServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
|
func (this *RPCClient) NodeGrantRPC() pb.NodeGrantServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewNodeGrantServiceClient(this.pickConn())
|
2020-07-29 19:34:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) NodeClusterRPC() pb.NodeClusterServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewNodeClusterServiceClient(this.pickConn())
|
2020-07-29 19:34:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-06 17:37:09 +08:00
|
|
|
|
func (this *RPCClient) NodeClusterFirewallActionRPC() pb.NodeClusterFirewallActionServiceClient {
|
|
|
|
|
|
return pb.NewNodeClusterFirewallActionServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-28 18:21:11 +08:00
|
|
|
|
func (this *RPCClient) NodeGroupRPC() pb.NodeGroupServiceClient {
|
|
|
|
|
|
return pb.NewNodeGroupServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-10 15:02:55 +08:00
|
|
|
|
func (this *RPCClient) NodeRegionRPC() pb.NodeRegionServiceClient {
|
|
|
|
|
|
return pb.NewNodeRegionServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-10 22:06:54 +08:00
|
|
|
|
func (this *RPCClient) NodePriceItemRPC() pb.NodePriceItemServiceClient {
|
|
|
|
|
|
return pb.NewNodePriceItemServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
|
func (this *RPCClient) NodeIPAddressRPC() pb.NodeIPAddressServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewNodeIPAddressServiceClient(this.pickConn())
|
2020-08-21 21:09:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-29 16:47:45 +08:00
|
|
|
|
func (this *RPCClient) NodeValueRPC() pb.NodeValueServiceClient {
|
|
|
|
|
|
return pb.NewNodeValueServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-04 21:02:25 +08:00
|
|
|
|
func (this *RPCClient) NodeThresholdRPC() pb.NodeThresholdServiceClient {
|
|
|
|
|
|
return pb.NewNodeThresholdServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
|
func (this *RPCClient) ServerRPC() pb.ServerServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewServerServiceClient(this.pickConn())
|
2020-07-29 19:34:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-25 16:40:49 +08:00
|
|
|
|
func (this *RPCClient) ServerClientSystemMonthlyStatRPC() pb.ServerClientSystemMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerClientSystemMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ServerClientBrowserMonthlyStatRPC() pb.ServerClientBrowserMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerClientBrowserMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ServerRegionCountryMonthlyStatRPC() pb.ServerRegionCountryMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerRegionCountryMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ServerRegionProvinceMonthlyStatRPC() pb.ServerRegionProvinceMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerRegionProvinceMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ServerRegionCityMonthlyStatRPC() pb.ServerRegionCityMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerRegionCityMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ServerRegionProviderMonthlyStatRPC() pb.ServerRegionProviderMonthlyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerRegionProviderMonthlyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-26 18:41:23 +08:00
|
|
|
|
func (this *RPCClient) ServerHTTPFirewallDailyStatRPC() pb.ServerHTTPFirewallDailyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerHTTPFirewallDailyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-08 11:22:44 +08:00
|
|
|
|
func (this *RPCClient) ServerDailyStatRPC() pb.ServerDailyStatServiceClient {
|
|
|
|
|
|
return pb.NewServerDailyStatServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 20:53:08 +08:00
|
|
|
|
func (this *RPCClient) ServerGroupRPC() pb.ServerGroupServiceClient {
|
|
|
|
|
|
return pb.NewServerGroupServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-06 16:19:34 +08:00
|
|
|
|
func (this *RPCClient) APINodeRPC() pb.APINodeServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewAPINodeServiceClient(this.pickConn())
|
2020-09-06 16:19:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-14 21:24:21 +08:00
|
|
|
|
func (this *RPCClient) UserNodeRPC() pb.UserNodeServiceClient {
|
|
|
|
|
|
return pb.NewUserNodeServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-08 17:54:57 +08:00
|
|
|
|
func (this *RPCClient) DBNodeRPC() pb.DBNodeServiceClient {
|
|
|
|
|
|
return pb.NewDBNodeServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-06 16:32:23 +08:00
|
|
|
|
func (this *RPCClient) MonitorNodeRPC() pb.MonitorNodeServiceClient {
|
|
|
|
|
|
return pb.NewMonitorNodeServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-19 16:14:09 +08:00
|
|
|
|
func (this *RPCClient) DBRPC() pb.DBServiceClient {
|
|
|
|
|
|
return pb.NewDBServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-22 11:36:51 +08:00
|
|
|
|
func (this *RPCClient) OriginRPC() pb.OriginServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewOriginServiceClient(this.pickConn())
|
2020-09-13 20:37:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-15 14:44:52 +08:00
|
|
|
|
func (this *RPCClient) HTTPWebRPC() pb.HTTPWebServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPWebServiceClient(this.pickConn())
|
2020-09-15 14:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) ReverseProxyRPC() pb.ReverseProxyServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewReverseProxyServiceClient(this.pickConn())
|
2020-09-15 14:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
|
func (this *RPCClient) HTTPGzipRPC() pb.HTTPGzipServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPGzipServiceClient(this.pickConn())
|
2020-09-16 09:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 20:29:13 +08:00
|
|
|
|
func (this *RPCClient) HTTPHeaderRPC() pb.HTTPHeaderServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPHeaderServiceClient(this.pickConn())
|
2020-09-16 20:29:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) HTTPHeaderPolicyRPC() pb.HTTPHeaderPolicyServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPHeaderPolicyServiceClient(this.pickConn())
|
2020-09-16 20:29:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-20 08:58:48 +08:00
|
|
|
|
func (this *RPCClient) HTTPPageRPC() pb.HTTPPageServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPPageServiceClient(this.pickConn())
|
2020-09-20 08:58:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-20 11:56:49 +08:00
|
|
|
|
func (this *RPCClient) HTTPAccessLogPolicyRPC() pb.HTTPAccessLogPolicyServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPAccessLogPolicyServiceClient(this.pickConn())
|
2020-09-20 11:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-20 16:28:16 +08:00
|
|
|
|
func (this *RPCClient) HTTPCachePolicyRPC() pb.HTTPCachePolicyServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPCachePolicyServiceClient(this.pickConn())
|
2020-09-20 16:28:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-20 20:12:43 +08:00
|
|
|
|
func (this *RPCClient) HTTPFirewallPolicyRPC() pb.HTTPFirewallPolicyServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPFirewallPolicyServiceClient(this.pickConn())
|
2020-09-20 20:12:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-07 11:18:07 +08:00
|
|
|
|
func (this *RPCClient) HTTPFirewallRuleGroupRPC() pb.HTTPFirewallRuleGroupServiceClient {
|
|
|
|
|
|
return pb.NewHTTPFirewallRuleGroupServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-08 11:11:37 +08:00
|
|
|
|
func (this *RPCClient) HTTPFirewallRuleSetRPC() pb.HTTPFirewallRuleSetServiceClient {
|
|
|
|
|
|
return pb.NewHTTPFirewallRuleSetServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
|
func (this *RPCClient) HTTPLocationRPC() pb.HTTPLocationServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPLocationServiceClient(this.pickConn())
|
2020-09-21 19:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-26 19:54:02 +08:00
|
|
|
|
func (this *RPCClient) HTTPWebsocketRPC() pb.HTTPWebsocketServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPWebsocketServiceClient(this.pickConn())
|
2020-09-26 19:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-28 16:25:26 +08:00
|
|
|
|
func (this *RPCClient) HTTPRewriteRuleRPC() pb.HTTPRewriteRuleServiceClient {
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return pb.NewHTTPRewriteRuleServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-13 20:01:43 +08:00
|
|
|
|
// HTTPAccessLogRPC 访问日志
|
2020-10-10 19:22:17 +08:00
|
|
|
|
func (this *RPCClient) HTTPAccessLogRPC() pb.HTTPAccessLogServiceClient {
|
|
|
|
|
|
return pb.NewHTTPAccessLogServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-10 21:13:09 +08:00
|
|
|
|
func (this *RPCClient) HTTPFastcgiRPC() pb.HTTPFastcgiServiceClient {
|
|
|
|
|
|
return pb.NewHTTPFastcgiServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-19 21:35:38 +08:00
|
|
|
|
func (this *RPCClient) HTTPAuthPolicyRPC() pb.HTTPAuthPolicyServiceClient {
|
|
|
|
|
|
return pb.NewHTTPAuthPolicyServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-30 17:46:38 +08:00
|
|
|
|
func (this *RPCClient) SSLCertRPC() pb.SSLCertServiceClient {
|
|
|
|
|
|
return pb.NewSSLCertServiceClient(this.pickConn())
|
2020-09-28 16:25:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-01 16:01:04 +08:00
|
|
|
|
func (this *RPCClient) SSLPolicyRPC() pb.SSLPolicyServiceClient {
|
|
|
|
|
|
return pb.NewSSLPolicyServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-02 17:22:24 +08:00
|
|
|
|
func (this *RPCClient) SysSettingRPC() pb.SysSettingServiceClient {
|
|
|
|
|
|
return pb.NewSysSettingServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-20 20:18:12 +08:00
|
|
|
|
func (this *RPCClient) MessageRPC() pb.MessageServiceClient {
|
|
|
|
|
|
return pb.NewMessageServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-05 20:48:13 +08:00
|
|
|
|
func (this *RPCClient) MessageRecipientGroupRPC() pb.MessageRecipientGroupServiceClient {
|
|
|
|
|
|
return pb.NewMessageRecipientGroupServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) MessageRecipientRPC() pb.MessageRecipientServiceClient {
|
|
|
|
|
|
return pb.NewMessageRecipientServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) MessageMediaRPC() pb.MessageMediaServiceClient {
|
|
|
|
|
|
return pb.NewMessageMediaServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) MessageMediaInstanceRPC() pb.MessageMediaInstanceServiceClient {
|
|
|
|
|
|
return pb.NewMessageMediaInstanceServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) MessageTaskRPC() pb.MessageTaskServiceClient {
|
|
|
|
|
|
return pb.NewMessageTaskServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) MessageTaskLogRPC() pb.MessageTaskLogServiceClient {
|
|
|
|
|
|
return pb.NewMessageTaskLogServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-12 19:19:59 +08:00
|
|
|
|
func (this *RPCClient) MessageReceiverRPC() pb.MessageReceiverServiceClient {
|
|
|
|
|
|
return pb.NewMessageReceiverServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-04 15:50:53 +08:00
|
|
|
|
func (this *RPCClient) IPLibraryRPC() pb.IPLibraryServiceClient {
|
|
|
|
|
|
return pb.NewIPLibraryServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-07 19:40:18 +08:00
|
|
|
|
func (this *RPCClient) IPListRPC() pb.IPListServiceClient {
|
|
|
|
|
|
return pb.NewIPListServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) IPItemRPC() pb.IPItemServiceClient {
|
|
|
|
|
|
return pb.NewIPItemServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-04 15:50:53 +08:00
|
|
|
|
func (this *RPCClient) FileRPC() pb.FileServiceClient {
|
|
|
|
|
|
return pb.NewFileServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) FileChunkRPC() pb.FileChunkServiceClient {
|
|
|
|
|
|
return pb.NewFileChunkServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-06 11:02:26 +08:00
|
|
|
|
func (this *RPCClient) RegionCountryRPC() pb.RegionCountryServiceClient {
|
|
|
|
|
|
return pb.NewRegionCountryServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) RegionProvinceRPC() pb.RegionProvinceServiceClient {
|
|
|
|
|
|
return pb.NewRegionProvinceServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-10 20:30:47 +08:00
|
|
|
|
func (this *RPCClient) LogRPC() pb.LogServiceClient {
|
|
|
|
|
|
return pb.NewLogServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-11 21:32:19 +08:00
|
|
|
|
func (this *RPCClient) DNSProviderRPC() pb.DNSProviderServiceClient {
|
|
|
|
|
|
return pb.NewDNSProviderServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-12 14:41:34 +08:00
|
|
|
|
func (this *RPCClient) DNSDomainRPC() pb.DNSDomainServiceClient {
|
|
|
|
|
|
return pb.NewDNSDomainServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-14 21:28:14 +08:00
|
|
|
|
func (this *RPCClient) DNSRPC() pb.DNSServiceClient {
|
|
|
|
|
|
return pb.NewDNSServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-27 22:59:46 +08:00
|
|
|
|
func (this *RPCClient) DNSTaskRPC() pb.DNSTaskServiceClient {
|
|
|
|
|
|
return pb.NewDNSTaskServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-24 17:36:42 +08:00
|
|
|
|
func (this *RPCClient) ACMEUserRPC() pb.ACMEUserServiceClient {
|
|
|
|
|
|
return pb.NewACMEUserServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-25 21:19:07 +08:00
|
|
|
|
func (this *RPCClient) ACMETaskRPC() pb.ACMETaskServiceClient {
|
|
|
|
|
|
return pb.NewACMETaskServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-04 16:00:55 +08:00
|
|
|
|
func (this *RPCClient) UserRPC() pb.UserServiceClient {
|
|
|
|
|
|
return pb.NewUserServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-11 21:39:24 +08:00
|
|
|
|
func (this *RPCClient) UserBillRPC() pb.UserBillServiceClient {
|
|
|
|
|
|
return pb.NewUserBillServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-16 10:48:45 +08:00
|
|
|
|
func (this *RPCClient) UserAccessKeyRPC() pb.UserAccessKeyServiceClient {
|
|
|
|
|
|
return pb.NewUserAccessKeyServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-24 17:15:53 +08:00
|
|
|
|
func (this *RPCClient) LoginRPC() pb.LoginServiceClient {
|
|
|
|
|
|
return pb.NewLoginServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-17 16:47:29 +08:00
|
|
|
|
func (this *RPCClient) NodeTaskRPC() pb.NodeTaskServiceClient {
|
|
|
|
|
|
return pb.NewNodeTaskServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-13 20:01:43 +08:00
|
|
|
|
func (this *RPCClient) AuthorityKeyRPC() pb.AuthorityKeyServiceClient {
|
|
|
|
|
|
return pb.NewAuthorityKeyServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-13 21:23:13 +08:00
|
|
|
|
func (this *RPCClient) AuthorityNodeRPC() pb.AuthorityNodeServiceClient {
|
|
|
|
|
|
return pb.NewAuthorityNodeServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-03 11:32:59 +08:00
|
|
|
|
func (this *RPCClient) LatestItemRPC() pb.LatestItemServiceClient {
|
|
|
|
|
|
return pb.NewLatestItemServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-25 15:47:40 +08:00
|
|
|
|
func (this *RPCClient) NSClusterRPC() pb.NSClusterServiceClient {
|
|
|
|
|
|
return pb.NewNSClusterServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-26 14:43:29 +08:00
|
|
|
|
func (this *RPCClient) NSNodeRPC() pb.NSNodeServiceClient {
|
|
|
|
|
|
return pb.NewNSNodeServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-27 17:09:53 +08:00
|
|
|
|
func (this *RPCClient) NSDomainRPC() pb.NSDomainServiceClient {
|
|
|
|
|
|
return pb.NewNSDomainServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *RPCClient) NSRecordRPC() pb.NSRecordServiceClient {
|
|
|
|
|
|
return pb.NewNSRecordServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-30 16:31:12 +08:00
|
|
|
|
func (this *RPCClient) NSRouteRPC() pb.NSRouteServiceClient {
|
|
|
|
|
|
return pb.NewNSRouteServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-02 11:53:08 +08:00
|
|
|
|
func (this *RPCClient) NSAccessLogRPC() pb.NSAccessLogServiceClient {
|
|
|
|
|
|
return pb.NewNSAccessLogServiceClient(this.pickConn())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-13 20:01:43 +08:00
|
|
|
|
// Context 构造Admin上下文
|
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
|
|
|
|
|
|
}
|
2020-09-30 17:46:38 +08:00
|
|
|
|
|
2021-04-13 20:01:43 +08:00
|
|
|
|
// APIContext 构造API上下文
|
2020-10-13 20:05:29 +08:00
|
|
|
|
func (this *RPCClient) APIContext(apiNodeId int64) context.Context {
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
m := maps.Map{
|
|
|
|
|
|
"timestamp": time.Now().Unix(),
|
|
|
|
|
|
"type": "api",
|
|
|
|
|
|
"userId": apiNodeId,
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-13 20:01:43 +08:00
|
|
|
|
// UpdateConfig 修改配置
|
2021-02-24 09:00:12 +08:00
|
|
|
|
func (this *RPCClient) UpdateConfig(config *configs.APIConfig) error {
|
|
|
|
|
|
this.apiConfig = config
|
|
|
|
|
|
return this.init()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-02 14:37:28 +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" {
|
2021-01-16 17:31:10 +08:00
|
|
|
|
conn, err = grpc.Dial(u.Host, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(128*1024*1024)))
|
2020-11-02 14:37:28 +08:00
|
|
|
|
} else if u.Scheme == "https" {
|
|
|
|
|
|
conn, err = grpc.Dial(u.Host, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
|
|
|
|
|
InsecureSkipVerify: true,
|
2021-01-16 17:31:10 +08:00
|
|
|
|
})), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(128*1024*1024)))
|
2020-11-02 14:37:28 +08:00
|
|
|
|
} 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")
|
|
|
|
|
|
}
|
2021-02-24 10:44:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 这里不需要加锁,因为会和pickConn冲突
|
2020-11-02 14:37:28 +08:00
|
|
|
|
this.conns = conns
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-30 17:46:38 +08:00
|
|
|
|
// 随机选择一个连接
|
|
|
|
|
|
func (this *RPCClient) pickConn() *grpc.ClientConn {
|
2020-11-02 14:37:28 +08:00
|
|
|
|
this.locker.Lock()
|
|
|
|
|
|
defer this.locker.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
// 检查连接状态
|
|
|
|
|
|
if len(this.conns) > 0 {
|
|
|
|
|
|
availableConns := []*grpc.ClientConn{}
|
2020-11-15 11:57:43 +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 14:37:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(availableConns) > 0 {
|
|
|
|
|
|
return availableConns[rands.Int(0, len(availableConns)-1)]
|
|
|
|
|
|
}
|
2020-11-13 18:22:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 关闭
|
|
|
|
|
|
for _, conn := range this.conns {
|
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
|
}
|
2020-11-02 14:37:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重新初始化
|
|
|
|
|
|
err := this.init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
// 错误提示已经在构造对象时打印过,所以这里不再重复打印
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-30 17:46:38 +08:00
|
|
|
|
if len(this.conns) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2020-11-02 14:37:28 +08:00
|
|
|
|
|
2020-09-30 17:46:38 +08:00
|
|
|
|
return this.conns[rands.Int(0, len(this.conns)-1)]
|
|
|
|
|
|
}
|