From 8d77d3fb56b4fe242f6345996765d658bfb53858 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Wed, 7 Feb 2018 16:39:59 -0600 Subject: [PATCH] fixed issue where multiple bundles used the same instance of http.Server --- gpanel.go | 16 +++++++++++----- pkg/gpaccount/apihandler.go | 4 ++-- pkg/gpaccount/gpaccount.go | 8 +++----- pkg/gpaccount/servehttp.go | 7 ++++--- pkg/gpaccount/toggle.go | 10 +++++----- pkg/public/public.go | 8 +++----- pkg/public/restart.go | 8 ++++---- pkg/public/toggle.go | 10 +++++----- pkg/router/proxy.go | 23 +++++++++++++++++++++-- pkg/router/router.go | 37 +++++++++++++++++++++++-------------- 10 files changed, 81 insertions(+), 50 deletions(-) diff --git a/gpanel.go b/gpanel.go index 50d0a8c..b5d293a 100644 --- a/gpanel.go +++ b/gpanel.go @@ -3,14 +3,20 @@ package main import ( "log" "net/http" + "strconv" "github.com/Ennovar/gPanel/pkg/gpserver" "github.com/Ennovar/gPanel/pkg/router" ) func main() { - mains := gpserver.New() - router := router.New() + const serverPort = 2082 + + const insecurePort = 2080 + const securePort = 2443 + + server := gpserver.New() + router := router.New(insecurePort, securePort) if router == nil { log.Fatal("Error starting router") @@ -18,7 +24,7 @@ func main() { router.Start() log.Print("To Exit: CTRL+C") - log.Print("Domain router is listening on localhost:2080") - log.Print("Listening (server) on localhost:2082, serving out of the server/document_root/ directory...") - http.ListenAndServe("localhost:2082", mains) + log.Print("Domain router is listening on localhost:" + strconv.Itoa(insecurePort) + " (HTTP) and localhost:" + strconv.Itoa(securePort) + " (HTTPS)") + log.Print("gPanel Server is listening on localhost:" + strconv.Itoa(serverPort) + ", serving out of the server/document_root/ directory...") + http.ListenAndServe("localhost:"+strconv.Itoa(serverPort), server) } diff --git a/pkg/gpaccount/apihandler.go b/pkg/gpaccount/apihandler.go index 8bc52c4..abf6ee6 100644 --- a/pkg/gpaccount/apihandler.go +++ b/pkg/gpaccount/apihandler.go @@ -18,9 +18,9 @@ import ( func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (bool, bool) { path := req.URL.Path[1:] if len(path) == 0 { - path = (con.Directory + "index.html") + path = con.Directory + "index.html" } else { - path = (con.Directory + path) + path = con.Directory + path } splitUrl := strings.SplitN(path, "api", 2) diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index e786291..d6142e8 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -22,11 +22,9 @@ type Controller struct { Status int AccountLogger *log.Logger APILogger *log.Logger + Server http.Server } -var controller Controller -var httpserver http.Server - // New returns a new Controller reference. func New(dir, name string, accPort, pubPort int) *Controller { f, err := os.OpenFile(dir+"logs/account_errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) @@ -37,7 +35,7 @@ func New(dir, name string, accPort, pubPort int) *Controller { apiLogger := log.New(f, "API :: ", 3) accountLogger := log.New(f, "ACCOUNT :: ", 3) - controller = Controller{ + controller := Controller{ Directory: dir, DocumentRoot: "account/", Name: name, @@ -49,7 +47,7 @@ func New(dir, name string, accPort, pubPort int) *Controller { APILogger: apiLogger, } - httpserver = http.Server{ + controller.Server = http.Server{ Addr: "localhost:" + strconv.Itoa(controller.Port), Handler: &controller, ReadTimeout: 30 * time.Second, diff --git a/pkg/gpaccount/servehttp.go b/pkg/gpaccount/servehttp.go index 140e0a7..4027cb4 100644 --- a/pkg/gpaccount/servehttp.go +++ b/pkg/gpaccount/servehttp.go @@ -6,8 +6,9 @@ import ( "os" "strconv" - "github.com/Ennovar/gPanel/pkg/routing" "strings" + + "github.com/Ennovar/gPanel/pkg/routing" ) // ServeHTTP function routes all requests for the private webhost server. It is used in the main @@ -15,9 +16,9 @@ import ( func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { path := req.URL.Path[1:] if len(path) == 0 { - path = (con.DocumentRoot + "index.html") + path = con.DocumentRoot + "index.html" } else { - path = (con.DocumentRoot + path) + path = con.DocumentRoot + path } if strings.HasSuffix(path, "index.html") { diff --git a/pkg/gpaccount/toggle.go b/pkg/gpaccount/toggle.go index 6179fef..5c349fb 100644 --- a/pkg/gpaccount/toggle.go +++ b/pkg/gpaccount/toggle.go @@ -9,12 +9,12 @@ import ( func (con *Controller) Start() error { if con.Status == 1 { - return errors.New("Account server is already on.") + return errors.New("account server is already on") } con.Status = 1 - go httpserver.ListenAndServe() - log.Printf("gPanel account server now serving out of %s%s on port %d\n", con.Directory, con.DocumentRoot, con.Port) + go con.Server.ListenAndServe() + log.Printf("gPanel Account %v now serving out of %s on port %d\n", con.Name, con.DocumentRoot, con.Port) return nil } @@ -23,7 +23,7 @@ func (con *Controller) Stop(graceful bool) error { context, cancel := context.WithTimeout(context.Background(), con.GracefulShutdownTimeout) defer cancel() - err := httpserver.Shutdown(context) + err := con.Server.Shutdown(context) if err == nil { return nil } @@ -31,7 +31,7 @@ func (con *Controller) Stop(graceful bool) error { log.Printf("Graceful shutdown failed attempting forced: %v\n", err) } - if err := httpserver.Close(); err != nil { + if err := con.Server.Close(); err != nil { return err } diff --git a/pkg/public/public.go b/pkg/public/public.go index 19c01cf..c931a55 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -18,11 +18,9 @@ type Controller struct { Status int PublicLogger *log.Logger LoadTimeLogger *log.Logger + Server http.Server } -var controller Controller -var server http.Server - // New function returns a new PublicWeb type. func New(dir, accountDir string, port int) *Controller { ph, lh, err := getLogHandles(dir) @@ -30,7 +28,7 @@ func New(dir, accountDir string, port int) *Controller { log.Fatalf("Error trying to start logging instances within %v: %v", dir, err.Error()) } - controller = Controller{ + controller := Controller{ Directory: dir, AccountDirectory: accountDir, Port: port, @@ -40,7 +38,7 @@ func New(dir, accountDir string, port int) *Controller { LoadTimeLogger: lh, } - server = http.Server{ + controller.Server = http.Server{ Addr: "localhost:" + strconv.Itoa(controller.Port), Handler: &controller, ReadTimeout: 30 * time.Second, diff --git a/pkg/public/restart.go b/pkg/public/restart.go index 8fbc5f1..9b601a2 100644 --- a/pkg/public/restart.go +++ b/pkg/public/restart.go @@ -15,24 +15,24 @@ func (con *Controller) Restart(graceful bool) error { context, cancel := context.WithTimeout(context.Background(), con.GracefulShutdownTimeout) defer cancel() - err := server.Shutdown(context) + err := con.Server.Shutdown(context) if err != nil { fmt.Printf("Graceful shutdown failed attempting forced: %v\n", err) - err = server.Close() + err = con.Server.Close() if err != nil { return err } } } - err := server.Close() + err := con.Server.Close() if err != nil { return err } con.Status = 1 - go server.ListenAndServe() + go con.Server.ListenAndServe() return nil } diff --git a/pkg/public/toggle.go b/pkg/public/toggle.go index fa39307..f2cac0e 100644 --- a/pkg/public/toggle.go +++ b/pkg/public/toggle.go @@ -10,12 +10,12 @@ import ( // Start function starts listening on the public server func (con *Controller) Start() error { if con.Status == 1 { - return errors.New("Public server is already on.") + return errors.New("public server is already on") } con.Status = 1 - go server.ListenAndServe() - log.Printf("Public server now serving out of %s on port %d\n", con.Directory+"public/", con.Port) + go con.Server.ListenAndServe() + log.Printf("Public server now serving out of %s on port %d\n", con.Directory+"document_root/", con.Port) return nil } @@ -25,7 +25,7 @@ func (con *Controller) Stop(graceful bool) error { context, cancel := context.WithTimeout(context.Background(), con.GracefulShutdownTimeout) defer cancel() - err := server.Shutdown(context) + err := con.Server.Shutdown(context) if err == nil { return nil } @@ -33,7 +33,7 @@ func (con *Controller) Stop(graceful bool) error { log.Printf("Graceful shutdown failed attempting forced: %v\n", err) } - if err := server.Close(); err != nil { + if err := con.Server.Close(); err != nil { return err } diff --git a/pkg/router/proxy.go b/pkg/router/proxy.go index 88b2ae3..24299c9 100644 --- a/pkg/router/proxy.go +++ b/pkg/router/proxy.go @@ -28,7 +28,26 @@ func (customTrip) RoundTrip(req *http.Request) (*http.Response, error) { return http.DefaultTransport.RoundTrip(req) } -func proxyDirector(req *http.Request) { +func proxyDirectorSecure(req *http.Request) { + host := req.Host + if strings.Count(host, ".") == 2 { + host = strings.SplitN(host, ".", 2)[1] //Remove sub-domain + } + + req.Header.Set("Host", req.Host) + req.URL.Scheme = "https" + + mutex.Lock() + if d, ok := domainToPort[host]; ok { + mutex.Unlock() + req.URL.Host = "127.0.0.1:" + strconv.Itoa(d.PublicPort) + } else { + mutex.Unlock() + req.URL.Host = "fail" + } +} + +func proxyDirectorInsecure(req *http.Request) { host := req.Host if strings.Count(host, ".") == 2 { host = strings.SplitN(host, ".", 2)[1] //Remove sub-domain @@ -40,7 +59,7 @@ func proxyDirector(req *http.Request) { mutex.Lock() if d, ok := domainToPort[host]; ok { mutex.Unlock() - req.URL.Host = "127.0.0.1:" + strconv.Itoa(d) + req.URL.Host = "127.0.0.1:" + strconv.Itoa(d.PublicPort) } else { mutex.Unlock() req.URL.Host = "fail" diff --git a/pkg/router/router.go b/pkg/router/router.go index 084a5da..7d8da92 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -13,12 +13,14 @@ import ( ) type Router struct { - Port int + InsecurePort int + SecurePort int } -var server http.Server -var domainToPort map[string]int +var secureServer http.Server +var insecureServer http.Server +var domainToPort map[string]database.Struct_Domain var mutex = &sync.Mutex{} func RefreshMap() bool { @@ -36,33 +38,39 @@ func RefreshMap() bool { } mutex.Lock() - domainToPort = make(map[string]int) + domainToPort = make(map[string]database.Struct_Domain) for k, v := range client { - domainToPort[k] = v.PublicPort + domainToPort[k] = v } mutex.Unlock() return true } -func New() *Router { +func New(insecure, secure int) *Router { if !RefreshMap() { return nil } r := Router{ - Port: 2080, + InsecurePort: insecure, + SecurePort: secure, } - server = http.Server{ - Addr: "localhost:" + strconv.Itoa(r.Port), + insecureServer = http.Server{ + Addr: "localhost:" + strconv.Itoa(r.InsecurePort), Handler: &httputil.ReverseProxy{ - Director: proxyDirector, + Director: proxyDirectorInsecure, + Transport: customTrip{}, + }, + } + + secureServer = http.Server{ + Addr: "localhost:" + strconv.Itoa(r.SecurePort), + Handler: &httputil.ReverseProxy{ + Director: proxyDirectorSecure, Transport: customTrip{}, }, - ReadTimeout: 5 * time.Second, - WriteTimeout: 5 * time.Second, - MaxHeaderBytes: 0, } // Start scheduled map refresher @@ -85,5 +93,6 @@ func New() *Router { } func (r *Router) Start() { - go server.ListenAndServe() + go insecureServer.ListenAndServe() + go secureServer.ListenAndServe() }