aboutsummaryrefslogtreecommitdiffstats
path: root/internal/port/main.go
blob: 5fb3dae967dbec6f9bb7dfbfc0708e68eac9ee0f (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package port

import (
	"crypto/md5"
	"fmt"
	"html"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
	"strings"

	"github.com/NYTimes/gziphandler"
	"github.com/davidbyttow/govips/pkg/vips"
	"github.com/gobuffalo/packr/v2"
	"github.com/temoto/robotstxt"
)

type AssetList struct {
	Style         string
	JS            string
	FontRegularW  string
	FontRegularW2 string
	FontBoldW     string
	FontBoldW2    string
}

type startTemplateVariables struct {
	Title   string
	URL     string
	Assets  AssetList
	Content string
}

func DefaultHandler(tpl *template.Template, startpagetext string, assetList AssetList) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		if err := tpl.Execute(w, startTemplateVariables{
			Title:   "Gopher/Gemini proxy",
			Assets:  assetList,
			Content: startpagetext,
		}); err != nil {
			log.Println("Template error: " + err.Error())
		}
	}
}

// RobotsTxtHandler returns the contents of the robots.txt file
// if configured and valid.
func RobotsTxtHandler(robotstxtdata []byte) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		if robotstxtdata == nil {
			http.Error(w, "Not Found", http.StatusNotFound)
			return
		}

		w.Header().Set("Content-Type", "text/plain")
		w.Write(robotstxtdata)
	}
}

func FaviconHandler(favicondata []byte) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		if favicondata == nil {
			http.Error(w, "Not Found", http.StatusNotFound)
			return
		}

		w.Header().Set("Content-Type", "image/vnd.microsoft.icon")
		w.Header().Set("Cache-Control", "max-age=2592000")
		w.Write(favicondata)
	}
}

func StyleHandler(styledata []byte) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Type", "text/css")
		w.Header().Set("Cache-Control", "max-age=2592000")
		w.Write(styledata)
	}
}

func JavaScriptHandler(jsdata []byte) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-Type", "text/javascript")
		w.Header().Set("Cache-Control", "max-age=2592000")
		w.Write(jsdata)
	}
}

func FontHandler(woff2 bool, fontdata []byte) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		if fontdata == nil {
			http.Error(w, "Not Found", http.StatusNotFound)
			return
		}

		if woff2 {
			w.Header().Set("Content-Type", "font/woff2")
		} else {
			w.Header().Set("Content-Type", "font/woff")
		}
		w.Header().Set("Cache-Control", "max-age=2592000")

		w.Write(fontdata)
	}
}

