diff --git a/.gitignore b/.gitignore index 66fd13c..6392056 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,30 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib +# ---> Go +# Compiled Object files, Static and Dynamic libs (Shared Objects) +src/*.o +src/*.a +src/*.so +src/*.exe -# Test binary, built with `go test -c` -*.test +# Folders +src/_obj +src/_test -# Output of the go coverage tool, specifically when used with LiteIDE -*.out +src/*.cgo1.go +src/*.cgo2.c +src/_cgo_defun.c +src/_cgo_gotypes.go +src/_cgo_export.* -# Dependency directories (remove the comment below to include it) -# vendor/ +src/_testmain.go + +src/*.exe +src/*.test +src/*.prof + +src/sys.db +src/src/sys.db.lock +src/conf/* +src/ReverseProxy_*_* +src/Zoraxy_*_* +src/certs/* +src/rules/* \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 385e21b..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022 Toby Chui - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index 99a0686..b919a46 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,129 @@ -# NoobyRP -A very simple to use reverse proxy server written in Go + +# Zoraxy + +General purpose request (reverse) proxy and forwarding tool for low power devices. Now written in Go! + +### Features + +- Simple to use interface with detail in-system instructions + +- Reverse Proxy + + - Subdomain Reverse Proxy + + - Virtual Directory Reverse Proxy + +- Redirection Rules + +- TLS / SSL setup and deploy + +- Blacklist by country or IP address (single IP, CIDR or wildcard for beginners :D) + +- (More features work in progress) + +## Usage + +Zoraxy provide basic authentication system for standalone mode. To use it in standalone mode, follow the instruction below for your desired deployment platform. + +### Standalone Mode + +Standalone mode is the default mode for Zoraxy. This allow single account to manage your reverse proxy server just like a home router. This mode is suitable for new owners for homelab or makers start growing their web services into multiple servers. + +#### Linux + +```bash +//Download the latest zoraxy binary and web.tar.gz from the Release page +sudo chmod 775 ./zoraxy web.tar.gz +sudo ./zoraxy -port=:8000 +``` + +#### Windows + +Download the binary executable and web.tar.gz, put them into the same folder and double click the binary file to start it. + +#### Raspberry Pi + +The installation method is same as Linux. If you are using Raspberry Pi 4 or newer models, pick the arm64 release. For older version of Pis, use the arm (armv6) version instead. + +#### Other ARM SBCs or Android phone with Termux + +The installation method is same as Linux. For other ARM SBCs, please refer to your SBC's CPU architecture and pick the one that is suitable for your device. + +### External Permission Managment Mode + +If you already have a up-stream reverse proxy server in place with permission management, you can use Zoraxy in noauth mode. To enable no-auth mode, start Zoraxy with the following flag + +```bash +./zoraxy -noauth=true +``` + +*Note: For security reaons, you should only enable no-auth if you are running Zoraxy in a trusted environment or with another authentication management proxy in front.* + +#### Use with ArozOS + +[ArozOS ](https://arozos.com)subservice is a build in permission managed reverse proxy server. To use zoraxy with arozos, connect to your arozos host via ssh and use the following command to install zoraxy + +```bash +# cd into your arozos subservice folder. Sometime it is under ~/arozos/src/subservice +cd ~/arozos/subservices +mkdir zoraxy +cd ./zoraxy + +# Download the release binary from Github release +wget {binary executable link from release page} +wget {web.tar.gz link from release page} + +# Set permission. Change this if required +sudo chmod 775 -R ./ + +# Start zoraxy to see if the downloaded arch is correct. If yes, you should +# see it start unzipping +./zoraxy + +# After the unzip done, press Ctrl + C to kill it +# Rename it to valid arozos subservice binary format +mv ./zoraxy zoraxy_linux_amd64 + +# If you are using SBCs with different CPU arch +mv ./zoraxy zoraxy_linux_arm +mv ./zoraxy zoraxy_linux_arm64 + +# Restart arozos +sudo systemctl restart arozos +``` + +To start the module, go to System Settings > Modules > Subservice and enable it in the menu. You should be able to see a new module named "Zoraxy" pop up in the start menu. + +## Build from Source + +*Requirement: Go 1.17 or above* + +```bash +git clone https://github.com/tobychui/zoraxy +cd ./zoraxy/src +go mod tidy +go build + +./zoraxy +``` + +### Forward Modes + +#### Proxy Modes + +There are two mode in the ReverseProxy Subservice + +1. vdir mode (Virtual Dirctories) +2. subd mode (Subdomain Proxying Mode) + +Vdir mode proxy web request based on the virtual directories given in the request URL. For example, when configured to redirect /example to example.com, any visits to {your_domain}/example will be proxied to example.com. + +Subd mode proxy web request based on sub-domain exists in the request URL. For example, when configured to redirect example.localhost to example.com, any visits that includes example.localhost (e.g. example.localhost/page1) will be proxied to example.com (e.g. example.com/page1) + +#### Root Proxy + +Root proxy is the main proxy destination where if all proxy root name did not match, the request will be proxied to this request. If you are working with ArozOS system in default configuration, you can set this to localhost:8080 for any unknown request to be handled by the host ArozOS system + +## License + +To be decided (Currently: All Right Reserved) diff --git a/common.go b/common.go deleted file mode 100644 index 270e181..0000000 --- a/common.go +++ /dev/null @@ -1,185 +0,0 @@ -package main - -import ( - "bufio" - "encoding/base64" - "errors" - "io/ioutil" - "log" - "net/http" - "os" - "strconv" - "strings" - "time" -) - -/* - Basic Response Functions - - Send response with ease -*/ -//Send text response with given w and message as string -func sendTextResponse(w http.ResponseWriter, msg string) { - w.Write([]byte(msg)) -} - -//Send JSON response, with an extra json header -func sendJSONResponse(w http.ResponseWriter, json string) { - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(json)) -} - -func sendErrorResponse(w http.ResponseWriter, errMsg string) { - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{\"error\":\"" + errMsg + "\"}")) -} - -func sendOK(w http.ResponseWriter) { - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("\"OK\"")) -} - -/* - The paramter move function (mv) - - You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in - r (HTTP Request Object) - getParamter (string, aka $_GET['This string]) - - Will return - Paramter string (if any) - Error (if error) - -*/ -func mv(r *http.Request, getParamter string, postMode bool) (string, error) { - if postMode == false { - //Access the paramter via GET - keys, ok := r.URL.Query()[getParamter] - - if !ok || len(keys[0]) < 1 { - //log.Println("Url Param " + getParamter +" is missing") - return "", errors.New("GET paramter " + getParamter + " not found or it is empty") - } - - // Query()["key"] will return an array of items, - // we only want the single item. - key := keys[0] - return string(key), nil - } else { - //Access the parameter via POST - r.ParseForm() - x := r.Form.Get(getParamter) - if len(x) == 0 || x == "" { - return "", errors.New("POST paramter " + getParamter + " not found or it is empty") - } - return string(x), nil - } - -} - -func stringInSlice(a string, list []string) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - -func fileExists(filename string) bool { - _, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return true -} - -func IsDir(path string) bool { - if fileExists(path) == false { - return false - } - fi, err := os.Stat(path) - if err != nil { - log.Fatal(err) - return false - } - switch mode := fi.Mode(); { - case mode.IsDir(): - return true - case mode.IsRegular(): - return false - } - return false -} - -func inArray(arr []string, str string) bool { - for _, a := range arr { - if a == str { - return true - } - } - return false -} - -func timeToString(targetTime time.Time) string { - return targetTime.Format("2006-01-02 15:04:05") -} - -func IntToString(number int) string { - return strconv.Itoa(number) -} - -func StringToInt(number string) (int, error) { - return strconv.Atoi(number) -} - -func StringToInt64(number string) (int64, error) { - i, err := strconv.ParseInt(number, 10, 64) - if err != nil { - return -1, err - } - return i, nil -} - -func Int64ToString(number int64) string { - convedNumber := strconv.FormatInt(number, 10) - return convedNumber -} - -func GetUnixTime() int64 { - return time.Now().Unix() -} - -func LoadImageAsBase64(filepath string) (string, error) { - if !fileExists(filepath) { - return "", errors.New("File not exists") - } - f, _ := os.Open(filepath) - reader := bufio.NewReader(f) - content, _ := ioutil.ReadAll(reader) - encoded := base64.StdEncoding.EncodeToString(content) - return string(encoded), nil -} - -//Get the IP address of the current authentication user -func getUserIPAddr(w http.ResponseWriter, r *http.Request) { - requestPort, _ := mv(r, "port", false) - showPort := false - if requestPort == "true" { - //Show port as well - showPort = true - } - IPAddress := r.Header.Get("X-Real-Ip") - if IPAddress == "" { - IPAddress = r.Header.Get("X-Forwarded-For") - } - if IPAddress == "" { - IPAddress = r.RemoteAddr - } - if !showPort { - IPAddress = IPAddress[:strings.LastIndex(IPAddress, ":")] - - } - w.Write([]byte(IPAddress)) - return -} diff --git a/go.mod b/go.mod deleted file mode 100644 index 8257426..0000000 --- a/go.mod +++ /dev/null @@ -1,9 +0,0 @@ -module imuslab.com/arozos/ReverseProxy - -go 1.16 - -require ( - github.com/boltdb/bolt v1.3.1 - github.com/gorilla/websocket v1.4.2 - golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect -) diff --git a/go.sum b/go.sum deleted file mode 100644 index 11d5fd5..0000000 --- a/go.sum +++ /dev/null @@ -1,6 +0,0 @@ -github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= -github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg= -golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/img/title.png b/img/title.png new file mode 100644 index 0000000..a9170c5 Binary files /dev/null and b/img/title.png differ diff --git a/img/title.psd b/img/title.psd new file mode 100644 index 0000000..fb855c4 Binary files /dev/null and b/img/title.psd differ diff --git a/main.go b/main.go deleted file mode 100644 index 923232f..0000000 --- a/main.go +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import ( - "log" - "net/http" - "os" - "os/signal" - "syscall" - - "imuslab.com/arozos/ReverseProxy/mod/aroz" - "imuslab.com/arozos/ReverseProxy/mod/database" -) - -var ( - handler *aroz.ArozHandler - sysdb *database.Database -) - -//Kill signal handler. Do something before the system the core terminate. -func SetupCloseHandler() { - c := make(chan os.Signal, 2) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - go func() { - <-c - log.Println("\r- Shutting down demo module.") - //Do other things like close database or opened files - sysdb.Close() - - os.Exit(0) - }() -} - -func main() { - //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information - handler = aroz.HandleFlagParse(aroz.ServiceInfo{ - Name: "ReverseProxy", - Desc: "Basic reverse proxy listener", - Group: "Network", - IconPath: "reverseproxy/img/small_icon.png", - Version: "0.1", - StartDir: "reverseproxy/index.html", - SupportFW: true, - LaunchFWDir: "reverseproxy/index.html", - SupportEmb: false, - InitFWSize: []int{1080, 580}, - }) - - //Register the standard web services urls - fs := http.FileServer(http.Dir("./web")) - - http.Handle("/", fs) - - SetupCloseHandler() - - //Create database - db, err := database.NewDatabase("sys.db", false) - if err != nil { - log.Fatal(err) - } - - sysdb = db - - //Start the reverse proxy server in go routine - go func() { - ReverseProxtInit() - }() - - //Any log println will be shown in the core system via STDOUT redirection. But not STDIN. - log.Println("ReverseProxy started. Listening on " + handler.Port) - err = http.ListenAndServe(handler.Port, nil) - if err != nil { - log.Fatal(err) - } - -} diff --git a/mod/database/doc.txt b/mod/database/doc.txt deleted file mode 100644 index a1cf3fc..0000000 --- a/mod/database/doc.txt +++ /dev/null @@ -1,44 +0,0 @@ - -package database // import "imuslab.com/arozos/mod/database" - - -TYPES - -type Database struct { - Db *bolt.DB - ReadOnly bool -} - -func NewDatabase(dbfile string, readOnlyMode bool) (*Database, error) - -func (d *Database) Close() - -func (d *Database) Delete(tableName string, key string) error - Delete a value from the database table given tablename and key - - err := sysdb.Delete("MyTable", "username/message"); - -func (d *Database) DropTable(tableName string) error - -func (d *Database) KeyExists(tableName string, key string) bool - -func (d *Database) ListTable(tableName string) ([][][]byte, error) - -func (d *Database) NewTable(tableName string) error - -func (d *Database) Read(tableName string, key string, assignee interface{}) error - -func (d *Database) UpdateReadWriteMode(readOnly bool) - -func (d *Database) Write(tableName string, key string, value interface{}) error - Write to database with given tablename and key. Example Usage: type demo - struct{ - - content string - - } thisDemo := demo{ - - content: "Hello World", - - } err := sysdb.Write("MyTable", "username/message",thisDemo); - diff --git a/mod/dynamicproxy/dynamicproxy.go b/mod/dynamicproxy/dynamicproxy.go deleted file mode 100644 index 555283c..0000000 --- a/mod/dynamicproxy/dynamicproxy.go +++ /dev/null @@ -1,216 +0,0 @@ -package dynamicproxy - -import ( - "context" - "errors" - "log" - "net/http" - "net/url" - "strconv" - "strings" - "sync" - "time" - - "imuslab.com/arozos/ReverseProxy/mod/dynamicproxy/dpcore" - "imuslab.com/arozos/ReverseProxy/mod/reverseproxy" -) - -/* - Allow users to setup manual proxying for specific path - -*/ -type Router struct { - ListenPort int - ProxyEndpoints *sync.Map - SubdomainEndpoint *sync.Map - Running bool - Root *ProxyEndpoint - mux http.Handler - useTLS bool - server *http.Server -} - -type RouterOption struct { - Port int -} - -type ProxyEndpoint struct { - Root string - Domain string - RequireTLS bool - Proxy *dpcore.ReverseProxy `json:"-"` -} - -type SubdomainEndpoint struct { - MatchingDomain string - Domain string - RequireTLS bool - Proxy *reverseproxy.ReverseProxy `json:"-"` -} - -type ProxyHandler struct { - Parent *Router -} - -func NewDynamicProxy(port int) (*Router, error) { - proxyMap := sync.Map{} - domainMap := sync.Map{} - thisRouter := Router{ - ListenPort: port, - ProxyEndpoints: &proxyMap, - SubdomainEndpoint: &domainMap, - Running: false, - useTLS: false, - server: nil, - } - - thisRouter.mux = &ProxyHandler{ - Parent: &thisRouter, - } - - return &thisRouter, nil -} - -//Start the dynamic routing -func (router *Router) StartProxyService() error { - //Create a new server object - if router.server != nil { - return errors.New("Reverse proxy server already running") - } - - if router.Root == nil { - return errors.New("Reverse proxy router root not set") - } - - router.server = &http.Server{Addr: ":" + strconv.Itoa(router.ListenPort), Handler: router.mux} - router.Running = true - go func() { - err := router.server.ListenAndServe() - log.Println("[DynamicProxy] " + err.Error()) - }() - - return nil -} - -func (router *Router) StopProxyService() error { - if router.server == nil { - return errors.New("Reverse proxy server already stopped") - } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - err := router.server.Shutdown(ctx) - if err != nil { - return err - } - - //Discard the server object - router.server = nil - router.Running = false - return nil -} - -/* - Add an URL into a custom proxy services -*/ -func (router *Router) AddVirtualDirectoryProxyService(rootname string, domain string, requireTLS bool) error { - if domain[len(domain)-1:] == "/" { - domain = domain[:len(domain)-1] - } - - if rootname[len(rootname)-1:] == "/" { - rootname = rootname[:len(rootname)-1] - } - - webProxyEndpoint := domain - if requireTLS { - webProxyEndpoint = "https://" + webProxyEndpoint - } else { - webProxyEndpoint = "http://" + webProxyEndpoint - } - //Create a new proxy agent for this root - path, err := url.Parse(webProxyEndpoint) - if err != nil { - return err - } - - proxy := dpcore.NewDynamicProxyCore(path, rootname) - - endpointObject := ProxyEndpoint{ - Root: rootname, - Domain: domain, - RequireTLS: requireTLS, - Proxy: proxy, - } - - router.ProxyEndpoints.Store(rootname, &endpointObject) - - log.Println("Adding Proxy Rule: ", rootname+" to "+domain) - return nil -} - -/* - Remove routing from RP - -*/ -func (router *Router) RemoveProxy(ptype string, key string) error { - if ptype == "vdir" { - router.ProxyEndpoints.Delete(key) - return nil - } else if ptype == "subd" { - router.SubdomainEndpoint.Delete(key) - return nil - } - return errors.New("invalid ptype") -} - -/* - Add an default router for the proxy server -*/ -func (router *Router) SetRootProxy(proxyLocation string, requireTLS bool) error { - if proxyLocation[len(proxyLocation)-1:] == "/" { - proxyLocation = proxyLocation[:len(proxyLocation)-1] - } - - webProxyEndpoint := proxyLocation - if requireTLS { - webProxyEndpoint = "https://" + webProxyEndpoint - } else { - webProxyEndpoint = "http://" + webProxyEndpoint - } - //Create a new proxy agent for this root - path, err := url.Parse(webProxyEndpoint) - if err != nil { - return err - } - - proxy := dpcore.NewDynamicProxyCore(path, "") - - rootEndpoint := ProxyEndpoint{ - Root: "/", - Domain: proxyLocation, - RequireTLS: requireTLS, - Proxy: proxy, - } - - router.Root = &rootEndpoint - return nil -} - -//Do all the main routing in here -func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if strings.Contains(r.Host, ".") { - //This might be a subdomain. See if there are any subdomain proxy router for this - sep := h.Parent.getSubdomainProxyEndpointFromHostname(r.Host) - if sep != nil { - h.subdomainRequest(w, r, sep) - return - } - } - - targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(r.RequestURI) - if targetProxyEndpoint != nil { - h.proxyRequest(w, r, targetProxyEndpoint) - } else { - h.proxyRequest(w, r, h.Parent.Root) - } -} diff --git a/reverseproxy.go b/reverseproxy.go deleted file mode 100644 index c94405c..0000000 --- a/reverseproxy.go +++ /dev/null @@ -1,201 +0,0 @@ -package main - -import ( - "encoding/json" - "log" - "net/http" - "path/filepath" - - "imuslab.com/arozos/ReverseProxy/mod/dynamicproxy" -) - -var ( - dynamicProxyRouter *dynamicproxy.Router -) - -//Add user customizable reverse proxy -func ReverseProxtInit() { - dprouter, err := dynamicproxy.NewDynamicProxy(80) - if err != nil { - log.Println(err.Error()) - return - } - - dynamicProxyRouter = dprouter - - http.HandleFunc("/enable", ReverseProxyHandleOnOff) - http.HandleFunc("/add", ReverseProxyHandleAddEndpoint) - http.HandleFunc("/status", ReverseProxyStatus) - http.HandleFunc("/list", ReverseProxyList) - http.HandleFunc("/del", DeleteProxyEndpoint) - - //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) - dynamicProxyRouter.StopProxyService() - fmt.Println("Proxy stopped") - }() - */ - log.Println("Dynamic Proxy service started") - -} - -func ReverseProxyHandleOnOff(w http.ResponseWriter, r *http.Request) { - enable, _ := mv(r, "enable", true) //Support root, vdir and subd - if enable == "true" { - err := dynamicProxyRouter.StartProxyService() - if err != nil { - sendErrorResponse(w, err.Error()) - return - } - } else { - err := dynamicProxyRouter.StopProxyService() - if err != nil { - sendErrorResponse(w, err.Error()) - return - } - } - - sendOK(w) -} - -func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) { - eptype, err := mv(r, "type", true) //Support root, vdir and subd - if err != nil { - sendErrorResponse(w, "type not defined") - return - } - - endpoint, err := mv(r, "ep", true) - if err != nil { - sendErrorResponse(w, "endpoint not defined") - return - } - - tls, _ := mv(r, "tls", true) - if tls == "" { - tls = "false" - } - - 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" { - subdomain, err := mv(r, "rootname", true) - if err != nil { - 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) - -} - -func DeleteProxyEndpoint(w http.ResponseWriter, r *http.Request) { - ep, err := mv(r, "ep", true) - if err != nil { - sendErrorResponse(w, "Invalid ep given") - } - - ptype, err := mv(r, "ptype", true) - if err != nil { - sendErrorResponse(w, "Invalid ptype given") - } - - err = dynamicProxyRouter.RemoveProxy(ptype, ep) - if err != nil { - sendErrorResponse(w, err.Error()) - } - - RemoveReverseProxyConfig(ep) - sendOK(w) -} - -func ReverseProxyStatus(w http.ResponseWriter, r *http.Request) { - js, _ := json.Marshal(dynamicProxyRouter) - sendJSONResponse(w, string(js)) -} - -func ReverseProxyList(w http.ResponseWriter, r *http.Request) { - eptype, err := mv(r, "type", true) //Support root, vdir and subd - if err != nil { - sendErrorResponse(w, "type not defined") - return - } - - if eptype == "vdir" { - results := []*dynamicproxy.ProxyEndpoint{} - dynamicProxyRouter.ProxyEndpoints.Range(func(key, value interface{}) bool { - results = append(results, value.(*dynamicproxy.ProxyEndpoint)) - return true - }) - - js, _ := json.Marshal(results) - sendJSONResponse(w, string(js)) - } else if eptype == "subd" { - results := []*dynamicproxy.SubdomainEndpoint{} - dynamicProxyRouter.SubdomainEndpoint.Range(func(key, value interface{}) bool { - results = append(results, value.(*dynamicproxy.SubdomainEndpoint)) - return true - }) - 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") - } -} diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..2abb9ce --- /dev/null +++ b/src/Makefile @@ -0,0 +1,54 @@ +# PLATFORMS := darwin/amd64 darwin/arm64 freebsd/amd64 linux/386 linux/amd64 linux/arm linux/arm64 linux/mipsle windows/386 windows/amd64 windows/arm windows/arm64 +PLATFORMS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm linux/arm64 linux/mipsle linux/riscv64 windows/amd64 windows/arm64 +temp = $(subst /, ,$@) +os = $(word 1, $(temp)) +arch = $(word 2, $(temp)) + +all: web.tar.gz $(PLATFORMS) fixwindows zoraxy_file_checksum.sha1 + +binary: $(PLATFORMS) + +hash: zoraxy_file_checksum.sha1 + +web: web.tar.gz + +clean: + rm -f zoraxy_*_* + rm -f web.tar.gz + +$(PLATFORMS): + @echo "Building $(os)/$(arch)" + GOROOT_FINAL=Git/ GOOS=$(os) GOARCH=$(arch) GOARM=6 go build -o './dist/zoraxy_$(os)_$(arch)' -ldflags "-s -w" -trimpath + + +fixwindows: + -mv ./dist/zoraxy_windows_amd64 ./dist/zoraxy_windows_amd64.exe + -mv ./dist/zoraxy_windows_arm64 ./dist/zoraxy_windows_arm64.exe + +web.tar.gz: + + @echo "Removing old build resources, if exists" + -rm -rf ./dist/ + -mkdir ./dist/ + + @echo "Moving subfolders to build folder" + -cp -r ./web ./dist/web/ + -cp -r ./system ./dist/system/ + + @ echo "Remove sensitive information" + -rm -rf ./dist/certs/ + -rm -rf ./dist/conf/ + -rm -rf ./dist/rules/ + + + @echo "Creating tarball for all required files" + -rm ./dist/web.tar.gz + -cd ./dist/ && tar -czf ./web.tar.gz system/ web/ + + @echo "Clearing up unzipped folder structures" + -rm -rf "./dist/web" + -rm -rf "./dist/system" + +zoraxy_file_checksum.sha1: + @echo "Generating the checksum, if sha1sum installed" + -sha1sum ./dist/web.tar.gz > ./dist/zoraxy_file_checksum.sha1 \ No newline at end of file diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..a456db2 --- /dev/null +++ b/src/README.md @@ -0,0 +1,136 @@ +# Zoraxy + +General purpose request (reverse) proxy and forwarding tool for low power devices. Now written in Go! + +### Features + +- Simple to use interface with detail in-system instructions + +- Reverse Proxy + + - Subdomain Reverse Proxy + + - Virtual Directory Reverse Proxy + +- Redirection Rules + +- TLS / SSL setup and deploy + +- Blacklist by country or IP address (single IP, CIDR or wildcard for beginners :D) + +- (More features work in progress) + +## Usage + +Zoraxy provide basic authentication system for standalone mode. To use it in standalone mode, follow the instruction below for your desired deployment platform. + +### Standalone Mode + +Standalone mode is the default mode for Zoraxy. This allow single account to manage your reverse proxy server just like a home router. This mode is suitable for new owners for homelab or makers start growing their web services into multiple servers. + +#### Linux + +```bash +//Download the latest zoraxy binary and web.tar.gz from the Release page +sudo chmod 775 ./zoraxy web.tar.gz +sudo ./zoraxy -port=:8000 +``` + +#### Windows + +Download the binary executable and web.tar.gz, put them into the same folder and double click the binary file to start it. + +#### Raspberry Pi + +The installation method is same as Linux. If you are using Raspberry Pi 4 or newer models, pick the arm64 release. For older version of Pis, use the arm (armv6) version instead. + +#### Other ARM SBCs or Android phone with Termux + +The installation method is same as Linux. For other ARM SBCs, please refer to your SBC's CPU architecture and pick the one that is suitable for your device. + +### External Permission Managment Mode + +If you already have a up-stream reverse proxy server in place with permission management, you can use Zoraxy in noauth mode. To enable no-auth mode, start Zoraxy with the following flag + +```bash +./zoraxy -noauth=true +``` + +*Note: For security reaons, you should only enable no-auth if you are running Zoraxy in a trusted environment or with another authentication management proxy in front.* + +#### Use with ArozOS + +[ArozOS ](https://arozos.com)subservice is a build in permission managed reverse proxy server. To use zoraxy with arozos, connect to your arozos host via ssh and use the following command to install zoraxy + +```bash +# cd into your arozos subservice folder. Sometime it is under ~/arozos/src/subservice +cd ~/arozos/subservices +mkdir zoraxy +cd ./zoraxy + +# Download the release binary from Github release +wget {binary executable link from release page} +wget {web.tar.gz link from release page} + +# Set permission. Change this if required +sudo chmod 775 -R ./ + +# Start zoraxy to see if the downloaded arch is correct. If yes, you should +# see it start unzipping +./zoraxy + +# After the unzip done, press Ctrl + C to kill it +# Rename it to valid arozos subservice binary format +mv ./zoraxy zoraxy_linux_amd64 + +# If you are using SBCs with different CPU arch +mv ./zoraxy zoraxy_linux_arm +mv ./zoraxy zoraxy_linux_arm64 + +# Restart arozos +sudo systemctl restart arozos + + +``` + +To start the module, go to System Settings > Modules > Subservice and enable it in the menu. You should be able to see a new module named "Zoraxy" pop up in the start menu. + + + +## Build from Source + +*Requirement: Go 1.17 or above* + +```bash +git clone https://github.com/tobychui/zoraxy +cd ./zoraxy/src +go mod tidy +go build + +./zoraxy +``` + + + +### Forward Modes + +#### Proxy Modes + +There are two mode in the ReverseProxy Subservice + +1. vdir mode (Virtual Dirctories) +2. subd mode (Subdomain Proxying Mode) + +Vdir mode proxy web request based on the virtual directories given in the request URL. For example, when configured to redirect /example to example.com, any visits to {your_domain}/example will be proxied to example.com. + +Subd mode proxy web request based on sub-domain exists in the request URL. For example, when configured to redirect example.localhost to example.com, any visits that includes example.localhost (e.g. example.localhost/page1) will be proxied to example.com (e.g. example.com/page1) + +#### Root Proxy + +Root proxy is the main proxy destination where if all proxy root name did not match, the request will be proxied to this request. If you are working with ArozOS system in default configuration, you can set this to localhost:8080 for any unknown request to be handled by the host ArozOS system + + + +## License + +To be decided (Currently: All Right Reserved) diff --git a/src/api.go b/src/api.go new file mode 100644 index 0000000..9713e62 --- /dev/null +++ b/src/api.go @@ -0,0 +1,153 @@ +package main + +import ( + "encoding/json" + "net/http" + + "imuslab.com/zoraxy/mod/auth" + "imuslab.com/zoraxy/mod/utils" +) + +/* + API.go + + This file contains all the API called by the web management interface + +*/ + +func initAPIs() { + requireAuth := !(*noauth || handler.IsUsingExternalPermissionManager()) + authRouter := auth.NewManagedHTTPRouter(auth.RouterOption{ + AuthAgent: authAgent, + RequireAuth: requireAuth, + DeniedHandler: func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "401 - Unauthorized", http.StatusUnauthorized) + }, + }) + + //Register the standard web services urls + fs := http.FileServer(http.Dir("./web")) + if requireAuth { + //Add a layer of middleware for auth control + authHandler := AuthFsHandler(fs) + http.Handle("/", authHandler) + } else { + http.Handle("/", fs) + } + + //Authentication APIs + registerAuthAPIs(requireAuth) + + //Reverse proxy + authRouter.HandleFunc("/api/proxy/enable", ReverseProxyHandleOnOff) + authRouter.HandleFunc("/api/proxy/add", ReverseProxyHandleAddEndpoint) + authRouter.HandleFunc("/api/proxy/status", ReverseProxyStatus) + authRouter.HandleFunc("/api/proxy/list", ReverseProxyList) + authRouter.HandleFunc("/api/proxy/del", DeleteProxyEndpoint) + authRouter.HandleFunc("/api/proxy/setIncoming", HandleIncomingPortSet) + authRouter.HandleFunc("/api/proxy/useHttpsRedirect", HandleUpdateHttpsRedirect) + authRouter.HandleFunc("/api/proxy/requestIsProxied", HandleManagementProxyCheck) + + //TLS / SSL config + authRouter.HandleFunc("/api/cert/tls", handleToggleTLSProxy) + authRouter.HandleFunc("/api/cert/upload", handleCertUpload) + authRouter.HandleFunc("/api/cert/list", handleListCertificate) + authRouter.HandleFunc("/api/cert/checkDefault", handleDefaultCertCheck) + authRouter.HandleFunc("/api/cert/delete", handleCertRemove) + + //Redirection config + authRouter.HandleFunc("/api/redirect/list", handleListRedirectionRules) + authRouter.HandleFunc("/api/redirect/add", handleAddRedirectionRule) + authRouter.HandleFunc("/api/redirect/delete", handleDeleteRedirectionRule) + + //Blacklist APIs + authRouter.HandleFunc("/api/blacklist/list", handleListBlacklisted) + authRouter.HandleFunc("/api/blacklist/country/add", handleCountryBlacklistAdd) + authRouter.HandleFunc("/api/blacklist/country/remove", handleCountryBlacklistRemove) + authRouter.HandleFunc("/api/blacklist/ip/add", handleIpBlacklistAdd) + authRouter.HandleFunc("/api/blacklist/ip/remove", handleIpBlacklistRemove) + authRouter.HandleFunc("/api/blacklist/enable", handleBlacklistEnable) + + //Statistic API + authRouter.HandleFunc("/api/stats/summary", statisticCollector.HandleTodayStatLoad) + + //Upnp + authRouter.HandleFunc("/api/upnp/discover", handleUpnpDiscover) + //If you got APIs to add, append them here +} + +//Function to renders Auth related APIs +func registerAuthAPIs(requireAuth bool) { + //Auth APIs + http.HandleFunc("/api/auth/login", authAgent.HandleLogin) + http.HandleFunc("/api/auth/logout", authAgent.HandleLogout) + http.HandleFunc("/api/auth/checkLogin", func(w http.ResponseWriter, r *http.Request) { + if requireAuth { + authAgent.CheckLogin(w, r) + } else { + utils.SendJSONResponse(w, "true") + } + }) + http.HandleFunc("/api/auth/username", func(w http.ResponseWriter, r *http.Request) { + username, err := authAgent.GetUserName(w, r) + if err != nil { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + + js, _ := json.Marshal(username) + utils.SendJSONResponse(w, string(js)) + }) + http.HandleFunc("/api/auth/userCount", func(w http.ResponseWriter, r *http.Request) { + uc := authAgent.GetUserCounts() + js, _ := json.Marshal(uc) + utils.SendJSONResponse(w, string(js)) + }) + http.HandleFunc("/api/auth/register", func(w http.ResponseWriter, r *http.Request) { + if authAgent.GetUserCounts() == 0 { + //Allow register root admin + authAgent.HandleRegisterWithoutEmail(w, r, func(username, reserved string) { + + }) + } else { + //This function is disabled + utils.SendErrorResponse(w, "Root management account already exists") + } + }) + http.HandleFunc("/api/auth/changePassword", func(w http.ResponseWriter, r *http.Request) { + username, err := authAgent.GetUserName(w, r) + if err != nil { + http.Error(w, "401 - Unauthorized", http.StatusUnauthorized) + return + } + + oldPassword, err := utils.PostPara(r, "oldPassword") + if err != nil { + utils.SendErrorResponse(w, "empty current password") + return + } + newPassword, err := utils.PostPara(r, "newPassword") + if err != nil { + utils.SendErrorResponse(w, "empty new password") + return + } + confirmPassword, _ := utils.PostPara(r, "confirmPassword") + + if newPassword != confirmPassword { + utils.SendErrorResponse(w, "confirm password not match") + return + } + + //Check if the old password correct + oldPasswordCorrect, _ := authAgent.ValidateUsernameAndPasswordWithReason(username, oldPassword) + if !oldPasswordCorrect { + utils.SendErrorResponse(w, "Invalid current password given") + return + } + + //Change the password of the root user + authAgent.UnregisterUser(username) + authAgent.CreateUserAccount(username, newPassword, "") + }) + +} diff --git a/src/blacklist.go b/src/blacklist.go new file mode 100644 index 0000000..3ed83cf --- /dev/null +++ b/src/blacklist.go @@ -0,0 +1,102 @@ +package main + +import ( + "encoding/json" + "net/http" + + "imuslab.com/zoraxy/mod/utils" +) + +/* + blacklist.go + + This script file is added to extend the + reverse proxy function to include + banning a specific IP address or country code +*/ + +//List a of blacklisted ip address or country code +func handleListBlacklisted(w http.ResponseWriter, r *http.Request) { + bltype, err := utils.GetPara(r, "type") + if err != nil { + bltype = "country" + } + + resulst := []string{} + if bltype == "country" { + resulst = geodbStore.GetAllBlacklistedCountryCode() + } else if bltype == "ip" { + resulst = geodbStore.GetAllBlacklistedIp() + } + + js, _ := json.Marshal(resulst) + utils.SendJSONResponse(w, string(js)) + +} + +func handleCountryBlacklistAdd(w http.ResponseWriter, r *http.Request) { + countryCode, err := utils.PostPara(r, "cc") + if err != nil { + utils.SendErrorResponse(w, "invalid or empty country code") + return + } + + geodbStore.AddCountryCodeToBlackList(countryCode) + + utils.SendOK(w) +} + +func handleCountryBlacklistRemove(w http.ResponseWriter, r *http.Request) { + countryCode, err := utils.PostPara(r, "cc") + if err != nil { + utils.SendErrorResponse(w, "invalid or empty country code") + return + } + + geodbStore.RemoveCountryCodeFromBlackList(countryCode) + + utils.SendOK(w) +} + +func handleIpBlacklistAdd(w http.ResponseWriter, r *http.Request) { + ipAddr, err := utils.PostPara(r, "ip") + if err != nil { + utils.SendErrorResponse(w, "invalid or empty ip address") + return + } + + geodbStore.AddIPToBlackList(ipAddr) +} + +func handleIpBlacklistRemove(w http.ResponseWriter, r *http.Request) { + ipAddr, err := utils.PostPara(r, "ip") + if err != nil { + utils.SendErrorResponse(w, "invalid or empty ip address") + return + } + + geodbStore.RemoveIPFromBlackList(ipAddr) + + utils.SendOK(w) +} + +func handleBlacklistEnable(w http.ResponseWriter, r *http.Request) { + enable, err := utils.PostPara(r, "enable") + if err != nil { + //Return the current enabled state + currentEnabled := geodbStore.Enabled + js, _ := json.Marshal(currentEnabled) + utils.SendJSONResponse(w, string(js)) + } else { + if enable == "true" { + geodbStore.ToggleBlacklist(true) + } else if enable == "false" { + geodbStore.ToggleBlacklist(false) + } else { + utils.SendErrorResponse(w, "invalid enable state: only true and false is accepted") + return + } + + utils.SendOK(w) + } +} diff --git a/src/cert.go b/src/cert.go new file mode 100644 index 0000000..897bc2a --- /dev/null +++ b/src/cert.go @@ -0,0 +1,189 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "os" + "path/filepath" + + "imuslab.com/zoraxy/mod/utils" +) + +// Check if the default certificates is correctly setup +func handleDefaultCertCheck(w http.ResponseWriter, r *http.Request) { + type CheckResult struct { + DefaultPubExists bool + DefaultPriExists bool + } + + pub, pri := tlsCertManager.DefaultCertExistsSep() + js, _ := json.Marshal(CheckResult{ + pub, + pri, + }) + + utils.SendJSONResponse(w, string(js)) +} + +// Return a list of domains where the certificates covers +func handleListCertificate(w http.ResponseWriter, r *http.Request) { + filenames, err := tlsCertManager.ListCertDomains() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + showDate, _ := utils.GetPara(r, "date") + if showDate == "true" { + type CertInfo struct { + Domain string + LastModifiedDate string + } + + results := []*CertInfo{} + + for _, filename := range filenames { + fileInfo, err := os.Stat(filepath.Join(tlsCertManager.CertStore, filename+".crt")) + if err != nil { + utils.SendErrorResponse(w, "invalid domain certificate discovered: "+filename) + return + } + modifiedTime := fileInfo.ModTime().Format("2006-01-02 15:04:05") + + thisCertInfo := CertInfo{ + Domain: filename, + LastModifiedDate: modifiedTime, + } + + results = append(results, &thisCertInfo) + } + + js, _ := json.Marshal(results) + w.Header().Set("Content-Type", "application/json") + w.Write(js) + } else { + response, err := json.Marshal(filenames) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Write(response) + } + +} + +// Handle front-end toggling TLS mode +func handleToggleTLSProxy(w http.ResponseWriter, r *http.Request) { + currentTlsSetting := false + if sysdb.KeyExists("settings", "usetls") { + sysdb.Read("settings", "usetls", ¤tTlsSetting) + } + + newState, err := utils.PostPara(r, "set") + if err != nil { + //No setting. Get the current status + js, _ := json.Marshal(currentTlsSetting) + utils.SendJSONResponse(w, string(js)) + } else { + if newState == "true" { + sysdb.Write("settings", "usetls", true) + log.Println("Enabling TLS mode on reverse proxy") + dynamicProxyRouter.UpdateTLSSetting(true) + } else if newState == "false" { + sysdb.Write("settings", "usetls", false) + log.Println("Disabling TLS mode on reverse proxy") + dynamicProxyRouter.UpdateTLSSetting(false) + } else { + utils.SendErrorResponse(w, "invalid state given. Only support true or false") + return + } + + utils.SendOK(w) + + } +} + +// Handle upload of the certificate +func handleCertUpload(w http.ResponseWriter, r *http.Request) { + // check if request method is POST + if r.Method != "POST" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + // get the key type + keytype, err := utils.GetPara(r, "ktype") + overWriteFilename := "" + if err != nil { + http.Error(w, "Not defined key type (pub / pri)", http.StatusBadRequest) + return + } + + // get the domain + domain, err := utils.GetPara(r, "domain") + if err != nil { + //Assume localhost + domain = "default" + } + + if keytype == "pub" { + overWriteFilename = domain + ".crt" + } else if keytype == "pri" { + overWriteFilename = domain + ".key" + } else { + http.Error(w, "Not supported keytype: "+keytype, http.StatusBadRequest) + return + } + + // parse multipart form data + err = r.ParseMultipartForm(10 << 20) // 10 MB + if err != nil { + http.Error(w, "Failed to parse form data", http.StatusBadRequest) + return + } + + // get file from form data + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "Failed to get file", http.StatusBadRequest) + return + } + defer file.Close() + + // create file in upload directory + os.MkdirAll("./certs", 0775) + f, err := os.Create(filepath.Join("./certs", overWriteFilename)) + if err != nil { + http.Error(w, "Failed to create file", http.StatusInternalServerError) + return + } + defer f.Close() + + // copy file contents to destination file + _, err = io.Copy(f, file) + if err != nil { + http.Error(w, "Failed to save file", http.StatusInternalServerError) + return + } + + // send response + fmt.Fprintln(w, "File upload successful!") +} + +// Handle cert remove +func handleCertRemove(w http.ResponseWriter, r *http.Request) { + domain, err := utils.PostPara(r, "domain") + if err != nil { + utils.SendErrorResponse(w, "invalid domain given") + return + } + err = tlsCertManager.RemoveCert(domain) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + } +} diff --git a/config.go b/src/config.go similarity index 82% rename from config.go rename to src/config.go index 0d1970e..4031de3 100644 --- a/config.go +++ b/src/config.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" "strings" + + "imuslab.com/zoraxy/mod/utils" ) type Record struct { @@ -35,9 +37,10 @@ func SaveReverseProxyConfig(ptype string, rootname string, proxyTarget string, u func RemoveReverseProxyConfig(rootname string) error { filename := getFilenameFromRootName(rootname) - log.Println("Config Removed: ", filepath.Join("conf", filename)) - if fileExists(filepath.Join("conf", filename)) { - err := os.Remove(filepath.Join("conf", filename)) + removePendingFile := strings.ReplaceAll(filepath.Join("conf", filename), "\\", "/") + log.Println("Config Removed: ", removePendingFile) + if utils.FileExists(removePendingFile) { + err := os.Remove(removePendingFile) if err != nil { log.Println(err.Error()) return err @@ -48,7 +51,7 @@ func RemoveReverseProxyConfig(rootname string) error { return nil } -//Return ptype, rootname and proxyTarget, error if any +// Return ptype, rootname and proxyTarget, error if any func LoadReverseProxyConfig(filename string) (*Record, error) { thisRecord := Record{} configContent, err := ioutil.ReadFile(filename) diff --git a/src/geoip.go b/src/geoip.go new file mode 100644 index 0000000..1940609 --- /dev/null +++ b/src/geoip.go @@ -0,0 +1,39 @@ +package main + +import ( + "net" + "net/http" + "strings" + + "github.com/oschwald/geoip2-golang" +) + +func getCountryCodeFromRequest(r *http.Request) string { + countryCode := "" + + // Get the IP address of the user from the request headers + ipAddress := r.Header.Get("X-Forwarded-For") + if ipAddress == "" { + ipAddress = strings.Split(r.RemoteAddr, ":")[0] + } + + // Open the GeoIP database + db, err := geoip2.Open("./system/GeoIP2-Country.mmdb") + if err != nil { + // Handle the error + return countryCode + } + defer db.Close() + + // Look up the country code for the IP address + record, err := db.Country(net.ParseIP(ipAddress)) + if err != nil { + // Handle the error + return countryCode + } + + // Get the ISO country code from the record + countryCode = record.Country.IsoCode + + return countryCode +} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..e51d749 --- /dev/null +++ b/src/go.mod @@ -0,0 +1,12 @@ +module imuslab.com/zoraxy + +go 1.16 + +require ( + github.com/boltdb/bolt v1.3.1 + github.com/gorilla/sessions v1.2.1 + github.com/gorilla/websocket v1.4.2 + github.com/oschwald/geoip2-golang v1.8.0 + gitlab.com/NebulousLabs/go-upnp v0.0.0-20211002182029-11da932010b6 + golang.org/x/sys v0.6.0 // indirect +) diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..2dba50b --- /dev/null +++ b/src/go.sum @@ -0,0 +1,46 @@ +github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= +github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= +github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= +github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI= +github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/oschwald/geoip2-golang v1.8.0 h1:KfjYB8ojCEn/QLqsDU0AzrJ3R5Qa9vFlx3z6SLNcKTs= +github.com/oschwald/geoip2-golang v1.8.0/go.mod h1:R7bRvYjOeaoenAp9sKRS8GX5bJWcZ0laWO5+DauEktw= +github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg= +github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.3/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40 h1:dizWJqTWjwyD8KGcMOwgrkqu1JIkofYgKkmDeNE7oAs= +gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40/go.mod h1:rOnSnoRyxMI3fe/7KIbVcsHRGxe30OONv8dEgo+vCfA= +gitlab.com/NebulousLabs/go-upnp v0.0.0-20211002182029-11da932010b6 h1:WKij6HF8ECp9E7K0E44dew9NrRDGiNR5u4EFsXnJUx4= +gitlab.com/NebulousLabs/go-upnp v0.0.0-20211002182029-11da932010b6/go.mod h1:vhrHTGDh4YR7wK8Z+kRJ+x8SF/6RUM3Vb64Si5FD0L8= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 h1:4qWs8cYYH6PoEFy4dfhDFgoMGkwAcETd+MmPdCPMzUc= +golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..d4ac5c9 --- /dev/null +++ b/src/main.go @@ -0,0 +1,184 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "path/filepath" + "syscall" + "time" + + "imuslab.com/zoraxy/mod/aroz" + "imuslab.com/zoraxy/mod/auth" + "imuslab.com/zoraxy/mod/database" + "imuslab.com/zoraxy/mod/dynamicproxy/redirection" + "imuslab.com/zoraxy/mod/geodb" + "imuslab.com/zoraxy/mod/statistic" + "imuslab.com/zoraxy/mod/tlscert" + "imuslab.com/zoraxy/mod/upnp" + "imuslab.com/zoraxy/mod/utils" +) + +//General flags +var noauth = flag.Bool("noauth", false, "Disable authentication for management interface") +var showver = flag.Bool("version", false, "Show version of this server") + +var ( + name = "Zoraxy" + version = "2.1" + + handler *aroz.ArozHandler + sysdb *database.Database + authAgent *auth.AuthAgent + tlsCertManager *tlscert.Manager + redirectTable *redirection.RuleTable + geodbStore *geodb.Store + statisticCollector *statistic.Collector + upnpClient *upnp.UPnPClient +) + +// Kill signal handler. Do something before the system the core terminate. +func SetupCloseHandler() { + c := make(chan os.Signal, 2) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + log.Println("\r- Shutting down " + name) + geodbStore.Close() + statisticCollector.Close() + + //Close database, final + sysdb.Close() + os.Exit(0) + }() +} + +func main() { + //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information + handler = aroz.HandleFlagParse(aroz.ServiceInfo{ + Name: name, + Desc: "Dynamic Reverse Proxy Server", + Group: "Network", + IconPath: "Zoraxy/img/small_icon.png", + Version: version, + StartDir: "Zoraxy/index.html", + SupportFW: true, + LaunchFWDir: "Zoraxy/index.html", + SupportEmb: false, + InitFWSize: []int{1080, 580}, + }) + + if *showver { + fmt.Println(name + " - Version " + version) + os.Exit(0) + } + + SetupCloseHandler() + + //Check if all required files are here + ValidateSystemFiles() + + //Create database + db, err := database.NewDatabase("sys.db", false) + if err != nil { + log.Fatal(err) + } + sysdb = db + //Create tables for the database + sysdb.NewTable("settings") + + //Create an auth agent + sessionKey, err := auth.GetSessionKey(sysdb) + if err != nil { + log.Fatal(err) + } + authAgent = auth.NewAuthenticationAgent(name, []byte(sessionKey), sysdb, true, func(w http.ResponseWriter, r *http.Request) { + //Not logged in. Redirecting to login page + http.Redirect(w, r, "/login.html", http.StatusTemporaryRedirect) + }) + + //Create a TLS certificate manager + tlsCertManager, err = tlscert.NewManager("./certs") + if err != nil { + panic(err) + } + + //Create a redirection rule table + redirectTable, err = redirection.NewRuleTable("./rules") + if err != nil { + panic(err) + } + + //Create a geodb store + geodbStore, err = geodb.NewGeoDb(sysdb, "./system/GeoLite2-Country.mmdb") + if err != nil { + panic(err) + } + + //Create a statistic collector + statisticCollector, err = statistic.NewStatisticCollector(statistic.CollectorOption{ + Database: sysdb, + }) + if err != nil { + panic(err) + } + + //Create a upnp client + err = initUpnp() + if err != nil { + panic(err) + } + + //Initiate management interface APIs + initAPIs() + + //Start the reverse proxy server in go routine + go func() { + ReverseProxtInit() + }() + + time.Sleep(500 * time.Millisecond) + //Any log println will be shown in the core system via STDOUT redirection. But not STDIN. + log.Println("ReverseProxy started. Visit control panel at http://localhost" + handler.Port) + err = http.ListenAndServe(handler.Port, nil) + + if err != nil { + log.Fatal(err) + } + +} + +//Unzip web.tar.gz if file exists +func ValidateSystemFiles() error { + if !utils.FileExists("./web") || !utils.FileExists("./system") { + //Check if the web.tar.gz exists + if utils.FileExists("./web.tar.gz") { + //Unzip the file + f, err := os.Open("./web.tar.gz") + if err != nil { + return err + } + + err = utils.ExtractTarGzipByStream(filepath.Clean("./"), f, true) + if err != nil { + return err + } + + err = f.Close() + if err != nil { + return err + } + + //Delete the web.tar.gz + os.Remove("./web.tar.gz") + } else { + return errors.New("system files not found") + } + } + return errors.New("system files not found or corrupted") + +} diff --git a/mod/aroz/aroz.go b/src/mod/aroz/aroz.go similarity index 83% rename from mod/aroz/aroz.go rename to src/mod/aroz/aroz.go index 2dc3236..2296b7b 100644 --- a/mod/aroz/aroz.go +++ b/src/mod/aroz/aroz.go @@ -9,6 +9,7 @@ import ( "os" ) +//To be used with arozos system type ArozHandler struct { Port string restfulEndpoint string @@ -33,14 +34,14 @@ type ServiceInfo struct { //This function will request the required flag from the startup paramters and parse it to the need of the arozos. func HandleFlagParse(info ServiceInfo) *ArozHandler { - var infoRequestMode = flag.Bool("info", false, "Show information about this subservice") - var port = flag.String("port", ":8000", "The default listening endpoint for this subservice") - var restful = flag.String("rpt", "http://localhost:8080/api/ajgi/interface", "The RESTFUL Endpoint of the parent") + var infoRequestMode = flag.Bool("info", false, "Show information about this program in JSON") + var port = flag.String("port", ":8000", "Management web interface listening port") + var restful = flag.String("rpt", "", "Reserved") //Parse the flags flag.Parse() - if *infoRequestMode == true { + if *infoRequestMode { //Information request mode - jsonString, _ := json.Marshal(info) + jsonString, _ := json.MarshalIndent(info, "", " ") fmt.Println(string(jsonString)) os.Exit(0) } @@ -58,6 +59,11 @@ func (a *ArozHandler) GetUserInfoFromRequest(w http.ResponseWriter, r *http.Requ return username, token } +func (a *ArozHandler) IsUsingExternalPermissionManager() bool { + return !(a.restfulEndpoint == "") +} + +//Request gateway interface for advance permission sandbox control func (a *ArozHandler) RequestGatewayInterface(token string, script string) (*http.Response, error) { resp, err := http.PostForm(a.restfulEndpoint, url.Values{"token": {token}, "script": {script}}) diff --git a/mod/aroz/doc.txt b/src/mod/aroz/doc.txt similarity index 100% rename from mod/aroz/doc.txt rename to src/mod/aroz/doc.txt diff --git a/src/mod/auth/auth.go b/src/mod/auth/auth.go new file mode 100644 index 0000000..46f42f9 --- /dev/null +++ b/src/mod/auth/auth.go @@ -0,0 +1,478 @@ +package auth + +/* + + author: tobychui +*/ + +import ( + "crypto/rand" + "crypto/sha512" + "errors" + "net/http" + "net/mail" + "strings" + + "encoding/hex" + "log" + + "github.com/gorilla/sessions" + db "imuslab.com/zoraxy/mod/database" + "imuslab.com/zoraxy/mod/utils" +) + +type AuthAgent struct { + //Session related + SessionName string + SessionStore *sessions.CookieStore + Database *db.Database + LoginRedirectionHandler func(http.ResponseWriter, *http.Request) +} + +type AuthEndpoints struct { + Login string + Logout string + Register string + CheckLoggedIn string + Autologin string +} + +//Constructor +func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent { + store := sessions.NewCookieStore(key) + err := sysdb.NewTable("auth") + if err != nil { + log.Println("Failed to create auth database. Terminating.") + panic(err) + } + + //Create a new AuthAgent object + newAuthAgent := AuthAgent{ + SessionName: sessionName, + SessionStore: store, + Database: sysdb, + LoginRedirectionHandler: loginRedirectionHandler, + } + + //Return the authAgent + return &newAuthAgent +} + +func GetSessionKey(sysdb *db.Database) (string, error) { + sysdb.NewTable("auth") + sessionKey := "" + if !sysdb.KeyExists("auth", "sessionkey") { + key := make([]byte, 32) + rand.Read(key) + sessionKey = string(key) + sysdb.Write("auth", "sessionkey", sessionKey) + log.Println("[Auth] New authentication session key generated") + } else { + log.Println("[Auth] Authentication session key loaded from database") + err := sysdb.Read("auth", "sessionkey", &sessionKey) + if err != nil { + return "", errors.New("database read error. Is the database file corrupted?") + } + } + return sessionKey, nil +} + +//This function will handle an http request and redirect to the given login address if not logged in +func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request)) { + if a.CheckAuth(r) { + //User already logged in + handler(w, r) + } else { + //User not logged in + a.LoginRedirectionHandler(w, r) + } +} + +//Handle login request, require POST username and password +func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) { + + //Get username from request using POST mode + username, err := utils.PostPara(r, "username") + if err != nil { + //Username not defined + log.Println("[Auth] " + r.RemoteAddr + " trying to login with username: " + username) + utils.SendErrorResponse(w, "Username not defined or empty.") + return + } + + //Get password from request using POST mode + password, err := utils.PostPara(r, "password") + if err != nil { + //Password not defined + utils.SendErrorResponse(w, "Password not defined or empty.") + return + } + + //Get rememberme settings + rememberme := false + rmbme, _ := utils.PostPara(r, "rmbme") + if rmbme == "true" { + rememberme = true + } + + //Check the database and see if this user is in the database + passwordCorrect, rejectionReason := a.ValidateUsernameAndPasswordWithReason(username, password) + //The database contain this user information. Check its password if it is correct + if passwordCorrect { + //Password correct + // Set user as authenticated + a.LoginUserByRequest(w, r, username, rememberme) + + //Print the login message to console + log.Println(username + " logged in.") + utils.SendOK(w) + } else { + //Password incorrect + log.Println(username + " login request rejected: " + rejectionReason) + + utils.SendErrorResponse(w, rejectionReason) + return + } +} + +func (a *AuthAgent) ValidateUsernameAndPassword(username string, password string) bool { + succ, _ := a.ValidateUsernameAndPasswordWithReason(username, password) + return succ +} + +//validate the username and password, return reasons if the auth failed +func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, password string) (bool, string) { + hashedPassword := Hash(password) + var passwordInDB string + err := a.Database.Read("auth", "passhash/"+username, &passwordInDB) + if err != nil { + //User not found or db exception + log.Println("[Auth] " + username + " login with incorrect password") + return false, "Invalid username or password" + } + + if passwordInDB == hashedPassword { + return true, "" + } else { + return false, "Invalid username or password" + } +} + +//Login the user by creating a valid session for this user +func (a *AuthAgent) LoginUserByRequest(w http.ResponseWriter, r *http.Request, username string, rememberme bool) { + session, _ := a.SessionStore.Get(r, a.SessionName) + + session.Values["authenticated"] = true + session.Values["username"] = username + session.Values["rememberMe"] = rememberme + + //Check if remember me is clicked. If yes, set the maxage to 1 week. + if rememberme { + session.Options = &sessions.Options{ + MaxAge: 3600 * 24 * 7, //One week + Path: "/", + } + } else { + session.Options = &sessions.Options{ + MaxAge: 3600 * 1, //One hour + Path: "/", + } + } + session.Save(r, w) +} + +//Handle logout, reply OK after logged out. WILL NOT DO REDIRECTION +func (a *AuthAgent) HandleLogout(w http.ResponseWriter, r *http.Request) { + username, err := a.GetUserName(w, r) + if username != "" { + log.Println(username + " logged out.") + } + // Revoke users authentication + err = a.Logout(w, r) + if err != nil { + utils.SendErrorResponse(w, "Logout failed") + return + } + + w.Write([]byte("OK")) +} + +func (a *AuthAgent) Logout(w http.ResponseWriter, r *http.Request) error { + session, err := a.SessionStore.Get(r, a.SessionName) + if err != nil { + return err + } + session.Values["authenticated"] = false + session.Values["username"] = nil + session.Save(r, w) + return nil +} + +//Get the current session username from request +func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string, error) { + if a.CheckAuth(r) { + //This user has logged in. + session, _ := a.SessionStore.Get(r, a.SessionName) + return session.Values["username"].(string), nil + } else { + //This user has not logged in. + return "", errors.New("user not logged in") + } +} + +//Get the current session user email from request +func (a *AuthAgent) GetUserEmail(w http.ResponseWriter, r *http.Request) (string, error) { + if a.CheckAuth(r) { + //This user has logged in. + session, _ := a.SessionStore.Get(r, a.SessionName) + username := session.Values["username"].(string) + userEmail := "" + err := a.Database.Read("auth", "email/"+username, &userEmail) + if err != nil { + return "", err + } + + return userEmail, nil + } else { + //This user has not logged in. + return "", errors.New("user not logged in") + } +} + +//Check if the user has logged in, return true / false in JSON +func (a *AuthAgent) CheckLogin(w http.ResponseWriter, r *http.Request) { + if a.CheckAuth(r) { + utils.SendJSONResponse(w, "true") + } else { + utils.SendJSONResponse(w, "false") + } +} + +//Handle new user register. Require POST username, password, group. +func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request, callback func(string, string)) { + //Get username from request + newusername, err := utils.PostPara(r, "username") + if err != nil { + utils.SendErrorResponse(w, "Missing 'username' paramter") + return + } + + //Get password from request + password, err := utils.PostPara(r, "password") + if err != nil { + utils.SendErrorResponse(w, "Missing 'password' paramter") + return + } + + //Get email from request + email, err := utils.PostPara(r, "email") + if err != nil { + utils.SendErrorResponse(w, "Missing 'email' paramter") + return + } + + _, err = mail.ParseAddress(email) + if err != nil { + utils.SendErrorResponse(w, "Invalid or malformed email") + return + } + + //Ok to proceed create this user + err = a.CreateUserAccount(newusername, password, email) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Do callback if exists + if callback != nil { + callback(newusername, email) + } + + //Return to the client with OK + utils.SendOK(w) + log.Println("[Auth] New user " + newusername + " added to system.") +} + +//Handle new user register without confirmation email. Require POST username, password, group. +func (a *AuthAgent) HandleRegisterWithoutEmail(w http.ResponseWriter, r *http.Request, callback func(string, string)) { + //Get username from request + newusername, err := utils.PostPara(r, "username") + if err != nil { + utils.SendErrorResponse(w, "Missing 'username' paramter") + return + } + + //Get password from request + password, err := utils.PostPara(r, "password") + if err != nil { + utils.SendErrorResponse(w, "Missing 'password' paramter") + return + } + + //Ok to proceed create this user + err = a.CreateUserAccount(newusername, password, "") + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Do callback if exists + if callback != nil { + callback(newusername, "") + } + + //Return to the client with OK + utils.SendOK(w) + log.Println("[Auth] Admin account created: " + newusername) +} + +//Check authentication from request header's session value +func (a *AuthAgent) CheckAuth(r *http.Request) bool { + session, err := a.SessionStore.Get(r, a.SessionName) + if err != nil { + return false + } + // Check if user is authenticated + if auth, ok := session.Values["authenticated"].(bool); !ok || !auth { + return false + } + return true +} + +//Handle de-register of users. Require POST username. +//THIS FUNCTION WILL NOT CHECK FOR PERMISSION. PLEASE USE WITH PERMISSION HANDLER +func (a *AuthAgent) HandleUnregister(w http.ResponseWriter, r *http.Request) { + //Check if the user is logged in + if !a.CheckAuth(r) { + //This user has not logged in + utils.SendErrorResponse(w, "Login required to remove user from the system.") + return + } + + //Get username from request + username, err := utils.PostPara(r, "username") + if err != nil { + utils.SendErrorResponse(w, "Missing 'username' paramter") + return + } + + err = a.UnregisterUser(username) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + //Return to the client with OK + utils.SendOK(w) + log.Println("[Auth] User " + username + " has been removed from the system.") +} + +func (a *AuthAgent) UnregisterUser(username string) error { + //Check if the user exists in the system database. + if !a.Database.KeyExists("auth", "passhash/"+username) { + //This user do not exists. + return errors.New("this user does not exists") + } + + //OK! Remove the user from the database + a.Database.Delete("auth", "passhash/"+username) + a.Database.Delete("auth", "email/"+username) + return nil +} + +//Get the number of users in the system +func (a *AuthAgent) GetUserCounts() int { + entries, _ := a.Database.ListTable("auth") + usercount := 0 + for _, keypairs := range entries { + if strings.Contains(string(keypairs[0]), "passhash/") { + //This is a user registry + usercount++ + } + } + + if usercount == 0 { + log.Println("There are no user in the database.") + } + return usercount +} + +//List all username within the system +func (a *AuthAgent) ListUsers() []string { + entries, _ := a.Database.ListTable("auth") + results := []string{} + for _, keypairs := range entries { + if strings.Contains(string(keypairs[0]), "passhash/") { + username := strings.Split(string(keypairs[0]), "/")[1] + results = append(results, username) + } + } + return results +} + +//Check if the given username exists +func (a *AuthAgent) UserExists(username string) bool { + userpasswordhash := "" + err := a.Database.Read("auth", "passhash/"+username, &userpasswordhash) + if err != nil || userpasswordhash == "" { + return false + } + return true +} + +//Update the session expire time given the request header. +func (a *AuthAgent) UpdateSessionExpireTime(w http.ResponseWriter, r *http.Request) bool { + session, _ := a.SessionStore.Get(r, a.SessionName) + if session.Values["authenticated"].(bool) { + //User authenticated. Extend its expire time + rememberme := session.Values["rememberMe"].(bool) + //Extend the session expire time + if rememberme { + session.Options = &sessions.Options{ + MaxAge: 3600 * 24 * 7, //One week + Path: "/", + } + } else { + session.Options = &sessions.Options{ + MaxAge: 3600 * 1, //One hour + Path: "/", + } + } + session.Save(r, w) + return true + } else { + return false + } +} + +//Create user account +func (a *AuthAgent) CreateUserAccount(newusername string, password string, email string) error { + //Check user already exists + if a.UserExists(newusername) { + return errors.New("user with same name already exists") + } + + key := newusername + hashedPassword := Hash(password) + err := a.Database.Write("auth", "passhash/"+key, hashedPassword) + if err != nil { + return err + } + + if email != "" { + err = a.Database.Write("auth", "email/"+key, email) + if err != nil { + return err + } + } + + return nil +} + +//Hash the given raw string into sha512 hash +func Hash(raw string) string { + h := sha512.New() + h.Write([]byte(raw)) + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/src/mod/auth/router.go b/src/mod/auth/router.go new file mode 100644 index 0000000..546cae0 --- /dev/null +++ b/src/mod/auth/router.go @@ -0,0 +1,53 @@ +package auth + +import ( + "errors" + "log" + "net/http" +) + +type RouterOption struct { + AuthAgent *AuthAgent + RequireAuth bool //This router require authentication + DeniedHandler func(http.ResponseWriter, *http.Request) //Things to do when request is rejected + +} + +type RouterDef struct { + option RouterOption + endpoints map[string]func(http.ResponseWriter, *http.Request) +} + +func NewManagedHTTPRouter(option RouterOption) *RouterDef { + return &RouterDef{ + option: option, + endpoints: map[string]func(http.ResponseWriter, *http.Request){}, + } +} + +func (router *RouterDef) HandleFunc(endpoint string, handler func(http.ResponseWriter, *http.Request)) error { + //Check if the endpoint already registered + if _, exist := router.endpoints[endpoint]; exist { + log.Println("WARNING! Duplicated registering of web endpoint: " + endpoint) + return errors.New("endpoint register duplicated") + } + + authAgent := router.option.AuthAgent + + //OK. Register handler + http.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) { + //Check authentication of the user + if router.option.RequireAuth { + authAgent.HandleCheckAuth(w, r, func(w http.ResponseWriter, r *http.Request) { + handler(w, r) + }) + } else { + handler(w, r) + } + + }) + + router.endpoints[endpoint] = handler + + return nil +} diff --git a/src/mod/database/database.go b/src/mod/database/database.go new file mode 100644 index 0000000..9726df8 --- /dev/null +++ b/src/mod/database/database.go @@ -0,0 +1,120 @@ +package database + +/* + ArOZ Online Database Access Module + author: tobychui + + This is an improved Object oriented base solution to the original + aroz online database script. +*/ + +import ( + "sync" +) + +type Database struct { + Db interface{} //This will be nil on openwrt and *bolt.DB in the rest of the systems + Tables sync.Map + ReadOnly bool +} + +func NewDatabase(dbfile string, readOnlyMode bool) (*Database, error) { + return newDatabase(dbfile, readOnlyMode) +} + +/* + Create / Drop a table + Usage: + err := sysdb.NewTable("MyTable") + err := sysdb.DropTable("MyTable") +*/ + +func (d *Database) UpdateReadWriteMode(readOnly bool) { + d.ReadOnly = readOnly +} + +//Dump the whole db into a log file +func (d *Database) Dump(filename string) ([]string, error) { + return d.dump(filename) +} + +//Create a new table +func (d *Database) NewTable(tableName string) error { + return d.newTable(tableName) +} + +//Check is table exists +func (d *Database) TableExists(tableName string) bool { + return d.tableExists(tableName) +} + +//Drop the given table +func (d *Database) DropTable(tableName string) error { + return d.dropTable(tableName) +} + +/* + Write to database with given tablename and key. Example Usage: + type demo struct{ + content string + } + thisDemo := demo{ + content: "Hello World", + } + err := sysdb.Write("MyTable", "username/message",thisDemo); +*/ +func (d *Database) Write(tableName string, key string, value interface{}) error { + return d.write(tableName, key, value) +} + +/* + Read from database and assign the content to a given datatype. Example Usage: + + type demo struct{ + content string + } + thisDemo := new(demo) + err := sysdb.Read("MyTable", "username/message",&thisDemo); +*/ + +func (d *Database) Read(tableName string, key string, assignee interface{}) error { + return d.read(tableName, key, assignee) +} + +func (d *Database) KeyExists(tableName string, key string) bool { + return d.keyExists(tableName, key) +} + +/* + Delete a value from the database table given tablename and key + + err := sysdb.Delete("MyTable", "username/message"); +*/ +func (d *Database) Delete(tableName string, key string) error { + return d.delete(tableName, key) +} + +/* + //List table example usage + //Assume the value is stored as a struct named "groupstruct" + + entries, err := sysdb.ListTable("test") + if err != nil { + panic(err) + } + for _, keypairs := range entries{ + log.Println(string(keypairs[0])) + group := new(groupstruct) + json.Unmarshal(keypairs[1], &group) + log.Println(group); + } + +*/ + +func (d *Database) ListTable(tableName string) ([][][]byte, error) { + return d.listTable(tableName) +} + +func (d *Database) Close() { + d.close() +} diff --git a/mod/database/database.go b/src/mod/database/database_core.go similarity index 52% rename from mod/database/database.go rename to src/mod/database/database_core.go index efbdf70..3035fc8 100644 --- a/mod/database/database.go +++ b/src/mod/database/database_core.go @@ -1,13 +1,8 @@ +//go:build !mipsle && !riscv64 +// +build !mipsle,!riscv64 + package database -/* - ArOZ Online Database Access Module - author: tobychui - - This is an improved Object oriented base solution to the original - aroz online database script. -*/ - import ( "encoding/json" "errors" @@ -17,15 +12,11 @@ import ( "github.com/boltdb/bolt" ) -type Database struct { - Db *bolt.DB - Tables sync.Map - ReadOnly bool -} - -func NewDatabase(dbfile string, readOnlyMode bool) (*Database, error) { +func newDatabase(dbfile string, readOnlyMode bool) (*Database, error) { db, err := bolt.Open(dbfile, 0600, nil) - log.Println("Key-value Database Service Started: " + dbfile) + if err != nil { + return nil, err + } tableMap := sync.Map{} //Build the table list from database @@ -43,19 +34,8 @@ func NewDatabase(dbfile string, readOnlyMode bool) (*Database, error) { }, err } -/* - Create / Drop a table - Usage: - err := sysdb.NewTable("MyTable") - err := sysdb.DropTable("MyTable") -*/ - -func (d *Database) UpdateReadWriteMode(readOnly bool) { - d.ReadOnly = readOnly -} - //Dump the whole db into a log file -func (d *Database) Dump(filename string) ([]string, error) { +func (d *Database) dump(filename string) ([]string, error) { results := []string{} d.Tables.Range(func(tableName, v interface{}) bool { @@ -74,12 +54,12 @@ func (d *Database) Dump(filename string) ([]string, error) { } //Create a new table -func (d *Database) NewTable(tableName string) error { +func (d *Database) newTable(tableName string) error { if d.ReadOnly == true { return errors.New("Operation rejected in ReadOnly mode") } - err := d.Db.Update(func(tx *bolt.Tx) error { + err := d.Db.(*bolt.DB).Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte(tableName)) if err != nil { return err @@ -92,7 +72,7 @@ func (d *Database) NewTable(tableName string) error { } //Check is table exists -func (d *Database) TableExists(tableName string) bool { +func (d *Database) tableExists(tableName string) bool { if _, ok := d.Tables.Load(tableName); ok { return true } @@ -100,12 +80,12 @@ func (d *Database) TableExists(tableName string) bool { } //Drop the given table -func (d *Database) DropTable(tableName string) error { +func (d *Database) dropTable(tableName string) error { if d.ReadOnly == true { return errors.New("Operation rejected in ReadOnly mode") } - err := d.Db.Update(func(tx *bolt.Tx) error { + err := d.Db.(*bolt.DB).Update(func(tx *bolt.Tx) error { err := tx.DeleteBucket([]byte(tableName)) if err != nil { return err @@ -115,18 +95,9 @@ func (d *Database) DropTable(tableName string) error { return err } -/* - Write to database with given tablename and key. Example Usage: - type demo struct{ - content string - } - thisDemo := demo{ - content: "Hello World", - } - err := sysdb.Write("MyTable", "username/message",thisDemo); -*/ -func (d *Database) Write(tableName string, key string, value interface{}) error { - if d.ReadOnly == true { +//Write to table +func (d *Database) write(tableName string, key string, value interface{}) error { + if d.ReadOnly { return errors.New("Operation rejected in ReadOnly mode") } @@ -134,8 +105,11 @@ func (d *Database) Write(tableName string, key string, value interface{}) error if err != nil { return err } - err = d.Db.Update(func(tx *bolt.Tx) error { + err = d.Db.(*bolt.DB).Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte(tableName)) + if err != nil { + return err + } b := tx.Bucket([]byte(tableName)) err = b.Put([]byte(key), jsonString) return err @@ -143,18 +117,8 @@ func (d *Database) Write(tableName string, key string, value interface{}) error return err } -/* - Read from database and assign the content to a given datatype. Example Usage: - - type demo struct{ - content string - } - thisDemo := new(demo) - err := sysdb.Read("MyTable", "username/message",&thisDemo); -*/ - -func (d *Database) Read(tableName string, key string, assignee interface{}) error { - err := d.Db.View(func(tx *bolt.Tx) error { +func (d *Database) read(tableName string, key string, assignee interface{}) error { + err := d.Db.(*bolt.DB).View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(tableName)) v := b.Get([]byte(key)) json.Unmarshal(v, &assignee) @@ -163,9 +127,14 @@ func (d *Database) Read(tableName string, key string, assignee interface{}) erro return err } -func (d *Database) KeyExists(tableName string, key string) bool { +func (d *Database) keyExists(tableName string, key string) bool { resultIsNil := false - err := d.Db.View(func(tx *bolt.Tx) error { + if !d.TableExists(tableName) { + //Table not exists. Do not proceed accessing key + log.Println("[DB] ERROR: Requesting key from table that didn't exist!!!") + return false + } + err := d.Db.(*bolt.DB).View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(tableName)) v := b.Get([]byte(key)) if v == nil { @@ -185,17 +154,12 @@ func (d *Database) KeyExists(tableName string, key string) bool { } } -/* - Delete a value from the database table given tablename and key - - err := sysdb.Delete("MyTable", "username/message"); -*/ -func (d *Database) Delete(tableName string, key string) error { - if d.ReadOnly == true { +func (d *Database) delete(tableName string, key string) error { + if d.ReadOnly { return errors.New("Operation rejected in ReadOnly mode") } - err := d.Db.Update(func(tx *bolt.Tx) error { + err := d.Db.(*bolt.DB).Update(func(tx *bolt.Tx) error { tx.Bucket([]byte(tableName)).Delete([]byte(key)) return nil }) @@ -203,26 +167,9 @@ func (d *Database) Delete(tableName string, key string) error { return err } -/* - //List table example usage - //Assume the value is stored as a struct named "groupstruct" - - entries, err := sysdb.ListTable("test") - if err != nil { - panic(err) - } - for _, keypairs := range entries{ - log.Println(string(keypairs[0])) - group := new(groupstruct) - json.Unmarshal(keypairs[1], &group) - log.Println(group); - } - -*/ - -func (d *Database) ListTable(tableName string) ([][][]byte, error) { +func (d *Database) listTable(tableName string) ([][][]byte, error) { var results [][][]byte - err := d.Db.View(func(tx *bolt.Tx) error { + err := d.Db.(*bolt.DB).View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(tableName)) c := b.Cursor() @@ -234,7 +181,6 @@ func (d *Database) ListTable(tableName string) ([][][]byte, error) { return results, err } -func (d *Database) Close() { - d.Db.Close() - return +func (d *Database) close() { + d.Db.(*bolt.DB).Close() } diff --git a/src/mod/database/database_openwrt.go b/src/mod/database/database_openwrt.go new file mode 100644 index 0000000..9df3f03 --- /dev/null +++ b/src/mod/database/database_openwrt.go @@ -0,0 +1,208 @@ +//go:build mipsle || riscv64 +// +build mipsle riscv64 + +package database + +import ( + "encoding/json" + "errors" + "log" + "os" + "path/filepath" + "strings" + "sync" +) + +func newDatabase(dbfile string, readOnlyMode bool) (*Database, error) { + dbRootPath := filepath.ToSlash(filepath.Clean(dbfile)) + dbRootPath = "fsdb/" + dbRootPath + err := os.MkdirAll(dbRootPath, 0755) + if err != nil { + return nil, err + } + + tableMap := sync.Map{} + //build the table list from file system + files, err := filepath.Glob(filepath.Join(dbRootPath, "/*")) + if err != nil { + return nil, err + } + + for _, file := range files { + if isDirectory(file) { + tableMap.Store(filepath.Base(file), "") + } + } + + log.Println("Filesystem Emulated Key-value Database Service Started: " + dbRootPath) + return &Database{ + Db: dbRootPath, + Tables: tableMap, + ReadOnly: readOnlyMode, + }, nil +} + +func (d *Database) dump(filename string) ([]string, error) { + //Get all file objects from root + rootfiles, err := filepath.Glob(filepath.Join(d.Db.(string), "/*")) + if err != nil { + return []string{}, err + } + + //Filter out the folders + rootFolders := []string{} + for _, file := range rootfiles { + if !isDirectory(file) { + rootFolders = append(rootFolders, filepath.Base(file)) + } + } + + return rootFolders, nil +} + +func (d *Database) newTable(tableName string) error { + if d.ReadOnly { + return errors.New("Operation rejected in ReadOnly mode") + } + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + if !fileExists(tablePath) { + return os.MkdirAll(tablePath, 0755) + } + return nil +} + +func (d *Database) tableExists(tableName string) bool { + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + if _, err := os.Stat(tablePath); errors.Is(err, os.ErrNotExist) { + return false + } + + if !isDirectory(tablePath) { + return false + } + + return true +} + +func (d *Database) dropTable(tableName string) error { + if d.ReadOnly { + return errors.New("Operation rejected in ReadOnly mode") + } + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + if d.tableExists(tableName) { + return os.RemoveAll(tablePath) + } else { + return errors.New("table not exists") + } + +} + +func (d *Database) write(tableName string, key string, value interface{}) error { + if d.ReadOnly { + return errors.New("Operation rejected in ReadOnly mode") + } + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + js, err := json.Marshal(value) + if err != nil { + return err + } + + key = strings.ReplaceAll(key, "/", "-SLASH_SIGN-") + + return os.WriteFile(filepath.Join(tablePath, key+".entry"), js, 0755) +} + +func (d *Database) read(tableName string, key string, assignee interface{}) error { + if !d.keyExists(tableName, key) { + return errors.New("key not exists") + } + + key = strings.ReplaceAll(key, "/", "-SLASH_SIGN-") + + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + entryPath := filepath.Join(tablePath, key+".entry") + content, err := os.ReadFile(entryPath) + if err != nil { + return err + } + + err = json.Unmarshal(content, &assignee) + return err +} + +func (d *Database) keyExists(tableName string, key string) bool { + key = strings.ReplaceAll(key, "/", "-SLASH_SIGN-") + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + entryPath := filepath.Join(tablePath, key+".entry") + return fileExists(entryPath) +} + +func (d *Database) delete(tableName string, key string) error { + if d.ReadOnly { + return errors.New("Operation rejected in ReadOnly mode") + } + if !d.keyExists(tableName, key) { + return errors.New("key not exists") + } + key = strings.ReplaceAll(key, "/", "-SLASH_SIGN-") + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + entryPath := filepath.Join(tablePath, key+".entry") + + return os.Remove(entryPath) +} + +func (d *Database) listTable(tableName string) ([][][]byte, error) { + if !d.tableExists(tableName) { + return [][][]byte{}, errors.New("table not exists") + } + tablePath := filepath.Join(d.Db.(string), filepath.Base(tableName)) + entries, err := filepath.Glob(filepath.Join(tablePath, "/*.entry")) + if err != nil { + return [][][]byte{}, err + } + + var results [][][]byte = [][][]byte{} + for _, entry := range entries { + if !isDirectory(entry) { + //Read it + key := filepath.Base(entry) + key = strings.TrimSuffix(key, filepath.Ext(key)) + key = strings.ReplaceAll(key, "-SLASH_SIGN-", "/") + + bkey := []byte(key) + bval := []byte("") + c, err := os.ReadFile(entry) + if err != nil { + break + } + + bval = c + results = append(results, [][]byte{bkey, bval}) + } + } + return results, nil +} + +func (d *Database) close() { + //Nothing to close as it is file system +} + +func isDirectory(path string) bool { + fileInfo, err := os.Stat(path) + if err != nil { + return false + } + + return fileInfo.IsDir() +} + +func fileExists(name string) bool { + _, err := os.Stat(name) + if err == nil { + return true + } + if errors.Is(err, os.ErrNotExist) { + return false + } + return false +} diff --git a/src/mod/dynamicproxy/Server.go b/src/mod/dynamicproxy/Server.go new file mode 100644 index 0000000..9b9522e --- /dev/null +++ b/src/mod/dynamicproxy/Server.go @@ -0,0 +1,67 @@ +package dynamicproxy + +import ( + "net/http" + "os" + "strings" + + "imuslab.com/zoraxy/mod/geodb" +) + +/* + Server.go + + Main server for dynamic proxy core +*/ + +func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + //Check if this ip is in blacklist + clientIpAddr := geodb.GetRequesterIP(r) + if h.Parent.Option.GeodbStore.IsBlacklisted(clientIpAddr) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusForbidden) + template, err := os.ReadFile("./web/forbidden.html") + if err != nil { + w.Write([]byte("403 - Forbidden")) + } else { + w.Write(template) + } + h.logRequest(r, false, 403, "blacklist") + return + } + + //Check if this is a redirection url + if h.Parent.Option.RedirectRuleTable.IsRedirectable(r) { + statusCode := h.Parent.Option.RedirectRuleTable.HandleRedirect(w, r) + h.logRequest(r, statusCode != 500, statusCode, "redirect") + return + } + + //Extract request host to see if it is virtual directory or subdomain + domainOnly := r.Host + if strings.Contains(r.Host, ":") { + hostPath := strings.Split(r.Host, ":") + domainOnly = hostPath[0] + } + + if strings.Contains(r.Host, ".") { + //This might be a subdomain. See if there are any subdomain proxy router for this + //Remove the port if any + + sep := h.Parent.getSubdomainProxyEndpointFromHostname(domainOnly) + if sep != nil { + h.subdomainRequest(w, r, sep) + return + } + } + + //Clean up the request URI + proxyingPath := strings.TrimSpace(r.RequestURI) + + targetProxyEndpoint := h.Parent.getTargetProxyEndpointFromRequestURI(proxyingPath) + if targetProxyEndpoint != nil { + h.proxyRequest(w, r, targetProxyEndpoint) + } else { + h.proxyRequest(w, r, h.Parent.Root) + } +} diff --git a/src/mod/dynamicproxy/domainsniff/domainsniff.go b/src/mod/dynamicproxy/domainsniff/domainsniff.go new file mode 100644 index 0000000..52ba9a4 --- /dev/null +++ b/src/mod/dynamicproxy/domainsniff/domainsniff.go @@ -0,0 +1,23 @@ +package domainsniff + +import ( + "net" + "time" +) + +//Check if the domain is reachable and return err if not reachable +func DomainReachableWithError(domain string) error { + timeout := 1 * time.Second + conn, err := net.DialTimeout("tcp", domain, timeout) + if err != nil { + return err + } + + conn.Close() + return nil +} + +//Check if domain reachable +func DomainReachable(domain string) bool { + return DomainReachableWithError(domain) == nil +} diff --git a/mod/dynamicproxy/dpcore/LICENSE b/src/mod/dynamicproxy/dpcore/LICENSE similarity index 100% rename from mod/dynamicproxy/dpcore/LICENSE rename to src/mod/dynamicproxy/dpcore/LICENSE diff --git a/mod/dynamicproxy/dpcore/dpcore.go b/src/mod/dynamicproxy/dpcore/dpcore.go similarity index 98% rename from mod/dynamicproxy/dpcore/dpcore.go rename to src/mod/dynamicproxy/dpcore/dpcore.go index af0f7a8..6128a6f 100644 --- a/mod/dynamicproxy/dpcore/dpcore.go +++ b/src/mod/dynamicproxy/dpcore/dpcore.go @@ -2,7 +2,6 @@ package dpcore import ( "errors" - "fmt" "io" "log" "net" @@ -277,7 +276,7 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro p.logf("http: proxy error: %v", err) } - rw.WriteHeader(http.StatusBadGateway) + //rw.WriteHeader(http.StatusBadGateway) return err } @@ -290,15 +289,14 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro p.logf("http: proxy error: %v", err) } - rw.WriteHeader(http.StatusBadGateway) + //rw.WriteHeader(http.StatusBadGateway) return err } } //Custom header rewriter functions if res.Header.Get("Location") != "" { - //Custom redirection fto this rproxy relative path - fmt.Println(res.Header.Get("Location")) + //Custom redirection to this rproxy relative path res.Header.Set("Location", filepath.ToSlash(filepath.Join(p.Prepender, res.Header.Get("Location")))) } // Copy header from response to client. diff --git a/src/mod/dynamicproxy/dynamicproxy.go b/src/mod/dynamicproxy/dynamicproxy.go new file mode 100644 index 0000000..edd7df6 --- /dev/null +++ b/src/mod/dynamicproxy/dynamicproxy.go @@ -0,0 +1,310 @@ +package dynamicproxy + +import ( + "context" + "crypto/tls" + "errors" + "log" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" + + "imuslab.com/zoraxy/mod/dynamicproxy/dpcore" + "imuslab.com/zoraxy/mod/dynamicproxy/redirection" + "imuslab.com/zoraxy/mod/geodb" + "imuslab.com/zoraxy/mod/reverseproxy" + "imuslab.com/zoraxy/mod/statistic" + "imuslab.com/zoraxy/mod/tlscert" +) + +/* + Zoraxy Dynamic Proxy +*/ +type RouterOption struct { + Port int + UseTls bool + ForceHttpsRedirect bool + TlsManager *tlscert.Manager + RedirectRuleTable *redirection.RuleTable + GeodbStore *geodb.Store + StatisticCollector *statistic.Collector +} + +type Router struct { + Option *RouterOption + ProxyEndpoints *sync.Map + SubdomainEndpoint *sync.Map + Running bool + Root *ProxyEndpoint + mux http.Handler + server *http.Server + tlsListener net.Listener +} + +type ProxyEndpoint struct { + Root string + Domain string + RequireTLS bool + Proxy *dpcore.ReverseProxy `json:"-"` +} + +type SubdomainEndpoint struct { + MatchingDomain string + Domain string + RequireTLS bool + Proxy *reverseproxy.ReverseProxy `json:"-"` +} + +type ProxyHandler struct { + Parent *Router +} + +func NewDynamicProxy(option RouterOption) (*Router, error) { + proxyMap := sync.Map{} + domainMap := sync.Map{} + thisRouter := Router{ + Option: &option, + ProxyEndpoints: &proxyMap, + SubdomainEndpoint: &domainMap, + Running: false, + server: nil, + } + + thisRouter.mux = &ProxyHandler{ + Parent: &thisRouter, + } + + return &thisRouter, nil +} + +// Update TLS setting in runtime. Will restart the proxy server +// if it is already running in the background +func (router *Router) UpdateTLSSetting(tlsEnabled bool) { + router.Option.UseTls = tlsEnabled + router.Restart() +} + +// Update https redirect, which will require updates +func (router *Router) UpdateHttpToHttpsRedirectSetting(useRedirect bool) { + router.Option.ForceHttpsRedirect = useRedirect + router.Restart() +} + +// Start the dynamic routing +func (router *Router) StartProxyService() error { + //Create a new server object + if router.server != nil { + return errors.New("Reverse proxy server already running") + } + + if router.Root == nil { + return errors.New("Reverse proxy router root not set") + } + + config := &tls.Config{ + GetCertificate: router.Option.TlsManager.GetCert, + } + + if router.Option.UseTls { + //Serve with TLS mode + ln, err := tls.Listen("tcp", ":"+strconv.Itoa(router.Option.Port), config) + if err != nil { + log.Println(err) + return err + } + router.tlsListener = ln + router.server = &http.Server{Addr: ":" + strconv.Itoa(router.Option.Port), Handler: router.mux} + router.Running = true + + if router.Option.Port == 443 && router.Option.ForceHttpsRedirect { + //Add a 80 to 443 redirector + httpServer := &http.Server{ + Addr: ":80", + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusTemporaryRedirect) + }), + ReadTimeout: 3 * time.Second, + WriteTimeout: 3 * time.Second, + IdleTimeout: 120 * time.Second, + } + + log.Println("Starting HTTP-to-HTTPS redirector (port 80)") + go func() { + //Start another router to check if the router.server is killed. If yes, kill this server as well + go func() { + for router.server != nil { + time.Sleep(100 * time.Millisecond) + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + httpServer.Shutdown(ctx) + log.Println(":80 to :433 redirection listener stopped") + }() + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Could not start server: %v\n", err) + } + }() + } + log.Println("Reverse proxy service started in the background (TLS mode)") + go func() { + if err := router.server.Serve(ln); err != nil && err != http.ErrServerClosed { + log.Fatalf("Could not start server: %v\n", err) + } + }() + } else { + //Serve with non TLS mode + router.tlsListener = nil + router.server = &http.Server{Addr: ":" + strconv.Itoa(router.Option.Port), Handler: router.mux} + router.Running = true + log.Println("Reverse proxy service started in the background (Plain HTTP mode)") + go func() { + router.server.ListenAndServe() + //log.Println("[DynamicProxy] " + err.Error()) + }() + } + + return nil +} + +func (router *Router) StopProxyService() error { + if router.server == nil { + return errors.New("Reverse proxy server already stopped") + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + err := router.server.Shutdown(ctx) + if err != nil { + return err + } + + if router.tlsListener != nil { + router.tlsListener.Close() + } + + //Discard the server object + router.tlsListener = nil + router.server = nil + router.Running = false + return nil +} + +// Restart the current router if it is running. +// Startup the server if it is not running initially +func (router *Router) Restart() error { + //Stop the router if it is already running + if router.Running { + err := router.StopProxyService() + if err != nil { + return err + } + } + + //Start the server + err := router.StartProxyService() + return err +} + +/* + Check if a given request is accessed via a proxied subdomain +*/ + +func (router *Router) IsProxiedSubdomain(r *http.Request) bool { + hostname := r.Header.Get("X-Forwarded-Host") + if hostname == "" { + hostname = r.Host + } + hostname = strings.Split(hostname, ":")[0] + subdEndpoint := router.getSubdomainProxyEndpointFromHostname(hostname) + return subdEndpoint != nil +} + +/* +Add an URL into a custom proxy services +*/ +func (router *Router) AddVirtualDirectoryProxyService(rootname string, domain string, requireTLS bool) error { + if domain[len(domain)-1:] == "/" { + domain = domain[:len(domain)-1] + } + + if rootname[len(rootname)-1:] == "/" { + rootname = rootname[:len(rootname)-1] + } + + webProxyEndpoint := domain + if requireTLS { + webProxyEndpoint = "https://" + webProxyEndpoint + } else { + webProxyEndpoint = "http://" + webProxyEndpoint + } + //Create a new proxy agent for this root + path, err := url.Parse(webProxyEndpoint) + if err != nil { + return err + } + + proxy := dpcore.NewDynamicProxyCore(path, rootname) + + endpointObject := ProxyEndpoint{ + Root: rootname, + Domain: domain, + RequireTLS: requireTLS, + Proxy: proxy, + } + + router.ProxyEndpoints.Store(rootname, &endpointObject) + + log.Println("Adding Proxy Rule: ", rootname+" to "+domain) + return nil +} + +/* +Remove routing from RP +*/ +func (router *Router) RemoveProxy(ptype string, key string) error { + //fmt.Println(ptype, key) + if ptype == "vdir" { + router.ProxyEndpoints.Delete(key) + return nil + } else if ptype == "subd" { + router.SubdomainEndpoint.Delete(key) + return nil + } + return errors.New("invalid ptype") +} + +/* +Add an default router for the proxy server +*/ +func (router *Router) SetRootProxy(proxyLocation string, requireTLS bool) error { + if proxyLocation[len(proxyLocation)-1:] == "/" { + proxyLocation = proxyLocation[:len(proxyLocation)-1] + } + + webProxyEndpoint := proxyLocation + if requireTLS { + webProxyEndpoint = "https://" + webProxyEndpoint + } else { + webProxyEndpoint = "http://" + webProxyEndpoint + } + //Create a new proxy agent for this root + path, err := url.Parse(webProxyEndpoint) + if err != nil { + return err + } + + proxy := dpcore.NewDynamicProxyCore(path, "") + + rootEndpoint := ProxyEndpoint{ + Root: "/", + Domain: proxyLocation, + RequireTLS: requireTLS, + Proxy: proxy, + } + + router.Root = &rootEndpoint + return nil +} diff --git a/mod/dynamicproxy/proxyRequestHandler.go b/src/mod/dynamicproxy/proxyRequestHandler.go similarity index 58% rename from mod/dynamicproxy/proxyRequestHandler.go rename to src/mod/dynamicproxy/proxyRequestHandler.go index a4397dc..9608075 100644 --- a/mod/dynamicproxy/proxyRequestHandler.go +++ b/src/mod/dynamicproxy/proxyRequestHandler.go @@ -1,21 +1,32 @@ package dynamicproxy import ( + "errors" "log" + "net" "net/http" "net/url" + "strings" - "imuslab.com/arozos/ReverseProxy/mod/websocketproxy" + "imuslab.com/zoraxy/mod/geodb" + "imuslab.com/zoraxy/mod/statistic" + "imuslab.com/zoraxy/mod/websocketproxy" ) func (router *Router) getTargetProxyEndpointFromRequestURI(requestURI string) *ProxyEndpoint { var targetProxyEndpoint *ProxyEndpoint = nil router.ProxyEndpoints.Range(func(key, value interface{}) bool { rootname := key.(string) - if len(requestURI) >= len(rootname) && requestURI[:len(rootname)] == rootname { + if strings.HasPrefix(requestURI, rootname) { thisProxyEndpoint := value.(*ProxyEndpoint) targetProxyEndpoint = thisProxyEndpoint } + /* + if len(requestURI) >= len(rootname) && requestURI[:len(rootname)] == rootname { + thisProxyEndpoint := value.(*ProxyEndpoint) + targetProxyEndpoint = thisProxyEndpoint + } + */ return true }) @@ -42,7 +53,7 @@ func (router *Router) rewriteURL(rooturl string, requestURL string) string { func (h *ProxyHandler) subdomainRequest(w http.ResponseWriter, r *http.Request, target *SubdomainEndpoint) { r.Header.Set("X-Forwarded-Host", r.Host) requestURL := r.URL.String() - if r.Header["Upgrade"] != nil && r.Header["Upgrade"][0] == "websocket" { + if r.Header["Upgrade"] != nil && strings.ToLower(r.Header["Upgrade"][0]) == "websocket" { //Handle WebSocket request. Forward the custom Upgrade header and rewrite origin r.Header.Set("A-Upgrade", "websocket") wsRedirectionEndpoint := target.Domain @@ -58,6 +69,7 @@ func (h *ProxyHandler) subdomainRequest(w http.ResponseWriter, r *http.Request, if target.RequireTLS { u, _ = url.Parse("wss://" + wsRedirectionEndpoint + requestURL) } + h.logRequest(r, true, 101, "subdomain-websocket") wspHandler := websocketproxy.NewProxy(u) wspHandler.ServeHTTP(w, r) return @@ -65,17 +77,27 @@ func (h *ProxyHandler) subdomainRequest(w http.ResponseWriter, r *http.Request, r.Host = r.URL.Host err := target.Proxy.ServeHTTP(w, r) + var dnsError *net.DNSError if err != nil { - log.Println(err.Error()) + if errors.As(err, &dnsError) { + http.ServeFile(w, r, "./web/hosterror.html") + log.Println(err.Error()) + h.logRequest(r, false, 404, "subdomain-http") + } else { + http.ServeFile(w, r, "./web/rperror.html") + log.Println(err.Error()) + h.logRequest(r, false, 521, "subdomain-http") + } } + h.logRequest(r, true, 200, "subdomain-http") } func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, target *ProxyEndpoint) { rewriteURL := h.Parent.rewriteURL(target.Root, r.RequestURI) r.URL, _ = url.Parse(rewriteURL) r.Header.Set("X-Forwarded-Host", r.Host) - if r.Header["Upgrade"] != nil && r.Header["Upgrade"][0] == "websocket" { + if r.Header["Upgrade"] != nil && strings.ToLower(r.Header["Upgrade"][0]) == "websocket" { //Handle WebSocket request. Forward the custom Upgrade header and rewrite origin r.Header.Set("A-Upgrade", "websocket") wsRedirectionEndpoint := target.Domain @@ -86,6 +108,7 @@ func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, targ if target.RequireTLS { u, _ = url.Parse("wss://" + wsRedirectionEndpoint + r.URL.String()) } + h.logRequest(r, true, 101, "vdir-websocket") wspHandler := websocketproxy.NewProxy(u) wspHandler.ServeHTTP(w, r) return @@ -93,7 +116,34 @@ func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, targ r.Host = r.URL.Host err := target.Proxy.ServeHTTP(w, r) + var dnsError *net.DNSError if err != nil { - log.Println(err.Error()) + if errors.As(err, &dnsError) { + http.ServeFile(w, r, "./web/hosterror.html") + log.Println(err.Error()) + h.logRequest(r, false, 404, "vdir-http") + } else { + http.ServeFile(w, r, "./web/rperror.html") + log.Println(err.Error()) + h.logRequest(r, false, 521, "vdir-http") + } + } + h.logRequest(r, true, 200, "vdir-http") + +} + +func (h *ProxyHandler) logRequest(r *http.Request, succ bool, statusCode int, forwardType string) { + if h.Parent.Option.StatisticCollector != nil { + go func() { + requestInfo := statistic.RequestInfo{ + IpAddr: geodb.GetRequesterIP(r), + RequestOriginalCountryISOCode: h.Parent.Option.GeodbStore.GetRequesterCountryISOCode(r), + Succ: succ, + StatusCode: statusCode, + ForwardType: forwardType, + } + h.Parent.Option.StatisticCollector.RecordRequest(requestInfo) + }() + } } diff --git a/src/mod/dynamicproxy/redirection/handler.go b/src/mod/dynamicproxy/redirection/handler.go new file mode 100644 index 0000000..66a627d --- /dev/null +++ b/src/mod/dynamicproxy/redirection/handler.go @@ -0,0 +1,53 @@ +package redirection + +import ( + "log" + "net/http" + "strings" +) + +/* + handler.go + + This script store the handlers use for handling + redirection request +*/ + +//Check if a request URL is a redirectable URI +func (t *RuleTable) IsRedirectable(r *http.Request) bool { + requestPath := r.Host + r.URL.Path + rr := t.MatchRedirectRule(requestPath) + return rr != nil +} + +//Handle the redirect request, return after calling this function to prevent +//multiple write to the response writer +//Return the status code of the redirection handling +func (t *RuleTable) HandleRedirect(w http.ResponseWriter, r *http.Request) int { + requestPath := r.Host + r.URL.Path + rr := t.MatchRedirectRule(requestPath) + if rr != nil { + redirectTarget := rr.TargetURL + //Always pad a / at the back of the target URL + if redirectTarget[len(redirectTarget)-1:] != "/" { + redirectTarget += "/" + } + if rr.ForwardChildpath { + //Remove the first / in the path + redirectTarget += r.URL.Path[1:] + "?" + r.URL.RawQuery + } + + if !strings.HasPrefix(redirectTarget, "http://") && !strings.HasPrefix(redirectTarget, "https://") { + redirectTarget = "http://" + redirectTarget + } + + http.Redirect(w, r, redirectTarget, rr.StatusCode) + return rr.StatusCode + } else { + //Invalid usage + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("500 - Internal Server Error")) + log.Println("Target request URL do not have matching redirect rule. Check with IsRedirectable before calling HandleRedirect!") + return 500 + } +} diff --git a/src/mod/dynamicproxy/redirection/redirection.go b/src/mod/dynamicproxy/redirection/redirection.go new file mode 100644 index 0000000..4b164fd --- /dev/null +++ b/src/mod/dynamicproxy/redirection/redirection.go @@ -0,0 +1,162 @@ +package redirection + +import ( + "encoding/json" + "log" + "os" + "path" + "path/filepath" + "strings" + "sync" + + "imuslab.com/zoraxy/mod/utils" +) + +type RuleTable struct { + configPath string //The location where the redirection rules is stored + rules sync.Map //Store the redirection rules for this reverse proxy instance +} + +type RedirectRules struct { + RedirectURL string //The matching URL to redirect + TargetURL string //The destination redirection url + ForwardChildpath bool //Also redirect the pathname + StatusCode int //Status Code for redirection +} + +func NewRuleTable(configPath string) (*RuleTable, error) { + thisRuleTable := RuleTable{ + rules: sync.Map{}, + configPath: configPath, + } + //Load all the rules from the config path + if !utils.FileExists(configPath) { + os.MkdirAll(configPath, 0775) + } + + // Load all the *.json from the configPath + files, err := filepath.Glob(filepath.Join(configPath, "*.json")) + if err != nil { + return nil, err + } + + // Parse the json content into RedirectRules + var rules []*RedirectRules + for _, file := range files { + b, err := os.ReadFile(file) + if err != nil { + continue + } + + thisRule := RedirectRules{} + + err = json.Unmarshal(b, &thisRule) + if err != nil { + continue + } + + rules = append(rules, &thisRule) + } + + //Map the rules into the sync map + for _, rule := range rules { + log.Println("Redirection rule added: " + rule.RedirectURL + " -> " + rule.TargetURL) + thisRuleTable.rules.Store(rule.RedirectURL, rule) + } + + return &thisRuleTable, nil +} + +func (t *RuleTable) AddRedirectRule(redirectURL string, destURL string, forwardPathname bool, statusCode int) error { + // Create a new RedirectRules object with the given parameters + newRule := &RedirectRules{ + RedirectURL: redirectURL, + TargetURL: destURL, + ForwardChildpath: forwardPathname, + StatusCode: statusCode, + } + + // Convert the redirectURL to a valid filename by replacing "/" with "-" and "." with "_" + filename := strings.ReplaceAll(strings.ReplaceAll(redirectURL, "/", "-"), ".", "_") + ".json" + + // Create the full file path by joining the t.configPath with the filename + filepath := path.Join(t.configPath, filename) + + // Create a new file for writing the JSON data + file, err := os.Create(filepath) + if err != nil { + log.Printf("Error creating file %s: %s", filepath, err) + return err + } + defer file.Close() + + // Encode the RedirectRules object to JSON and write it to the file + err = json.NewEncoder(file).Encode(newRule) + if err != nil { + log.Printf("Error encoding JSON to file %s: %s", filepath, err) + return err + } + + // Store the RedirectRules object in the sync.Map + t.rules.Store(redirectURL, newRule) + + return nil +} + +func (t *RuleTable) DeleteRedirectRule(redirectURL string) error { + // Convert the redirectURL to a valid filename by replacing "/" with "-" and "." with "_" + filename := strings.ReplaceAll(strings.ReplaceAll(redirectURL, "/", "-"), ".", "_") + ".json" + + // Create the full file path by joining the t.configPath with the filename + filepath := path.Join(t.configPath, filename) + + // Check if the file exists + if _, err := os.Stat(filepath); os.IsNotExist(err) { + return nil // File doesn't exist, nothing to delete + } + + // Delete the file + if err := os.Remove(filepath); err != nil { + log.Printf("Error deleting file %s: %s", filepath, err) + return err + } + + // Delete the key-value pair from the sync.Map + t.rules.Delete(redirectURL) + + return nil +} + +// Get a list of all the redirection rules +func (t *RuleTable) GetAllRedirectRules() []*RedirectRules { + rules := []*RedirectRules{} + t.rules.Range(func(key, value interface{}) bool { + r, ok := value.(*RedirectRules) + if ok { + rules = append(rules, r) + } + return true + }) + return rules +} + +// Check if a given request URL matched any of the redirection rule +func (t *RuleTable) MatchRedirectRule(requestedURL string) *RedirectRules { + // Iterate through all the keys in the rules map + var targetRedirectionRule *RedirectRules = nil + var maxMatch int = 0 + + t.rules.Range(func(key interface{}, value interface{}) bool { + // Check if the requested URL starts with the key as a prefix + if strings.HasPrefix(requestedURL, key.(string)) { + // This request URL matched the domain + if len(key.(string)) > maxMatch { + maxMatch = len(key.(string)) + targetRedirectionRule = value.(*RedirectRules) + } + } + return true + }) + + return targetRedirectionRule +} diff --git a/mod/dynamicproxy/subdomain.go b/src/mod/dynamicproxy/subdomain.go similarity index 94% rename from mod/dynamicproxy/subdomain.go rename to src/mod/dynamicproxy/subdomain.go index 7682fbc..ef1873b 100644 --- a/mod/dynamicproxy/subdomain.go +++ b/src/mod/dynamicproxy/subdomain.go @@ -4,7 +4,7 @@ import ( "log" "net/url" - "imuslab.com/arozos/ReverseProxy/mod/reverseproxy" + "imuslab.com/zoraxy/mod/reverseproxy" ) /* diff --git a/src/mod/geodb/geodb.go b/src/mod/geodb/geodb.go new file mode 100644 index 0000000..e2f7fa2 --- /dev/null +++ b/src/mod/geodb/geodb.go @@ -0,0 +1,244 @@ +package geodb + +import ( + "net" + "net/http" + "strings" + + "github.com/oschwald/geoip2-golang" + "imuslab.com/zoraxy/mod/database" +) + +type Store struct { + Enabled bool + geodb *geoip2.Reader + sysdb *database.Database +} + +type CountryInfo struct { + CountryIsoCode string + ContinetCode string +} + +func NewGeoDb(sysdb *database.Database, dbfile string) (*Store, error) { + db, err := geoip2.Open(dbfile) + if err != nil { + return nil, err + } + + err = sysdb.NewTable("blacklist-cn") + if err != nil { + return nil, err + } + + err = sysdb.NewTable("blacklist-ip") + if err != nil { + return nil, err + } + + err = sysdb.NewTable("blacklist") + if err != nil { + return nil, err + } + + blacklistEnabled := false + sysdb.Read("blacklist", "enabled", &blacklistEnabled) + + return &Store{ + Enabled: blacklistEnabled, + geodb: db, + sysdb: sysdb, + }, nil +} + +func (s *Store) ToggleBlacklist(enabled bool) { + s.sysdb.Write("blacklist", "enabled", enabled) + s.Enabled = enabled +} + +func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) { + // If you are using strings that may be invalid, check that ip is not nil + ip := net.ParseIP(ipstring) + record, err := s.geodb.City(ip) + if err != nil { + return nil, err + } + return &CountryInfo{ + record.Country.IsoCode, + record.Continent.Code, + }, nil +} + +func (s *Store) Close() { + s.geodb.Close() +} + +func (s *Store) AddCountryCodeToBlackList(countryCode string) { + countryCode = strings.ToLower(countryCode) + s.sysdb.Write("blacklist-cn", countryCode, true) +} + +func (s *Store) RemoveCountryCodeFromBlackList(countryCode string) { + countryCode = strings.ToLower(countryCode) + s.sysdb.Delete("blacklist-cn", countryCode) +} + +func (s *Store) IsCountryCodeBlacklisted(countryCode string) bool { + countryCode = strings.ToLower(countryCode) + var isBlacklisted bool = false + s.sysdb.Read("blacklist-cn", countryCode, &isBlacklisted) + return isBlacklisted +} + +func (s *Store) GetAllBlacklistedCountryCode() []string { + bannedCountryCodes := []string{} + entries, err := s.sysdb.ListTable("blacklist-cn") + if err != nil { + return bannedCountryCodes + } + for _, keypairs := range entries { + ip := string(keypairs[0]) + bannedCountryCodes = append(bannedCountryCodes, ip) + } + + return bannedCountryCodes +} + +func (s *Store) AddIPToBlackList(ipAddr string) { + s.sysdb.Write("blacklist-ip", ipAddr, true) +} + +func (s *Store) RemoveIPFromBlackList(ipAddr string) { + s.sysdb.Delete("blacklist-ip", ipAddr) +} + +func (s *Store) IsIPBlacklisted(ipAddr string) bool { + var isBlacklisted bool = false + s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted) + if isBlacklisted { + return true + } + + //Check for IP wildcard and CIRD rules + AllBlacklistedIps := s.GetAllBlacklistedIp() + for _, blacklistRule := range AllBlacklistedIps { + wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule) + if wildcardMatch { + return true + } + + cidrMatch := MatchIpCIDR(ipAddr, blacklistRule) + if cidrMatch { + return true + } + } + + return false +} + +func (s *Store) GetAllBlacklistedIp() []string { + bannedIps := []string{} + entries, err := s.sysdb.ListTable("blacklist-ip") + if err != nil { + return bannedIps + } + + for _, keypairs := range entries { + ip := string(keypairs[0]) + bannedIps = append(bannedIps, ip) + } + + return bannedIps +} + +//Check if a IP address is blacklisted, in either country or IP blacklist +func (s *Store) IsBlacklisted(ipAddr string) bool { + if !s.Enabled { + //Blacklist not enabled. Always return false + return false + } + + if ipAddr == "" { + //Unable to get the target IP address + return false + } + + countryCode, err := s.ResolveCountryCodeFromIP(ipAddr) + if err != nil { + return false + } + + if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) { + return true + } + + if s.IsIPBlacklisted(ipAddr) { + return true + } + + return false +} + +func (s *Store) GetRequesterCountryISOCode(r *http.Request) string { + ipAddr := GetRequesterIP(r) + if ipAddr == "" { + return "" + } + countryCode, err := s.ResolveCountryCodeFromIP(ipAddr) + if err != nil { + return "" + } + + return countryCode.CountryIsoCode +} + +//Utilities function +func GetRequesterIP(r *http.Request) string { + ip := r.Header.Get("X-Forwarded-For") + if ip == "" { + ip = r.Header.Get("X-Real-IP") + if ip == "" { + ip = strings.Split(r.RemoteAddr, ":")[0] + } + } + return ip +} + +//Match the IP address with a wildcard string +func MatchIpWildcard(ipAddress, wildcard string) bool { + // Split IP address and wildcard into octets + ipOctets := strings.Split(ipAddress, ".") + wildcardOctets := strings.Split(wildcard, ".") + + // Check that both have 4 octets + if len(ipOctets) != 4 || len(wildcardOctets) != 4 { + return false + } + + // Check each octet to see if it matches the wildcard or is an exact match + for i := 0; i < 4; i++ { + if wildcardOctets[i] == "*" { + continue + } + if ipOctets[i] != wildcardOctets[i] { + return false + } + } + + return true +} + +//Match ip address with CIDR +func MatchIpCIDR(ip string, cidr string) bool { + // parse the CIDR string + _, cidrnet, err := net.ParseCIDR(cidr) + if err != nil { + return false + } + + // parse the IP address + ipAddr := net.ParseIP(ip) + + // check if the IP address is within the CIDR range + return cidrnet.Contains(ipAddr) +} diff --git a/mod/reverseproxy/LICENSE b/src/mod/reverseproxy/LICENSE similarity index 100% rename from mod/reverseproxy/LICENSE rename to src/mod/reverseproxy/LICENSE diff --git a/mod/reverseproxy/README.md b/src/mod/reverseproxy/README.md similarity index 100% rename from mod/reverseproxy/README.md rename to src/mod/reverseproxy/README.md diff --git a/mod/reverseproxy/reverse.go b/src/mod/reverseproxy/reverse.go similarity index 99% rename from mod/reverseproxy/reverse.go rename to src/mod/reverseproxy/reverse.go index 7d7e0db..678287e 100644 --- a/mod/reverseproxy/reverse.go +++ b/src/mod/reverseproxy/reverse.go @@ -276,7 +276,7 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro p.logf("http: proxy error: %v", err) } - rw.WriteHeader(http.StatusBadGateway) + //rw.WriteHeader(http.StatusBadGateway) return err } @@ -288,7 +288,7 @@ func (p *ReverseProxy) ProxyHTTP(rw http.ResponseWriter, req *http.Request) erro if p.Verbal { p.logf("http: proxy error: %v", err) } - rw.WriteHeader(http.StatusBadGateway) + //rw.WriteHeader(http.StatusBadGateway) return err } } diff --git a/src/mod/statistic/handler.go b/src/mod/statistic/handler.go new file mode 100644 index 0000000..57e084b --- /dev/null +++ b/src/mod/statistic/handler.go @@ -0,0 +1,76 @@ +package statistic + +import ( + "encoding/json" + "net/http" + + "imuslab.com/zoraxy/mod/utils" +) + +/* + Handler.go + + This script handles incoming request for loading the statistic of the day + +*/ + +func (c *Collector) HandleTodayStatLoad(w http.ResponseWriter, r *http.Request) { + type DailySummaryExport struct { + TotalRequest int64 //Total request of the day + ErrorRequest int64 //Invalid request of the day, including error or not found + ValidRequest int64 //Valid request of the day + + ForwardTypes map[string]int + RequestOrigin map[string]int + RequestClientIp map[string]int + } + + fast, err := utils.GetPara(r, "fast") + if err != nil { + fast = "false" + } + d := c.DailySummary + if fast == "true" { + //Only return the counter + exported := DailySummaryExport{ + TotalRequest: d.TotalRequest, + ErrorRequest: d.ErrorRequest, + ValidRequest: d.ValidRequest, + } + js, _ := json.Marshal(exported) + utils.SendJSONResponse(w, string(js)) + } else { + //Return everything + exported := DailySummaryExport{ + TotalRequest: d.TotalRequest, + ErrorRequest: d.ErrorRequest, + ValidRequest: d.ValidRequest, + ForwardTypes: make(map[string]int), + RequestOrigin: make(map[string]int), + RequestClientIp: make(map[string]int), + } + + // Export ForwardTypes sync.Map + d.ForwardTypes.Range(func(key, value interface{}) bool { + exported.ForwardTypes[key.(string)] = value.(int) + return true + }) + + // Export RequestOrigin sync.Map + d.RequestOrigin.Range(func(key, value interface{}) bool { + exported.RequestOrigin[key.(string)] = value.(int) + return true + }) + + // Export RequestClientIp sync.Map + d.RequestClientIp.Range(func(key, value interface{}) bool { + exported.RequestClientIp[key.(string)] = value.(int) + return true + }) + + js, _ := json.Marshal(exported) + + utils.SendJSONResponse(w, string(js)) + } + +} diff --git a/src/mod/statistic/statistic.go b/src/mod/statistic/statistic.go new file mode 100644 index 0000000..ed11968 --- /dev/null +++ b/src/mod/statistic/statistic.go @@ -0,0 +1,184 @@ +package statistic + +import ( + "strings" + "sync" + "time" + + "imuslab.com/zoraxy/mod/database" +) + +/* + Statistic Package + + This packet is designed to collection information + and store them for future analysis +*/ + +//Faststat, a interval summary for all collected data and avoid +//looping through every data everytime a overview is needed +type DailySummary struct { + TotalRequest int64 //Total request of the day + ErrorRequest int64 //Invalid request of the day, including error or not found + ValidRequest int64 //Valid request of the day + //Type counters + ForwardTypes *sync.Map //Map that hold the forward types + RequestOrigin *sync.Map //Map that hold [country ISO code]: visitor counter + RequestClientIp *sync.Map //Map that hold all unique request IPs +} + +type RequestInfo struct { + IpAddr string + RequestOriginalCountryISOCode string + Succ bool + StatusCode int + ForwardType string +} + +type CollectorOption struct { + Database *database.Database +} + +type Collector struct { + rtdataStopChan chan bool + DailySummary *DailySummary + Option *CollectorOption +} + +func NewStatisticCollector(option CollectorOption) (*Collector, error) { + option.Database.NewTable("stats") + + //Create the collector object + thisCollector := Collector{ + DailySummary: newDailySummary(), + Option: &option, + } + + //Load the stat if exists for today + //This will exists if the program was forcefully restarted + year, month, day := time.Now().Date() + summary := thisCollector.LoadSummaryOfDay(year, month, day) + if summary != nil { + thisCollector.DailySummary = summary + } + + //Schedule the realtime statistic clearing at midnight everyday + rtstatStopChan := thisCollector.ScheduleResetRealtimeStats() + thisCollector.rtdataStopChan = rtstatStopChan + + return &thisCollector, nil +} + +//Write the current in-memory summary to database file +func (c *Collector) SaveSummaryOfDay() { + //When it is called in 0:00am, make sure it is stored as yesterday key + t := time.Now().Add(-30 * time.Second) + summaryKey := t.Format("02_01_2006") + c.Option.Database.Write("stats", summaryKey, c.DailySummary) +} + +//Load the summary of a day given +func (c *Collector) LoadSummaryOfDay(year int, month time.Month, day int) *DailySummary { + date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local) + summaryKey := date.Format("02_01_2006") + var targetSummary = newDailySummary() + c.Option.Database.Read("stats", summaryKey, &targetSummary) + return targetSummary +} + +//This function gives the current slot in the 288- 5 minutes interval of the day +func (c *Collector) GetCurrentRealtimeStatIntervalId() int { + now := time.Now() + startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix() + secondsSinceStartOfDay := now.Unix() - startOfDay + interval := secondsSinceStartOfDay / (5 * 60) + return int(interval) +} + +func (c *Collector) Close() { + //Stop the ticker + c.rtdataStopChan <- true + + //Write the buffered data into database + c.SaveSummaryOfDay() + +} + +//Main function to record all the inbound traffics +//Note that this function run in go routine and might have concurrent R/W issue +//Please make sure there is no racing paramters in this function +func (c *Collector) RecordRequest(ri RequestInfo) { + go func() { + c.DailySummary.TotalRequest++ + if ri.Succ { + c.DailySummary.ValidRequest++ + } else { + c.DailySummary.ErrorRequest++ + } + + //Store the request info into correct types of maps + ft, ok := c.DailySummary.ForwardTypes.Load(ri.ForwardType) + if !ok { + c.DailySummary.ForwardTypes.Store(ri.ForwardType, 1) + } else { + c.DailySummary.ForwardTypes.Store(ri.ForwardType, ft.(int)+1) + } + + originISO := strings.ToLower(ri.RequestOriginalCountryISOCode) + fo, ok := c.DailySummary.RequestOrigin.Load(originISO) + if !ok { + c.DailySummary.RequestOrigin.Store(originISO, 1) + } else { + c.DailySummary.RequestOrigin.Store(originISO, fo.(int)+1) + } + + fi, ok := c.DailySummary.RequestClientIp.Load(ri.IpAddr) + if !ok { + c.DailySummary.RequestClientIp.Store(ri.IpAddr, 1) + } else { + c.DailySummary.RequestClientIp.Store(ri.IpAddr, fi.(int)+1) + } + }() +} + +//nightly task +func (c *Collector) ScheduleResetRealtimeStats() chan bool { + doneCh := make(chan bool) + + go func() { + defer close(doneCh) + + for { + // calculate duration until next midnight + now := time.Now() + + // Get midnight of the next day in the local time zone + midnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location()) + + // Calculate the duration until midnight + duration := midnight.Sub(now) + select { + case <-time.After(duration): + // store daily summary to database and reset summary + c.SaveSummaryOfDay() + c.DailySummary = newDailySummary() + case <-doneCh: + // stop the routine + return + } + } + }() + + return doneCh +} + +func newDailySummary() *DailySummary { + return &DailySummary{ + TotalRequest: 0, + ErrorRequest: 0, + ValidRequest: 0, + ForwardTypes: &sync.Map{}, + RequestOrigin: &sync.Map{}, + RequestClientIp: &sync.Map{}, + } +} diff --git a/src/mod/tlscert/helper.go b/src/mod/tlscert/helper.go new file mode 100644 index 0000000..10c52da --- /dev/null +++ b/src/mod/tlscert/helper.go @@ -0,0 +1,60 @@ +package tlscert + +import ( + "path/filepath" + "strings" +) + +//This remove the certificates in the list where either the +//public key or the private key is missing +func getCertPairs(certFiles []string) []string { + crtMap := make(map[string]bool) + keyMap := make(map[string]bool) + + for _, filename := range certFiles { + if filepath.Ext(filename) == ".crt" { + crtMap[strings.TrimSuffix(filename, ".crt")] = true + } else if filepath.Ext(filename) == ".key" { + keyMap[strings.TrimSuffix(filename, ".key")] = true + } + } + + var result []string + for domain := range crtMap { + if keyMap[domain] { + result = append(result, domain) + } + } + + return result +} + +//Get the cloest subdomain certificate from a list of domains +func matchClosestDomainCertificate(subdomain string, domains []string) string { + var matchingDomain string = "" + maxLength := 0 + + for _, domain := range domains { + if strings.HasSuffix(subdomain, "."+domain) && len(domain) > maxLength { + matchingDomain = domain + maxLength = len(domain) + } + } + + return matchingDomain +} + +//Check if a requesting domain is a subdomain of a given domain +func isSubdomain(subdomain, domain string) bool { + subdomainParts := strings.Split(subdomain, ".") + domainParts := strings.Split(domain, ".") + if len(subdomainParts) < len(domainParts) { + return false + } + for i := range domainParts { + if subdomainParts[len(subdomainParts)-1-i] != domainParts[len(domainParts)-1-i] { + return false + } + } + return true +} diff --git a/src/mod/tlscert/tlscert.go b/src/mod/tlscert/tlscert.go new file mode 100644 index 0000000..3a57041 --- /dev/null +++ b/src/mod/tlscert/tlscert.go @@ -0,0 +1,172 @@ +package tlscert + +import ( + "crypto/tls" + "crypto/x509" + "encoding/pem" + "io" + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" + + "imuslab.com/zoraxy/mod/utils" +) + +type Manager struct { + CertStore string + verbal bool +} + +func NewManager(certStore string) (*Manager, error) { + if !utils.FileExists(certStore) { + os.MkdirAll(certStore, 0775) + } + + thisManager := Manager{ + CertStore: certStore, + verbal: true, + } + + return &thisManager, nil +} + +func (m *Manager) ListCertDomains() ([]string, error) { + filenames, err := m.ListCerts() + if err != nil { + return []string{}, err + } + + //Remove certificates where there are missing public key or private key + filenames = getCertPairs(filenames) + + return filenames, nil +} + +func (m *Manager) ListCerts() ([]string, error) { + certs, err := ioutil.ReadDir(m.CertStore) + if err != nil { + return []string{}, err + } + + filenames := make([]string, 0, len(certs)) + for _, cert := range certs { + if !cert.IsDir() { + filenames = append(filenames, cert.Name()) + } + } + + return filenames, nil +} + +func (m *Manager) GetCert(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, error) { + //Check if the domain corrisponding cert exists + pubKey := "./system/localhost.crt" + priKey := "./system/localhost.key" + + if utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".crt")) && utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".key")) { + pubKey = filepath.Join(m.CertStore, helloInfo.ServerName+".crt") + priKey = filepath.Join(m.CertStore, helloInfo.ServerName+".key") + + } else { + domainCerts, _ := m.ListCertDomains() + cloestDomainCert := matchClosestDomainCertificate(helloInfo.ServerName, domainCerts) + if cloestDomainCert != "" { + //There is a matching parent domain for this subdomain. Use this instead. + pubKey = filepath.Join(m.CertStore, cloestDomainCert+".crt") + priKey = filepath.Join(m.CertStore, cloestDomainCert+".key") + } else if m.DefaultCertExists() { + //Use default.crt and default.key + pubKey = filepath.Join(m.CertStore, "default.crt") + priKey = filepath.Join(m.CertStore, "default.key") + if m.verbal { + log.Println("No matching certificate found. Serving with default") + } + } else { + if m.verbal { + log.Println("Matching certificate not found. Serving with default. Requesting server name: ", helloInfo.ServerName) + } + } + } + + //Load the cert and serve it + cer, err := tls.LoadX509KeyPair(pubKey, priKey) + if err != nil { + log.Println(err) + return nil, nil + } + + return &cer, nil +} + +//Check if both the default cert public key and private key exists +func (m *Manager) DefaultCertExists() bool { + return utils.FileExists(filepath.Join(m.CertStore, "default.crt")) && utils.FileExists(filepath.Join(m.CertStore, "default.key")) +} + +//Check if the default cert exists returning seperate results for pubkey and prikey +func (m *Manager) DefaultCertExistsSep() (bool, bool) { + return utils.FileExists(filepath.Join(m.CertStore, "default.crt")), utils.FileExists(filepath.Join(m.CertStore, "default.key")) +} + +//Delete the cert if exists +func (m *Manager) RemoveCert(domain string) error { + pubKey := filepath.Join(m.CertStore, domain+".crt") + priKey := filepath.Join(m.CertStore, domain+".key") + if utils.FileExists(pubKey) { + err := os.Remove(pubKey) + if err != nil { + return err + } + } + + if utils.FileExists(priKey) { + err := os.Remove(priKey) + if err != nil { + return err + } + } + + return nil +} + +//Check if the given file is a valid TLS file +func IsValidTLSFile(file io.Reader) bool { + // Read the contents of the uploaded file + contents, err := io.ReadAll(file) + if err != nil { + // Handle the error + return false + } + + // Parse the contents of the file as a PEM-encoded certificate or key + block, _ := pem.Decode(contents) + if block == nil { + // The file is not a valid PEM-encoded certificate or key + return false + } + + // Parse the certificate or key + if strings.Contains(block.Type, "CERTIFICATE") { + // The file contains a certificate + cert, err := x509.ParseCertificate(block.Bytes) + if err != nil { + // Handle the error + return false + } + // Check if the certificate is a valid TLS/SSL certificate + return cert.IsCA == false && cert.KeyUsage&x509.KeyUsageDigitalSignature != 0 && cert.KeyUsage&x509.KeyUsageKeyEncipherment != 0 + } else if strings.Contains(block.Type, "PRIVATE KEY") { + // The file contains a private key + _, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + // Handle the error + return false + } + return true + } else { + return false + } + +} diff --git a/src/mod/upnp/upnp.go b/src/mod/upnp/upnp.go new file mode 100644 index 0000000..686ef4c --- /dev/null +++ b/src/mod/upnp/upnp.go @@ -0,0 +1,127 @@ +package upnp + +import ( + "errors" + "log" + "sync" + "time" + + "gitlab.com/NebulousLabs/go-upnp" +) + +/* + uPNP Module + + This module handles uPNP Connections to the gateway router and create a port forward entry + for the host system at the given port (set with -port paramter) +*/ + +type UPnPClient struct { + Connection *upnp.IGD //UPnP conenction object + ExternalIP string //Storage of external IP address + RequiredPorts []int //All the required ports will be recored + PolicyNames sync.Map //Name for the required port nubmer +} + +func NewUPNPClient() (*UPnPClient, error) { + //Create uPNP forwarding in the NAT router + log.Println("Discovering UPnP router in Local Area Network...") + d, err := upnp.Discover() + if err != nil { + return &UPnPClient{}, err + } + + // discover external IP + ip, err := d.ExternalIP() + if err != nil { + return &UPnPClient{}, err + } + + //Create the final obejcts + newUPnPObject := &UPnPClient{ + Connection: d, + ExternalIP: ip, + RequiredPorts: []int{}, + } + + return newUPnPObject, nil +} + +func (u *UPnPClient) ForwardPort(portNumber int, ruleName string) error { + log.Println("UPnP forwarding new port: ", portNumber, "for "+ruleName+" service") + + //Check if port already forwarded + _, ok := u.PolicyNames.Load(portNumber) + if ok { + //Port already forward. Ignore this request + return errors.New("Port already forwarded") + } + + // forward a port + err := u.Connection.Forward(uint16(portNumber), ruleName) + if err != nil { + return err + } + + u.RequiredPorts = append(u.RequiredPorts, portNumber) + u.PolicyNames.Store(portNumber, ruleName) + return nil +} + +func (u *UPnPClient) ClosePort(portNumber int) error { + //Check if port is opened + portOpened := false + newRequiredPort := []int{} + for _, thisPort := range u.RequiredPorts { + if thisPort != portNumber { + newRequiredPort = append(newRequiredPort, thisPort) + } else { + portOpened = true + } + } + + if portOpened { + //Update the port list + u.RequiredPorts = newRequiredPort + + // Close the port + log.Println("Closing UPnP Port Forward: ", portNumber) + err := u.Connection.Clear(uint16(portNumber)) + + //Delete the name registry + u.PolicyNames.Delete(portNumber) + + if err != nil { + log.Println(err) + return err + } + } + return nil +} + +// Renew forward rules, prevent router lease time from flushing the Upnp config +func (u *UPnPClient) RenewForwardRules() { + portsToRenew := u.RequiredPorts + for _, thisPort := range portsToRenew { + ruleName, ok := u.PolicyNames.Load(thisPort) + if !ok { + continue + } + u.ClosePort(thisPort) + time.Sleep(100 * time.Millisecond) + u.ForwardPort(thisPort, ruleName.(string)) + } + log.Println("UPnP Port Forward rule renew completed") +} + +func (u *UPnPClient) Close() { + //Shutdown the default UPnP Object + if u != nil { + for _, portNumber := range u.RequiredPorts { + err := u.Connection.Clear(uint16(portNumber)) + if err != nil { + log.Println(err) + } + } + } +} diff --git a/src/mod/utils/conv.go b/src/mod/utils/conv.go new file mode 100644 index 0000000..9256b11 --- /dev/null +++ b/src/mod/utils/conv.go @@ -0,0 +1,16 @@ +package utils + +import "strconv" + +func StringToInt64(number string) (int64, error) { + i, err := strconv.ParseInt(number, 10, 64) + if err != nil { + return -1, err + } + return i, nil +} + +func Int64ToString(number int64) string { + convedNumber := strconv.FormatInt(number, 10) + return convedNumber +} diff --git a/src/mod/utils/template.go b/src/mod/utils/template.go new file mode 100644 index 0000000..e5772a8 --- /dev/null +++ b/src/mod/utils/template.go @@ -0,0 +1,19 @@ +package utils + +import ( + "net/http" +) + +/* + Web Template Generator + + This is the main system core module that perform function similar to what PHP did. + To replace part of the content of any file, use {{paramter}} to replace it. + + +*/ + +func SendHTMLResponse(w http.ResponseWriter, msg string) { + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(msg)) +} diff --git a/src/mod/utils/utils.go b/src/mod/utils/utils.go new file mode 100644 index 0000000..077524e --- /dev/null +++ b/src/mod/utils/utils.go @@ -0,0 +1,230 @@ +package utils + +import ( + "archive/tar" + "bufio" + "compress/gzip" + "encoding/base64" + "errors" + "io" + "log" + "net/http" + "os" + "path/filepath" + "strings" + "time" +) + +/* + Common + + Some commonly used functions in ArozOS + +*/ + +// Response related +func SendTextResponse(w http.ResponseWriter, msg string) { + w.Write([]byte(msg)) +} + +// Send JSON response, with an extra json header +func SendJSONResponse(w http.ResponseWriter, json string) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(json)) +} + +func SendErrorResponse(w http.ResponseWriter, errMsg string) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("{\"error\":\"" + errMsg + "\"}")) +} + +func SendOK(w http.ResponseWriter) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("\"OK\"")) +} + +/* + The paramter move function (mv) + + You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in + r (HTTP Request Object) + getParamter (string, aka $_GET['This string]) + + Will return + Paramter string (if any) + Error (if error) + +*/ +/* +func Mv(r *http.Request, getParamter string, postMode bool) (string, error) { + if postMode == false { + //Access the paramter via GET + keys, ok := r.URL.Query()[getParamter] + + if !ok || len(keys[0]) < 1 { + //log.Println("Url Param " + getParamter +" is missing") + return "", errors.New("GET paramter " + getParamter + " not found or it is empty") + } + + // Query()["key"] will return an array of items, + // we only want the single item. + key := keys[0] + return string(key), nil + } else { + //Access the parameter via POST + r.ParseForm() + x := r.Form.Get(getParamter) + if len(x) == 0 || x == "" { + return "", errors.New("POST paramter " + getParamter + " not found or it is empty") + } + return string(x), nil + } + +} +*/ + +// Get GET parameter +func GetPara(r *http.Request, key string) (string, error) { + keys, ok := r.URL.Query()[key] + if !ok || len(keys[0]) < 1 { + return "", errors.New("invalid " + key + " given") + } else { + return keys[0], nil + } +} + +// Get POST paramter +func PostPara(r *http.Request, key string) (string, error) { + r.ParseForm() + x := r.Form.Get(key) + if x == "" { + return "", errors.New("invalid " + key + " given") + } else { + return x, nil + } +} + +func FileExists(filename string) bool { + _, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return true +} + +func IsDir(path string) bool { + if FileExists(path) == false { + return false + } + fi, err := os.Stat(path) + if err != nil { + log.Fatal(err) + return false + } + switch mode := fi.Mode(); { + case mode.IsDir(): + return true + case mode.IsRegular(): + return false + } + return false +} + +func TimeToString(targetTime time.Time) string { + return targetTime.Format("2006-01-02 15:04:05") +} + +func LoadImageAsBase64(filepath string) (string, error) { + if !FileExists(filepath) { + return "", errors.New("File not exists") + } + f, _ := os.Open(filepath) + reader := bufio.NewReader(f) + content, _ := io.ReadAll(reader) + encoded := base64.StdEncoding.EncodeToString(content) + return string(encoded), nil +} + +// Use for redirections +func ConstructRelativePathFromRequestURL(requestURI string, redirectionLocation string) string { + if strings.Count(requestURI, "/") == 1 { + //Already root level + return redirectionLocation + } + for i := 0; i < strings.Count(requestURI, "/")-1; i++ { + redirectionLocation = "../" + redirectionLocation + } + + return redirectionLocation +} + +// Check if given string in a given slice +func StringInArray(arr []string, str string) bool { + for _, a := range arr { + if a == str { + return true + } + } + return false +} + +func StringInArrayIgnoreCase(arr []string, str string) bool { + smallArray := []string{} + for _, item := range arr { + smallArray = append(smallArray, strings.ToLower(item)) + } + + return StringInArray(smallArray, strings.ToLower(str)) +} + +func ExtractTarGzipByStream(basedir string, gzipStream io.Reader, onErrorResumeNext bool) error { + uncompressedStream, err := gzip.NewReader(gzipStream) + if err != nil { + return err + } + + tarReader := tar.NewReader(uncompressedStream) + + for { + header, err := tarReader.Next() + + if err == io.EOF { + break + } + + if err != nil { + return err + } + + switch header.Typeflag { + case tar.TypeDir: + err := os.Mkdir(header.Name, 0755) + if err != nil { + if !onErrorResumeNext { + return err + } + + } + case tar.TypeReg: + outFile, err := os.Create(filepath.Join(basedir, header.Name)) + if err != nil { + if !onErrorResumeNext { + return err + } + } + _, err = io.Copy(outFile, tarReader) + if err != nil { + if !onErrorResumeNext { + return err + } + } + outFile.Close() + + default: + //Unknown filetype, continue + + } + + } + return nil +} diff --git a/mod/websocketproxy/LICENSE.md b/src/mod/websocketproxy/LICENSE.md similarity index 100% rename from mod/websocketproxy/LICENSE.md rename to src/mod/websocketproxy/LICENSE.md diff --git a/mod/websocketproxy/README.md b/src/mod/websocketproxy/README.md similarity index 100% rename from mod/websocketproxy/README.md rename to src/mod/websocketproxy/README.md diff --git a/mod/websocketproxy/websocketproxy.go b/src/mod/websocketproxy/websocketproxy.go similarity index 100% rename from mod/websocketproxy/websocketproxy.go rename to src/mod/websocketproxy/websocketproxy.go diff --git a/mod/websocketproxy/websocketproxy_test.go b/src/mod/websocketproxy/websocketproxy_test.go similarity index 100% rename from mod/websocketproxy/websocketproxy_test.go rename to src/mod/websocketproxy/websocketproxy_test.go diff --git a/src/redirect.go b/src/redirect.go new file mode 100644 index 0000000..e8b7222 --- /dev/null +++ b/src/redirect.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/json" + "net/http" + "strconv" + + "imuslab.com/zoraxy/mod/utils" +) + +/* + Redirect.go + + This script handle all the http handlers + related to redirection function in the reverse proxy +*/ + +func handleListRedirectionRules(w http.ResponseWriter, r *http.Request) { + rules := redirectTable.GetAllRedirectRules() + js, _ := json.Marshal(rules) + utils.SendJSONResponse(w, string(js)) +} + +func handleAddRedirectionRule(w http.ResponseWriter, r *http.Request) { + redirectUrl, err := utils.PostPara(r, "redirectUrl") + if err != nil { + utils.SendErrorResponse(w, "redirect url cannot be empty") + return + } + destUrl, err := utils.PostPara(r, "destUrl") + if err != nil { + utils.SendErrorResponse(w, "destination url cannot be empty") + } + + forwardChildpath, err := utils.PostPara(r, "forwardChildpath") + if err != nil { + //Assume true + forwardChildpath = "true" + } + + redirectTypeString, err := utils.PostPara(r, "redirectType") + if err != nil { + redirectTypeString = "307" + } + + redirectionStatusCode, err := strconv.Atoi(redirectTypeString) + if err != nil { + utils.SendErrorResponse(w, "invalid status code number") + return + } + + err = redirectTable.AddRedirectRule(redirectUrl, destUrl, forwardChildpath == "true", redirectionStatusCode) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + utils.SendOK(w) +} + +func handleDeleteRedirectionRule(w http.ResponseWriter, r *http.Request) { + redirectUrl, err := utils.PostPara(r, "redirectUrl") + if err != nil { + utils.SendErrorResponse(w, "redirect url cannot be empty") + return + } + + err = redirectTable.DeleteRedirectRule(redirectUrl) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + utils.SendOK(w) +} diff --git a/src/reverseproxy.go b/src/reverseproxy.go new file mode 100644 index 0000000..3df14a6 --- /dev/null +++ b/src/reverseproxy.go @@ -0,0 +1,314 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "path/filepath" + "sort" + "strconv" + "strings" + "time" + + "imuslab.com/zoraxy/mod/dynamicproxy" + "imuslab.com/zoraxy/mod/utils" +) + +var ( + dynamicProxyRouter *dynamicproxy.Router +) + +// Add user customizable reverse proxy +func ReverseProxtInit() { + inboundPort := 80 + if sysdb.KeyExists("settings", "inbound") { + sysdb.Read("settings", "inbound", &inboundPort) + log.Println("Serving inbound port ", inboundPort) + } else { + log.Println("Inbound port not set. Using default (80)") + } + + useTls := false + sysdb.Read("settings", "usetls", &useTls) + if useTls { + log.Println("TLS mode enabled. Serving proxxy request with TLS") + } else { + log.Println("TLS mode disabled. Serving proxy request with plain http") + } + + forceHttpsRedirect := false + sysdb.Read("settings", "redirect", &forceHttpsRedirect) + if forceHttpsRedirect { + log.Println("Force HTTPS mode enabled") + } else { + log.Println("Force HTTPS mode disabled") + } + + dprouter, err := dynamicproxy.NewDynamicProxy(dynamicproxy.RouterOption{ + Port: inboundPort, + UseTls: useTls, + ForceHttpsRedirect: forceHttpsRedirect, + TlsManager: tlsCertManager, + RedirectRuleTable: redirectTable, + GeodbStore: geodbStore, + StatisticCollector: statisticCollector, + }) + if err != nil { + log.Println(err.Error()) + return + } + + dynamicProxyRouter = dprouter + + //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 + //Not sure why but delay must be added if you have another + //reverse proxy server in front of this service + time.Sleep(300 * time.Millisecond) + dynamicProxyRouter.StartProxyService() + log.Println("Dynamic Reverse Proxy service started") + +} + +func ReverseProxyHandleOnOff(w http.ResponseWriter, r *http.Request) { + + enable, _ := utils.PostPara(r, "enable") //Support root, vdir and subd + if enable == "true" { + err := dynamicProxyRouter.StartProxyService() + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + } else { + //Check if it is loopback + if dynamicProxyRouter.IsProxiedSubdomain(r) { + //Loopback routing. Turning it off will make the user lost control + //of the whole system. Do not allow shutdown + utils.SendErrorResponse(w, "Unable to shutdown in loopback rp mode. Remove proxy rules for management interface and retry.") + return + } + + err := dynamicProxyRouter.StopProxyService() + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + } + + utils.SendOK(w) +} + +func ReverseProxyHandleAddEndpoint(w http.ResponseWriter, r *http.Request) { + eptype, err := utils.PostPara(r, "type") //Support root, vdir and subd + if err != nil { + utils.SendErrorResponse(w, "type not defined") + return + } + + endpoint, err := utils.PostPara(r, "ep") + if err != nil { + utils.SendErrorResponse(w, "endpoint not defined") + return + } + + tls, _ := utils.PostPara(r, "tls") + if tls == "" { + tls = "false" + } + + useTLS := (tls == "true") + rootname := "" + if eptype == "vdir" { + vdir, err := utils.PostPara(r, "rootname") + if err != nil { + utils.SendErrorResponse(w, "vdir not defined") + return + } + + if !strings.HasPrefix(vdir, "/") { + vdir = "/" + vdir + } + rootname = vdir + dynamicProxyRouter.AddVirtualDirectoryProxyService(vdir, endpoint, useTLS) + + } else if eptype == "subd" { + subdomain, err := utils.PostPara(r, "rootname") + if err != nil { + utils.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 + utils.SendErrorResponse(w, "Invalid endpoint type") + return + } + + //Save it + SaveReverseProxyConfig(eptype, rootname, endpoint, useTLS) + + utils.SendOK(w) + +} + +func DeleteProxyEndpoint(w http.ResponseWriter, r *http.Request) { + ep, err := utils.GetPara(r, "ep") + if err != nil { + utils.SendErrorResponse(w, "Invalid ep given") + } + + ptype, err := utils.PostPara(r, "ptype") + if err != nil { + utils.SendErrorResponse(w, "Invalid ptype given") + } + + err = dynamicProxyRouter.RemoveProxy(ptype, ep) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + } + + RemoveReverseProxyConfig(ep) + utils.SendOK(w) +} + +func ReverseProxyStatus(w http.ResponseWriter, r *http.Request) { + js, _ := json.Marshal(dynamicProxyRouter) + utils.SendJSONResponse(w, string(js)) +} + +func ReverseProxyList(w http.ResponseWriter, r *http.Request) { + eptype, err := utils.PostPara(r, "type") //Support root, vdir and subd + if err != nil { + utils.SendErrorResponse(w, "type not defined") + return + } + + if eptype == "vdir" { + results := []*dynamicproxy.ProxyEndpoint{} + dynamicProxyRouter.ProxyEndpoints.Range(func(key, value interface{}) bool { + results = append(results, value.(*dynamicproxy.ProxyEndpoint)) + return true + }) + + sort.Slice(results, func(i, j int) bool { + return results[i].Domain < results[j].Domain + }) + + js, _ := json.Marshal(results) + utils.SendJSONResponse(w, string(js)) + } else if eptype == "subd" { + results := []*dynamicproxy.SubdomainEndpoint{} + dynamicProxyRouter.SubdomainEndpoint.Range(func(key, value interface{}) bool { + results = append(results, value.(*dynamicproxy.SubdomainEndpoint)) + return true + }) + + sort.Slice(results, func(i, j int) bool { + return results[i].MatchingDomain < results[j].MatchingDomain + }) + + js, _ := json.Marshal(results) + utils.SendJSONResponse(w, string(js)) + } else if eptype == "root" { + js, _ := json.Marshal(dynamicProxyRouter.Root) + utils.SendJSONResponse(w, string(js)) + } else { + utils.SendErrorResponse(w, "Invalid type given") + } +} + +// Handle https redirect +func HandleUpdateHttpsRedirect(w http.ResponseWriter, r *http.Request) { + useRedirect, err := utils.GetPara(r, "set") + if err != nil { + currentRedirectToHttps := false + //Load the current status + err = sysdb.Read("settings", "redirect", ¤tRedirectToHttps) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + js, _ := json.Marshal(currentRedirectToHttps) + utils.SendJSONResponse(w, string(js)) + } else { + if useRedirect == "true" { + sysdb.Write("settings", "redirect", true) + log.Println("Updating force HTTPS redirection to true") + dynamicProxyRouter.UpdateHttpToHttpsRedirectSetting(true) + } else if useRedirect == "false" { + sysdb.Write("settings", "redirect", false) + log.Println("Updating force HTTPS redirection to false") + dynamicProxyRouter.UpdateHttpToHttpsRedirectSetting(false) + } + + utils.SendOK(w) + } +} + +//Handle checking if the current user is accessing via the reverse proxied interface +//Of the management interface. +func HandleManagementProxyCheck(w http.ResponseWriter, r *http.Request) { + isProxied := dynamicProxyRouter.IsProxiedSubdomain(r) + js, _ := json.Marshal(isProxied) + utils.SendJSONResponse(w, string(js)) +} + +// Handle incoming port set. Change the current proxy incoming port +func HandleIncomingPortSet(w http.ResponseWriter, r *http.Request) { + newIncomingPort, err := utils.PostPara(r, "incoming") + if err != nil { + utils.SendErrorResponse(w, "invalid incoming port given") + return + } + + newIncomingPortInt, err := strconv.Atoi(newIncomingPort) + if err != nil { + utils.SendErrorResponse(w, "invalid incoming port given") + return + } + + //Stop and change the setting of the reverse proxy service + if dynamicProxyRouter.Running { + dynamicProxyRouter.StopProxyService() + dynamicProxyRouter.Option.Port = newIncomingPortInt + dynamicProxyRouter.StartProxyService() + } else { + //Only change setting but not starting the proxy service + dynamicProxyRouter.Option.Port = newIncomingPortInt + } + + sysdb.Write("settings", "inbound", newIncomingPortInt) + + utils.SendOK(w) +} diff --git a/src/router.go b/src/router.go new file mode 100644 index 0000000..e8b7a31 --- /dev/null +++ b/src/router.go @@ -0,0 +1,32 @@ +package main + +import ( + "net/http" + "strings" +) + +/* + router.go + + This script holds the static resources router + for the reverse proxy service +*/ + +func AuthFsHandler(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Allow access to /script/*, /img/pubic/* and /login.html without authentication + if strings.HasPrefix(r.URL.Path, "/script/") || strings.HasPrefix(r.URL.Path, "/img/public/") || r.URL.Path == "/login.html" || r.URL.Path == "/favicon.png" { + handler.ServeHTTP(w, r) + return + } + + // check authentication + if !authAgent.CheckAuth(r) { + http.Redirect(w, r, "/login.html", http.StatusTemporaryRedirect) + return + } + + //Authenticated + handler.ServeHTTP(w, r) + }) +} diff --git a/src/system/GeoLite2-Country.mmdb b/src/system/GeoLite2-Country.mmdb new file mode 100644 index 0000000..b4e4159 Binary files /dev/null and b/src/system/GeoLite2-Country.mmdb differ diff --git a/src/system/gomod-license.csv b/src/system/gomod-license.csv new file mode 100644 index 0000000..dafa288 --- /dev/null +++ b/src/system/gomod-license.csv @@ -0,0 +1,17 @@ +github.com/boltdb/bolt,https://github.com/boltdb/bolt/blob/v1.3.1/LICENSE,MIT +github.com/gorilla/securecookie,https://github.com/gorilla/securecookie/blob/v1.1.1/LICENSE,BSD-3-Clause +github.com/gorilla/sessions,https://github.com/gorilla/sessions/blob/v1.2.1/LICENSE,BSD-3-Clause +github.com/gorilla/websocket,https://github.com/gorilla/websocket/blob/v1.4.2/LICENSE,BSD-2-Clause +github.com/oschwald/geoip2-golang,https://github.com/oschwald/geoip2-golang/blob/v1.8.0/LICENSE,ISC +github.com/oschwald/maxminddb-golang,https://github.com/oschwald/maxminddb-golang/blob/v1.10.0/LICENSE,ISC +gitlab.com/NebulousLabs/fastrand,https://gitlab.com/NebulousLabs/fastrand/blob/603482d69e40/LICENSE,MIT +gitlab.com/NebulousLabs/go-upnp,https://gitlab.com/NebulousLabs/go-upnp/blob/11da932010b6/LICENSE,MIT +gitlab.com/NebulousLabs/go-upnp/goupnp,https://gitlab.com/NebulousLabs/go-upnp/blob/11da932010b6/goupnp\LICENSE,BSD-2-Clause +golang.org/x/crypto/blake2b,https://cs.opensource.google/go/x/crypto/+/0c34fe9e:LICENSE,BSD-3-Clause +golang.org/x/net/html,https://cs.opensource.google/go/x/net/+/afb366fc:LICENSE,BSD-3-Clause +golang.org/x/sys,https://cs.opensource.google/go/x/sys/+/v0.6.0:LICENSE,BSD-3-Clause +golang.org/x/text,https://cs.opensource.google/go/x/text/+/v0.3.6:LICENSE,BSD-3-Clause +imuslab.com/zoraxy,Unknown,MIT +imuslab.com/zoraxy/mod/dynamicproxy/dpcore,Unknown,MIT +imuslab.com/zoraxy/mod/reverseproxy,Unknown,MIT +imuslab.com/zoraxy/mod/websocketproxy,Unknown,MIT diff --git a/src/system/localhost.crt b/src/system/localhost.crt new file mode 100644 index 0000000..d59e5b9 --- /dev/null +++ b/src/system/localhost.crt @@ -0,0 +1,34 @@ +-----BEGIN CERTIFICATE----- +MIIF8TCCA9mgAwIBAgIUavNWjB6rlfRLpeXJ9TXb2FVrENYwDQYJKoZIhvcNAQEL +BQAwbjELMAkGA1UEBhMCR0wxEjAQBgNVBAgMCU1pbGt5IFdheTEOMAwGA1UEBwwF +RWFydGgxEDAOBgNVBAoMB2ltdXNsYWIxDzANBgNVBAsMBkFyb3pPUzEYMBYGA1UE +AwwPd3d3LmltdXNsYWIuY29tMB4XDTIxMDkxNzA4NTkyNFoXDTQ5MDIwMTA4NTky +NFowbjELMAkGA1UEBhMCR0wxEjAQBgNVBAgMCU1pbGt5IFdheTEOMAwGA1UEBwwF +RWFydGgxEDAOBgNVBAoMB2ltdXNsYWIxDzANBgNVBAsMBkFyb3pPUzEYMBYGA1UE +AwwPd3d3LmltdXNsYWIuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC +AgEAsBpf9ufRYOfdKft+51EibpqhA9yw6YstxL5BNselx3ETVnu7vYRIlH0ypgPN +nKguZ+BcN4mJFjQ36N4VpN7ySVfOCSCZz7lPvPfLib9iukBodBYQNAzMkKcLjyoY +gS8MD99cqe7s48k4JKp6b2WOmn2OtVZIS7AKZvVsRJNblhy7C3LkLnASKF0jb/ia +MGRAE+QV/zznvGg9FhNgQWWUil2Oesx3elj4KwlcHNX+c9pZz6yVgJrerj0s94OD +EuueiqAFOWsZrpp754ffC45PbeTNiflQ1B3aqkTtl5bL88ESgwMdtb1JGWN5HIS1 +Tq2d/3PgqbtvUEhggaFDbe0OxG2V33HqEfeG3BpZpYhCB3I7FPpRC/Tp8PACY13N +HYB9P5hRU/DnINhHjMCLKxHsolhiphWuxSuNIIojRL62zj7JwjnBgcghQzVFJ4O4 +TBfeMDadLII3ndDtsmR1dIba7fg+CWWdv4Zs0XGqHOaiHNclc7BhJF8SgiQxjxjm +Fh1ZsJm3LxPsw/iCl7ILE7+1aBQlBjEj0yBvMttkEDhRbILxXFPMALG/qakPvW9O +7WWClAc03ei/JFdq2camuY62/Tf1HB+TSpGWYH+cSIqsu3V5u29jmdZjrjnuM7Fz +GEjNSCsrMhSLYLkMJmrDGdFQBB31x24o9IXtyrfKZiwxMlUCAwEAAaOBhjCBgzAL +BgNVHQ8EBAMCBeAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwQAYDVR0RBDkwN4IBKoIQ +aW11c2xhYi5pbnRlcm5hbIISbm90LmZvci5wcm9kdWN0aW9uggxkZXYudXNlLm9u +bHkwHQYDVR0OBBYEFISIH/rn8RX1hcNf4rQajJR7FEdMMA0GCSqGSIb3DQEBCwUA +A4ICAQBVldF/qjWyGJ5TiZMiXly/So9zR3Xq7O1qayqYxb5SxvhZYCtsFVrAl6Ux +5bTZ0XQagjck2VouHOG6s98DpaslWFw9N8ADAmljQ8WL1hT5Ij1LXs2sF0FqttFf +YgoT5BOjnHZGlN+FgzAkdF91cYrfZwLm63jvAQtIHwjMSeymy2Fq8gdEZxagYuwG +gLkZxw1YG+gP778CKHT2Ff232kH+5up460aGLHLvg+xHQIWBt2FNGdv68u57hWxh +XXji4/DewQ0RdJW1JdpSg4npebDNiXpo9pKY/SxU056raOtPA94U/h12cHVkszT7 +IxdFC2PszAblbSZhHKGE0C6SbATsqvK4gz6e4h7HWVuPPNWpPW2BNjvyenpijV/E +YsSe6F7uQE/I/iHp9VMcjWuwItqed9yKDeOfDH4+pidowbSJQ97xYfZge36ZEUHC +2ZdQsR0qS+t2h0KlEDN7FNxai3ikSB1bs2AjtU67ofGtoIz/HD70TT6zHKhISZgI +w/4/SY7Hd+P+AWSdJwo+ycZYZlXajqh/cxVJ0zVBr5vKC9KnJ+IjnQ/q7CLcxM4W +aAFC1jakdPz7qO+xNVLQRf8lVnPJNtI88OrlL4n02JlLS/QUSwELXFW0bOKP33jm +PIbPdeP8k0XVe9wlI7MzUQC8pCt+gQ77awTt83Nxp9Xdn1Zbqw== +-----END CERTIFICATE----- diff --git a/src/system/localhost.key b/src/system/localhost.key new file mode 100644 index 0000000..35e2cb5 --- /dev/null +++ b/src/system/localhost.key @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCwGl/259Fg590p ++37nUSJumqED3LDpiy3EvkE2x6XHcRNWe7u9hEiUfTKmA82cqC5n4Fw3iYkWNDfo +3hWk3vJJV84JIJnPuU+898uJv2K6QGh0FhA0DMyQpwuPKhiBLwwP31yp7uzjyTgk +qnpvZY6afY61VkhLsApm9WxEk1uWHLsLcuQucBIoXSNv+JowZEAT5BX/POe8aD0W +E2BBZZSKXY56zHd6WPgrCVwc1f5z2lnPrJWAmt6uPSz3g4MS656KoAU5axmumnvn +h98Ljk9t5M2J+VDUHdqqRO2XlsvzwRKDAx21vUkZY3kchLVOrZ3/c+Cpu29QSGCB +oUNt7Q7EbZXfceoR94bcGlmliEIHcjsU+lEL9Onw8AJjXc0dgH0/mFFT8Ocg2EeM +wIsrEeyiWGKmFa7FK40giiNEvrbOPsnCOcGByCFDNUUng7hMF94wNp0sgjed0O2y +ZHV0htrt+D4JZZ2/hmzRcaoc5qIc1yVzsGEkXxKCJDGPGOYWHVmwmbcvE+zD+IKX +sgsTv7VoFCUGMSPTIG8y22QQOFFsgvFcU8wAsb+pqQ+9b07tZYKUBzTd6L8kV2rZ +xqa5jrb9N/UcH5NKkZZgf5xIiqy7dXm7b2OZ1mOuOe4zsXMYSM1IKysyFItguQwm +asMZ0VAEHfXHbij0he3Kt8pmLDEyVQIDAQABAoICAATmtwUILqujyGQCu+V0PKEX +bKPO4J2fYga3xNjhdZu3afJePztnEx4O3foA4RgbFi+N7wMcsNQNYAD7LV8JVXT1 +HKbkYWOGpNF9lAyhZv4IDOAuPQU11fuwqoGxij0OMie+77VLEQzF7OoYVJAFI5Lp +K6+gVyLEI4X6DqlZ8JKc+he3euJP/DFjZjkXkjMGl0H2dyZDa6+ytwCGSYeIbDnt +oKmKR0kAcOfBuu6ShiJzUUyWYRLTPJ9c1IOPBXbhV+hDy+FtOanCYvBut6Z6r3s/ +gvj0F2vP6OYURQiTCdoe5YT/8TO9sOsj+Zrxlpo5+svBTd9reA2j9gulkVrd3itN +c2Ee7fyuyrCRnEcKoT6BI8/LqH5eWGQKKS9WhOz26VkrorcYYZN3g4ayv+MiiSIm +jeo/kAWCqT5ylvlw2gaCbPjB4kbx7yMI/myjgF0R4+aNQaHpXa2qqEORitGx40M7 +T1V2JIxnsa83TBwumunkYC2pX7bNS0a1VuCNxUafJRKEcvKhWmiRHaWddZn46G8N +E56qFzSaLbkd+J71jso9llK5joGIQTt2pbKUdV9LIm5Nsbtp2VgF9URIw5RZFftx +PfSm9XM9DtWuxheO4gNwAuOvtaOxztNMvSkQzhTOggSRpt15hFd7CeBrpK43feAH +b2pMequB8MHpUieyxlwBAoIBAQC5IRbaKx+fSEbYeIySUwbN8GCJQl+wmvc1gqCC +DflEQqxTvCGBB5SLHHurTT0ubhXkvbrtuS5f4IC+htzKSuwlqn3lS0aOXtFP2tT6 +D9iiMxLxIId5l6dD+PjMWtQcWc8wUQ7+ieRgxybDqiCWMyTbvNgwlkcIbRxmcqyN +4/LmmgzTnr5CH0DC/J7xpUJuX9LPVb4ZvBYjz5X++Yb7pCa+kXp0Z6yU48bG3sRe +yiUKp3Z4vDoOkMLHTPvTQLG81rQuJnBUw2uLWM0kg1AwteZcQ/gH1ilVbJzMBnKm +mtuJWtoPnM2zIhCsURngmBN+qxOb5kchMSvPzAQBCw7HBjWpAoIBAQDzhLQO434G +XhyDcdkdMRbDZ8Q8PqtOloAbczMuPGgwHV7rVe/BvnJS7HDDebwlJBD8nhGvgBrp +CsjNGHjSQC7ydUa8dP4Aw/46izdR8DsAwqGZq+tZhkY5CS88QpflUT5rftW0RObn +Cb/gDzdxHy35/scSICxa2HwcZnqXqfEwnbjkxFwBYFSt6hRiwNhDhd6ZxKa6gt56 +DS9uIxt1IhKgXZfIw1Vo0mHHFLsB7czGZ0O24ya31Es0bUWGgWIcxvKw6MqKhFWw +ncCakVg278UYUm/zt6Dcrn3XYnK7Pr944AiKO21PMQhG7Rb+OVwxgjMhk7/BCt+k +sPR1Dct5pqrNAoIBAAl2jYp9ZdJoiWaLUvQv1ks0nFqnz+hhI33SvY2oVTOODO0C +0tubnZY20IODITt8WRYmNKXuL1arTSlwD10v0z5hpqnP3T1tz1k7oGNf5/zyi2dT ++FjYza4FzgH0Kp+AX7zih9evCMOBqpOZ4KyM1Ld+wbZKGDtwCGGcPwHJwyLSgRFY +LfWHT3IoI5/KiMjHkSkUAvGh0afm9o3gB2xZibl4CkBlBEdgFUsZHASUZKxUvxOQ +247fC3XQk5bK2csDVpZ9VISgsKCg22ugYrr6sVnKB6Wu5tH9CU7MjZPCmrI8uKTP +qRwdA6krRB1c6LIy4H+5l600rD6k+Rdsj0bRJHECggEAeBXSrRzmAsHaEb/MryaL +8SR0krjcxU5WMjMm5AAJ6OAy9J5WMxZ1TgsmuF6Jt08HyWsxkXf8zTryNqGAwz2/ +aPUIQtr2fu4nqjsItrFeh0tzYVJ0JpueeXXcAz1bpkvgGiZbwB/SNdCK/DTExFX5 +2DQZewi+lrX2zhKDFdNKCw1cJgPm0w7r8y9hiilK/FFBqlZdWdA7Ybiq0Qci/Som +QUqmFOyua5iDeybv6U2ZE6XMsJ1ndHON+naAOIoJFePNvguuBYyorQW9+vr9o2mt +qgbNCkRdYTXy/ImhxlB1H2hrDa+sgcbOLBuyoP8sRYXNLRutDccM7iwNAMQiuQTF +aQKCAQEAiKPwUodT6LNu4lrSbsDAYIqWwlfM0wwUhudT5UTVHSYI3ap0QOiEuzOl +IJVdx+vx7rQW7l+JIL6s4shA7mzpzuTVlhRuDuGZx0qQLP7INVpCLzIEbYGI2dL7 +WLhJd4eYKltJ+BG7S51tq9/6rVcUDn5DKzyGNyeGhOnaYkk+eTm483+vpOP2/ITi +cbVv3mx4qE7zMPIxIufm+c8RonadJzYiq1uMk8t0TrcW/B9RTly/Y96kamjyU5b0 +OcLdRcx3ppKAxHD9AvwAR6SiuNLfNjM9KZM40zM5goMrCJJzwgb7UGeMuw2z7L9F ++iSj2pW0Rbdy7oOcFRF/iM2GwFYc1Q== +-----END PRIVATE KEY----- diff --git a/src/upnp.go b/src/upnp.go new file mode 100644 index 0000000..4ea2773 --- /dev/null +++ b/src/upnp.go @@ -0,0 +1,210 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + "net/url" + "regexp" + "strconv" + "time" + + "imuslab.com/zoraxy/mod/upnp" + "imuslab.com/zoraxy/mod/utils" +) + +var upnpEnabled = false +var preforwardMap map[int]string + +func initUpnp() error { + go func() { + //Let UPnP discovery run in background + var err error + upnpClient, err = upnp.NewUPNPClient() + if err != nil { + log.Println("UPnP router discover error: ", err.Error()) + return + } + + if upnpEnabled { + //Forward all the ports + for port, policyName := range preforwardMap { + upnpClient.ForwardPort(port, policyName) + log.Println("Upnp forwarding ", port, " for "+policyName) + time.Sleep(300 * time.Millisecond) + } + } + }() + + //Check if the upnp was enabled + sysdb.NewTable("upnp") + sysdb.Read("upnp", "enabled", &upnpEnabled) + + //Load all the ports from database + portsMap := map[int]string{} + sysdb.Read("upnp", "portmap", &portsMap) + preforwardMap = portsMap + + return nil +} + +func handleUpnpDiscover(w http.ResponseWriter, r *http.Request) { + restart, err := utils.PostPara(r, "restart") + if err != nil { + type UpnpInfo struct { + ExternalIp string + RouterIp string + } + + if upnpClient == nil { + utils.SendErrorResponse(w, "No UPnP router discovered") + return + } + + parsedUrl, _ := url.Parse(upnpClient.Connection.Location()) + ipWithPort := parsedUrl.Host + + result := UpnpInfo{ + ExternalIp: upnpClient.ExternalIP, + RouterIp: ipWithPort, + } + + //Show if there is a upnpclient + js, _ := json.Marshal(result) + utils.SendJSONResponse(w, string(js)) + } else { + if restart == "true" { + //Close the upnp client if exists + if upnpClient != nil { + saveForwardingPortsToDatabase() + upnpClient.Close() + } + + //Restart a new one + initUpnp() + + utils.SendOK(w) + } + } +} + +func handleToggleUPnP(w http.ResponseWriter, r *http.Request) { + newMode, err := utils.PostPara(r, "mode") + if err != nil { + //Send the current mode to client side + js, _ := json.Marshal(upnpEnabled) + utils.SendJSONResponse(w, string(js)) + } else { + if newMode == "true" { + upnpEnabled = true + sysdb.Read("upnp", "enabled", true) + + log.Println("UPnP Enabled. Forwarding all required ports") + //Mount all Upnp requests from preforward Map + for port, policyName := range preforwardMap { + upnpClient.ForwardPort(port, policyName) + log.Println("Upnp forwarding ", port, " for "+policyName) + time.Sleep(300 * time.Millisecond) + } + + utils.SendOK(w) + return + + } else if newMode == "false" { + upnpEnabled = false + sysdb.Read("upnp", "enabled", false) + log.Println("UPnP disabled. Closing all forwarded ports") + //Save the current forwarded ports + saveForwardingPortsToDatabase() + + //Unmount all Upnp request + for _, port := range upnpClient.RequiredPorts { + upnpClient.ClosePort(port) + log.Println("UPnP port closed: ", port) + time.Sleep(300 * time.Millisecond) + } + + //done + utils.SendOK(w) + return + } + } +} + +func filterRFC2141(input string) string { + rfc2141 := regexp.MustCompile(`^[\w\-.!~*'()]*(\%[\da-fA-F]{2}[\w\-.!~*'()]*)*$`) + var result []rune + for _, char := range input { + if char <= 127 && rfc2141.MatchString(string(char)) { + result = append(result, char) + } + } + return string(result) +} + +func handleAddUpnpPort(w http.ResponseWriter, r *http.Request) { + portString, err := utils.PostPara(r, "port") + if err != nil { + utils.SendErrorResponse(w, "invalid port given") + return + } + + portNumber, err := strconv.Atoi(portString) + if err != nil { + utils.SendErrorResponse(w, "invalid port given") + return + } + + policyName, err := utils.PostPara(r, "name") + if err != nil { + utils.SendErrorResponse(w, "invalid policy name") + return + } + + policyName = filterRFC2141(policyName) + + err = upnpClient.ForwardPort(portNumber, policyName) + if err != nil { + utils.SendErrorResponse(w, err.Error()) + return + } + + saveForwardingPortsToDatabase() + + utils.SendOK(w) +} + +func handleRemoveUpnpPort(w http.ResponseWriter, r *http.Request) { + portString, err := utils.PostPara(r, "port") + if err != nil { + utils.SendErrorResponse(w, "invalid port given") + return + } + + portNumber, err := strconv.Atoi(portString) + if err != nil { + utils.SendErrorResponse(w, "invalid port given") + return + } + + saveForwardingPortsToDatabase() + + upnpClient.ClosePort(portNumber) +} + +func saveForwardingPortsToDatabase() { + //Move the sync map to map[int]string + m := make(map[int]string) + upnpClient.PolicyNames.Range(func(key, value interface{}) bool { + if k, ok := key.(int); ok { + if v, ok := value.(string); ok { + m[k] = v + } + } + return true + }) + + preforwardMap = m + sysdb.Write("upnp", "portmap", &preforwardMap) + +} diff --git a/src/web/components/blacklist.html b/src/web/components/blacklist.html new file mode 100644 index 0000000..ac5d864 --- /dev/null +++ b/src/web/components/blacklist.html @@ -0,0 +1,599 @@ + +
Setup blacklist based on estimated IP geographic location or IP address
+ ++ This will block all requests from the selected country. The requester's location is estimated from their IP address and may not be 100% accurate.
+ +ISO Code | +Remove | +
---|
Black a certain IP or IP range
+IP Address | +Remove | +
---|
Setup TLS cert for different domains of your reverse proxy server names
+ +Key Type | +Exists | +
---|---|
Default Public Key | ++ |
Default Private Key | ++ |
Provide certificates for multiple domains reverse proxy
+Domain | +Last Update | +Remove | +
---|
Add exception case for redirecting any matching URLs
+Redirection URL | ++ | Destination URL | +Copy Pathname | +Status Code | +Remove | +
---|---|---|---|---|---|
+ | + | + | + |
Add path redirection to your domain
+ +For all routing not found in the proxy rule, will be redirected to the proxy root server.
+You can create a proxy endpoing by subdomain or virtual directories
+Inbound Port (Port to be proxied)
+Visitor Counts
+Country ISO Code | +Visitor Count | +
---|
Proxy Request Types
+Proxy Type | +Count | +
---|
Matching Domain | +Proxy To | +Remove | +
---|
Port forward using UPnP protocol
+ + +Forwarded Ports
+Port Forwarded | +Name of Rule | ++ |
---|---|---|
80 | +HTTP | ++ |
22 | +SSH | ++ |
You might find these tools helpful when setting up your gateway server
+ +Functions to help management the current account
+No experience with CIDR notations? Here are some tools you can use to make setting up easier.
+Results:
Results:
Virtual Directory | +Proxy To | +Remove | +
---|---|---|
test | +test | ++ |
You do not have permission to view this directory or page.
+ This might be caused by the site admin has blacklisted your country or IP address
Target Host Not Found
+ +Working
+Working
+Not Found
+The reverse proxy target domain is not found.
For more information, see the error message on the reverse proxy terminal.
Please try again in a few minutes
+Reverse Proxy by imuslab, Licensed under MIT
+