|
@@ -10,6 +10,7 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "imuslab.com/zoraxy/mod/auth"
|
|
|
"imuslab.com/zoraxy/mod/dynamicproxy"
|
|
|
"imuslab.com/zoraxy/mod/uptime"
|
|
|
"imuslab.com/zoraxy/mod/utils"
|
|
@@ -77,29 +78,27 @@ func ReverseProxtInit() {
|
|
|
})
|
|
|
} else if record.ProxyType == "subd" {
|
|
|
dynamicProxyRouter.AddSubdomainRoutingService(&dynamicproxy.SubdOptions{
|
|
|
- MatchingDomain: record.Rootname,
|
|
|
- Domain: record.ProxyTarget,
|
|
|
- RequireTLS: record.UseTLS,
|
|
|
+ MatchingDomain: record.Rootname,
|
|
|
+ Domain: record.ProxyTarget,
|
|
|
+ RequireTLS: record.UseTLS,
|
|
|
+ SkipCertValidations: record.SkipTlsValidation,
|
|
|
+ RequireBasicAuth: record.RequireBasicAuth,
|
|
|
+ BasicAuthCredentials: record.BasicAuthCredentials,
|
|
|
})
|
|
|
} else if record.ProxyType == "vdir" {
|
|
|
dynamicProxyRouter.AddVirtualDirectoryProxyService(&dynamicproxy.VdirOptions{
|
|
|
- RootName: record.Rootname,
|
|
|
- Domain: record.ProxyTarget,
|
|
|
- RequireTLS: record.UseTLS,
|
|
|
+ RootName: record.Rootname,
|
|
|
+ Domain: record.ProxyTarget,
|
|
|
+ RequireTLS: record.UseTLS,
|
|
|
+ SkipCertValidations: record.SkipTlsValidation,
|
|
|
+ RequireBasicAuth: record.RequireBasicAuth,
|
|
|
+ BasicAuthCredentials: record.BasicAuthCredentials,
|
|
|
})
|
|
|
} 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
|
|
|
//Not sure why but delay must be added if you have another
|
|
|
//reverse proxy server in front of this service
|
|
@@ -122,7 +121,6 @@ func ReverseProxtInit() {
|
|
|
}
|
|
|
|
|
|
func ReverseProxyHandleOnOff(w http.ResponseWriter, r *http.Request) {
|
|
|
-
|
|
|
enable, _ := utils.PostPara(r, "enable") //Support root, vdir and subd
|
|
|
if enable == "true" {
|
|
|
err := dynamicProxyRouter.StartProxyService()
|
|
@@ -168,6 +166,49 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
useTLS := (tls == "true")
|
|
|
+
|
|
|
+ stv, _ := utils.PostPara(r, "tlsval")
|
|
|
+ if stv == "" {
|
|
|
+ stv = "false"
|
|
|
+ }
|
|
|
+
|
|
|
+ skipTlsValidation := (stv == "true")
|
|
|
+
|
|
|
+ rba, _ := utils.PostPara(r, "bauth")
|
|
|
+ if rba == "" {
|
|
|
+ rba = "false"
|
|
|
+ }
|
|
|
+
|
|
|
+ requireBasicAuth := (rba == "true")
|
|
|
+
|
|
|
+ //Prase the basic auth to correct structure
|
|
|
+ cred, _ := utils.PostPara(r, "cred")
|
|
|
+ basicAuthCredentials := []*dynamicproxy.BasicAuthCredentials{}
|
|
|
+ if requireBasicAuth {
|
|
|
+ preProcessCredentials := []*dynamicproxy.BasicAuthUnhashedCredentials{}
|
|
|
+ err = json.Unmarshal([]byte(cred), &preProcessCredentials)
|
|
|
+ if err != nil {
|
|
|
+ utils.SendErrorResponse(w, "invalid user credentials")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //Check if there are empty password credentials
|
|
|
+ for _, credObj := range preProcessCredentials {
|
|
|
+ if strings.TrimSpace(credObj.Password) == "" {
|
|
|
+ utils.SendErrorResponse(w, credObj.Username+" has empty password")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //Convert and hash the passwords
|
|
|
+ for _, credObj := range preProcessCredentials {
|
|
|
+ basicAuthCredentials = append(basicAuthCredentials, &dynamicproxy.BasicAuthCredentials{
|
|
|
+ Username: credObj.Username,
|
|
|
+ PasswordHash: auth.Hash(credObj.Password),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
rootname := ""
|
|
|
if eptype == "vdir" {
|
|
|
vdir, err := utils.PostPara(r, "rootname")
|
|
@@ -183,9 +224,12 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
rootname = vdir
|
|
|
|
|
|
thisOption := dynamicproxy.VdirOptions{
|
|
|
- RootName: vdir,
|
|
|
- Domain: endpoint,
|
|
|
- RequireTLS: useTLS,
|
|
|
+ RootName: vdir,
|
|
|
+ Domain: endpoint,
|
|
|
+ RequireTLS: useTLS,
|
|
|
+ SkipCertValidations: skipTlsValidation,
|
|
|
+ RequireBasicAuth: requireBasicAuth,
|
|
|
+ BasicAuthCredentials: basicAuthCredentials,
|
|
|
}
|
|
|
dynamicProxyRouter.AddVirtualDirectoryProxyService(&thisOption)
|
|
|
|
|
@@ -197,9 +241,12 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
rootname = subdomain
|
|
|
thisOption := dynamicproxy.SubdOptions{
|
|
|
- MatchingDomain: subdomain,
|
|
|
- Domain: endpoint,
|
|
|
- RequireTLS: useTLS,
|
|
|
+ MatchingDomain: subdomain,
|
|
|
+ Domain: endpoint,
|
|
|
+ RequireTLS: useTLS,
|
|
|
+ SkipCertValidations: skipTlsValidation,
|
|
|
+ RequireBasicAuth: requireBasicAuth,
|
|
|
+ BasicAuthCredentials: basicAuthCredentials,
|
|
|
}
|
|
|
dynamicProxyRouter.AddSubdomainRoutingService(&thisOption)
|
|
|
} else if eptype == "root" {
|
|
@@ -216,7 +263,16 @@ func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
//Save it
|
|
|
- SaveReverseProxyConfig(eptype, rootname, endpoint, useTLS)
|
|
|
+ thisProxyConfigRecord := Record{
|
|
|
+ ProxyType: eptype,
|
|
|
+ Rootname: rootname,
|
|
|
+ ProxyTarget: endpoint,
|
|
|
+ UseTLS: useTLS,
|
|
|
+ SkipTlsValidation: skipTlsValidation,
|
|
|
+ RequireBasicAuth: requireBasicAuth,
|
|
|
+ BasicAuthCredentials: basicAuthCredentials,
|
|
|
+ }
|
|
|
+ SaveReverseProxyConfig(&thisProxyConfigRecord)
|
|
|
|
|
|
//Update utm if exists
|
|
|
if uptimeMonitor != nil {
|