2020-08-21 12:32:16 +08:00
|
|
|
|
package servers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-29 21:56:44 +08:00
|
|
|
|
"encoding/json"
|
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"
|
2023-06-30 18:08:30 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
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"
|
2023-06-07 17:24:56 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
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
|
|
|
|
|
|
|
2023-06-07 17:24:56 +08:00
|
|
|
|
this.getOSSHook()
|
|
|
|
|
|
|
2020-08-21 12:32:16 +08:00
|
|
|
|
this.Show()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *AddOriginPopupAction) RunPost(params struct {
|
|
|
|
|
|
Protocol string
|
|
|
|
|
|
Addr string
|
|
|
|
|
|
|
2023-06-23 11:43:36 +08:00
|
|
|
|
DomainsJSON []byte
|
|
|
|
|
|
Host string
|
|
|
|
|
|
FollowPort bool
|
|
|
|
|
|
Http2Enabled bool
|
2022-06-29 21:56:44 +08:00
|
|
|
|
|
2020-08-21 12:32:16 +08:00
|
|
|
|
Must *actions.Must
|
|
|
|
|
|
}) {
|
2023-06-07 17:24:56 +08:00
|
|
|
|
ossConfig, goNext, err := this.postOSSHook(params.Protocol)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if !goNext {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-06-07 16:23:37 +08:00
|
|
|
|
|
2023-06-07 17:24:56 +08:00
|
|
|
|
// 初始化
|
|
|
|
|
|
var pbAddr = &pb.NetworkAddress{
|
|
|
|
|
|
Protocol: params.Protocol,
|
|
|
|
|
|
}
|
|
|
|
|
|
var addrConfig = &serverconfigs.NetworkAddressConfig{
|
|
|
|
|
|
Protocol: serverconfigs.Protocol(params.Protocol),
|
2021-06-07 16:23:37 +08:00
|
|
|
|
}
|
2023-06-07 17:24:56 +08:00
|
|
|
|
var ossJSON []byte
|
2021-06-07 16:23:37 +08:00
|
|
|
|
|
2023-06-07 17:24:56 +08:00
|
|
|
|
if ossConfig != nil { // OSS
|
|
|
|
|
|
ossJSON, err = json.Marshal(ossConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = ossConfig.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.Fail("校验OSS配置时出错:" + err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
} else { // 普通源站
|
|
|
|
|
|
params.Must.
|
|
|
|
|
|
Field("addr", params.Addr).
|
|
|
|
|
|
Require("请输入源站地址")
|
|
|
|
|
|
|
|
|
|
|
|
var addr = params.Addr
|
|
|
|
|
|
|
|
|
|
|
|
// 是否是完整的地址
|
|
|
|
|
|
if (params.Protocol == "http" || params.Protocol == "https") && regexp.MustCompile(`^(http|https)://`).MatchString(addr) {
|
|
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
addr = u.Host
|
|
|
|
|
|
}
|
2021-06-07 16:23:37 +08:00
|
|
|
|
}
|
2022-06-15 19:40:07 +08:00
|
|
|
|
|
2023-06-07 17:24:56 +08:00
|
|
|
|
addr = regexp.MustCompile(`\s+`).ReplaceAllString(addr, "")
|
|
|
|
|
|
var portIndex = strings.LastIndex(addr, ":")
|
|
|
|
|
|
if portIndex < 0 {
|
|
|
|
|
|
if params.Protocol == "http" {
|
|
|
|
|
|
addr += ":80"
|
|
|
|
|
|
} else if params.Protocol == "https" {
|
|
|
|
|
|
addr += ":443"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.Fail("地址中需要带有端口")
|
|
|
|
|
|
}
|
|
|
|
|
|
portIndex = strings.LastIndex(addr, ":")
|
2022-06-15 19:40:07 +08:00
|
|
|
|
}
|
2023-06-07 17:24:56 +08:00
|
|
|
|
var host = addr[:portIndex]
|
|
|
|
|
|
var port = addr[portIndex+1:]
|
|
|
|
|
|
|
|
|
|
|
|
// 检查端口号
|
|
|
|
|
|
if port == "0" {
|
2022-06-15 19:40:07 +08:00
|
|
|
|
this.Fail("端口号不能为0")
|
|
|
|
|
|
}
|
2023-06-07 17:24:56 +08:00
|
|
|
|
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")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pbAddr = &pb.NetworkAddress{
|
|
|
|
|
|
Protocol: params.Protocol,
|
|
|
|
|
|
Host: host,
|
|
|
|
|
|
PortRange: port,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addrConfig = &serverconfigs.NetworkAddressConfig{
|
|
|
|
|
|
Protocol: serverconfigs.Protocol(params.Protocol),
|
|
|
|
|
|
Host: host,
|
|
|
|
|
|
PortRange: port,
|
2022-06-15 19:40:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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{
|
2023-06-23 11:43:36 +08:00
|
|
|
|
Name: "",
|
|
|
|
|
|
Addr: pbAddr,
|
|
|
|
|
|
OssJSON: ossJSON,
|
|
|
|
|
|
Description: "",
|
|
|
|
|
|
Weight: 10,
|
|
|
|
|
|
IsOn: true,
|
|
|
|
|
|
Domains: domains,
|
|
|
|
|
|
Host: params.Host,
|
|
|
|
|
|
FollowPort: params.FollowPort,
|
|
|
|
|
|
Http2Enabled: params.Http2Enabled,
|
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,
|
2023-06-07 17:24:56 +08:00
|
|
|
|
Addr: addrConfig,
|
|
|
|
|
|
OSS: ossConfig,
|
2020-08-21 12:32:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-07 17:24:56 +08:00
|
|
|
|
this.Data["origin"] = maps.Map{
|
|
|
|
|
|
"id": resp.OriginId,
|
|
|
|
|
|
"isOn": true,
|
|
|
|
|
|
"addr": addrConfig,
|
|
|
|
|
|
"addrSummary": origin.AddrSummary(),
|
|
|
|
|
|
}
|
2020-11-10 21:37:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建日志
|
2023-06-30 18:08:30 +08:00
|
|
|
|
defer this.CreateLogInfo(codes.ServerOrigin_LogCreateOrigin, resp.OriginId)
|
2020-11-10 21:37:48 +08:00
|
|
|
|
|
2020-08-21 12:32:16 +08:00
|
|
|
|
this.Success()
|
|
|
|
|
|
}
|