瀏覽代碼

auto update script executed

Toby Chui 1 年之前
父節點
當前提交
e0b67ce7b4
共有 2 個文件被更改,包括 23 次插入10 次删除
  1. 15 9
      mod/dynamicproxy/dynamicproxy.go
  2. 8 1
      web/components/status.html

+ 15 - 9
mod/dynamicproxy/dynamicproxy.go

@@ -44,6 +44,8 @@ type Router struct {
 	server            *http.Server
 	tlsListener       net.Listener
 	routingRules      []*RoutingRule
+
+	tlsRedirectStop chan bool
 }
 
 type ProxyEndpoint struct {
@@ -127,11 +129,8 @@ func (router *Router) StartProxyService() error {
 			httpServer := &http.Server{
 				Addr: ":80",
 				Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-					protocol := "http://"
-					if router.Option.UseTls {
-						protocol = "https://"
-					}
-					if router.Option.Port == 443 && router.Option.UseTls {
+					protocol := "https://"
+					if router.Option.Port == 443 {
 						http.Redirect(w, r, protocol+r.Host+r.RequestURI, http.StatusTemporaryRedirect)
 					} else {
 						http.Redirect(w, r, protocol+r.Host+":"+strconv.Itoa(router.Option.Port)+r.RequestURI, http.StatusTemporaryRedirect)
@@ -144,21 +143,23 @@ func (router *Router) StartProxyService() error {
 			}
 
 			log.Println("Starting HTTP-to-HTTPS redirector (port 80)")
+
+			//Create a redirection stop channel
+			stopChan := make(chan bool)
 			go func() {
 				//Start another router to check if the router.server is killed. If yes, kill this server as well
 				go func() {
-					for router.server != nil {
-						time.Sleep(100 * time.Millisecond)
-					}
+					<-stopChan
 					ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 					defer cancel()
 					httpServer.Shutdown(ctx)
-					log.Println(":80 to :433 redirection listener stopped")
+					log.Println("HTTP to HTTPS redirection listener stopped")
 				}()
 				if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
 					log.Fatalf("Could not start server: %v\n", err)
 				}
 			}()
+			router.tlsRedirectStop = stopChan
 		}
 		log.Println("Reverse proxy service started in the background (TLS mode)")
 		go func() {
@@ -196,10 +197,15 @@ func (router *Router) StopProxyService() error {
 		router.tlsListener.Close()
 	}
 
+	if router.tlsRedirectStop != nil {
+		router.tlsRedirectStop <- true
+	}
+
 	//Discard the server object
 	router.tlsListener = nil
 	router.server = nil
 	router.Running = false
+	router.tlsRedirectStop = nil
 	return nil
 }
 

+ 8 - 1
web/components/status.html

@@ -66,7 +66,7 @@
 <br>
 <div id="redirect" class="ui toggle checkbox" style="margin-top: 0.6em;">
     <input type="checkbox">
-    <label>Force redirect HTTP request to inbound port<br>
+    <label>Force redirect HTTP request to HTTPS<br>
         <small>(Only apply when listening port is not 80)</small></label>
 </div>
 <br>
@@ -342,11 +342,18 @@
         $.get("/api/cert/tls", function(data){
             if (data == true){
                 $("#tls").checkbox("set checked");
+            }else{
+                $("#redirect").addClass('disabled');
             }
 
             //Initiate the input listener on the checkbox
             $("#tls").find("input").on("change", function(){
                 let thisValue = $("#tls").checkbox("is checked");
+                if (thisValue){
+                    $("#redirect").removeClass('disabled');
+                }else{
+                    $("#redirect").addClass('disabled');
+                }
                 $.ajax({
                     url: "/api/cert/tls",
                     data: {set: thisValue},