diff --git a/internal/web/actions/default/servers/addOriginPopup.go b/internal/web/actions/default/servers/addOriginPopup.go index 9e39e906..ac45a06f 100644 --- a/internal/web/actions/default/servers/addOriginPopup.go +++ b/internal/web/actions/default/servers/addOriginPopup.go @@ -6,6 +6,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/actions" + "net/url" "regexp" "strings" ) @@ -36,10 +37,27 @@ func (this *AddOriginPopupAction) RunPost(params struct { Field("addr", params.Addr). Require("请输入源站地址") - addr := regexp.MustCompile(`\s+`).ReplaceAllString(params.Addr, "") - portIndex := strings.LastIndex(params.Addr, ":") + 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 + } + } + + addr = regexp.MustCompile(`\s+`).ReplaceAllString(addr, "") + portIndex := strings.LastIndex(addr, ":") if portIndex < 0 { - this.Fail("地址中需要带有端口") + if params.Protocol == "http" { + addr += ":80" + } else if params.Protocol == "https" { + addr += ":443" + } else { + this.Fail("地址中需要带有端口") + } + portIndex = strings.LastIndex(addr, ":") } host := addr[:portIndex] port := addr[portIndex+1:] diff --git a/web/views/@default/servers/addOriginPopup.html b/web/views/@default/servers/addOriginPopup.html index 061ce937..e34ae106 100644 --- a/web/views/@default/servers/addOriginPopup.html +++ b/web/views/@default/servers/addOriginPopup.html @@ -8,7 +8,7 @@ 源站协议 - @@ -28,8 +28,8 @@ 源站地址 - -

源站服务器地址,通常是一个IP(或域名)加端口,不需要加 http:// 或 https://

+ +

{{addrError}}源站服务器地址,通常是一个IP(或域名)加端口,不需要加 http:// 或 https://

diff --git a/web/views/@default/servers/addOriginPopup.js b/web/views/@default/servers/addOriginPopup.js new file mode 100644 index 00000000..30ffa251 --- /dev/null +++ b/web/views/@default/servers/addOriginPopup.js @@ -0,0 +1,44 @@ +Tea.context(function () { + this.addr = "" + this.protocol = "" + + this.addrError = "" + + if (this.serverType == "httpProxy") { + this.protocol = "http" + } + + this.changeProtocol = function () { + this.checkPort() + } + + this.changeAddr = function () { + if (this.serverType == "httpProxy") { + if (this.addr.startsWith("http://")) { + this.protocol = "http" + } else if (this.addr.startsWith("https://")) { + this.protocol = "https" + } + } + + this.checkPort() + } + + this.checkPort = function () { + this.addrError = "" + + // HTTP + if (this.protocol == "http") { + if (this.addr.endsWith(":443")) { + this.addrError = "443通常是HTTPS协议端口,请确认源站协议选择是否正确。" + } + } + + // HTTPS + if (this.protocol == "https") { + if (this.addr.endsWith(":80")) { + this.addrError = "80通常是HTTP协议端口,请确认源站协议选择是否正确。" + } + } + } +}) \ No newline at end of file