Add a "stealth mode" in case for whatever reason you don't want to show the entire world you're using a super obscure http server

Signed-off-by: arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-11-03 14:26:02 +00:00
parent 694f62f238
commit 8d27ad6b98
1 changed files with 54 additions and 6 deletions

48
main.go
View File

@ -58,6 +58,15 @@ type Config struct {
ConnectionString string `yaml:"connectionString" validate:"required_if=Type postgres"` ConnectionString string `yaml:"connectionString" validate:"required_if=Type postgres"`
Path string `yaml:"path" validate:"required_if=Type sqlite"` Path string `yaml:"path" validate:"required_if=Type sqlite"`
} `yaml:"database" validate:"required"` } `yaml:"database" validate:"required"`
Stealth struct {
Enabled bool `yaml:"enabled"`
Server string `yaml:"server" validate:"required_if=Enabled true"`
PHP struct {
Enabled bool `yaml:"enabled"`
Version string `yaml:"version" validate:"required_if=Enabled true"`
} `yaml:"php"`
ASPNet bool `yaml:"aspNet"`
}
} `yaml:"global" validate:"required"` } `yaml:"global" validate:"required"`
Routes []struct { Routes []struct {
Subdomain string `yaml:"subdomain" validate:"required"` Subdomain string `yaml:"subdomain" validate:"required"`
@ -136,8 +145,30 @@ func logger(next http.Handler) http.Handler {
func serverChanger(next http.Handler) http.Handler { func serverChanger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !config.Global.Stealth.Enabled {
w.Header().Set("Server", "Fulgens HTTP Server") w.Header().Set("Server", "Fulgens HTTP Server")
w.Header().Set("X-Powered-By", "Go net/http") w.Header().Set("X-Powered-By", "Go net/http")
} else {
switch config.Global.Stealth.Server {
case "nginx":
w.Header().Set("Server", "nginx")
}
var poweredBy strings.Builder
if config.Global.Stealth.PHP.Enabled {
poweredBy.WriteString("PHP/" + config.Global.Stealth.PHP.Version)
}
if config.Global.Stealth.ASPNet {
if poweredBy.Len() > 0 {
poweredBy.WriteString(", ")
}
poweredBy.WriteString("ASP.NET")
}
w.Header().Set("X-Powered-By", poweredBy.String())
}
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }
@ -457,11 +488,28 @@ func newFileServer(root string, directoryListing bool, path string) http.Handler
func serverError(w http.ResponseWriter, status int) { func serverError(w http.ResponseWriter, status int) {
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
w.WriteHeader(status) w.WriteHeader(status)
if !config.Global.Stealth.Enabled {
_, err := w.Write([]byte("<html><body><h2>" + strconv.Itoa(status) + " " + http.StatusText(status) + "</h2><span>Fulgens HTTP Server</span></body></html>")) _, err := w.Write([]byte("<html><body><h2>" + strconv.Itoa(status) + " " + http.StatusText(status) + "</h2><span>Fulgens HTTP Server</span></body></html>"))
if err != nil { if err != nil {
slog.Error("Error writing " + strconv.Itoa(status) + ": " + err.Error()) slog.Error("Error writing " + strconv.Itoa(status) + ": " + err.Error())
return return
} }
} else {
switch config.Global.Stealth.Server {
case "nginx":
_, err := w.Write([]byte("<html><head><title>" + strconv.Itoa(status) + " " + http.StatusText(status) + "</title></head>\n<body>\n<center><h1>" + strconv.Itoa(status) + " " + http.StatusText(status) + "</h1></center>\n<hr><center>nginx/1.27.2</center>\n\n\n</body></html>"))
if err != nil {
slog.Error("Error writing " + strconv.Itoa(status) + ": " + err.Error())
return
}
case "net/http":
_, err := w.Write([]byte(strconv.Itoa(status) + " " + http.StatusText(status)))
if err != nil {
slog.Error("Error writing " + strconv.Itoa(status) + ": " + err.Error())
return
}
}
}
} }
func hostRouter(w http.ResponseWriter, r *http.Request) { func hostRouter(w http.ResponseWriter, r *http.Request) {