fixed domain router issues and followed HTTP docs in regards to host field

This commit is contained in:
George Shaw 2018-01-23 14:38:49 -06:00
parent 14a83962c0
commit d57cb9afa3
2 changed files with 22 additions and 6 deletions

View file

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

View file

@ -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")