Skip to content

Commit

Permalink
feat: extend web templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rurreac committed Jul 30, 2024
1 parent 916f44e commit fefe269
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 100 deletions.
11 changes: 7 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"slider/pkg/sio"
"slider/pkg/slog"
"slider/pkg/ssocks"
"slider/pkg/web"
"sync"
"time"

Expand All @@ -46,7 +47,7 @@ type client struct {
sessionTrackMutex sync.Mutex
isListener bool
firstRun bool
webTemplate string
webTemplate web.Template
}

const clientHelp = `
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 3 additions & 20 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"net/http"
"slider/pkg/conf"
"slider/pkg/wtemplate"
"strings"
)

Expand All @@ -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)
}
Expand Down
101 changes: 101 additions & 0 deletions pkg/web/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package web

const (
apacheTemplate = `<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>`
nginxTemplate = `<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>`
iisTemplate = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IIS Windows Server</title>
<style type="text/css">
<!--
body {
color:#000000;
background-color:#0072C6;
margin:0;
}
#container {
margin-left:auto;
margin-right:auto;
text-align:center;
}
a img {
border:none;
}
-->
</style>
</head>
<body>
<div id="container">
<a href="http://go.microsoft.com/fwlink/?linkid=66138&amp;clcid=0x409"><img src="iisstart.png" alt="IIS" width="960" height="600" /></a>
</div>
</body>
</html>`
tomcatTemplate = `<!doctype html>
<html lang="en">
<head><title>HTTP Status 404 – Not Found</title>
<style type="text/css">
body {
font-family:Tahoma,Arial,sans-serif;
}
h1, h2, h3, b {
color:white;background-color:#525D76;
}
h1 {
font-size:22px;
}
h2 {
font-size:16px;
}
h3 {
font-size:14px;
}
p {
font-size:12px;
}
a {
color:black;
}
.line {
height:1px;background-color:#525D76;border:none;
}
</style></head>
<body>
<h1>HTTP Status 404 – Not Found</h1>
<hr class="line" />
<p><b>Type</b> Status Report</p>
<p><b>Message</b> The requested resource [&#47;] is not available</p>
<p><b>Description</b>
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p>
<hr class="line" /><h3>Apache Tomcat</h3></body></html>`
)
51 changes: 51 additions & 0 deletions pkg/web/web.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 0 additions & 52 deletions pkg/wtemplate/wtemplate.go

This file was deleted.

23 changes: 3 additions & 20 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"net/http"
"slider/pkg/conf"
"slider/pkg/wtemplate"
"strings"
)

Expand All @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"slider/pkg/scrypt"
"slider/pkg/sio"
"slider/pkg/slog"
"slider/pkg/web"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -49,7 +50,7 @@ type server struct {
authOn bool
certSaveOn bool
keepalive time.Duration
webTemplate string
webTemplate web.Template
}

func NewServer(args []string) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fefe269

Please sign in to comment.