mirror of
https://github.com/donl/gPanel.git
synced 2026-05-25 22:06:55 -06:00
fixed issue where multiple bundles used the same instance of http.Server
This commit is contained in:
parent
a27d5571ca
commit
8d77d3fb56
10 changed files with 81 additions and 50 deletions
16
gpanel.go
16
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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") {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue