可以在分组中设置一些全局配置选项

This commit is contained in:
GoEdgeLab
2021-09-22 19:39:38 +08:00
parent dd36ba5686
commit 765b030477
66 changed files with 1698 additions and 214 deletions

View File

@@ -0,0 +1,37 @@
package group
import (
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
GroupId int64
}) {
// 创建日志
defer this.CreateLog(oplogs.LevelInfo, "删除代理服务分组 %d", params.GroupId)
// 检查是否正在使用
countResp, err := this.RPC().ServerRPC().CountAllEnabledServersWithServerGroupId(this.AdminContext(), &pb.CountAllEnabledServersWithServerGroupIdRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
if countResp.Count > 0 {
this.Fail("此分组正在被使用不能删除,请修改相关服务后再删除")
}
_, err = this.RPC().ServerGroupRPC().DeleteServerGroup(this.AdminContext(), &pb.DeleteServerGroupRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,193 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package group
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "group.index")
}
func (this *IndexAction) RunGet(params struct {
GroupId int64
Keyword string
}) {
this.Data["keyword"] = params.Keyword
group, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["group"] = maps.Map{
"id": group.Id,
"name": group.Name,
}
// 是否有用户管理权限
this.Data["canVisitUser"] = configloaders.AllowModule(this.AdminId(), configloaders.AdminModuleCodeUser)
// 服务列表
countResp, err := this.RPC().ServerRPC().CountAllEnabledServersMatch(this.AdminContext(), &pb.CountAllEnabledServersMatchRequest{
ServerGroupId: params.GroupId,
Keyword: params.Keyword,
})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
// 服务列表
serversResp, err := this.RPC().ServerRPC().ListEnabledServersMatch(this.AdminContext(), &pb.ListEnabledServersMatchRequest{
Offset: page.Offset,
Size: page.Size,
ServerGroupId: params.GroupId,
Keyword: params.Keyword,
})
if err != nil {
this.ErrorPage(err)
return
}
serverMaps := []maps.Map{}
for _, server := range serversResp.Servers {
config := &serverconfigs.ServerConfig{}
err = json.Unmarshal(server.Config, config)
if err != nil {
this.ErrorPage(err)
return
}
// 端口列表
portMaps := []maps.Map{}
if len(server.HttpJSON) > 0 && config.HTTP.IsOn {
for _, listen := range config.HTTP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if config.HTTPS != nil && config.HTTPS.IsOn {
for _, listen := range config.HTTPS.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if config.TCP != nil && config.TCP.IsOn {
for _, listen := range config.TCP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if config.TLS != nil && config.TLS.IsOn {
for _, listen := range config.TLS.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
if config.Unix != nil && config.Unix.IsOn {
for _, listen := range config.Unix.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.Host,
})
}
}
if config.UDP != nil && config.UDP.IsOn {
for _, listen := range config.UDP.Listen {
portMaps = append(portMaps, maps.Map{
"protocol": listen.Protocol,
"portRange": listen.PortRange,
})
}
}
// 分组
groupMaps := []maps.Map{}
if len(server.ServerGroups) > 0 {
for _, group := range server.ServerGroups {
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
}
// 域名列表
serverNames := []*serverconfigs.ServerNameConfig{}
if server.IsAuditing || (server.AuditingResult != nil && !server.AuditingResult.IsOk) {
server.ServerNamesJSON = server.AuditingServerNamesJSON
}
auditingIsOk := true
if !server.IsAuditing && server.AuditingResult != nil && !server.AuditingResult.IsOk {
auditingIsOk = false
}
if len(server.ServerNamesJSON) > 0 {
err = json.Unmarshal(server.ServerNamesJSON, &serverNames)
if err != nil {
this.ErrorPage(err)
return
}
}
countServerNames := 0
for _, serverName := range serverNames {
if len(serverName.SubNames) == 0 {
countServerNames++
} else {
countServerNames += len(serverName.SubNames)
}
}
// 用户
var userMap maps.Map = nil
if server.User != nil {
userMap = maps.Map{
"id": server.User.Id,
"fullname": server.User.Fullname,
}
}
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
"isOn": server.IsOn,
"name": server.Name,
"cluster": maps.Map{
"id": server.NodeCluster.Id,
"name": server.NodeCluster.Name,
},
"ports": portMaps,
"serverTypeName": serverconfigs.FindServerType(server.Type).GetString("name"),
"groups": groupMaps,
"serverNames": serverNames,
"countServerNames": countServerNames,
"isAuditing": server.IsAuditing,
"auditingIsOk": auditingIsOk,
"user": userMap,
})
}
this.Data["servers"] = serverMaps
this.Show()
}

View File

@@ -0,0 +1,110 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package servergrouputils
import (
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
// InitGroup 初始化分组信息
func InitGroup(parent *actionutils.ParentAction, groupId int64, menuItem string) (*pb.ServerGroup, error) {
groupResp, err := parent.RPC().ServerGroupRPC().FindEnabledServerGroup(parent.AdminContext(), &pb.FindEnabledServerGroupRequest{ServerGroupId: groupId})
if err != nil {
return nil, err
}
var group = groupResp.ServerGroup
if group == nil {
return nil, errors.New("group with id '" + types.String(groupId) + "' not found")
}
parent.Data["group"] = maps.Map{
"id": group.Id,
"name": group.Name,
}
// 初始化设置菜单
if len(menuItem) > 0 {
// 获取设置概要信息
configInfoResp, err := parent.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(parent.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{ServerGroupId: groupId})
if err != nil {
return group, err
}
var urlPrefix = "/servers/groups/group/settings"
parent.Data["leftMenuItems"] = []maps.Map{
/**{
"name": "Web设置",
"url": urlPrefix + "/web?groupId=" + types.String(groupId),
"isActive": menuItem == "web",
},**/
{
"name": "HTTP反向代理",
"url": urlPrefix + "/httpReverseProxy?groupId=" + types.String(groupId),
"isActive": menuItem == "httpReverseProxy",
"isOn": configInfoResp.HasHTTPReverseProxy,
},
{
"name": "TCP反向代理",
"url": urlPrefix + "/tcpReverseProxy?groupId=" + types.String(groupId),
"isActive": menuItem == "tcpReverseProxy",
"isOn": configInfoResp.HasTCPReverseProxy,
},
{
"name": "UDP反向代理",
"url": urlPrefix + "/udpReverseProxy?groupId=" + types.String(groupId),
"isActive": menuItem == "udpReverseProxy",
"isOn": configInfoResp.HasUDPReverseProxy,
},
/**{
"name": "-",
"url": "",
},
{
"name": "WAF",
"url": urlPrefix + "/waf?groupId=" + types.String(groupId),
"isActive": menuItem == "waf",
},
{
"name": "缓存",
"url": urlPrefix + "/cache?groupId=" + types.String(groupId),
"isActive": menuItem == "cache",
},
{
"name": "访问日志",
"url": urlPrefix + "/accessLog?groupId=" + types.String(groupId),
"isActive": menuItem == "accessLog",
},
{
"name": "统计",
"url": urlPrefix + "/stat?groupId=" + types.String(groupId),
"isActive": menuItem == "stat",
},
{
"name": "Gzip压缩",
"url": urlPrefix + "/gzip?groupId=" + types.String(groupId),
"isActive": menuItem == "gzip",
},
{
"name": "特殊页面",
"url": urlPrefix + "/pages?groupId=" + types.String(groupId),
"isActive": menuItem == "page",
},
{
"name": "HTTP Header",
"url": urlPrefix + "/headers?groupId=" + types.String(groupId),
"isActive": menuItem == "header",
},
{
"name": "Websocket",
"url": urlPrefix + "/websocket?groupId=" + types.String(groupId),
"isActive": menuItem == "websocket",
},**/
}
}
return group, nil
}

View File

@@ -0,0 +1,87 @@
package httpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
)
// IndexAction 源站列表
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.FirstMenu("index")
}
func (this *IndexAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "httpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["serverType"] = "httpProxy"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupHTTPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupHTTPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyConfig"] = reverseProxy
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range reverseProxy.PrimaryOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range reverseProxy.BackupOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
backupOriginMaps = append(backupOriginMaps, m)
}
this.Data["primaryOrigins"] = primaryOriginMaps
this.Data["backupOrigins"] = backupOriginMaps
this.Show()
}

