You do not have permission to view this directory or page.
+ This might cause by the region limit setting of this site.
+
+
+ Request time:
+ Request URI:
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/mod/dynamicproxy/typedef.go b/src/mod/dynamicproxy/typedef.go
index 218bae5..2372f04 100644
--- a/src/mod/dynamicproxy/typedef.go
+++ b/src/mod/dynamicproxy/typedef.go
@@ -1,6 +1,7 @@
package dynamicproxy
import (
+ _ "embed"
"net"
"net/http"
"sync"
@@ -31,6 +32,7 @@ type RouterOption struct {
RedirectRuleTable *redirection.RuleTable
GeodbStore *geodb.Store //GeoIP blacklist and whitelist
StatisticCollector *statistic.Collector
+ WebDirectory string //The static web server directory containing the templates folder
}
type Router struct {
@@ -123,3 +125,11 @@ type SubdOptions struct {
BasicAuthCredentials []*BasicAuthCredentials
BasicAuthExceptionRules []*BasicAuthExceptionRule
}
+
+/*
+Web Templates
+*/
+var (
+ //go:embed templates/forbidden.html
+ page_forbidden []byte
+)
diff --git a/src/mod/utils/utils.go b/src/mod/utils/utils.go
index bd21c8b..9af3ebc 100644
--- a/src/mod/utils/utils.go
+++ b/src/mod/utils/utils.go
@@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
+ "strconv"
"strings"
"time"
)
@@ -76,6 +77,22 @@ func PostBool(r *http.Request, key string) (bool, error) {
return false, errors.New("invalid boolean given")
}
+// Get POST paramter as int
+func PostInt(r *http.Request, key string) (int, error) {
+ x, err := PostPara(r, key)
+ if err != nil {
+ return 0, err
+ }
+
+ x = strings.TrimSpace(x)
+ rx, err := strconv.Atoi(x)
+ if err != nil {
+ return 0, err
+ }
+
+ return rx, nil
+}
+
func FileExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
diff --git a/src/mod/webserv/handler.go b/src/mod/webserv/handler.go
index 0a4eba6..5db1837 100644
--- a/src/mod/webserv/handler.go
+++ b/src/mod/webserv/handler.go
@@ -1,5 +1,13 @@
package webserv
+import (
+ "encoding/json"
+ "net/http"
+ "strconv"
+
+ "imuslab.com/zoraxy/mod/utils"
+)
+
/*
Handler.go
@@ -7,3 +15,74 @@ package webserv
web server is directly listening to the TCP port
handlers in this script are for setting change only
*/
+
+type StaticWebServerStatus struct {
+ ListeningPort int
+ EnableDirectoryListing bool
+ WebRoot string
+ Running bool
+ EnableWebDirManager bool
+}
+
+// Handle getting current static web server status
+func (ws *WebServer) HandleGetStatus(w http.ResponseWriter, r *http.Request) {
+ listeningPortInt, _ := strconv.Atoi(ws.option.Port)
+ currentStatus := StaticWebServerStatus{
+ ListeningPort: listeningPortInt,
+ EnableDirectoryListing: ws.option.EnableDirectoryListing,
+ WebRoot: ws.option.WebRoot,
+ Running: ws.isRunning,
+ EnableWebDirManager: ws.option.EnableWebDirManager,
+ }
+
+ js, _ := json.Marshal(currentStatus)
+ utils.SendJSONResponse(w, string(js))
+}
+
+// Handle request for starting the static web server
+func (ws *WebServer) HandleStartServer(w http.ResponseWriter, r *http.Request) {
+ err := ws.Start()
+ if err != nil {
+ utils.SendErrorResponse(w, err.Error())
+ return
+ }
+ utils.SendOK(w)
+}
+
+// Handle request for stopping the static web server
+func (ws *WebServer) HandleStopServer(w http.ResponseWriter, r *http.Request) {
+ err := ws.Stop()
+ if err != nil {
+ utils.SendErrorResponse(w, err.Error())
+ return
+ }
+ utils.SendOK(w)
+}
+
+// Handle change server listening port request
+func (ws *WebServer) HandlePortChange(w http.ResponseWriter, r *http.Request) {
+ newPort, err := utils.PostInt(r, "port")
+ if err != nil {
+ utils.SendErrorResponse(w, "invalid port number given")
+ return
+ }
+
+ err = ws.ChangePort(strconv.Itoa(newPort))
+ if err != nil {
+ utils.SendErrorResponse(w, err.Error())
+ return
+ }
+ utils.SendOK(w)
+}
+
+// Change enable directory listing settings
+func (ws *WebServer) SetEnableDirectoryListing(w http.ResponseWriter, r *http.Request) {
+ enableList, err := utils.PostBool(r, "enable")
+ if err != nil {
+ utils.SendErrorResponse(w, "invalid setting given")
+ return
+ }
+
+ ws.option.EnableDirectoryListing = enableList
+ utils.SendOK(w)
+}
diff --git a/src/mod/webserv/middleware.go b/src/mod/webserv/middleware.go
index 1dade78..ed03037 100644
--- a/src/mod/webserv/middleware.go
+++ b/src/mod/webserv/middleware.go
@@ -22,7 +22,7 @@ func (ws *WebServer) resolveFileDiskPath(requestPath string) string {
// 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 !ws.option.EnableDirectoryListing {
if strings.HasSuffix(r.URL.Path, "/") {
//This is a folder. Let check if index exists
if utils.FileExists(filepath.Join(ws.resolveFileDiskPath(r.URL.Path), "index.html")) {
diff --git a/src/mod/webserv/templates/index.html b/src/mod/webserv/templates/index.html
new file mode 100644
index 0000000..8716959
--- /dev/null
+++ b/src/mod/webserv/templates/index.html
@@ -0,0 +1,61 @@
+
+
+ Hello Zoraxy
+
+
+
+
+
+
Welcome to Zoraxy Static Web Server!
+
+
+
If you see this page, that means your static web server is running.
+ By default, all the html files are stored under ./web/html/
+ relative to the zoraxy runtime directory.
+ You can upload your html files to your web directory via the Web Directory Manager.
+
+
+
\ No newline at end of file
diff --git a/src/mod/webserv/webserv.go b/src/mod/webserv/webserv.go
index 6548d92..542810c 100644
--- a/src/mod/webserv/webserv.go
+++ b/src/mod/webserv/webserv.go
@@ -1,6 +1,8 @@
package webserv
import (
+ "embed"
+ _ "embed"
"errors"
"fmt"
"log"
@@ -9,6 +11,7 @@ import (
"path/filepath"
"sync"
+ "imuslab.com/zoraxy/mod/database"
"imuslab.com/zoraxy/mod/utils"
)
@@ -18,11 +21,17 @@ import (
This module host a static web server
*/
+//go:embed templates/*
+var templates embed.FS
+
type WebServerOptions struct {
- Port string //Port for listening
- EnableDirectoryListing bool //Enable listing of directory
- WebRoot string //Folder for stroing the static web folders
+ Port string //Port for listening
+ EnableDirectoryListing bool //Enable listing of directory
+ WebRoot string //Folder for stroing the static web folders
+ EnableWebDirManager bool //Enable web file manager to handle files in web directory
+ Sysdb *database.Database //Database for storing configs
}
+
type WebServer struct {
mux *http.ServeMux
server *http.Server
@@ -31,13 +40,23 @@ type WebServer struct {
mu sync.Mutex
}
-// NewWebServer creates a new WebServer instance.
+// NewWebServer creates a new WebServer instance. One instance only
func NewWebServer(options *WebServerOptions) *WebServer {
if !utils.FileExists(options.WebRoot) {
- //Web root folder not exists. Create one
+ //Web root folder not exists. Create one with default templates
os.MkdirAll(filepath.Join(options.WebRoot, "html"), 0775)
os.MkdirAll(filepath.Join(options.WebRoot, "templates"), 0775)
+ indexTemplate, err := templates.ReadFile("templates/index.html")
+ if err != nil {
+ log.Println("Failed to read static wev server template file: ", err.Error())
+ } else {
+ os.WriteFile(filepath.Join(options.WebRoot, "html", "index.html"), indexTemplate, 0775)
+ }
+
}
+
+ //Create new table to store the config
+ options.Sysdb.NewTable("webserv")
return &WebServer{
mux: http.NewServeMux(),
option: options,
@@ -46,11 +65,31 @@ func NewWebServer(options *WebServerOptions) *WebServer {
}
}
+// Restore the configuration to previous config
+func (ws *WebServer) RestorePreviousState() {
+ //Set the port
+ port := ws.option.Port
+ ws.option.Sysdb.Read("webserv", "port", &port)
+ ws.option.Port = port
+
+ //Set the enable directory list
+ enableDirList := ws.option.EnableDirectoryListing
+ ws.option.Sysdb.Read("webserv", "dirlist", &enableDirList)
+ ws.option.EnableDirectoryListing = enableDirList
+
+ //Check the running state
+ webservRunning := false
+ ws.option.Sysdb.Read("webserv", "enabled", &webservRunning)
+ if webservRunning {
+ ws.Start()
+ } else {
+ ws.Stop()
+ }
+
+}
+
// ChangePort changes the server's port.
func (ws *WebServer) ChangePort(port string) error {
- ws.mu.Lock()
- defer ws.mu.Unlock()
-
if ws.isRunning {
if err := ws.Stop(); err != nil {
return err
@@ -60,6 +99,13 @@ func (ws *WebServer) ChangePort(port string) error {
ws.option.Port = port
ws.server.Addr = ":" + port
+ err := ws.Start()
+ if err != nil {
+ return err
+ }
+
+ ws.option.Sysdb.Write("webserv", "port", port)
+
return nil
}
@@ -100,7 +146,7 @@ func (ws *WebServer) Start() error {
log.Println("Static Web Server started. Listeing on :" + ws.option.Port)
ws.isRunning = true
-
+ ws.option.Sysdb.Write("webserv", "enabled", true)
return nil
}
@@ -118,17 +164,14 @@ func (ws *WebServer) Stop() error {
}
ws.isRunning = false
-
+ ws.option.Sysdb.Write("webserv", "enabled", false)
return nil
}
// UpdateDirectoryListing enables or disables directory listing.
func (ws *WebServer) UpdateDirectoryListing(enable bool) {
- ws.mu.Lock()
- defer ws.mu.Unlock()
-
ws.option.EnableDirectoryListing = enable
-
+ ws.option.Sysdb.Write("webserv", "dirlist", enable)
}
// Close stops the web server without returning an error.
diff --git a/src/reverseproxy.go b/src/reverseproxy.go
index e45953a..43c66de 100644
--- a/src/reverseproxy.go
+++ b/src/reverseproxy.go
@@ -64,6 +64,7 @@ func ReverseProxtInit() {
RedirectRuleTable: redirectTable,
GeodbStore: geodbStore,
StatisticCollector: statisticCollector,
+ WebDirectory: *staticWebServerRoot,
})
if err != nil {
log.Println(err.Error())
diff --git a/src/start.go b/src/start.go
index 483d9d2..a0a1b3a 100644
--- a/src/start.go
+++ b/src/start.go
@@ -217,13 +217,14 @@ func startupSequence() {
*/
staticWebServer = webserv.NewWebServer(&webserv.WebServerOptions{
- Port: "8081",
+ Sysdb: sysdb,
+ Port: "8081", //Default Port
WebRoot: *staticWebServerRoot,
EnableDirectoryListing: true,
+ EnableWebDirManager: *allowWebFileManager,
})
-
- //TODO: Connect UI element to static web server
- staticWebServer.Start()
+ //Restore the web server to previous shutdown state
+ staticWebServer.RestorePreviousState()
}
// This sequence start after everything is initialized
diff --git a/src/web/components/webserv.html b/src/web/components/webserv.html
index fcd8637..e8ac76f 100644
--- a/src/web/components/webserv.html
+++ b/src/web/components/webserv.html
@@ -4,6 +4,16 @@
A simple static web server that serve html css and js files
+
+
+
+
+ Running
+
Listening on :8081
+
+
+
+
Web Server Settings
@@ -29,10 +39,11 @@
-
+
Use http://127.0.0.1:8081 in proxy rules to access the web server
+ Changes are saved automatically
@@ -63,10 +74,108 @@
+
+
Web Directory Manager
+
Manage your files inside your web directory
+
+
+ If you do not want to enable web access to your web directory, you can disable this feature with -webfm=false startup paramter
+
+
+
+
+
+
+
+
+
+
+