修改反向代理实现

This commit is contained in:
刘祥超
2020-09-21 11:37:17 +08:00
parent bf408a1cec
commit a8ed53582c
8 changed files with 240 additions and 92 deletions

View File

@@ -0,0 +1,65 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
HTTPLocationStateEnabled = 1 // 已启用
HTTPLocationStateDisabled = 0 // 已禁用
)
type HTTPLocationDAO dbs.DAO
func NewHTTPLocationDAO() *HTTPLocationDAO {
return dbs.NewDAO(&HTTPLocationDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeHTTPLocations",
Model: new(HTTPLocation),
PkName: "id",
},
}).(*HTTPLocationDAO)
}
var SharedHTTPLocationDAO = NewHTTPLocationDAO()
// 启用条目
func (this *HTTPLocationDAO) EnableHTTPLocation(id uint32) error {
_, err := this.Query().
Pk(id).
Set("state", HTTPLocationStateEnabled).
Update()
return err
}
// 禁用条目
func (this *HTTPLocationDAO) DisableHTTPLocation(id uint32) error {
_, err := this.Query().
Pk(id).
Set("state", HTTPLocationStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *HTTPLocationDAO) FindEnabledHTTPLocation(id uint32) (*HTTPLocation, error) {
result, err := this.Query().
Pk(id).
Attr("state", HTTPLocationStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*HTTPLocation), err
}
// 根据主键查找名称
func (this *HTTPLocationDAO) FindHTTPLocationName(id uint32) (string, error) {
return this.Query().
Pk(id).
Result("name").
FindStringCol("")
}

View File

@@ -0,0 +1,5 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,38 @@
package models
// 路径规则配置
type HTTPLocation struct {
Id uint32 `field:"id"` // ID
TemplateId uint32 `field:"templateId"` // 模版ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
ParentId uint32 `field:"parentId"` // 父级ID
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
Pattern string `field:"pattern"` // 匹配规则
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
WebId uint32 `field:"webId"` // Web配置ID
ReverseProxy string `field:"reverseProxy"` // 反向代理
}
type HTTPLocationOperator struct {
Id interface{} // ID
TemplateId interface{} // 模版ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
ParentId interface{} // 父级ID
State interface{} // 状态
CreatedAt interface{} // 创建时间
Pattern interface{} // 匹配规则
IsOn interface{} // 是否启用
Name interface{} // 名称
Description interface{} // 描述
WebId interface{} // Web配置ID
ReverseProxy interface{} // 反向代理
}
func NewHTTPLocationOperator() *HTTPLocationOperator {
return &HTTPLocationOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -71,6 +71,7 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(reverseProxyId int64) (*s
}
config := &serverconfigs.ReverseProxyConfig{}
config.Id = int64(reverseProxy.Id)
config.IsOn = reverseProxy.IsOn == 1
schedulingConfig := &serverconfigs.SchedulingConfig{}
@@ -121,6 +122,7 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(reverseProxyId int64) (*s
// 创建反向代理
func (this *ReverseProxyDAO) CreateReverseProxy(schedulingJSON []byte, primaryOriginsJSON []byte, backupOriginsJSON []byte) (int64, error) {
op := NewReverseProxyOperator()
op.IsOn = false
op.State = ReverseProxyStateEnabled
if len(schedulingJSON) > 0 {
op.Scheduling = string(schedulingJSON)

View File

@@ -69,7 +69,7 @@ func (this *ServerDAO) FindEnabledServerType(serverId int64) (string, error) {
}
// 创建服务
func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serverconfigs.ServerType, name string, description string, serverNamesJSON string, httpJSON string, httpsJSON string, tcpJSON string, tlsJSON string, unixJSON string, udpJSON string, webId int64, reverseProxyId int64, clusterId int64, includeNodesJSON string, excludeNodesJSON string) (serverId int64, err error) {
func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serverconfigs.ServerType, name string, description string, serverNamesJSON string, httpJSON string, httpsJSON string, tcpJSON string, tlsJSON string, unixJSON string, udpJSON string, webId int64, reverseProxyJSON []byte, clusterId int64, includeNodesJSON string, excludeNodesJSON string) (serverId int64, err error) {
uniqueId, err := this.genUniqueId()
if err != nil {
return 0, err
@@ -83,29 +83,31 @@ func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serv
op.Type = serverType
op.Description = description
if len(serverNamesJSON) > 0 {
if IsNotNull(serverNamesJSON) {
op.ServerNames = serverNamesJSON
}
if len(httpJSON) > 0 {
if IsNotNull(httpJSON) {
op.Http = httpJSON
}
if len(httpsJSON) > 0 {
if IsNotNull(httpsJSON) {
op.Https = httpsJSON
}
if len(tcpJSON) > 0 {
if IsNotNull(tcpJSON) {
op.Tcp = tcpJSON
}
if len(tlsJSON) > 0 {
if IsNotNull(tlsJSON) {
op.Tls = tlsJSON
}
if len(unixJSON) > 0 {
if IsNotNull(unixJSON) {
op.Unix = unixJSON
}
if len(udpJSON) > 0 {
if IsNotNull(udpJSON) {
op.Udp = udpJSON
}
op.WebId = webId
op.ReverseProxyId = reverseProxyId
if len(reverseProxyJSON) > 0 {
op.ReverseProxy = reverseProxyJSON
}
op.ClusterId = clusterId
if len(includeNodesJSON) > 0 {
@@ -324,14 +326,14 @@ func (this *ServerDAO) UpdateServerNames(serverId int64, config []byte) error {
}
// 修改反向代理配置
func (this *ServerDAO) UpdateServerReverseProxy(serverId int64, reverseProxyId int64) error {
func (this *ServerDAO) UpdateServerReverseProxy(serverId int64, config []byte) error {
if serverId <= 0 {
return errors.New("serverId should not be smaller than 0")
}
_, err := this.Query().
Pk(serverId).
Set("reverseProxyId", reverseProxyId).
Update()
op := NewServerOperator()
op.Id = serverId
op.ReverseProxy = JSONBytes(config)
_, err := this.Save(op)
if err != nil {
return err
}
@@ -482,8 +484,15 @@ func (this *ServerDAO) ComposeServerConfig(serverId int64) (*serverconfigs.Serve
}
// ReverseProxy
if server.ReverseProxyId > 0 {
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(int64(server.ReverseProxyId))
if IsNotNull(server.ReverseProxy) {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal([]byte(server.ReverseProxy), reverseProxyRef)
if err != nil {
return nil, err
}
config.ReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(reverseProxyRef.ReverseProxyId)
if err != nil {
return nil, err
}
@@ -509,7 +518,7 @@ func (this *ServerDAO) RenewServerConfig(serverId int64) error {
}
// 根据条件获取反向代理配置
func (this *ServerDAO) FindReverseProxyConfig(serverId int64) (*serverconfigs.ReverseProxyConfig, error) {
func (this *ServerDAO) FindReverseProxyRef(serverId int64) (*serverconfigs.ReverseProxyRef, error) {
reverseProxy, err := this.Query().
Pk(serverId).
Result("reverseProxy").
@@ -520,7 +529,7 @@ func (this *ServerDAO) FindReverseProxyConfig(serverId int64) (*serverconfigs.Re
if len(reverseProxy) == 0 || reverseProxy == "null" {
return nil, nil
}
config := &serverconfigs.ReverseProxyConfig{}
config := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal([]byte(reverseProxy), config)
return config, err
}

View File

@@ -2,61 +2,61 @@ package models
// 服务
type Server struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
UniqueId string `field:"uniqueId"` // 唯一ID
UserId uint32 `field:"userId"` // 用户ID
AdminId uint32 `field:"adminId"` // 管理员ID
Type string `field:"type"` // 服务类型
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
ServerNames string `field:"serverNames"` // 域名列表
Http string `field:"http"` // HTTP配置
Https string `field:"https"` // HTTPS配置
Tcp string `field:"tcp"` // TCP配置
Tls string `field:"tls"` // TLS配置
Unix string `field:"unix"` // Unix配置
Udp string `field:"udp"` // UDP配置
WebId uint32 `field:"webId"` // WEB配置
ReverseProxyId uint32 `field:"reverseProxyId"` // 反向代理配置
GroupIds string `field:"groupIds"` // 分组ID列表
Config string `field:"config"` // 服务配置,自动生成
ClusterId uint32 `field:"clusterId"` // 集群ID
IncludeNodes string `field:"includeNodes"` // 部署条件
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
Version uint32 `field:"version"` // 版本号
CreatedAt uint32 `field:"createdAt"` // 创建时间
IsUpdating uint8 `field:"isUpdating"` // 是否正在更新
State uint8 `field:"state"` // 状态
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
UniqueId string `field:"uniqueId"` // 唯一ID
UserId uint32 `field:"userId"` // 用户ID
AdminId uint32 `field:"adminId"` // 管理员ID
Type string `field:"type"` // 服务类型
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
ServerNames string `field:"serverNames"` // 域名列表
Http string `field:"http"` // HTTP配置
Https string `field:"https"` // HTTPS配置
Tcp string `field:"tcp"` // TCP配置
Tls string `field:"tls"` // TLS配置
Unix string `field:"unix"` // Unix配置
Udp string `field:"udp"` // UDP配置
WebId uint32 `field:"webId"` // WEB配置
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
GroupIds string `field:"groupIds"` // 分组ID列表
Config string `field:"config"` // 服务配置,自动生成
ClusterId uint32 `field:"clusterId"` // 集群ID
IncludeNodes string `field:"includeNodes"` // 部署条件
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
Version uint32 `field:"version"` // 版本号
CreatedAt uint32 `field:"createdAt"` // 创建时间
IsUpdating uint8 `field:"isUpdating"` // 是否正在更新
State uint8 `field:"state"` // 状态
}
type ServerOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
UniqueId interface{} // 唯一ID
UserId interface{} // 用户ID
AdminId interface{} // 管理员ID
Type interface{} // 服务类型
Name interface{} // 名称
Description interface{} // 描述
ServerNames interface{} // 域名列表
Http interface{} // HTTP配置
Https interface{} // HTTPS配置
Tcp interface{} // TCP配置
Tls interface{} // TLS配置
Unix interface{} // Unix配置
Udp interface{} // UDP配置
WebId interface{} // WEB配置
ReverseProxyId interface{} // 反向代理配置
GroupIds interface{} // 分组ID列表
Config interface{} // 服务配置,自动生成
ClusterId interface{} // 集群ID
IncludeNodes interface{} // 部署条件
ExcludeNodes interface{} // 节点排除条件
Version interface{} // 版本号
CreatedAt interface{} // 创建时间
IsUpdating interface{} // 是否正在更新
State interface{} // 状态
Id interface{} // ID
IsOn interface{} // 是否启用
UniqueId interface{} // 唯一ID
UserId interface{} // 用户ID
AdminId interface{} // 管理员ID
Type interface{} // 服务类型
Name interface{} // 名称
Description interface{} // 描述
ServerNames interface{} // 域名列表
Http interface{} // HTTP配置
Https interface{} // HTTPS配置
Tcp interface{} // TCP配置
Tls interface{} // TLS配置
Unix interface{} // Unix配置
Udp interface{} // UDP配置
WebId interface{} // WEB配置
ReverseProxy interface{} // 反向代理配置
GroupIds interface{} // 分组ID列表
Config interface{} // 服务配置,自动生成
ClusterId interface{} // 集群ID
IncludeNodes interface{} // 部署条件
ExcludeNodes interface{} // 节点排除条件
Version interface{} // 版本号
CreatedAt interface{} // 创建时间
IsUpdating interface{} // 是否正在更新
State interface{} // 状态
}
func NewServerOperator() *ServerOperator {

View File

@@ -7,6 +7,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type ServerService struct {
@@ -18,7 +19,7 @@ func (this *ServerService) CreateServer(ctx context.Context, req *pb.CreateServe
if err != nil {
return nil, err
}
serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, string(req.ServerNamesJON), string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyId, req.ClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON))
serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, string(req.ServerNamesJON), string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyJSON, req.ClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON))
if err != nil {
return nil, err
}
@@ -340,7 +341,7 @@ func (this *ServerService) UpdateServerReverseProxy(ctx context.Context, req *pb
}
// 修改配置
err = models.SharedServerDAO.UpdateServerReverseProxy(req.ServerId, req.ReverseProxyId)
err = models.SharedServerDAO.UpdateServerReverseProxy(req.ServerId, req.ReverseProxyJSON)
if err != nil {
return nil, err
}
@@ -498,20 +499,20 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
}
return &pb.FindEnabledServerResponse{Server: &pb.Server{
Id: int64(server.Id),
Type: server.Type,
Name: server.Name,
Description: server.Description,
Config: []byte(server.Config),
ServerNamesJON: []byte(server.ServerNames),
HttpJSON: []byte(server.Http),
HttpsJSON: []byte(server.Https),
TcpJSON: []byte(server.Tcp),
TlsJSON: []byte(server.Tls),
UnixJSON: []byte(server.Unix),
UdpJSON: []byte(server.Udp),
WebId: int64(server.WebId),
ReverseProxyId: int64(server.ReverseProxyId),
Id: int64(server.Id),
Type: server.Type,
Name: server.Name,
Description: server.Description,
Config: []byte(server.Config),
ServerNamesJON: []byte(server.ServerNames),
HttpJSON: []byte(server.Http),
HttpsJSON: []byte(server.Https),
TcpJSON: []byte(server.Tcp),
TlsJSON: []byte(server.Tls),
UnixJSON: []byte(server.Unix),
UdpJSON: []byte(server.Udp),
WebId: int64(server.WebId),
ReverseProxyJSON: []byte(server.ReverseProxy),
IncludeNodes: []byte(server.IncludeNodes),
ExcludeNodes: []byte(server.ExcludeNodes),
@@ -539,26 +540,53 @@ func (this *ServerService) FindEnabledServerType(ctx context.Context, req *pb.Fi
}
// 查找反向代理设置
func (this *ServerService) FindServerReverseProxyConfig(ctx context.Context, req *pb.FindServerReverseProxyConfigRequest) (*pb.FindServerReverseProxyConfigResponse, error) {
func (this *ServerService) FindAndInitServerReverseProxyConfig(ctx context.Context, req *pb.FindAndInitServerReverseProxyConfigRequest) (*pb.FindAndInitServerReverseProxyConfigResponse, error) {
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
reverseProxy, err := models.SharedServerDAO.FindReverseProxyConfig(req.ServerId)
reverseProxyRef, err := models.SharedServerDAO.FindReverseProxyRef(req.ServerId)
if err != nil {
return nil, err
}
if reverseProxy == nil {
return &pb.FindServerReverseProxyConfigResponse{Config: nil}, nil
if reverseProxyRef == nil {
reverseProxyId, err := models.SharedReverseProxyDAO.CreateReverseProxy(nil, nil, nil)
if err != nil {
return nil, err
}
reverseProxyRef = &serverconfigs.ReverseProxyRef{
IsOn: false,
ReverseProxyId: reverseProxyId,
}
refJSON, err := json.Marshal(reverseProxyRef)
if err != nil {
return nil, err
}
err = models.SharedServerDAO.UpdateServerReverseProxy(req.ServerId, refJSON)
if err != nil {
return nil, err
}
}
configData, err := json.Marshal(reverseProxy)
reverseProxyConfig, err := models.SharedReverseProxyDAO.ComposeReverseProxyConfig(reverseProxyRef.ReverseProxyId)
if err != nil {
return nil, err
}
return &pb.FindServerReverseProxyConfigResponse{Config: configData}, nil
configJSON, err := json.Marshal(reverseProxyConfig)
if err != nil {
return nil, err
}
refJSON, err := json.Marshal(reverseProxyRef)
if err != nil {
return nil, err
}
return &pb.FindAndInitServerReverseProxyConfigResponse{ReverseProxy: configJSON, ReverseProxyRef: refJSON}, nil
}
// 初始化Web设置