Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
21872eb88b
5 changed files with 57 additions and 4 deletions
  1. 1 1
      main.go
  2. 19 2
      mod/webserv/middleware.go
  3. 12 1
      mod/webserv/webserv.go
  4. 16 0
      start.go
  5. 9 0
      www/html/index.html

+ 1 - 1
main.go

@@ -42,7 +42,7 @@ var ztAuthToken = flag.String("ztauth", "", "ZeroTier authtoken for the local no
 var ztAPIPort = flag.Int("ztport", 9993, "ZeroTier controller API port")
 var acmeAutoRenewInterval = flag.Int("autorenew", 86400, "ACME auto TLS/SSL certificate renew check interval (seconds)")
 var enableHighSpeedGeoIPLookup = flag.Bool("fastgeoip", false, "Enable high speed geoip lookup, require 1GB extra memory (Not recommend for low end devices)")
-var staticWebServerRoot = flag.String("webroot", "./web", "Static web server root folder. Only allow chnage in start paramters")
+var staticWebServerRoot = flag.String("webroot", "./www", "Static web server root folder. Only allow chnage in start paramters")
 
 var (
 	name        = "Zoraxy"

+ 19 - 2
mod/webserv/middleware.go

@@ -2,16 +2,33 @@ package webserv
 
 import (
 	"net/http"
+	"path/filepath"
 	"strings"
+
+	"imuslab.com/zoraxy/mod/utils"
 )
 
+// Convert a request path (e.g. /index.html) into physical path on disk
+func (ws *WebServer) resolveFileDiskPath(requestPath string) string {
+	fileDiskpath := filepath.Join(ws.option.WebRoot, "html", requestPath)
+
+	//Force convert it to slash even if the host OS is on Windows
+	fileDiskpath = filepath.Clean(fileDiskpath)
+	fileDiskpath = strings.ReplaceAll(fileDiskpath, "\\", "/")
+	return fileDiskpath
+
+}
+
 // File server middleware to handle directory listing (and future expansion)
 func (ws *WebServer) fsMiddleware(h http.Handler) http.Handler {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		if ws.option.EnableDirectoryListing {
 			if strings.HasSuffix(r.URL.Path, "/") {
-				http.NotFound(w, r)
-				return
+				//This is a folder. Let check if index exists
+				if !utils.FileExists(filepath.Join(ws.resolveFileDiskPath(r.URL.Path), "index.html")) {
+					http.NotFound(w, r)
+					return
+				}
 			}
 		}
 

+ 12 - 1
mod/webserv/webserv.go

@@ -3,8 +3,13 @@ package webserv
 import (
 	"errors"
 	"fmt"
+	"log"
 	"net/http"
+	"os"
+	"path/filepath"
 	"sync"
+
+	"imuslab.com/zoraxy/mod/utils"
 )
 
 /*
@@ -28,6 +33,11 @@ type WebServer struct {
 
 // NewWebServer creates a new WebServer instance.
 func NewWebServer(options *WebServerOptions) *WebServer {
+	if !utils.FileExists(options.WebRoot) {
+		//Web root folder not exists. Create one
+		os.MkdirAll(filepath.Join(options.WebRoot, "html"), 0775)
+		os.MkdirAll(filepath.Join(options.WebRoot, "templates"), 0775)
+	}
 	return &WebServer{
 		mux:       http.NewServeMux(),
 		option:    options,
@@ -72,7 +82,7 @@ func (ws *WebServer) Start() error {
 	ws.mux = http.NewServeMux()
 
 	//Create a static web server
-	fs := http.FileServer(http.Dir(ws.option.WebRoot))
+	fs := http.FileServer(http.Dir(filepath.Join(ws.option.WebRoot, "html")))
 	ws.mux.Handle("/", ws.fsMiddleware(fs))
 
 	ws.server = &http.Server{
@@ -88,6 +98,7 @@ func (ws *WebServer) Start() error {
 		}
 	}()
 
+	log.Println("Static Web Server started. Listeing on :" + ws.option.Port)
 	ws.isRunning = true
 
 	return nil

+ 16 - 0
start.go

@@ -22,6 +22,7 @@ import (
 	"imuslab.com/zoraxy/mod/statistic/analytic"
 	"imuslab.com/zoraxy/mod/tcpprox"
 	"imuslab.com/zoraxy/mod/tlscert"
+	"imuslab.com/zoraxy/mod/webserv"
 )
 
 /*
@@ -208,6 +209,21 @@ func startupSequence() {
 	if err != nil {
 		log.Fatal(err)
 	}
+
+	/*
+		Static Web Server
+
+		Start the static web server
+	*/
+
+	staticWebServer = webserv.NewWebServer(&webserv.WebServerOptions{
+		Port:                   "8081",
+		WebRoot:                *staticWebServerRoot,
+		EnableDirectoryListing: true,
+	})
+
+	//TODO: Connect UI element to static web server
+	staticWebServer.Start()
 }
 
 // This sequence start after everything is initialized

+ 9 - 0
www/html/index.html

@@ -0,0 +1,9 @@
+<html>
+    <head>
+        <title>Hello Static Web Server</title>
+    </head>
+    <body>
+        <h1>Hello Static Web Server!</h1>
+        <p>This is a testing page for the Zoraxy static web server </p>
+    </body>
+</html>