ソースを参照

auto update script executed

Toby Chui 1 年間 前
コミット
38e91c8173
2 ファイル変更28 行追加0 行削除
  1. 28 0
      mod/dynamicproxy/dynamicproxy.go
  2. BIN
      sys.db

+ 28 - 0
mod/dynamicproxy/dynamicproxy.go

@@ -111,6 +111,34 @@ func (router *Router) StartProxyService() error {
 		router.server = &http.Server{Addr: ":" + strconv.Itoa(router.ListenPort), Handler: router.mux}
 		router.Running = true
 
+		if router.ListenPort == 443 {
+			//Add a 80 to 443 redirector
+			httpServer := &http.Server{
+				Addr: ":80",
+				Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+					http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusTemporaryRedirect)
+				}),
+				ReadTimeout:  3 * time.Second,
+				WriteTimeout: 3 * time.Second,
+				IdleTimeout:  120 * time.Second,
+			}
+
+			log.Println("Starting HTTP-to-HTTPS redirector (port 80)")
+			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)
+					}
+					ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+					defer cancel()
+					httpServer.Shutdown(ctx)
+				}()
+				if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+					log.Fatalf("Could not start server: %v\n", err)
+				}
+			}()
+		}
 		log.Println("Reverse proxy service started in the background (TLS mode)")
 		go func() {
 			if err := router.server.Serve(ln); err != nil && err != http.ErrServerClosed {