Files
EdgeAdmin/internal/web/actions/default/servers/addOriginPopup.go

141 lines
3.0 KiB
Go
Raw Normal View History

2020-08-21 12:32:16 +08:00
package servers
import (
2022-06-29 21:56:44 +08:00
"encoding/json"
2020-11-10 21:37:48 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-08-21 12:32:16 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2022-06-15 19:40:07 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2020-09-15 14:44:52 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2020-09-13 20:37:07 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2020-08-21 12:32:16 +08:00
"github.com/iwind/TeaGo/actions"
2022-06-15 19:40:07 +08:00
"github.com/iwind/TeaGo/types"
2021-06-07 16:23:37 +08:00
"net/url"
2020-08-21 12:32:16 +08:00
"regexp"
"strings"
)
type AddOriginPopupAction struct {
actionutils.ParentAction
}
func (this *AddOriginPopupAction) Init() {
this.Nav("", "", "")
}
func (this *AddOriginPopupAction) RunGet(params struct {
ServerType string
}) {
this.Data["serverType"] = params.ServerType
this.Show()
}
func (this *AddOriginPopupAction) RunPost(params struct {
Protocol string
Addr string
2022-06-29 21:56:44 +08:00
DomainsJSON []byte
Host string
FollowPort bool
2020-08-21 12:32:16 +08:00
Must *actions.Must
}) {
params.Must.
Field("addr", params.Addr).
Require("请输入源站地址")
2022-06-15 19:40:07 +08:00
var addr = params.Addr
2021-06-07 16:23:37 +08:00
// 是否是完整的地址
if (params.Protocol == "http" || params.Protocol == "https") && regexp.MustCompile(`^(http|https)://`).MatchString(addr) {
u, err := url.Parse(addr)
if err == nil {
addr = u.Host
}
}
addr = regexp.MustCompile(`\s+`).ReplaceAllString(addr, "")
2022-06-29 21:56:44 +08:00
var portIndex = strings.LastIndex(addr, ":")
2020-08-21 12:32:16 +08:00
if portIndex < 0 {
2021-06-07 16:23:37 +08:00
if params.Protocol == "http" {
addr += ":80"
} else if params.Protocol == "https" {
addr += ":443"
} else {
this.Fail("地址中需要带有端口")
}
portIndex = strings.LastIndex(addr, ":")
2020-08-21 12:32:16 +08:00
}
2022-06-15 19:40:07 +08:00
var host = addr[:portIndex]
var port = addr[portIndex+1:]
// 检查端口号
if port == "0" {
this.Fail("端口号不能为0")
}
if !configutils.HasVariables(port) {
// 必须是整数
if !regexp.MustCompile(`^\d+$`).MatchString(port) {
this.Fail("端口号只能为整数")
}
var portInt = types.Int(port)
if portInt == 0 {
this.Fail("端口号不能为0")
}
if portInt > 65535 {
this.Fail("端口号不能大于65535")
}
}
2020-08-21 12:32:16 +08:00
2022-06-29 21:56:44 +08:00
// 专属域名
var domains = []string{}
if len(params.DomainsJSON) > 0 {
err := json.Unmarshal(params.DomainsJSON, &domains)
if err != nil {
this.ErrorPage(err)
return
}
// 去除可能误加的斜杠
for index, domain := range domains {
domains[index] = strings.TrimSuffix(domain, "/")
}
}
2020-09-22 11:36:51 +08:00
resp, err := this.RPC().OriginRPC().CreateOrigin(this.AdminContext(), &pb.CreateOriginRequest{
2020-09-13 20:37:07 +08:00
Name: "",
Addr: &pb.NetworkAddress{
Protocol: params.Protocol,
Host: host,
PortRange: port,
},
Description: "",
2021-06-07 15:43:43 +08:00
Weight: 10,
IsOn: true,
2022-06-29 21:56:44 +08:00
Domains: domains,
Host: params.Host,
FollowPort: params.FollowPort,
2020-09-13 20:37:07 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
2022-06-15 19:40:07 +08:00
var origin = &serverconfigs.OriginConfig{
2020-09-13 20:37:07 +08:00
Id: resp.OriginId,
2020-08-21 12:32:16 +08:00
IsOn: true,
Addr: &serverconfigs.NetworkAddressConfig{
2020-09-15 14:44:52 +08:00
Protocol: serverconfigs.Protocol(params.Protocol),
2020-08-21 12:32:16 +08:00
Host: host,
PortRange: port,
},
}
this.Data["origin"] = origin
2020-11-10 21:37:48 +08:00
// 创建日志
2020-11-20 15:32:42 +08:00
defer this.CreateLog(oplogs.LevelInfo, "创建源站 %d", resp.OriginId)
2020-11-10 21:37:48 +08:00
2020-08-21 12:32:16 +08:00
this.Success()
}