|
@@ -4,6 +4,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
+ "path/filepath"
|
|
|
|
|
|
"imuslab.com/arozos/ReverseProxy/mod/dynamicproxy"
|
|
|
)
|
|
@@ -26,13 +27,39 @@ func ReverseProxtInit() {
|
|
|
http.HandleFunc("/add", ReverseProxyHandleAddEndpoint)
|
|
|
http.HandleFunc("/status", ReverseProxyStatus)
|
|
|
http.HandleFunc("/list", ReverseProxyList)
|
|
|
+ http.HandleFunc("/del", DeleteProxyEndpoint)
|
|
|
|
|
|
- dynamicProxyRouter.SetRootProxy("192.168.0.107:8080", false)
|
|
|
- dynamicProxyRouter.AddSubdomainRoutingService("aroz.localhost", "192.168.0.107:8080/private/AOB/", false)
|
|
|
- dynamicProxyRouter.AddSubdomainRoutingService("loopback.localhost", "localhost:8080", false)
|
|
|
- dynamicProxyRouter.AddSubdomainRoutingService("git.localhost", "mc.alanyeung.co:3000", false)
|
|
|
- dynamicProxyRouter.AddVirtualDirectoryProxyService("/git/server/", "mc.alanyeung.co:3000", false)
|
|
|
+ //Load all conf from files
|
|
|
+ confs, _ := filepath.Glob("./conf/*.config")
|
|
|
+ for _, conf := range confs {
|
|
|
+ record, err := LoadReverseProxyConfig(conf)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Failed to load "+filepath.Base(conf), err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if record.ProxyType == "root" {
|
|
|
+ dynamicProxyRouter.SetRootProxy(record.ProxyTarget, record.UseTLS)
|
|
|
+ } else if record.ProxyType == "subd" {
|
|
|
+ dynamicProxyRouter.AddSubdomainRoutingService(record.Rootname, record.ProxyTarget, record.UseTLS)
|
|
|
+ } else if record.ProxyType == "vdir" {
|
|
|
+ dynamicProxyRouter.AddVirtualDirectoryProxyService(record.Rootname, record.ProxyTarget, record.UseTLS)
|
|
|
+ } else {
|
|
|
+ log.Println("Unsupported endpoint type: " + record.ProxyType + ". Skipping " + filepath.Base(conf))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ dynamicProxyRouter.SetRootProxy("192.168.0.107:8080", false)
|
|
|
+ dynamicProxyRouter.AddSubdomainRoutingService("aroz.localhost", "192.168.0.107:8080/private/AOB/", false)
|
|
|
+ dynamicProxyRouter.AddSubdomainRoutingService("loopback.localhost", "localhost:8080", false)
|
|
|
+ dynamicProxyRouter.AddSubdomainRoutingService("git.localhost", "mc.alanyeung.co:3000", false)
|
|
|
+ dynamicProxyRouter.AddVirtualDirectoryProxyService("/git/server/", "mc.alanyeung.co:3000", false)
|
|
|
+ */
|
|
|
+
|
|
|
+ //Start Service
|
|
|
dynamicProxyRouter.StartProxyService()
|
|
|
+
|
|
|
/*
|
|
|
go func() {
|
|
|
time.Sleep(10 * time.Second)
|
|
@@ -82,13 +109,14 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
useTLS := (tls == "true")
|
|
|
-
|
|
|
+ rootname := ""
|
|
|
if eptype == "vdir" {
|
|
|
vdir, err := mv(r, "rootname", true)
|
|
|
if err != nil {
|
|
|
sendErrorResponse(w, "vdir not defined")
|
|
|
return
|
|
|
}
|
|
|
+ rootname = vdir
|
|
|
dynamicProxyRouter.AddVirtualDirectoryProxyService(vdir, endpoint, useTLS)
|
|
|
|
|
|
} else if eptype == "subd" {
|
|
@@ -97,11 +125,20 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
sendErrorResponse(w, "subdomain not defined")
|
|
|
return
|
|
|
}
|
|
|
+ rootname = subdomain
|
|
|
dynamicProxyRouter.AddSubdomainRoutingService(subdomain, endpoint, useTLS)
|
|
|
} else if eptype == "root" {
|
|
|
+ rootname = "root"
|
|
|
dynamicProxyRouter.SetRootProxy(endpoint, useTLS)
|
|
|
+ } else {
|
|
|
+ //Invalid eptype
|
|
|
+ sendErrorResponse(w, "Invalid endpoint type")
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
+ //Save it
|
|
|
+ SaveReverseProxyConfig(eptype, rootname, endpoint, useTLS)
|
|
|
+
|
|
|
sendOK(w)
|
|
|
|
|
|
}
|
|
@@ -122,6 +159,7 @@ func DeleteProxyEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
sendErrorResponse(w, err.Error())
|
|
|
}
|
|
|
|
|
|
+ RemoveReverseProxyConfig(ep)
|
|
|
sendOK(w)
|
|
|
}
|
|
|
|
|
@@ -154,6 +192,9 @@ func ReverseProxyList(w http.ResponseWriter, r *http.Request) {
|
|
|
})
|
|
|
js, _ := json.Marshal(results)
|
|
|
sendJSONResponse(w, string(js))
|
|
|
+ } else if eptype == "root" {
|
|
|
+ js, _ := json.Marshal(dynamicProxyRouter.Root)
|
|
|
+ sendJSONResponse(w, string(js))
|
|
|
} else {
|
|
|
sendErrorResponse(w, "Invalid type given")
|
|
|
}
|