View File

@@ -0,0 +1,21 @@
package httpReverseProxy
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Data("teaMenu", "servers").
Data("teaSubMenu", "group").
Prefix("/servers/groups/group/settings/httpReverseProxy").
Get("", new(IndexAction)).
GetPost("/scheduling", new(SchedulingAction)).
GetPost("/setting", new(SettingAction)).
EndAll()
})
}

View File

@@ -0,0 +1,54 @@
package httpReverseProxy
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
)
type SchedulingAction struct {
actionutils.ParentAction
}
func (this *SchedulingAction) Init() {
this.FirstMenu("scheduling")
}
func (this *SchedulingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "httpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "http"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupHTTPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupHTTPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyId"] = reverseProxy.Id
schedulingCode := reverseProxy.FindSchedulingConfig().Code
schedulingMap := schedulingconfigs.FindSchedulingType(schedulingCode)
if schedulingMap == nil {
this.ErrorPage(errors.New("invalid scheduling code '" + schedulingCode + "'"))
return
}
this.Data["scheduling"] = schedulingMap
this.Show()
}

View File

@@ -0,0 +1,102 @@
package httpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type SettingAction struct {
actionutils.ParentAction
}
func (this *SettingAction) Init() {
this.FirstMenu("setting")
}
func (this *SettingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "httpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "http"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupHTTPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupHTTPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
this.Data["reverseProxyConfig"] = reverseProxy
this.Show()
}
func (this *SettingAction) RunPost(params struct {
GroupId int64
ReverseProxyRefJSON []byte
ReverseProxyJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改分组 %d 的反向代理设置", params.GroupId)
// TODO 校验配置
reverseProxyConfig := &serverconfigs.ReverseProxyConfig{}
err := json.Unmarshal(params.ReverseProxyJSON, reverseProxyConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = reverseProxyConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
// 设置是否启用
_, err = this.RPC().ServerGroupRPC().UpdateServerGroupHTTPReverseProxy(this.AdminContext(), &pb.UpdateServerGroupHTTPReverseProxyRequest{
ServerGroupId: params.GroupId,
ReverseProxyJSON: params.ReverseProxyRefJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
// 设置反向代理相关信息
_, err = this.RPC().ReverseProxyRPC().UpdateReverseProxy(this.AdminContext(), &pb.UpdateReverseProxyRequest{
ReverseProxyId: reverseProxyConfig.Id,
RequestHostType: types.Int32(reverseProxyConfig.RequestHostType),
RequestHost: reverseProxyConfig.RequestHost,
RequestURI: reverseProxyConfig.RequestURI,
StripPrefix: reverseProxyConfig.StripPrefix,
AutoFlush: reverseProxyConfig.AutoFlush,
AddHeaders: reverseProxyConfig.AddHeaders,
})
this.Success()
}

View File

@@ -0,0 +1,23 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package reverseProxy
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
GroupId int64
}) {
this.RedirectURL("/servers/groups/group/settings/httpReverseProxy?groupId=" + types.String(params.GroupId))
return
}

View File

@@ -0,0 +1,22 @@
package reverseProxy
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/locationutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(locationutils.NewLocationHelper()).
Helper(serverutils.NewServerHelper()).
Data("mainTab", "setting").
Prefix("/servers/groups/group/settings").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,87 @@
package tcpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
)
// IndexAction 源站列表
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.FirstMenu("index")
}
func (this *IndexAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "tcpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["serverType"] = "tcpProxy"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupTCPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupTCPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyConfig"] = reverseProxy
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range reverseProxy.PrimaryOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range reverseProxy.BackupOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
backupOriginMaps = append(backupOriginMaps, m)
}
this.Data["primaryOrigins"] = primaryOriginMaps
this.Data["backupOrigins"] = backupOriginMaps
this.Show()
}

