Browse Source

Changed upstream enable state to disable

Toby Chui 8 months ago
parent
commit
0a3b87c3bf

+ 2 - 2
mod/dynamicproxy/loadbalance/loadbalance.go

@@ -40,7 +40,7 @@ type Upstream struct {
 	//Load balancing configs
 	Priority int  //Prirotiy of fallback, set all to 0 for round robin
 	MaxConn  int  //Maxmium connection to this server, 0 for unlimited
-	Enabled  bool //If this upstream is enabled
+	Disabled bool //If this upstream is disabled
 
 	currentConnectionCounts atomic.Uint64 //Counter for number of client currently connected
 	proxy                   *dpcore.ReverseProxy
@@ -62,7 +62,7 @@ func NewLoadBalancer(options *Options) *RouteManager {
 // origin server that is ready
 func (m *RouteManager) UpstreamsReady(upstreams []*Upstream) bool {
 	for _, upstream := range upstreams {
-		if !upstream.Enabled {
+		if upstream.Disabled {
 			//This upstream is disabled. Assume offline
 			continue
 		}

+ 1 - 1
mod/update/v308/typedef308.go

@@ -21,7 +21,7 @@ type v308Upstream struct {
 	//Load balancing configs
 	Priority int  //Prirotiy of fallback, set all to 0 for round robin
 	MaxConn  int  //Maxmium connection to this server
-	Enabled  bool //If this upstream is enabled
+	Disabled bool //If this upstream is enabled
 }
 
 // A proxy endpoint record, a general interface for handling inbound routing

+ 1 - 1
mod/update/v308/v308.go

@@ -90,7 +90,7 @@ func convertV307ToV308(old v307ProxyEndpoint) v308ProxyEndpoint {
 			SkipWebSocketOriginCheck: old.SkipWebSocketOriginCheck,
 			Priority:                 0,
 			MaxConn:                  0,
-			Enabled:                  true,
+			Disabled:                 false,
 		}},
 		UseStickySession:             false,
 		Disabled:                     old.Disabled,

+ 8 - 2
reverseproxy.go

@@ -325,6 +325,7 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
 					SkipCertValidations:      skipTlsValidation,
 					SkipWebSocketOriginCheck: bypassWebsocketOriginCheck,
 					Priority:                 0,
+					Disabled:                 false,
 				},
 			},
 			UseStickySession: false, //TODO: Move options to webform
@@ -387,9 +388,10 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
 				{
 					OriginIpOrDomain:         endpoint,
 					RequireTLS:               useTLS,
-					SkipCertValidations:      false,
+					SkipCertValidations:      true,
 					SkipWebSocketOriginCheck: true,
 					Priority:                 0,
+					Disabled:                 false,
 				},
 			},
 			BypassGlobalTLS:   false,
@@ -402,7 +404,11 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
-		dynamicProxyRouter.SetProxyRouteAsRoot(preparedRootProxyRoute)
+		err = dynamicProxyRouter.SetProxyRouteAsRoot(preparedRootProxyRoute)
+		if err != nil {
+			utils.SendErrorResponse(w, "unable to update default site: "+err.Error())
+			return
+		}
 		proxyEndpointCreated = &rootRoutingEndpoint
 	} else {
 		//Invalid eptype

+ 0 - 21
web/components/httprp.html

@@ -308,12 +308,6 @@
                                 Security Options
                             </div>
                             <div class="content">
-                                <div class="ui checkbox" style="margin-top: 0.4em;">
-                                    <input type="checkbox" class="SkipWebSocketOriginCheck" ${wsCheckstate}>
-                                    <label>Skip WebSocket Origin Check<br>
-                                    <small>Check this to allow cross-origin websocket requests</small></label>
-                                </div>
-                                <br>
                                 <div class="ui checkbox" style="margin-top: 0.4em;">
                                     <input type="checkbox" onchange="handleToggleRateLimitInput();" class="RequireRateLimit" ${rateLimitCheckState}>
                                     <label>Require Rate Limit<br>
@@ -426,21 +420,6 @@
             }
         })
     }
-
-    //Clearn the proxy target value, make sure user do not enter http:// or https://
-    //and auto select TLS checkbox if https:// exists
-    function cleanProxyTargetValue(input){
-        let targetDomain = $(input).val().trim();
-        if (targetDomain.startsWith("http://")){
-            targetDomain = targetDomain.substr(7);
-            $(input).val(targetDomain);
-            $("#httpProxyList input.RequireTLS").parent().checkbox("set unchecked");
-        }else if (targetDomain.startsWith("https://")){
-            targetDomain = targetDomain.substr(8);
-            $(input).val(targetDomain);
-            $("#httpProxyList input.RequireTLS").parent().checkbox("set checked");
-        }
-    }
     
     /* button events */
     function editBasicAuthCredentials(uuid){

+ 30 - 3
web/components/rules.html

@@ -26,7 +26,7 @@
                     </div>
                     <div class="field">
                         <label>Target IP Address or Domain Name with port</label>
-                            <input type="text" id="proxyDomain" onchange="autoCheckTls(this.value);">
+                            <input type="text" id="proxyDomain" onchange="autoFillTargetTLS(this);">
                         <small>E.g. 192.168.0.101:8000 or example.com</small>
                     </div>
                     <div class="field dockerOptimizations" style="display:none;">
@@ -265,7 +265,26 @@
         }
     }
 
