-
Notifications
You must be signed in to change notification settings - Fork 4
/
gemini.go
420 lines (383 loc) · 11 KB
/
gemini.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2020 Paul Gorman. Licensed under the GPL.
// Gneto makes Gemini pages available over HTTP.
package main
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var htmlEscaper = strings.NewReplacer(
`&`, "&",
`'`, "'",
`<`, "<",
`"`, """,
)
// geminiQueryEscape returns a URL query string with "+" replaced by "%2O",
// as requred in secion 1.2 Gemini URI scheme of the Gemini specification.
func geminiQueryEscape(q string) string {
return strings.ReplaceAll(url.PathEscape(q), "+", "%2B")
}
// geminiToHTML reads Gemini text from rd, and writes its HTML equivalent to w.
// The source URL is stored in u.
func geminiToHTML(w http.ResponseWriter, u *url.URL, rd *bufio.Reader, td templateData) error {
var err error
list := false
pre := false
if envPassword != "" {
td.Logout = true
}
if len(clientCerts) > 0 {
td.ManageCerts = true
}
err = tmpls.ExecuteTemplate(w, "header-only.html.tmpl", td)
if err != nil {
log.Println("geminiToHTML:", err)
http.Error(w, "Internal Server Error", 500)
}
var eof error
var line string
for eof == nil {
line, eof = rd.ReadString("\n"[0])
if optLogLevel > 2 {
fmt.Println(line)
}
line = htmlEscaper.Replace(line)
if reGemPre.MatchString(line) {
if list {
list = false
io.WriteString(w, "</ul>\n")
}
if pre == true {
pre = false
io.WriteString(w, "</pre>\n")
continue
} else {
pre = true
// How can we provide alt text from reGemPre.FindStringSubmatch(line)[1]?
io.WriteString(w, "<pre>\n")
continue
}
} else {
if pre == true {
io.WriteString(w, line)
continue
}
}
if reGemBlank.MatchString(line) {
io.WriteString(w, "<br>\n")
} else if reGemH1.MatchString(line) {
if list == true {
list = false
io.WriteString(w, "</ul>\n")
}
io.WriteString(w, "<h1>"+reGemH1.FindStringSubmatch(line)[1]+"</h1>\n")
} else if reGemH2.MatchString(line) {
if list == true {
list = false
io.WriteString(w, "</ul>\n")
}
io.WriteString(w, "<h2>"+reGemH2.FindStringSubmatch(line)[1]+"</h2>\n")
} else if reGemH3.MatchString(line) {
if list == true {
list = false
io.WriteString(w, "</ul>\n")
}
io.WriteString(w, "<h3>"+reGemH3.FindStringSubmatch(line)[1]+"</h3>\n")
} else if reGemLink.MatchString(line) {
if list == true {
list = false
io.WriteString(w, "</ul>\n")
}
link := reGemLink.FindStringSubmatch(line)
lineURL, err := absoluteURL(u, link[1])
if err != nil {
io.WriteString(w, "<p>"+line+"</p>\n")
}
link[1] = lineURL.String()
if lineURL.Scheme == "gemini" {
if link[2] != "" {
io.WriteString(w, `<p><a href="/?url=`+geminiQueryEscape(link[1])+`">`+link[2]+
`</a> <span class="scheme"><a href="`+link[1]+`">[`+lineURL.Scheme+`]</a></span></p>`+"\n")
} else {
io.WriteString(w, `<p><a href="/?url=`+geminiQueryEscape(link[1])+`">`+link[1]+
`</a> <span class="scheme"><a href="`+link[1]+`">[`+lineURL.Scheme+`]</a></span></p>`+"\n")
}
} else {
if link[2] != "" {
io.WriteString(w, `<p><a href="`+link[1]+`">`+link[2]+
`</a> <span class="scheme"><a href="`+link[1]+`">[`+lineURL.Scheme+`]</a></span></p>`+"\n")
} else {
io.WriteString(w, `<p><a href="`+link[1]+`">`+link[1]+
`</a> <span class="scheme"><a href="`+link[1]+`">[`+lineURL.Scheme+`]</a></span></p>`+"\n")
}
}
} else if reGemList.MatchString(line) {
if list == false {
list = true
io.WriteString(w, "<ul>")
}
io.WriteString(w, "<li>"+reGemList.FindStringSubmatch(line)[1]+"</li>\n")
} else if reGemQuote.MatchString(line) {
if list == true {
list = false
io.WriteString(w, "</ul>")
}
io.WriteString(w, "<blockquote>"+reGemQuote.FindStringSubmatch(line)[1]+"</blockquote>\n")
} else {
if list {
list = false
io.WriteString(w, "</ul>\n")
}
io.WriteString(w, line+"<br>\n")
}
}
err = tmpls.ExecuteTemplate(w, "footer-only.html.tmpl", td)
if err != nil {
log.Println("geminiToHTML:", err)
http.Error(w, "Internal Server Error", 500)
}
return err
}
// proxyGemini finds the Gemini content at u.
func proxyGemini(w http.ResponseWriter, r *http.Request, u *url.URL) (*url.URL, error) {
var err error
var rd *bufio.Reader
var warning string
// Section 1.2 of the Gemini spec forbids userinfo URL components.
u.User = nil
if optHomeFile != "" && u.Scheme == "file" {
if optLogLevel > 1 {
log.Println("proxyGemini: home:", u.String())
}
f, err := os.Open(u.Path)
if err != nil {
return u, fmt.Errorf("proxyGemini: failed to open home file: %v", err)
}
var td templateData
if u.Scheme == "file" {
td.Title = "Gneto"
} else {
td.URL = u.String()
td.Warning = warning
td.Title = "Gneto " + td.URL
}
geminiToHTML(w, u, bufio.NewReader(f), td)
}
var port string
if u.Port() != "" {
port = u.Port()
} else {
port = "1965"
}
var clientCert tls.Certificate
var tc *tls.Config
if optHours != 0 {
clientCert = matchClientCert(u)
}
if len(clientCert.Certificate) == 0 {
tc = &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
}
} else {
tc = &tls.Config{
Certificates: []tls.Certificate{clientCert},
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
}
}
conn, err := tls.Dial("tcp", u.Hostname()+":"+port, tc)
if err != nil {
return u, fmt.Errorf("proxyGemini: tls.Dial error to %s: %v", u.String(), err)
}
defer conn.Close()
warning = checkServerCert(u, conn)
// Split the URL to avoid sending the fragment, if any, to the server.
fmt.Fprintf(conn, strings.SplitN(u.String(), "#", 2)[0]+"\r\n")
rd = bufio.NewReader(conn)
// Gemini specification section 3.1 forbids response headers not starting,
// with two digits, and a <META> longer than 1024 bytes.
header, _ := rd.Peek(1029)
if !reGemResponseHeader.Match(header) {
if optLogLevel > 1 {
log.Printf("proxyGemini: server %s sent malformed header: %s", u.Host, string(header))
}
return u, fmt.Errorf("proxyGemini: first 1029 bytes from %s did not contain a valid response header", u.Host)
}
status, err := rd.ReadString("\n"[0])
status = strings.Trim(status, "\r\n")
if err != nil {
return u, fmt.Errorf("proxyGemini: failed to read status line from buffer: %v", err)
}
if optLogLevel > 1 {
log.Printf("proxyGemini: %s status: %s", u.String(), status)
}
if !reStatus.MatchString(status) {
return u, fmt.Errorf("proxyGemini: invalid status line: %s", status)
}
switch status[0] {
case "1"[0]: // Status: input
var td templateData
td.URL = u.String()
td.Warning = warning
td.Title = "Gneto " + td.URL
if envPassword != "" {
td.Logout = true
}
if len(clientCerts) > 0 {
td.ManageCerts = true
}
td.Meta = status[3:]
switch status[1] {
case "1"[0]: // 11 == sensitive input/password
err = tmpls.ExecuteTemplate(w, "password.html.tmpl", td)
if err != nil {
err = fmt.Errorf("proxyGemini: failed to execute password template: %v", err)
break
}
default:
err = tmpls.ExecuteTemplate(w, "input.html.tmpl", td)
if err != nil {
err = fmt.Errorf("proxyGemini: failed to execute input template: %v", err)
break
}
}
case "2"[0]: // Status: success
if strings.Contains(status, " text/gemini") || len(strings.TrimSpace(status)) < 3 {
var td templateData
td.URL = u.String()
td.Title = "Gneto " + td.URL
c := reCharset.FindStringSubmatch(status)
if len(c) > 1 {
td.Charset = c[1]
} else {
td.Charset = "utf-8"
}
l := reLang.FindStringSubmatch(status)
if len(l) > 1 {
td.Lang = l[1]
} else {
td.Lang = optLang
}
if r.URL.Query().Get("source") != "" {
err = textToHTML(w, u, rd, td)
} else {
err = geminiToHTML(w, u, rd, td)
}
if err != nil {
break
}
} else if strings.Contains(status, " text") {
var td templateData
td.URL = u.String()
td.Title = "Gneto " + td.URL
err = textToHTML(w, u, rd, td)
if err != nil {
break
}
} else {
if optTextOnly {
err = fmt.Errorf("proxying of non-text types not allowed on this server")
} else {
err = serveFile(w, r, u, rd)
}
if err != nil {
break
}
}
case "3"[0]: // Status: redirect
var ru *url.URL
ru, err = url.Parse(strings.TrimSpace(strings.SplitAfterN(status, " ", 2)[1]))
if err != nil {
err = fmt.Errorf("proxyGemini: can't parse redirect URL %s: %v", strings.SplitAfterN(status, " ", 2)[1], err)
break
}
if ru.Host == "" {
ru.Host = u.Host
}
if ru.Scheme == "" {
ru.Scheme = u.Scheme
}
u = ru
errRedirect = errors.New(u.String())
err = errRedirect
case "6"[0]: // Status: Client certificate something
switch status[1] {
case "0"[0]: // 60 == Client certificate required
if optHours == 0 {
err = fmt.Errorf("proxyGemini: client certificated disabled by --hours option (status: %s)", status)
break
}
http.Redirect(w, r, "/certificate?url="+geminiQueryEscape(u.String()), http.StatusFound)
default: // Client certificat not autorized, not valid, etc.
err = fmt.Errorf("proxyGemini: %s", status)
}
default: // Statuses 40-59 indicate various failures.
err = fmt.Errorf("proxyGemini: status: %s", status)
}
return u, err
}
// serveFile saves a temporary file with the contents of rd, then serves it to w.
func serveFile(w http.ResponseWriter, r *http.Request, u *url.URL, rd *bufio.Reader) error {
var err error
fileName := u.String()[strings.LastIndex(u.String(), "/")+1:]
f, err := ioutil.TempFile("", "gneto*-"+fileName)
if err != nil {
err = fmt.Errorf("serveFile: failed to create temp file: %v", err)
}
defer os.Remove(f.Name()) // clean up
if _, err := f.ReadFrom(rd); err != nil {
err = fmt.Errorf("serveFile: failed to write to temp file: %v", err)
}
// Note: If we ever want to serve images inline, we'll have to revisit
// this content disposition header value.
w.Header().Set("Content-Disposition", "attachment; filename="+fileName)
http.ServeContent(w, r, fileName, time.Time{}, f)
if err := f.Close(); err != nil {
err = fmt.Errorf("serveFile: failed to close temp file: %v", err)
}
return err
}
// textToHTML reads non-Gemini text from rd, and writes its HTML equivalent to w.
// The source URL is stored in u.
func textToHTML(w http.ResponseWriter, u *url.URL, rd *bufio.Reader, td templateData) error {
var err error
if envPassword != "" {
td.Logout = true
}
if len(clientCerts) > 0 {
td.ManageCerts = true
}
err = tmpls.ExecuteTemplate(w, "header-only.html.tmpl", td)
if err != nil {
log.Println("textToHTML:", err)
http.Error(w, "Internal Server Error", 500)
}
io.WriteString(w, `<pre id="non-gemini-text">`+"\n")
var eof error
var line string
for eof == nil {
line, eof = rd.ReadString("\n"[0])
if optLogLevel > 2 {
fmt.Println(line)
}
line = htmlEscaper.Replace(line)
io.WriteString(w, line+"\n")
}
io.WriteString(w, "</pre>\n")
err = tmpls.ExecuteTemplate(w, "footer-only.html.tmpl", td)
if err != nil {
log.Println("textToHTML:", err)
http.Error(w, "Internal Server Error", 500)
}
return err
}