View File

@@ -0,0 +1,21 @@
package tcpReverseProxy
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Data("teaMenu", "servers").
Data("teaSubMenu", "group").
Prefix("/servers/groups/group/settings/tcpReverseProxy").
Get("", new(IndexAction)).
GetPost("/scheduling", new(SchedulingAction)).
GetPost("/setting", new(SettingAction)).
EndAll()
})
}

View File

@@ -0,0 +1,54 @@
package tcpReverseProxy
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
)
type SchedulingAction struct {
actionutils.ParentAction
}
func (this *SchedulingAction) Init() {
this.FirstMenu("scheduling")
}
func (this *SchedulingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "tcpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "tcp"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupTCPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupTCPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyId"] = reverseProxy.Id
schedulingCode := reverseProxy.FindSchedulingConfig().Code
schedulingMap := schedulingconfigs.FindSchedulingType(schedulingCode)
if schedulingMap == nil {
this.ErrorPage(errors.New("invalid scheduling code '" + schedulingCode + "'"))
return
}
this.Data["scheduling"] = schedulingMap
this.Show()
}

View File

@@ -0,0 +1,102 @@
package tcpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type SettingAction struct {
actionutils.ParentAction
}
func (this *SettingAction) Init() {
this.FirstMenu("setting")
}
func (this *SettingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "tcpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "tcp"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupTCPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupTCPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
this.Data["reverseProxyConfig"] = reverseProxy
this.Show()
}
func (this *SettingAction) RunPost(params struct {
GroupId int64
ReverseProxyRefJSON []byte
ReverseProxyJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改分组 %d 的反向代理设置", params.GroupId)
// TODO 校验配置
reverseProxyConfig := &serverconfigs.ReverseProxyConfig{}
err := json.Unmarshal(params.ReverseProxyJSON, reverseProxyConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = reverseProxyConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
// 设置是否启用
_, err = this.RPC().ServerGroupRPC().UpdateServerGroupTCPReverseProxy(this.AdminContext(), &pb.UpdateServerGroupTCPReverseProxyRequest{
ServerGroupId: params.GroupId,
ReverseProxyJSON: params.ReverseProxyRefJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
// 设置反向代理相关信息
_, err = this.RPC().ReverseProxyRPC().UpdateReverseProxy(this.AdminContext(), &pb.UpdateReverseProxyRequest{
ReverseProxyId: reverseProxyConfig.Id,
RequestHostType: types.Int32(reverseProxyConfig.RequestHostType),
RequestHost: reverseProxyConfig.RequestHost,
RequestURI: reverseProxyConfig.RequestURI,
StripPrefix: reverseProxyConfig.StripPrefix,
AutoFlush: reverseProxyConfig.AutoFlush,
AddHeaders: reverseProxyConfig.AddHeaders,
})
this.Success()
}

View File

@@ -0,0 +1,87 @@
package udpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/maps"
)
// IndexAction 源站列表
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.FirstMenu("index")
}
func (this *IndexAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "udpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["serverType"] = "udpProxy"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupUDPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupUDPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyConfig"] = reverseProxy
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range reverseProxy.PrimaryOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range reverseProxy.BackupOrigins {
if len(originConfig.Domains) == 0 {
originConfig.Domains = []string{}
}
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
"name": originConfig.Name,
"isOn": originConfig.IsOn,
"domains": originConfig.Domains,
}
backupOriginMaps = append(backupOriginMaps, m)
}
this.Data["primaryOrigins"] = primaryOriginMaps
this.Data["backupOrigins"] = backupOriginMaps
this.Show()
}

