阶段性提交

This commit is contained in:
刘祥超
2020-09-15 14:44:52 +08:00
parent ec3c0cafb3
commit eadf69b147
27 changed files with 1354 additions and 115 deletions

View File

@@ -1,22 +1,89 @@
package reverseProxy
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"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("", "setting", "index")
this.SecondMenu("reverseProxy")
this.FirstMenu("index")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
server, _, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId)
if !isOk {
return
}
this.Data["serverType"] = server.Type
this.Data["reverseProxyId"] = server.ReverseProxyId
if server.ReverseProxyId <= 0 {
// TODO 应该在界面上提示用户开启
this.ErrorPage(errors.New("reverse proxy should not be nil"))
return
}
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxy(this.AdminContext(), &pb.FindEnabledReverseProxyRequest{ReverseProxyId: server.ReverseProxyId})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := reverseProxyResp.ReverseProxy
if reverseProxy == nil {
// TODO 应该在界面上提示用户开启
this.ErrorPage(errors.New("reverse proxy should not be nil"))
return
}
primaryOrigins := []*serverconfigs.OriginServerConfig{}
backupOrigins := []*serverconfigs.OriginServerConfig{}
if len(reverseProxy.PrimaryOriginsJSON) > 0 {
err = json.Unmarshal(reverseProxy.PrimaryOriginsJSON, &primaryOrigins)
if err != nil {
this.ErrorPage(err)
return
}
}
if len(reverseProxy.BackupOriginsJSON) > 0 {
err = json.Unmarshal(reverseProxy.BackupOriginsJSON, &backupOrigins)
if err != nil {
this.ErrorPage(err)
return
}
}
primaryOriginMaps := []maps.Map{}
backupOriginMaps := []maps.Map{}
for _, originConfig := range primaryOrigins {
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
}
primaryOriginMaps = append(primaryOriginMaps, m)
}
for _, originConfig := range backupOrigins {
m := maps.Map{
"id": originConfig.Id,
"weight": originConfig.Weight,
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
}
backupOriginMaps = append(backupOriginMaps, m)
}
this.Data["primaryOrigins"] = primaryOriginMaps
this.Data["backupOrigins"] = backupOriginMaps
this.Show()
}

View File

@@ -11,8 +11,12 @@ func init() {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Data("mainTab", "setting").
Data("secondMenuItem", "reverseProxy").
Prefix("/servers/server/settings/reverseProxy").
Get("", new(IndexAction)).
GetPost("/scheduling", new(SchedulingAction)).
GetPost("/updateSchedulingPopup", new(UpdateSchedulingPopupAction)).
EndAll()
})
}

View File

@@ -0,0 +1,59 @@
package reverseProxy
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"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 {
ServerId int64
}) {
server, _, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId)
if !isOk {
return
}
if server.ReverseProxyId <= 0 {
// TODO 在界面上提示用户未开通,并提供开通按钮,用户点击后开通
this.WriteString("此服务尚未开通反向代理功能")
return
}
this.Data["reverseProxyId"] = server.ReverseProxyId
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxyConfig(this.AdminContext(), &pb.FindEnabledReverseProxyConfigRequest{
ReverseProxyId: server.ReverseProxyId,
})
if err != nil {
this.ErrorPage(err)
return
}
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(reverseProxyResp.Config, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
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,155 @@
package reverseProxy
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
// 修改调度算法
type UpdateSchedulingPopupAction struct {
actionutils.ParentAction
}
func (this *UpdateSchedulingPopupAction) Init() {
}
func (this *UpdateSchedulingPopupAction) RunGet(params struct {
Type string
ServerId int64
ReverseProxyId int64
}) {
this.Data["dataType"] = params.Type
this.Data["serverId"] = params.ServerId
this.Data["reverseProxyId"] = params.ReverseProxyId
_, serverConfig, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId)
if !isOk {
return
}
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxyConfig(this.AdminContext(), &pb.FindEnabledReverseProxyConfigRequest{
ReverseProxyId: params.ReverseProxyId,
})
if err != nil {
this.ErrorPage(err)
return
}
configData := reverseProxyResp.Config
reverseProxyConfig := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(configData, reverseProxyConfig)
if err != nil {
this.ErrorPage(err)
return
}
schedulingObject := &serverconfigs.SchedulingConfig{
Code: "random",
Options: nil,
}
if reverseProxyConfig.Scheduling != nil {
schedulingObject = reverseProxyConfig.Scheduling
}
this.Data["scheduling"] = schedulingObject
// 调度类型
schedulingTypes := []maps.Map{}
for _, m := range schedulingconfigs.AllSchedulingTypes() {
networks, ok := m["networks"]
if !ok {
continue
}
if !types.IsSlice(networks) {
continue
}
if (serverConfig.IsHTTP() && lists.Contains(networks, "http")) ||
(serverConfig.IsTCP() && lists.Contains(networks, "tcp")) ||
(serverConfig.IsUDP() && lists.Contains(networks, "udp")) ||
(serverConfig.IsUnix() && lists.Contains(networks, "unix")) {
schedulingTypes = append(schedulingTypes, m)
}
}
this.Data["schedulingTypes"] = schedulingTypes
this.Show()
}
func (this *UpdateSchedulingPopupAction) RunPost(params struct {
ServerId int64
ReverseProxyId int64
Type string
HashKey string
StickyType string
StickyParam string
Must *actions.Must
}) {
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxyConfig(this.AdminContext(), &pb.FindEnabledReverseProxyConfigRequest{ReverseProxyId: params.ReverseProxyId})
if err != nil {
this.ErrorPage(err)
return
}
configData := reverseProxyResp.Config
reverseProxy := &serverconfigs.ReverseProxyConfig{}
err = json.Unmarshal(configData, reverseProxy)
if err != nil {
this.ErrorPage(err)
return
}
if reverseProxy.Scheduling == nil {
reverseProxy.FindSchedulingConfig()
}
options := maps.Map{}
if params.Type == "hash" {
params.Must.
Field("hashKey", params.HashKey).
Require("请输入Key")
options["key"] = params.HashKey
} else if params.Type == "sticky" {
params.Must.
Field("stickyType", params.StickyType).
Require("请选择参数类型").
Field("stickyParam", params.StickyParam).
Require("请输入参数名").
Match("^[a-zA-Z0-9]+$", "参数名只能是英文字母和数字的组合").
MaxCharacters(50, "参数名长度不能超过50位")
options["type"] = params.StickyType
options["param"] = params.StickyParam
}
if schedulingconfigs.FindSchedulingType(params.Type) == nil {
this.Fail("不支持此种算法")
}
reverseProxy.Scheduling.Code = params.Type
reverseProxy.Scheduling.Options = options
schedulingData, err := json.Marshal(reverseProxy.Scheduling)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().ReverseProxyRPC().UpdateReverseProxyScheduling(this.AdminContext(), &pb.UpdateReverseProxySchedulingRequest{
ReverseProxyId: params.ReverseProxyId,
SchedulingJSON: schedulingData,
})
if err != nil {
this.ErrorPage(err)
}
this.Success()
}