-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
309 lines (274 loc) · 7.97 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"html"
"io"
"log"
"mime"
"net"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/boomlinde/dpi"
"github.com/boomlinde/gemini/client"
"github.com/boomlinde/gemini/gemini"
)
var tofuLocation string
var gem *client.Client
const pinprefix = "gemini:pin:"
const inputprefix = "gemini:input:"
var config struct {
AutoPin bool `json:"autoPin"`
}
func loadConfig(path string) error {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&config); err != nil {
return err
}
return nil
}
func main() {
log.SetPrefix("[dillo-gemini]")
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
if err := loadConfig(filepath.Join(usr.HomeDir, ".dillo", "gemini", "config.json")); err != nil {
log.Println("failed to load config")
}
gem = client.NewClient(filepath.Join(usr.HomeDir, ".dillo", "gemini", "pinned"))
gem.Dialer = &net.Dialer{
Timeout: 30 * time.Second,
}
err = dpi.AutoRun(func(tag map[string]string, w io.Writer) error {
// The writer handed to us from AutoRun will never return an error.
// It will fail silently and ignore writes after the first error.
switch tag["cmd"] {
case "open_url":
url := tag["url"]
// Handle special pin URI
if strings.HasPrefix(url, pinprefix) {
topin := url[len(pinprefix):]
log.Println("pinning certificate for", topin)
if err := gem.Pin(topin); err != nil {
interrpage(w, url, err)
return err
}
redirect(w, url, topin)
}
// Handle special input URI
if strings.HasPrefix(url, inputprefix) {
return handleInput(w, url[len(inputprefix):])
}
// Normalize the URL (somewhat; net/url is too relaxed)
norm, err := normalized(url)
if err != nil {
interrpage(w, url, err)
return err
}
url = norm
// Handle connection errors
request:
r, err := gem.Request(url)
if err != nil {
if client.Untrusted(err) || client.Invalid(err) {
if config.AutoPin {
log.Println("pinning certificate for", url)
if err := gem.Pin(url); err != nil {
interrpage(w, url, err)
return err
}
goto request
} else {
pinpage(w, url, err)
return dpi.Done
}
} else {
interrpage(w, url, err)
return err
}
}
defer r.Close()
header, err := client.GetHeader(r)
if err != nil {
interrpage(w, url, err)
return err
}
switch header.Code / 10 {
case 1:
if header.Code == 11 {
inputpage(w, url, "password", header.Meta)
} else {
inputpage(w, url, "text", header.Meta)
}
case 2:
mime, params, err := mime.ParseMediaType(header.Meta)
if err != nil {
interrpage(w, url, fmt.Errorf("failed to parse media type: %w", err))
return err
}
// We only support a limited number of charsets.
if charset, ok := params["charset"]; ok {
charset = strings.ToLower(charset)
if charset != "us-ascii" && charset != "utf-8" {
err := fmt.Errorf("unsupported encoding: %s", charset)
interrpage(w, url, err)
}
}
if mime == "text/gemini" {
lines, err := gemini.Itemize(r)
if err != nil {
interrpage(w, url, fmt.Errorf("failed to parse text/gemini: %w", err))
return err
}
startPage(w, url, "text/html")
w.Write([]byte("<!DOCTYPE html>\n"))
w.Write([]byte("<html>\n"))
w.Write([]byte("<head>\n"))
w.Write([]byte("<title>"))
w.Write([]byte(html.EscapeString(url)))
w.Write([]byte("</title>\n"))
w.Write([]byte("</head>\n"))
w.Write([]byte("<body id='gemini-plugin-body'>\n"))
w.Write([]byte("<div>\n"))
// error can never happen here
_ = gemini.ToHtml(lines, w)
w.Write([]byte("</div>\n"))
w.Write([]byte("</body>\n"))
w.Write([]byte("</html>\n"))
return dpi.Done
}
startPage(w, url, header.Meta)
if _, err := io.Copy(w, r); err != nil {
interrpage(w, url, fmt.Errorf("failed to write content: %w", err))
return err
}
case 3:
redirect(w, url, header.Meta)
default:
startPage(w, url, "text/plain")
fmt.Fprintf(w, "%d %s\n", header.Code, header.Meta)
}
return dpi.Done
case "DpiBye":
os.Exit(0)
}
return nil
})
if err != dpi.Done {
log.Fatal(err)
}
}
func redirect(w io.Writer, from, to string) {
startPage(w, from, "text/html")
w.Write([]byte("<!DOCTYPE html>\n"))
w.Write([]byte("<html>\n"))
w.Write([]byte("<head>\n"))
w.Write([]byte("<title>"))
fmt.Fprintf(w, "Redirecting to %s", html.EscapeString(to))
w.Write([]byte("</title>\n"))
fmt.Fprintf(w, "<meta http-equiv=\"Refresh\" content=\"0; url='%s'\" />", to)
w.Write([]byte("</head>\n"))
w.Write([]byte("<body id='gemini-plugin-body'>\n"))
w.Write([]byte("</body>\n"))
w.Write([]byte("</html>\n"))
}
func pinpage(w io.Writer, uri string, tlserr error) {
parsed, err := url.Parse(uri)
if err != nil {
panic(err)
}
startPage(w, parsed.String(), "text/html")
w.Write([]byte("<!DOCTYPE html>\n"))
w.Write([]byte("<html>"))
fmt.Fprintf(w, "<head><title>Pin %s</title></head>", html.EscapeString(parsed.String()))
w.Write([]byte("<body id='gemini-plugin-body'>"))
w.Write([]byte("<h2>Suspicious or unknown certificate</h2>"))
fmt.Fprintf(w, "<p><b>%s</b></p>", html.EscapeString(tlserr.Error()))
fmt.Fprintf(w, "<p><a href='gemini:pin:%s'>Pin %s and continue</a></p>", parsed, html.EscapeString(parsed.Hostname()))
w.Write([]byte("</body>"))
w.Write([]byte("</html>"))
}
func interrpage(w io.Writer, uri string, err error) {
startPage(w, uri, "text/html")
w.Write([]byte("<!DOCTYPE html>\n"))
w.Write([]byte("<html>"))
w.Write([]byte("<head><title>Error</title></head>"))
w.Write([]byte("<body id='gemini-plugin-body'>"))
fmt.Fprintf(w, "<h2>Error on %s</h2>", html.EscapeString(uri))
fmt.Fprintf(w, "<p><b>error: %s</b></p>", html.EscapeString(err.Error()))
w.Write([]byte("</body>"))
w.Write([]byte("</html>"))
}
func inputpage(w io.Writer, uri, typ, desc string) {
e := html.EscapeString
startPage(w, uri, "text/html")
w.Write([]byte("<!DOCTYPE html>\n"))
w.Write([]byte("<head>\n"))
w.Write([]byte("<title>"))
w.Write([]byte(e(uri)))
w.Write([]byte("</title>\n"))
w.Write([]byte("</head>\n"))
w.Write([]byte("<body id='gemini-plugin-body'>\n"))
w.Write([]byte("<div>\n"))
fmt.Fprintf(w, "<form action='%s%s' method='get'>\n", inputprefix, uri)
fmt.Fprintf(w, "<label for='q'>%s</label><br>\n", e(desc))
fmt.Fprintf(w, "<input type='%s' id='q' name='q'><br>", typ)
w.Write([]byte("<input type='submit' value='Submit'>"))
w.Write([]byte("</form>\n"))
w.Write([]byte("</div>\n"))
w.Write([]byte("</body>\n"))
w.Write([]byte("</html>\n"))
}
func handleInput(w io.Writer, u string) error {
spl := strings.SplitN(u, "?q=", 2)
if len(spl) != 2 {
return fmt.Errorf("malformed query: %s", u)
}
target := spl[0]
query, err := url.QueryUnescape(spl[1])
if len(spl) != 2 {
return fmt.Errorf("failed to unescape query: %w", err)
}
// hack to encode query with %20 instead of + for spaces
query = (&url.URL{Path: query}).String()
// hack to encode + as %2B
query = strings.Replace(query, "+", "%2B", -1)
redirect(w, inputprefix+u, target+"?"+query)
return dpi.Done
}
func startPage(w io.Writer, url, mime string) {
dpi.Tag(w, map[string]string{"cmd": "start_send_page", "url": url})
fmt.Fprintf(w, "Content-Type: %s\r\n\r\n", mime)
}
func normalized(in string) (string, error) {
u, err := url.Parse(in)
if err != nil {
return "", err
}
// RFC 3986 6.2.3
// In general, a URI that uses the generic syntax for authority
// with an empty path should be normalized to a path of "/".
if u.Path == "" && u.Host != "" {
u.Path = "/"
}
// Likewise, an explicit ":port", for which the port is empty or
// the default for the scheme, is equivalent to one where the port
// and its ":" delimiter are elided and thus should be removed by
// scheme-based normalization.
if strings.HasSuffix(u.Host, ":") {
u.Host = u.Host[:len(u.Host)-1]
}
return u.String(), nil
}