diff --git a/src/api.go b/src/api.go index eb99977..3371db5 100644 --- a/src/api.go +++ b/src/api.go @@ -34,6 +34,7 @@ func RegisterHTTPProxyAPIs(authRouter *auth.RouterDef) { authRouter.HandleFunc("/api/proxy/detail", ReverseProxyListDetail) authRouter.HandleFunc("/api/proxy/edit", ReverseProxyHandleEditEndpoint) authRouter.HandleFunc("/api/proxy/setAlias", ReverseProxyHandleAlias) + authRouter.HandleFunc("/api/proxy/setHostname", ReverseProxyHandleSetHostname) authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint) authRouter.HandleFunc("/api/proxy/updateCredentials", UpdateProxyBasicAuthCredentials) authRouter.HandleFunc("/api/proxy/tlscheck", domainsniff.HandleCheckSiteSupportTLS) diff --git a/src/reverseproxy.go b/src/reverseproxy.go index abb4e79..88a6a7d 100644 --- a/src/reverseproxy.go +++ b/src/reverseproxy.go @@ -673,6 +673,83 @@ func ReverseProxyHandleAlias(w http.ResponseWriter, r *http.Request) { utils.SendOK(w) } +func ReverseProxyHandleSetHostname(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + utils.SendErrorResponse(w, "Method not supported") + return + } + + originalRootnameOrMatchingDomain, err := utils.PostPara(r, "oldHostname") + if err != nil { + utils.SendErrorResponse(w, "Invalid original hostname given") + return + } + + newHostname, err := utils.PostPara(r, "newHostname") + if err != nil { + utils.SendErrorResponse(w, "Invalid new hostname given") + return + } + + originalRootnameOrMatchingDomain = strings.TrimSpace(originalRootnameOrMatchingDomain) + newHostname = strings.TrimSpace(newHostname) + if newHostname == "/" { + //Reserevd, reutrn error + utils.SendErrorResponse(w, "Invalid new hostname: system reserved path") + return + } + + //Check if the endpoint already exists + _, err = dynamicProxyRouter.LoadProxy(newHostname) + if err == nil { + //Endpoint already exists, return error + utils.SendErrorResponse(w, "Endpoint with this hostname already exists") + return + } + + //Clone, edit the endpoint and remove the original one + ept, err := dynamicProxyRouter.LoadProxy(originalRootnameOrMatchingDomain) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + newEndpoint := ept.Clone() + newEndpoint.RootOrMatchingDomain = newHostname + + //Prepare to replace the current routing rule + readyRoutingRule, err := dynamicProxyRouter.PrepareProxyRoute(newEndpoint) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Remove the old endpoint from runtime + err = dynamicProxyRouter.RemoveProxyEndpointByRootname(originalRootnameOrMatchingDomain) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Remove the config from file + err = RemoveReverseProxyConfig(originalRootnameOrMatchingDomain) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Add the new endpoint to runtime + dynamicProxyRouter.AddProxyRouteToRuntime(readyRoutingRule) + + //Save it to file + SaveReverseProxyConfig(newEndpoint) + + //Update uptime monitor targets + UpdateUptimeMonitorTargets() + + utils.SendOK(w) +} + func DeleteProxyEndpoint(w http.ResponseWriter, r *http.Request) { ep, err := utils.PostPara(r, "ep") if err != nil { diff --git a/src/web/components/httprp.html b/src/web/components/httprp.html index 88eaa20..7f71081 100644 --- a/src/web/components/httprp.html +++ b/src/web/components/httprp.html @@ -259,9 +259,21 @@
-

- +

+ +

+
+ + + +
@@ -324,7 +336,8 @@
-

Work In Progress

+

Work In Progress
+ Please use the outer-most menu TLS / SSL tab for now.


@@ -1020,6 +1033,50 @@ saveProxyInlineEdit(uuid); }); + //Bind the edit button + editor.find(".downstream_primary_hostname_edit_btn").off("click").on("click", function(){ + editor.find(".downstream_primary_hostname_edit_btn").parent().hide(); + editor.find(".downstream_primary_hostname_edit_input input").val(subd.RootOrMatchingDomain) + editor.find(".downstream_primary_hostname_edit_input").show(); + }); + editor.find(".cancelDownstreamHostnameBtn").off("click").on("click", function(){ + editor.find(".downstream_primary_hostname_edit_input").hide(); + editor.find(".downstream_primary_hostname_edit_btn").parent().show(); + }); + editor.find(".saveDownstreamHostnameBtn").off("click").on("click", function(){ + let newHostname = editor.find(".downstream_primary_hostname_edit_input input").val().trim(); + if (newHostname.length == 0){ + msgbox("Hostname cannot be empty", false); + return; + } + + if (newHostname == subd.RootOrMatchingDomain){ + //No need to update + editor.find(".downstream_primary_hostname_edit_input").hide(); + editor.find(".downstream_primary_hostname_edit_btn").parent().show(); + return; + } + $.cjax({ + url: "/api/proxy/setHostname", + method: "POST", + data: { + "oldHostname":subd.RootOrMatchingDomain, + "newHostname":newHostname, + }, + success: function(data){ + if (data.error != undefined){ + msgbox(data.error, false); + }else{ + //Update the current editing hostname to DOM + $("#httprpEditModal").attr("editing-host", encodeURIComponent(newHostname)); + //Grab the new hostname from server + resyncProxyEditorConfig(); + } + } + }); + }); + editor.find(".downstream_primary_hostname_edit_input").hide(); + editor.find(".downstream_primary_hostname_edit_btn").parent().show(); //Build the alias hostname list let aliasHTML = ""; diff --git a/src/web/components/rproot.html b/src/web/components/rproot.html index 0c39b43..c8f22d8 100644 --- a/src/web/components/rproot.html +++ b/src/web/components/rproot.html @@ -1,7 +1,7 @@

Default Site

-

Default routing options for inbound traffic (previously called Proxy Root)

+

Default routing options for inbound traffic

@@ -209,14 +209,13 @@ }) } - //Set the new proxy root option + //Set the new proxy root (aka default site) option function setProxyRoot(btn=undefined){ var newpr = $("#proxyRoot").val(); if (newpr.trim() == "" && currentDefaultSiteOption == 0){ //Fill in the web server info newpr = "127.0.0.1:" + $("#webserv_listenPort").val(); $("#proxyRoot").val(newpr); - } var rootReqTls = $("#rootReqTLS")[0].checked; diff --git a/src/web/index.html b/src/web/index.html index 27cd438..d87cb00 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -120,7 +120,7 @@
- +