增加配置分组配置

This commit is contained in:
GoEdgeLab
2021-09-22 19:39:55 +08:00
parent b7450ff0d5
commit 4f93551248
13 changed files with 1438 additions and 154 deletions

View File

@@ -210,8 +210,8 @@ func (this *NodeConfig) Init() error {
}
// AvailableGroups 根据网络地址和协议分组
func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerGroup {
groupMapping := map[string]*serverconfigs.ServerGroup{} // protocol://addr => Server Group
func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerAddressGroup {
groupMapping := map[string]*serverconfigs.ServerAddressGroup{} // protocol://addr => Server Group
for _, server := range this.Servers {
if !server.IsOk() || !server.IsOn {
continue
@@ -221,13 +221,13 @@ func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerGroup {
if ok {
group.Add(server)
} else {
group = serverconfigs.NewServerGroup(addr)
group = serverconfigs.NewServerAddressGroup(addr)
group.Add(server)
}
groupMapping[addr] = group
}
}
result := []*serverconfigs.ServerGroup{}
result := []*serverconfigs.ServerAddressGroup{}
for _, group := range groupMapping {
result = append(result, group)
}

View File

@@ -791,6 +791,7 @@ func (x *UpdateServerWebRequest) GetWebId() int64 {
return 0
}
// 修改服务的反向代理设置
type UpdateServerReverseProxyRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache

File diff suppressed because it is too large Load Diff

View File

@@ -199,6 +199,7 @@ message UpdateServerWebRequest {
int64 webId = 2;
}
// 修改服务的反向代理设置
message UpdateServerReverseProxyRequest {
int64 serverId = 1;
bytes reverseProxyJSON = 2;

View File

@@ -25,6 +25,27 @@ service ServerGroupService {
// 查找单个分组信息
rpc findEnabledServerGroup (FindEnabledServerGroupRequest) returns (FindEnabledServerGroupResponse);
// 查找HTTP反向代理设置
rpc findAndInitServerGroupHTTPReverseProxyConfig (FindAndInitServerGroupHTTPReverseProxyConfigRequest) returns (FindAndInitServerGroupHTTPReverseProxyConfigResponse);
// 查找TCP反向代理设置
rpc findAndInitServerGroupTCPReverseProxyConfig (FindAndInitServerGroupTCPReverseProxyConfigRequest) returns (FindAndInitServerGroupTCPReverseProxyConfigResponse);
// 查找UDP反向代理设置
rpc findAndInitServerGroupUDPReverseProxyConfig (FindAndInitServerGroupUDPReverseProxyConfigRequest) returns (FindAndInitServerGroupUDPReverseProxyConfigResponse);
// 修改分组的HTTP反向代理设置
rpc updateServerGroupHTTPReverseProxy (UpdateServerGroupHTTPReverseProxyRequest) returns (RPCSuccess);
// 修改分组的TCP反向代理设置
rpc updateServerGroupTCPReverseProxy (UpdateServerGroupTCPReverseProxyRequest) returns (RPCSuccess);
// 修改分组的UDP反向代理设置
rpc updateServerGroupUDPReverseProxy (UpdateServerGroupUDPReverseProxyRequest) returns (RPCSuccess);
// 取得分组的配置概要信息
rpc findEnabledServerGroupConfigInfo (FindEnabledServerGroupConfigInfoRequest) returns (FindEnabledServerGroupConfigInfoResponse);
}
// 创建分组
@@ -67,4 +88,64 @@ message FindEnabledServerGroupRequest {
message FindEnabledServerGroupResponse {
ServerGroup serverGroup = 1;
}
// 查找HTTP反向代理设置
message FindAndInitServerGroupHTTPReverseProxyConfigRequest {
int64 serverGroupId = 1;
}
message FindAndInitServerGroupHTTPReverseProxyConfigResponse {
bytes reverseProxyJSON = 1;
bytes reverseProxyRefJSON = 2;
}
// 查找TCP反向代理设置
message FindAndInitServerGroupTCPReverseProxyConfigRequest {
int64 serverGroupId = 1;
}
message FindAndInitServerGroupTCPReverseProxyConfigResponse {
bytes reverseProxyJSON = 1;
bytes reverseProxyRefJSON = 2;
}
// 查找UDP反向代理设置
message FindAndInitServerGroupUDPReverseProxyConfigRequest {
int64 serverGroupId = 1;
}
message FindAndInitServerGroupUDPReverseProxyConfigResponse {
bytes reverseProxyJSON = 1;
bytes reverseProxyRefJSON = 2;
}
// 修改分组的HTTP反向代理设置
message UpdateServerGroupHTTPReverseProxyRequest {
int64 serverGroupId = 1;
bytes reverseProxyJSON = 2;
}
// 修改分组的TCP反向代理设置
message UpdateServerGroupTCPReverseProxyRequest {
int64 serverGroupId = 1;
bytes reverseProxyJSON = 2;
}
// 修改分组的UDP反向代理设置
message UpdateServerGroupUDPReverseProxyRequest {
int64 serverGroupId = 1;
bytes reverseProxyJSON = 2;
}
// 取得分组的配置概要信息
message FindEnabledServerGroupConfigInfoRequest {
int64 serverGroupId = 1;
int64 serverId = 2;
}
message FindEnabledServerGroupConfigInfoResponse {
bool hasHTTPReverseProxy = 1;
bool hasTCPReverseProxy = 2;
bool hasUDPReverseProxy = 3;
}

View File

@@ -1,13 +1,13 @@
package serverconfigs
// 反向代理引用
// ReverseProxyRef 反向代理引用
type ReverseProxyRef struct {
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
ReverseProxyId int64 `yaml:"reverseProxyId" json:"reverseProxyId"` // 反向代理ID
}
// 初始化
// Init 初始化
func (this *ReverseProxyRef) Init() error {
return nil
}

View File

@@ -73,6 +73,6 @@ func (this *RandomScheduling) Summary() maps.Map {
"code": "random",
"name": "Random随机算法",
"description": "根据权重设置随机分配源站",
"networks": []string{"http", "tcp"},
"networks": []string{"http", "tcp", "udp", "unix"},
}
}

View File

@@ -75,6 +75,6 @@ func (this *RoundRobinScheduling) Summary() maps.Map {
"code": "roundRobin",
"name": "RoundRobin轮询算法",
"description": "根据权重,依次分配源站",
"networks": []string{"http", "tcp"},
"networks": []string{"http", "tcp", "udp", "unix"},
}
}

View File

@@ -0,0 +1,85 @@
package serverconfigs
import "strings"
type ServerAddressGroup struct {
fullAddr string
Servers []*ServerConfig
}
func NewServerAddressGroup(fullAddr string) *ServerAddressGroup {
return &ServerAddressGroup{fullAddr: fullAddr}
}
// Add 添加服务
func (this *ServerAddressGroup) Add(server *ServerConfig) {
this.Servers = append(this.Servers, server)
}
// FullAddr 获取完整的地址
func (this *ServerAddressGroup) FullAddr() string {
return this.fullAddr
}
// Protocol 获取当前分组的协议
func (this *ServerAddressGroup) Protocol() Protocol {
for _, p := range AllProtocols() {
if strings.HasPrefix(this.fullAddr, p.String()+":") {
return p
}
}
return ProtocolHTTP
}
// Addr 获取当前分组的地址
func (this *ServerAddressGroup) Addr() string {
protocol := this.Protocol()
if protocol == ProtocolUnix {
return strings.TrimPrefix(this.fullAddr, protocol.String()+":")
}
return strings.TrimPrefix(this.fullAddr, protocol.String()+"://")
}
// IsHTTP 判断当前分组是否为HTTP
func (this *ServerAddressGroup) IsHTTP() bool {
p := this.Protocol()
return p == ProtocolHTTP || p == ProtocolHTTP4 || p == ProtocolHTTP6
}
// IsHTTPS 判断当前分组是否为HTTPS
func (this *ServerAddressGroup) IsHTTPS() bool {
p := this.Protocol()
return p == ProtocolHTTPS || p == ProtocolHTTPS4 || p == ProtocolHTTPS6
}
// IsTCP 判断当前分组是否为TCP
func (this *ServerAddressGroup) IsTCP() bool {
p := this.Protocol()
return p == ProtocolTCP || p == ProtocolTCP4 || p == ProtocolTCP6
}
// IsTLS 判断当前分组是否为TLS
func (this *ServerAddressGroup) IsTLS() bool {
p := this.Protocol()
return p == ProtocolTLS || p == ProtocolTLS4 || p == ProtocolTLS6
}
// IsUnix 判断当前分组是否为Unix
func (this *ServerAddressGroup) IsUnix() bool {
p := this.Protocol()
return p == ProtocolUnix
}
// IsUDP 判断当前分组是否为UDP
func (this *ServerAddressGroup) IsUDP() bool {
p := this.Protocol()
return p == ProtocolUDP
}
// FirstServer 获取第一个Server
func (this *ServerAddressGroup) FirstServer() *ServerConfig {
if len(this.Servers) > 0 {
return this.Servers[0]
}
return nil
}

View File

@@ -5,29 +5,29 @@ import (
"testing"
)
func TestServerGroup_Protocol(t *testing.T) {
func TestServerAddressGroup_Protocol(t *testing.T) {
a := assert.NewAssertion(t)
{
group := NewServerGroup("tcp://127.0.0.1:1234")
group := NewServerAddressGroup("tcp://127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolTCP)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("http4://127.0.0.1:1234")
group := NewServerAddressGroup("http4://127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolHTTP4)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("127.0.0.1:1234")
group := NewServerAddressGroup("127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolHTTP)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("unix:/tmp/my.sock")
group := NewServerAddressGroup("unix:/tmp/my.sock")
a.IsTrue(group.Protocol() == ProtocolUnix)
a.IsTrue(group.Addr() == "/tmp/my.sock")
}

View File

@@ -41,6 +41,9 @@ type ServerConfig struct {
HTTPCachePolicyId int64 `yaml:"httpCachePolicyId" json:"httpCachePolicyId"`
HTTPCachePolicy *HTTPCachePolicy `yaml:"httpCachePolicy" json:"httpCachePolicy"` // 通过 HTTPCachePolicyId 获取
// 分组
Group *ServerGroupConfig `yaml:"group" json:"group"`
isOk bool
}
@@ -56,6 +59,23 @@ func NewServerConfig() *ServerConfig {
}
func (this *ServerConfig) Init() error {
// 分解Group
if this.Group != nil && this.Group.IsOn {
// reverse proxy
if this.IsHTTPFamily() && this.Group.HTTPReverseProxyRef != nil && this.Group.HTTPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.HTTPReverseProxyRef
this.ReverseProxy = this.Group.HTTPReverseProxy
}
if this.IsTCPFamily() && this.Group.TCPReverseProxyRef != nil && this.Group.TCPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.TCPReverseProxyRef
this.ReverseProxy = this.Group.TCPReverseProxy
}
if this.IsUDPFamily() && this.Group.UDPReverseProxyRef != nil && this.Group.UDPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.UDPReverseProxyRef
this.ReverseProxy = this.Group.UDPReverseProxy
}
}
if this.HTTP != nil {
err := this.HTTP.Init()
if err != nil {

View File

@@ -1,85 +0,0 @@
package serverconfigs
import "strings"
type ServerGroup struct {
fullAddr string
Servers []*ServerConfig
}
func NewServerGroup(fullAddr string) *ServerGroup {
return &ServerGroup{fullAddr: fullAddr}
}
// 添加服务
func (this *ServerGroup) Add(server *ServerConfig) {
this.Servers = append(this.Servers, server)
}
// 获取完整的地址
func (this *ServerGroup) FullAddr() string {
return this.fullAddr
}
// 获取当前分组的协议
func (this *ServerGroup) Protocol() Protocol {
for _, p := range AllProtocols() {
if strings.HasPrefix(this.fullAddr, p.String()+":") {
return p
}
}
return ProtocolHTTP
}
// 获取当前分组的地址
func (this *ServerGroup) Addr() string {
protocol := this.Protocol()
if protocol == ProtocolUnix {
return strings.TrimPrefix(this.fullAddr, protocol.String()+":")
}
return strings.TrimPrefix(this.fullAddr, protocol.String()+"://")
}
// 判断当前分组是否为HTTP
func (this *ServerGroup) IsHTTP() bool {
p := this.Protocol()
return p == ProtocolHTTP || p == ProtocolHTTP4 || p == ProtocolHTTP6
}
// 判断当前分组是否为HTTPS
func (this *ServerGroup) IsHTTPS() bool {
p := this.Protocol()
return p == ProtocolHTTPS || p == ProtocolHTTPS4 || p == ProtocolHTTPS6
}
// 判断当前分组是否为TCP
func (this *ServerGroup) IsTCP() bool {
p := this.Protocol()
return p == ProtocolTCP || p == ProtocolTCP4 || p == ProtocolTCP6
}
// 判断当前分组是否为TLS
func (this *ServerGroup) IsTLS() bool {
p := this.Protocol()
return p == ProtocolTLS || p == ProtocolTLS4 || p == ProtocolTLS6
}
// 判断当前分组是否为Unix
func (this *ServerGroup) IsUnix() bool {
p := this.Protocol()
return p == ProtocolUnix
}
// 判断当前分组是否为UDP
func (this *ServerGroup) IsUDP() bool {
p := this.Protocol()
return p == ProtocolUDP
}
// 获取第一个Server
func (this *ServerGroup) FirstServer() *ServerConfig {
if len(this.Servers) > 0 {
return this.Servers[0]
}
return nil
}

View File

@@ -0,0 +1,18 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
// ServerGroupConfig 服务分组配置
type ServerGroupConfig struct {
Id int64 `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"`
IsOn bool `yaml:"isOn" json:"isOn"`
// 反向代理配置
HTTPReverseProxyRef *ReverseProxyRef `yaml:"httpReverseProxyRef" json:"httpReverseProxyRef"`
HTTPReverseProxy *ReverseProxyConfig `yaml:"httpReverseProxy" json:"httpReverseProxy"`
TCPReverseProxyRef *ReverseProxyRef `yaml:"tcpReverseProxyRef" json:"tcpReverseProxyRef"`
TCPReverseProxy *ReverseProxyConfig `yaml:"tcpReverseProxy" json:"tcpReverseProxy"`
UDPReverseProxyRef *ReverseProxyRef `yaml:"udpReverseProxyRef" json:"udpReverseProxyRef"`
UDPReverseProxy *ReverseProxyConfig `yaml:"udpReverseProxy" json:"udpReverseProxy"`
}