|
@@ -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 {
|