-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
about.go
110 lines (93 loc) · 2.51 KB
/
about.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bytes"
"fmt"
"html/template"
"io"
"net"
"net/http"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
var pageTmpl = template.Must(template.ParseFS(content, "page.html"))
func about(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
renderPage("about.md", w, req, rConn)
}
func fun(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
renderPage("fun/index.md", w, req, rConn)
}
func renderPage(page string, w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
remoteAddr := rConn.RemoteAddr().(*net.TCPAddr)
w.Header().Add("X-Super-Cow-Powers", "curl "+*flagHost+"/moo")
w.Header().Add("Cache-Control", "no-store")
f, err := content.Open(page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
var buf bytes.Buffer
source, _ := io.ReadAll(f)
md := goldmark.New(
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
))
if err := md.Convert(source, &buf); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params := map[string]interface{}{
"IPv4": remoteAddr.IP.To4(),
"IPv6": remoteAddr.IP,
"RemoteAddr": remoteAddr,
"Request": req,
"Devel": len(*flagLocation) == 0,
}
mdBuf := strings.Builder{}
mdTmpl, err := template.New(page).Parse(buf.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := mdTmpl.Execute(&mdBuf, params); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params["Content"] = template.HTML(mdBuf.String())
if err := pageTmpl.Execute(w, params); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func funThing(w http.ResponseWriter, req *http.Request, rConn *RecordingConn) {
s := strings.Split(req.URL.Path, "/")
if len(s) < 2 {
http.Error(w, "Not found", http.StatusNotFound)
return
}
page := s[len(s)-1]
for _, r := range page {
if r < 'a' || r > 'z' {
http.Error(w, "Not found", http.StatusNotFound)
return
}
}
name := fmt.Sprintf("fun/%s.html", page)
f, err := content.Open(name)
if err != nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
fi, _ := f.Stat()
ff, ok := f.(io.ReadSeeker)
if !ok {
http.Error(w, "Not found", http.StatusNotFound)
return
}
http.ServeContent(w, req, name, fi.ModTime(), ff)
}