diff --git a/client/client.go b/client/client.go index 0bf1d0b..21ab4d5 100644 --- a/client/client.go +++ b/client/client.go @@ -20,6 +20,7 @@ import ( "slider/pkg/sio" "slider/pkg/slog" "slider/pkg/ssocks" + "slider/pkg/web" "sync" "time" @@ -46,7 +47,7 @@ type client struct { sessionTrackMutex sync.Mutex isListener bool firstRun bool - webTemplate string + webTemplate web.Template } const clientHelp = ` @@ -73,7 +74,7 @@ func NewClient(args []string) { port := clientFlags.Int("port", 8081, "Listener Port") address := clientFlags.String("address", "0.0.0.0", "Address the Listener will bind to") retry := clientFlags.Bool("retry", false, "Retries reconnection indefinitely") - webTemplate := clientFlags.String("template", "", "Mimic default web server page [apache|nginx]") + webTemplate := clientFlags.String("template", "", "Mimic web server page [apache|iis|nginx|tomcat]") clientFlags.Usage = func() { fmt.Println(clientHelp) clientFlags.PrintDefaults() @@ -144,9 +145,11 @@ func NewClient(args []string) { if *listener { c.isListener = *listener - if *webTemplate != "" { - c.webTemplate = *webTemplate + t, tErr := web.GetTemplate(*webTemplate) + if tErr != nil { + c.Logger.Errorf("%v", tErr) } + c.webTemplate = t fmtAddress := fmt.Sprintf("%s:%d", *address, *port) clientAddr, rErr := net.ResolveTCPAddr("tcp", fmtAddress) diff --git a/client/handler.go b/client/handler.go index cc9d81e..703ee2e 100644 --- a/client/handler.go +++ b/client/handler.go @@ -3,7 +3,6 @@ package client import ( "net/http" "slider/pkg/conf" - "slider/pkg/wtemplate" "strings" ) @@ -14,28 +13,12 @@ func (c *client) handleHTTPConn(w http.ResponseWriter, r *http.Request) { return } - var svrHeader string - status := http.StatusNotFound - tmpl := "Not Found" - - if c.webTemplate != "" { - t, tErr := wtemplate.GetTemplate(c.webTemplate) - if tErr == nil { - svrHeader = c.webTemplate - status = http.StatusOK - tmpl = t - } - } - - w.Header().Add("server", svrHeader) - var wErr error switch r.URL.Path { - case "/health": - _, wErr = w.Write([]byte("OK")) case "/": - w.WriteHeader(status) - _, wErr = w.Write([]byte(tmpl)) + w.Header().Add("server", c.webTemplate.ServerHeader) + w.WriteHeader(c.webTemplate.StatusCode) + _, wErr = w.Write([]byte(c.webTemplate.HtmlTemplate)) default: http.Redirect(w, r, "/", http.StatusMovedPermanently) } diff --git a/pkg/web/template.go b/pkg/web/template.go new file mode 100644 index 0000000..348a7f4 --- /dev/null +++ b/pkg/web/template.go @@ -0,0 +1,101 @@ +package web + +const ( + apacheTemplate = `

It works!

+

This is the default web page for this server.

+

The web server software is running but no content has been added, yet.

+` + nginxTemplate = ` + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, the nginx web server is successfully installed and +working. Further configuration is required.

+ +

For online documentation and support please refer to +nginx.org.
+Commercial support is available at +nginx.com.

+ +

Thank you for using nginx.

+ +` + iisTemplate = ` + + + +IIS Windows Server + + + +
+IIS +
+ +` + tomcatTemplate = ` + +HTTP Status 404 – Not Found + + +

HTTP Status 404 – Not Found

+
+

Type Status Report

+

Message The requested resource [/] is not available

+

Description +The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

+

Apache Tomcat

` +) diff --git a/pkg/web/web.go b/pkg/web/web.go new file mode 100644 index 0000000..6a1ff39 --- /dev/null +++ b/pkg/web/web.go @@ -0,0 +1,51 @@ +package web + +import ( + "fmt" + "net/http" + "strings" +) + +type Template struct { + HtmlTemplate string + ServerHeader string + StatusCode int +} + +var ( + enabledTemplates = map[string]Template{ + "apache": { + HtmlTemplate: apacheTemplate, + ServerHeader: "Apache", + StatusCode: http.StatusOK, + }, + "nginx": { + HtmlTemplate: nginxTemplate, + ServerHeader: "nginx", + StatusCode: http.StatusOK, + }, + "iis": { + HtmlTemplate: iisTemplate, + ServerHeader: "Microsoft-IIS", + StatusCode: http.StatusOK, + }, + "tomcat": { + HtmlTemplate: tomcatTemplate, + ServerHeader: "Apache Tomcat", + StatusCode: http.StatusNotFound, + }, + } +) + +func GetTemplate(n string) (Template, error) { + t, ok := enabledTemplates[strings.ToLower(n)] + if !ok { + return Template{ + HtmlTemplate: "OK", + StatusCode: http.StatusOK, + ServerHeader: "", + }, fmt.Errorf("template not found, using default") + } + + return t, nil +} diff --git a/pkg/wtemplate/wtemplate.go b/pkg/wtemplate/wtemplate.go deleted file mode 100644 index 6d07940..0000000 --- a/pkg/wtemplate/wtemplate.go +++ /dev/null @@ -1,52 +0,0 @@ -package wtemplate - -import "fmt" - -const ( - apacheTemplate = `

It works!

-

This is the default web page for this server.

-

The web server software is running but no content has been added, yet.

-` - nginxTemplate = ` - - -Welcome to nginx! - - - -

Welcome to nginx!

-

If you see this page, the nginx web server is successfully installed and -working. Further configuration is required.

- -

For online documentation and support please refer to -nginx.org.
-Commercial support is available at -nginx.com.

- -

Thank you for using nginx.

- -` -) - -var ( - knownTemplates = map[string]string{ - "apache": apacheTemplate, - "nginx": nginxTemplate, - } -) - -func GetTemplate(b string) (string, error) { - var err error - t, ok := knownTemplates[b] - if !ok { - err = fmt.Errorf("backend template not found") - } - - return t, err -} diff --git a/server/handler.go b/server/handler.go index 7299c3a..611eca4 100644 --- a/server/handler.go +++ b/server/handler.go @@ -6,7 +6,6 @@ import ( "net" "net/http" "slider/pkg/conf" - "slider/pkg/wtemplate" "strings" ) @@ -17,28 +16,12 @@ func (s *server) handleHTTPClient(w http.ResponseWriter, r *http.Request) { return } - var svrHeader string - status := http.StatusNotFound - tmpl := "Not Found" - - if s.webTemplate != "" { - t, tErr := wtemplate.GetTemplate(s.webTemplate) - if tErr == nil { - svrHeader = s.webTemplate - status = http.StatusOK - tmpl = t - } - } - - w.Header().Add("server", svrHeader) - var wErr error switch r.URL.Path { - case "/health": - _, wErr = w.Write([]byte("OK")) case "/": - w.WriteHeader(status) - _, wErr = w.Write([]byte(tmpl)) + w.Header().Add("server", s.webTemplate.ServerHeader) + w.WriteHeader(s.webTemplate.StatusCode) + _, wErr = w.Write([]byte(s.webTemplate.HtmlTemplate)) default: http.Redirect(w, r, "/", http.StatusMovedPermanently) } diff --git a/server/server.go b/server/server.go index 0a08d81..0ff861c 100644 --- a/server/server.go +++ b/server/server.go @@ -13,6 +13,7 @@ import ( "slider/pkg/scrypt" "slider/pkg/sio" "slider/pkg/slog" + "slider/pkg/web" "sync" "syscall" "time" @@ -49,7 +50,7 @@ type server struct { authOn bool certSaveOn bool keepalive time.Duration - webTemplate string + webTemplate web.Template } func NewServer(args []string) { @@ -63,7 +64,7 @@ func NewServer(args []string) { certJarFile := serverFlags.String("certs", "", "Path of a valid slider-certs json file") keyStore := serverFlags.Bool("keystore", false, "Store Server key for later use") keyPath := serverFlags.String("keypath", "", "Path for reading or storing a Server key") - webTemplate := serverFlags.String("template", "", "Mimic default web server page [apache|nginx]") + webTemplate := serverFlags.String("template", "", "Mimic web server page [apache|iis|nginx|tomcat]") serverFlags.Usage = func() { fmt.Println(serverHelp) serverFlags.PrintDefaults() @@ -172,9 +173,11 @@ func NewServer(args []string) { } } - if *webTemplate != "" { - s.webTemplate = *webTemplate + t, tErr := web.GetTemplate(*webTemplate) + if tErr != nil { + s.Logger.Errorf("%v", tErr) } + s.webTemplate = t fmtAddress := fmt.Sprintf("%s:%d", *address, *port) serverAddr, rErr := net.ResolveTCPAddr("tcp", fmtAddress)