+    //Clearn the proxy target value, make sure user do not enter http:// or https://
+    //and auto select TLS checkbox if https:// exists
+    function autoFillTargetTLS(input){
+        let targetDomain = $(input).val().trim();
+        if (targetDomain.startsWith("http://")){
+            targetDomain = targetDomain.substr(7);
+            $(input).val(targetDomain);
+            $("#reqTls").parent().checkbox("set unchecked");
+        }else if (targetDomain.startsWith("https://")){
+            targetDomain = targetDomain.substr(8);
+            $(input).val(targetDomain);
+            $("#reqTls").parent().checkbox("set checked");
+        }else{
+            //No http or https was given. Sniff it
+            autoCheckTls(targetDomain);
+        }
+    }
+    
 
+    //Automatic check if the site require TLS and check the checkbox if needed
     function autoCheckTls(targetDomain){
        $.ajax({
             url: "/api/proxy/tlscheck",
@@ -460,15 +479,23 @@
     
     /* UI Element Initialization */
     function initAdvanceSettingsAccordion(){
-        if ($("#advanceProxyRules").length > 0){
+        function hasClickEvent(element) {
+            var events = $._data(element, "events");
+            return events && events.click && events.click.length > 0;
+        }
+
+        if (!hasClickEvent($("#advanceProxyRules"))){
+            // Not sure why sometime the accordion events are not binding
+            // to the DOM element. This makes sure the element is binded
+            // correctly by checking it again after 300ms
             $("#advanceProxyRules").accordion();
             $("#newProxyRuleAccessFilter").parent().dropdown();
-        }else{
             setTimeout(function(){
                 initAdvanceSettingsAccordion();
             }, 300);
         }
     }
+    initAdvanceSettingsAccordion();
    
  
    

+ 110 - 10
web/snippet/upstreams.html

@@ -11,6 +11,25 @@
                 top: 0.6em;
                 right: 0.6em;
             }
+
+            .upstreamLink{
+                max-width: 220px;
+                display: inline-block;
+                word-break: break-all;
+            }
+
+            .ui.toggle.checkbox input:checked ~ label::before{
+                background-color: #00ca52 !important;
+            }
+
+            @media (max-width: 499px) {
+                .upstreamActions{
+                    position: relative;
+                    margin-top: 1em;
+                    margin-left: 0.4em;
+                    margin-bottom: 0.4em;
+                }
+            }
         </style>
     </head>
     <body>
@@ -18,15 +37,58 @@
         <div class="ui container">
             <div class="ui header">
                 <div class="content">
-                    Upstreams / Origins
+                    Upstreams / Load Balance
                     <div class="sub header epname"></div>
                 </div>
             </div>
             <div class="ui divider"></div>
-            <div id="upstreamTable">
-                <div class="ui segment">
-                    <a></a>
+            <div class="ui small pointing secondary menu">
+                <a class="item active narrowpadding" data-tab="upstreamlist">Upstreams</a>
+                <a class="item narrowpadding" data-tab="newupstream">Add Upstream</a>
+            </div>
+            <div class="ui tab basic segment active" data-tab="upstreamlist">
+                <!-- A list of current existing upstream on this reverse proxy-->
+                <div id="upstreamTable">
+                    <div class="ui segment">
+                        <a><i class="ui loading spinner icon"></i> Loading</a>
+                    </div>
+                </div>
+                <div class="ui message">
+                    <i class="ui blue info circle icon"></i> Round-robin load balancing algorithm will be used for upstreams with same priority. Lower priority origin server will be used as fallback when all the higher priority server are offline.
+                 </div>
+            </div>
+            <div class="ui tab basic segment" data-tab="newupstream">
+                <!-- Web Form to create a new upstream -->
+                <h4 class="ui header">
+                    <i class="green add icon"></i>
+                    <div class="content">
+                        Add Upstream Server
+                        <div class="sub header">Create new fallback or load balance upstream</div>
+                    </div>
+                </h4>
+                <p style="margin-bottom: 0.4em;">Target IP address with port</p>
+                <div class="ui fluid small input">
+                    <input type="text" placeholder="" onchange="cleanProxyTargetValue(this);"><br>
+                </div>
+                <small>E.g. 192.168.0.101:8000 or example.com</small>
+                <br><br>
+                <div class="ui checkbox">
+                    <input type="checkbox" id="requireTLS">
+                    <label>Require TLS<br>
+                        <small>Proxy target require HTTPS connection</small></label>
+                </div><br>
+                <div class="ui checkbox" style="margin-top: 0.6em;">
+                    <input type="checkbox" id="skipTlsVerification">
+                     <label>Skip Verification<br>
+                        <small>Check this if proxy target is using self signed certificates</small></label>
+                </div><br>
+                 <div class="ui checkbox" style="margin-top: 0.4em;">
+                    <input type="checkbox" id="SkipWebSocketOriginCheck" ${upstream.SkipWebSocketOriginCheck?"checked":""}>
+                    <label>Skip WebSocket Origin Check<br>
+                    <small>Check this to allow cross-origin websocket requests</small></label>
                 </div>
+                <br><br>
+                <button class="ui basic button"><i class="ui green circle check icon"></i> Create</button>
             </div>
             <div class="ui divider"></div>
             <div class="field" >
@@ -40,7 +102,7 @@
         <script>
             let origins = [];
             let editingEndpoint = {};
-
+            $('.menu .item').tab();
            
 
             function initOriginList(){
@@ -70,14 +132,23 @@
                                         }
                                     }
 
+                                    //Priority Arrows 
+                                    let upArrowClass = "";
+                                    if (upstream.Priority == 0 ){
+                                        //Cannot go any higher
+                                        upArrowClass = "disabled";
+                                    }
                                     let url = `${upstream.RequireTLS?"https://":"http://"}${upstream.OriginIpOrDomain}`
                                     
                                     $("#upstreamTable").append(`<div class="ui segment">
                                         <h4 class="ui header">
-                                            ${tlsIcon}
+                                             <div class="ui toggle checkbox" style="display:inline-block;">
+                                                <input type="checkbox" name="enabled" style="margin-top: 0.4em;" ${!upstream.Disabled?"checked":""}>
+                                                <label></label>
+                                            </div>
                                             <div class="content">
-                                            <a href="${url}" target="_blank">${upstream.OriginIpOrDomain}</a>
-                                                <div class="sub header">Online</div>
+                                            <a href="${url}" target="_blank" class="upstreamLink">${upstream.OriginIpOrDomain} ${tlsIcon}</a>
+                                                <div class="sub header">Online | Priority: ${upstream.Priority}</div>
                                             </div>
                                         </h4>
                                         <div class="ui divider"></div>
@@ -90,15 +161,27 @@
                                             <input type="checkbox" name="example">
                                              <label>Skip Verification<br>
                                                 <small>Check this if proxy target is using self signed certificates</small></label>
-                                        </div>
+                                        </div><br>
+                                         <div class="ui checkbox" style="margin-top: 0.4em;">
+                                            <input type="checkbox" class="SkipWebSocketOriginCheck" ${upstream.SkipWebSocketOriginCheck?"checked":""}>
+                                            <label>Skip WebSocket Origin Check<br>
+                                            <small>Check this to allow cross-origin websocket requests</small></label>
+                                        </div><br>
+                                       
                                         <div class="upstreamActions">
+                                            <!-- Change Priority -->
+                                            <button class="ui basic circular icon button ${upArrowClass} highPriorityButton" title="Higher Priority"><i class="ui arrow up icon"></i></button>
+                                            <button class="ui basic circular icon button lowPriorityButton" title="Lower Priority"><i class="ui arrow down icon"></i></button>
                                             <button class="ui basic circular icon button" title="Edit Upstream Destination"><i class="ui grey edit icon"></i></button>
                                             <button class="ui basic circular icon button" title="Remove Upstream"><i class="ui red trash icon"></i></button>
                                         </div>
-                                        
                                     </div>`);
                                 });
 
+                                if (data.Origins.length == 1){
+                                    $(".lowPriorityButton").addClass('disabled');
+                                }
+
                                 $(".ui.checkbox").checkbox();
                             }else{
                                 //Assume no origins
@@ -114,6 +197,23 @@
             }
             
 
+            /* New Upstream Origin Functions */
+
+            //Clearn the proxy target value, make sure user do not enter http:// or https://
+            //and auto select TLS checkbox if https:// exists
+            function cleanProxyTargetValue(input){
+                let targetDomain = $(input).val().trim();
+                if (targetDomain.startsWith("http://")){
+                    targetDomain = targetDomain.substr(7);
+                    $(input).val(targetDomain);
+                    $("#requireTLS").parent().checkbox("set unchecked");
+                }else if (targetDomain.startsWith("https://")){
+                    targetDomain = targetDomain.substr(8);
+                    $(input).val(targetDomain);
+                    $("#requireTLS").parent().checkbox("set checked");
+                }
+            }
+
             if (window.location.hash.length > 1){
                 let payloadHash = window.location.hash.substr(1);
                 try{