// ListenAndServe creates a listening HTTP server bound to
// the interface specified by bind and sets up a Gopher to HTTP
// proxy proxying requests as requested and by default will prozy
// to a Gopher server address specified by uri if no servers is
// specified by the request. The robots argument is a pointer to
// a robotstxt.RobotsData struct for testing user agents against
// a configurable robots.txt file.
func ListenAndServe(bind, startpagefile string, robotsfile string, robotsdebug bool, vipsconcurrency int) error {
	box := packr.New("assets", "../../assets")

	//
	// Robots

	var robotsdata *robotstxt.RobotsData

	robotstxtdata, err := ioutil.ReadFile(robotsfile)
	if err != nil {
		log.Printf("error reading robots.txt: %s", err)
		robotstxtdata = nil
	} else {
		robotsdata, err = robotstxt.FromBytes(robotstxtdata)
		if err != nil {
			log.Printf("error reading robots.txt: %s", err)
			robotstxtdata = nil
		}
	}

	//
	// Fonts

	fontRegularWData, err := box.Find("iosevka-fixed-ss03-regular.woff")
	if err != nil {
		fontRegularWData = []byte{}
	}
	fontRegularWAsset := fmt.Sprintf("/iosevka-fixed-ss03-regular-%x.woff", md5.Sum(fontRegularWData))

	fontRegularW2Data, err := box.Find("iosevka-fixed-ss03-regular.woff2")
	if err != nil {
		fontRegularW2Data = []byte{}
	}
	fontRegularW2Asset := fmt.Sprintf("/iosevka-fixed-ss03-regular-%x.woff2", md5.Sum(fontRegularW2Data))

	fontBoldWData, err := box.Find("iosevka-fixed-ss03-bold.woff")
	if err != nil {
		fontBoldWData = []byte{}
	}
	fontBoldWAsset := fmt.Sprintf("/iosevka-fixed-ss03-bold-%x.woff", md5.Sum(fontBoldWData))

	fontBoldW2Data, err := box.Find("iosevka-fixed-ss03-bold.woff2")
	if err != nil {
		fontBoldW2Data = []byte{}
	}
	fontBoldW2Asset := fmt.Sprintf("/iosevka-fixed-ss03-bold-%x.woff2", md5.Sum(fontBoldW2Data))

	//
	// Stylesheet

	styledata, err := box.Find("style.css")
	if err != nil {
		styledata = []byte{}
	}
	styleAsset := fmt.Sprintf("/style-%x.css", md5.Sum(styledata))

	//
	// JavaScript

	jsdata, err := box.Find("main.js")
	if err != nil {
		jsdata = []byte{}
	}
	jsAsset := fmt.Sprintf("/main-%x.js", md5.Sum(jsdata))

	//
	// Favicon

	favicondata, err := box.Find("favicon.ico")
	if err != nil {
		favicondata = []byte{}
	}

	//
	// Start page text

	startpagedata, err := ioutil.ReadFile(startpagefile)
	if err != nil {
		startpagedata, err = box.Find("startpage.txt")
		if err != nil {
			startpagedata = []byte{}
		}
	}
	startpagetext := string(startpagedata)

	//
	//

	funcMap := template.FuncMap{
		"safeHtml": func(s string) template.HTML {
			return template.HTML(s)
		},
		"safeCss": func(s string) template.CSS {
			return template.CSS(s)
		},
		"safeJs": func(s string) template.JS {
			return template.JS(s)
		},
		"HTMLEscape": func(s string) string {
			return html.EscapeString(s)
		},
		"split": strings.Split,
		"last": func(s []string) string {
			return s[len(s)-1]
		},
		"pop": func(s []string) []string {
			return s[:len(s)-1]
		},
		"replace": func(pattern, output string, input interface{}) string {
			var re = regexp.MustCompile(pattern)
			var inputStr = fmt.Sprintf("%v", input)
			return re.ReplaceAllString(inputStr, output)
		},
		"trimLeftChar": func(s string) string {
			for i := range s {
				if i > 0 {
					return s[i:]
				}
			}
			return s[:0]
		},
		"hasPrefix": func(s string, prefix string) bool {
			return strings.HasPrefix(s, prefix)
		},
		"hasSuffix": func(s string, suffix string) bool {
			return strings.HasSuffix(s, suffix)
		},
		"title": func(s string) string {
			return strings.Title(s)
		},
		"string": func(s interface{}) string {
			return fmt.Sprint(s)
		},
	}

	//

	tplBox := packr.New("templates", "./tpl")

	templates := template.New("main.html").Funcs(funcMap)

	for _, filename := range tplBox.List() {
		if strings.HasSuffix(filename, ".html") {
			tplStr, _ := tplBox.FindString(filename)
			templates, _ = templates.New(filename).Parse(tplStr)
		}
	}

	//

	startpageTpl := templates.Lookup("startpage.html")
	geminiTpl := templates.Lookup("gemini.html")
	gopherTpl := templates.Lookup("gopher.html")

	//
	//

	vips.Startup(&vips.Config{
		ConcurrencyLevel: vipsconcurrency,
	})

	assets := AssetList{
		Style:         styleAsset,
		JS:            jsAsset,
		FontRegularW:  fontRegularWAsset,
		FontRegularW2: fontRegularW2Asset,
		FontBoldW:     fontBoldWAsset,
		FontBoldW2:    fontBoldW2Asset,
	}

	http.Handle("/", gziphandler.GzipHandler(DefaultHandler(startpageTpl, startpagetext, assets)))
	http.Handle("/gopher/", gziphandler.GzipHandler(GopherHandler(gopherTpl, robotsdata, assets, robotsdebug)))
	http.Handle("/gemini/", gziphandler.GzipHandler(GeminiHandler(geminiTpl, robotsdata, assets, robotsdebug)))
	http.Handle("/robots.txt", gziphandler.GzipHandler(RobotsTxtHandler(robotstxtdata)))
	http.Handle("/favicon.ico", gziphandler.GzipHandler(FaviconHandler(favicondata)))
	http.Handle(styleAsset, gziphandler.GzipHandler(StyleHandler(styledata)))
	http.Handle(jsAsset, gziphandler.GzipHandler(JavaScriptHandler(jsdata)))
	http.HandleFunc(fontRegularWAsset, FontHandler(false, fontRegularWData))
	http.HandleFunc(fontRegularW2Asset, FontHandler(true, fontRegularW2Data))
	http.HandleFunc(fontBoldWAsset, FontHandler(false, fontBoldWData))
	http.HandleFunc(fontBoldW2Asset, FontHandler(true, fontBoldW2Data))
	//http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))

	return http.ListenAndServe(bind, nil)
}