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