View File

@@ -0,0 +1,21 @@
package udpReverseProxy
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Data("teaMenu", "servers").
Data("teaSubMenu", "group").
Prefix("/servers/groups/group/settings/udpReverseProxy").
Get("", new(IndexAction)).
GetPost("/scheduling", new(SchedulingAction)).
GetPost("/setting", new(SettingAction)).
EndAll()
})
}

View File

@@ -0,0 +1,54 @@
package udpReverseProxy
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
)
type SchedulingAction struct {
actionutils.ParentAction
}
func (this *SchedulingAction) Init() {
this.FirstMenu("scheduling")
}
func (this *SchedulingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "udpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "udp"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupUDPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupUDPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyId"] = reverseProxy.Id
schedulingCode := reverseProxy.FindSchedulingConfig().Code
schedulingMap := schedulingconfigs.FindSchedulingType(schedulingCode)
if schedulingMap == nil {
this.ErrorPage(errors.New("invalid scheduling code '" + schedulingCode + "'"))
return
}
this.Data["scheduling"] = schedulingMap
this.Show()
}

View File

@@ -0,0 +1,102 @@
package udpReverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type SettingAction struct {
actionutils.ParentAction
}
func (this *SettingAction) Init() {
this.FirstMenu("setting")
}
func (this *SettingAction) RunGet(params struct {
GroupId int64
}) {
_, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "udpReverseProxy")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["family"] = "udp"
reverseProxyResp, err := this.RPC().ServerGroupRPC().FindAndInitServerGroupUDPReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerGroupUDPReverseProxyConfigRequest{ServerGroupId: params.GroupId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyRefJSON, reverseProxyRef)
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.ReverseProxyJSON, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["reverseProxyRef"] = reverseProxyRef
this.Data["reverseProxyConfig"] = reverseProxy
this.Show()
}
func (this *SettingAction) RunPost(params struct {
GroupId int64
ReverseProxyRefJSON []byte
ReverseProxyJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改分组 %d 的反向代理设置", params.GroupId)
// TODO 校验配置
reverseProxyConfig := &serverconfigs.ReverseProxyConfig{}
err := json.Unmarshal(params.ReverseProxyJSON, reverseProxyConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = reverseProxyConfig.Init()
if err != nil {
this.Fail("配置校验失败:" + err.Error())
}
// 设置是否启用
_, err = this.RPC().ServerGroupRPC().UpdateServerGroupUDPReverseProxy(this.AdminContext(), &pb.UpdateServerGroupUDPReverseProxyRequest{
ServerGroupId: params.GroupId,
ReverseProxyJSON: params.ReverseProxyRefJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
// 设置反向代理相关信息
_, err = this.RPC().ReverseProxyRPC().UpdateReverseProxy(this.AdminContext(), &pb.UpdateReverseProxyRequest{
ReverseProxyId: reverseProxyConfig.Id,
RequestHostType: types.Int32(reverseProxyConfig.RequestHostType),
RequestHost: reverseProxyConfig.RequestHost,
RequestURI: reverseProxyConfig.RequestURI,
StripPrefix: reverseProxyConfig.StripPrefix,
AutoFlush: reverseProxyConfig.AutoFlush,
AddHeaders: reverseProxyConfig.AddHeaders,
})
this.Success()
}

View File

@@ -0,0 +1,59 @@
package group
import (
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/groups/group/servergrouputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "group.update")
}
func (this *UpdateAction) RunGet(params struct {
GroupId int64
}) {
group, err := servergrouputils.InitGroup(this.Parent(), params.GroupId, "")
if err != nil {
this.ErrorPage(err)
return
}
this.Data["group"] = maps.Map{
"id": group.Id,
"name": group.Name,
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
GroupId int64
Name string
Must *actions.Must
}) {
// 创建日志
defer this.CreateLog(oplogs.LevelInfo, "修改代理服务分组 %d", params.GroupId)
params.Must.
Field("name", params.Name).
Require("请输入分组名称")
_, err := this.RPC().ServerGroupRPC().UpdateServerGroup(this.AdminContext(), &pb.UpdateServerGroupRequest{
ServerGroupId: params.GroupId,
Name: params.Name,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}