From 05ef046e5567e6986f56db4b2692cf304eba0be6 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Tue, 23 Jan 2018 15:50:18 -0600 Subject: [PATCH] subdomains now work --- pkg/gpaccount/gpaccount.go | 2 +- pkg/public/public.go | 8 ++++--- pkg/public/servehttp.go | 43 +++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/pkg/gpaccount/gpaccount.go b/pkg/gpaccount/gpaccount.go index 6380ba5..e786291 100644 --- a/pkg/gpaccount/gpaccount.go +++ b/pkg/gpaccount/gpaccount.go @@ -42,7 +42,7 @@ func New(dir, name string, accPort, pubPort int) *Controller { DocumentRoot: "account/", Name: name, Port: accPort, - Public: public.New("/home/"+name+"/", pubPort), + Public: public.New("/home/"+name+"/", dir, pubPort), GracefulShutdownTimeout: 5 * time.Second, Status: 0, AccountLogger: accountLogger, diff --git a/pkg/public/public.go b/pkg/public/public.go index 3fffde7..19c01cf 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -12,6 +12,7 @@ import ( type Controller struct { Directory string + AccountDirectory string Port int GracefulShutdownTimeout time.Duration Status int @@ -23,15 +24,16 @@ var controller Controller var server http.Server // New function returns a new PublicWeb type. -func New(dir string, port int) *Controller { +func New(dir, accountDir string, port int) *Controller { ph, lh, err := getLogHandles(dir) if err != nil { log.Fatalf("Error trying to start logging instances within %v: %v", dir, err.Error()) } controller = Controller{ - Directory: dir, - Port: port, + Directory: dir, + AccountDirectory: accountDir, + Port: port, GracefulShutdownTimeout: 5 * time.Second, Status: 0, PublicLogger: ph, diff --git a/pkg/public/servehttp.go b/pkg/public/servehttp.go index 368caef..5d0067b 100644 --- a/pkg/public/servehttp.go +++ b/pkg/public/servehttp.go @@ -12,6 +12,7 @@ import ( "strings" "time" + "github.com/Ennovar/gPanel/pkg/database" "github.com/Ennovar/gPanel/pkg/routing" ) @@ -92,10 +93,46 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { } path := req.URL.Path[1:] - if len(path) == 0 { - path = con.Directory + "document_root/" + "index.html" + if strings.HasPrefix(req.Host, "www") { + if len(path) == 0 { + path = con.Directory + "document_root/" + "index.html" + } else { + path = con.Directory + "document_root/" + path + } } else { - path = con.Directory + "document_root/" + path + if strings.Count(req.Host, ".") == 2 { + subdomain := strings.SplitN(req.Host, ".", 2)[0] //Remove sub-domain + + ds, err := database.Open(con.AccountDirectory + database.DB_MAIN) + if err != nil || ds == nil { + con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error()) + routing.HttpThrowStatus(http.StatusInternalServerError, res) + return + } + + var sdRoot database.StructSubdomain + + err = ds.Get(database.BUCKET_SUBDOMAINS, []byte(subdomain), &sdRoot) + if err != nil { + con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error()) + routing.HttpThrowStatus(http.StatusInternalServerError, res) + return + } + + _ = ds.Close() + + if len(path) == 0 { + path = con.Directory + "document_root/" + sdRoot.Root + "/index.html" + } else { + path = con.Directory + "document_root/" + sdRoot.Root + "/" + path + } + } else { + if len(path) == 0 { + path = con.Directory + "document_root/" + "index.html" + } else { + path = con.Directory + "document_root/" + path + } + } } contentType, err := routing.GetContentType(path)