|
@@ -24,21 +24,25 @@ import (
|
|
|
|
|
|
*/
|
|
|
type Router struct {
|
|
|
- ListenPort int
|
|
|
- ProxyEndpoints *sync.Map
|
|
|
- SubdomainEndpoint *sync.Map
|
|
|
- Running bool
|
|
|
- Root *ProxyEndpoint
|
|
|
- tlsCertManager *tlscert.Manager
|
|
|
- mux http.Handler
|
|
|
- TlsManager *tlscert.Manager
|
|
|
- useTLS bool
|
|
|
- server *http.Server
|
|
|
- tlsListener net.Listener
|
|
|
+ ListenPort int
|
|
|
+ ProxyEndpoints *sync.Map
|
|
|
+ SubdomainEndpoint *sync.Map
|
|
|
+ Running bool
|
|
|
+ Root *ProxyEndpoint
|
|
|
+ tlsCertManager *tlscert.Manager
|
|
|
+ mux http.Handler
|
|
|
+ TlsManager *tlscert.Manager
|
|
|
+ useTLS bool
|
|
|
+ useHttpToHttpsRedirect bool
|
|
|
+ server *http.Server
|
|
|
+ tlsListener net.Listener
|
|
|
}
|
|
|
|
|
|
type RouterOption struct {
|
|
|
- Port int
|
|
|
+ Port int
|
|
|
+ UseTls bool
|
|
|
+ ForceHttpsRedirect bool
|
|
|
+ TlsManager *tlscert.Manager
|
|
|
}
|
|
|
|
|
|
type ProxyEndpoint struct {
|
|
@@ -59,17 +63,18 @@ type ProxyHandler struct {
|
|
|
Parent *Router
|
|
|
}
|
|
|
|
|
|
-func NewDynamicProxy(port int, useTls bool, tlsManager *tlscert.Manager) (*Router, error) {
|
|
|
+func NewDynamicProxy(option RouterOption) (*Router, error) {
|
|
|
proxyMap := sync.Map{}
|
|
|
domainMap := sync.Map{}
|
|
|
thisRouter := Router{
|
|
|
- ListenPort: port,
|
|
|
- ProxyEndpoints: &proxyMap,
|
|
|
- SubdomainEndpoint: &domainMap,
|
|
|
- Running: false,
|
|
|
- tlsCertManager: tlsManager,
|
|
|
- useTLS: useTls,
|
|
|
- server: nil,
|
|
|
+ ListenPort: option.Port,
|
|
|
+ ProxyEndpoints: &proxyMap,
|
|
|
+ SubdomainEndpoint: &domainMap,
|
|
|
+ Running: false,
|
|
|
+ tlsCertManager: option.TlsManager,
|
|
|
+ useTLS: option.UseTls,
|
|
|
+ useHttpToHttpsRedirect: option.ForceHttpsRedirect,
|
|
|
+ server: nil,
|
|
|
}
|
|
|
|
|
|
thisRouter.mux = &ProxyHandler{
|
|
@@ -83,6 +88,13 @@ func NewDynamicProxy(port int, useTls bool, tlsManager *tlscert.Manager) (*Route
|
|
|
//if it is already running in the background
|
|
|
func (router *Router) UpdateTLSSetting(tlsEnabled bool) {
|
|
|
router.useTLS = tlsEnabled
|
|
|
+ router.Restart()
|
|
|
+}
|
|
|
+
|
|
|
+//Update https redirect, which will require updates
|
|
|
+func (router *Router) UpdateHttpToHttpsRedirectSetting(useRedirect bool) {
|
|
|
+ router.useHttpToHttpsRedirect = useRedirect
|
|
|
+ router.Restart()
|
|
|
}
|
|
|
|
|
|
//Start the dynamic routing
|
|
@@ -111,7 +123,7 @@ func (router *Router) StartProxyService() error {
|
|
|
router.server = &http.Server{Addr: ":" + strconv.Itoa(router.ListenPort), Handler: router.mux}
|
|
|
router.Running = true
|
|
|
|
|
|
- if router.ListenPort == 443 {
|
|
|
+ if router.ListenPort == 443 && router.useHttpToHttpsRedirect {
|
|
|
//Add a 80 to 443 redirector
|
|
|
httpServer := &http.Server{
|
|
|
Addr: ":80",
|
|
@@ -133,6 +145,7 @@ func (router *Router) StartProxyService() error {
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
defer cancel()
|
|
|
httpServer.Shutdown(ctx)
|
|
|
+ log.Println(":80 to :433 redirection listener stopped")
|
|
|
}()
|
|
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
log.Fatalf("Could not start server: %v\n", err)
|