diff --git a/pkg/gpserver/servehttp.go b/pkg/gpserver/servehttp.go index a4a778a..a1a2530 100644 --- a/pkg/gpserver/servehttp.go +++ b/pkg/gpserver/servehttp.go @@ -6,8 +6,9 @@ import ( "os" "strconv" - "github.com/Ennovar/gPanel/pkg/routing" "strings" + + "github.com/Ennovar/gPanel/pkg/routing" ) func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { @@ -18,6 +19,11 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { path = (con.Directory + con.DocumentRoot + path) } + if strings.HasSuffix(path, "throw400") { + http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + if strings.HasSuffix(path, "index.html") { if con.checkAuth(res, req) == true { http.Redirect(res, req, "gPanel.html", http.StatusFound) diff --git a/pkg/router/router.go b/pkg/router/router.go index 86a75c4..ee66754 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -6,9 +6,11 @@ import ( "strconv" "time" - "github.com/Ennovar/gPanel/pkg/database" "log" + "strings" "sync" + + "github.com/Ennovar/gPanel/pkg/database" ) type Router struct { @@ -57,14 +59,22 @@ func New() *Router { Addr: "localhost:" + strconv.Itoa(r.Port), Handler: &httputil.ReverseProxy{ Director: func(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 = "http" + mutex.Lock() - if d, ok := domainToPort[req.Host]; ok { + if d, ok := domainToPort[host]; ok { mutex.Unlock() - req.Header.Set("Host", req.Host) - req.URL.Scheme = "http" req.URL.Host = "127.0.0.1:" + strconv.Itoa(d) } else { mutex.Unlock() + req.URL.Host = "127.0.0.1:2082" + req.URL.Path = "/throw400" } }, }, @@ -78,7 +88,7 @@ func New() *Router { go func() { for { select { - case <- ticker.C: + case <-ticker.C: if !RefreshMap() { ticker.Stop() log.Fatal("Error refreshing domain/bundle pairing for router")