-
Notifications
You must be signed in to change notification settings - Fork 5
/
duckling-proxy.go
224 lines (172 loc) · 7.07 KB
/
duckling-proxy.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
package main
import (
"github.com/LukeEmmet/html2gemini"
gemini "github.com/makeworld-the-better-one/go-gemini"
flag "github.com/spf13/pflag"
"strconv"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"time"
)
var version = "0.2.1"
type WebPipeHandler struct {
}
var (
citationStart = flag.IntP("citationStart", "s", 1, "Start citations from this index")
citationMarkers = flag.BoolP("citationMarkers", "m", false, "Use footnote style citation markers")
numberedLinks = flag.BoolP("numberedLinks", "n", false, "Number the links")
prettyTables = flag.BoolP("prettyTables", "r", false, "Pretty tables - works with most simple tables")
emitImagesAsLinks = flag.BoolP("emitImagesAsLinks", "e", false, "Emit links to included images")
linkEmitFrequency = flag.IntP("linkEmitFrequency", "l", 2, "Emit gathered links through the document after this number of paragraphs")
serverCert = flag.StringP("serverCert", "c", "", "serverCert path. ")
serverKey = flag.StringP("serverKey", "k", "", "serverKey path. ")
userAgent = flag.StringP("userAgent", "u", "", "User agent for HTTP requests\n")
maxDownloadTime = flag.IntP("maxDownloadTime", "t", 10, "Max download time (s)\n")
maxConnectTime = flag.IntP("maxConnectTime", "T", 5, "Max connect time (s)\n")
port = flag.IntP("port", "p", 1965, "Server port")
address = flag.StringP("address", "a", "127.0.0.1", "Bind to address\n")
unfiltered = flag.BoolP("unfiltered", "", false, "Do not filter text/html to text/gemini")
verFlag = flag.BoolP("version", "v", false, "Find out what version of Duckling Proxy you're running")
)
func fatal(format string, a ...interface{}) {
urlError(format, a...)
os.Exit(1)
}
func urlError(format string, a ...interface{}) {
format = "Error: " + strings.TrimRight(format, "\n") + "\n"
fmt.Fprintf(os.Stderr, format, a...)
}
func info(format string, a ...interface{}) {
format = "Info: " + strings.TrimRight(format, "\n") + "\n"
fmt.Fprintf(os.Stderr, format, a...)
}
func check(e error) {
if e != nil {
panic(e)
os.Exit(1)
}
}
func htmlToGmi(inputHtml string) (string, error) {
//convert html to gmi
options := html2gemini.NewOptions()
options.PrettyTables = *prettyTables
options.CitationStart = *citationStart
options.LinkEmitFrequency = *linkEmitFrequency
options.CitationMarkers = *citationMarkers
options.NumberedLinks = *numberedLinks
options.EmitImagesAsLinks = *emitImagesAsLinks
//dont use an extra line to separate header from body, but
//do separate each row visually
options.PrettyTablesOptions.HeaderLine = false
options.PrettyTablesOptions.RowLine = true
//pretty tables option is somewhat experimental
//and the column positions not always correct
//so use invisible borders of spaces for now
options.PrettyTablesOptions.CenterSeparator = " "
options.PrettyTablesOptions.ColumnSeparator = " "
options.PrettyTablesOptions.RowSeparator = " "
ctx := html2gemini.NewTraverseContext(*options)
return html2gemini.FromString(inputHtml, *ctx)
}
func (h WebPipeHandler) Handle(r gemini.Request) *gemini.Response {
url := r.URL.String()
if r.URL.Scheme != "http" && r.URL.Scheme != "https" {
//any other schemes are not implemented by this proxy
return &gemini.Response{53, "Scheme not supported: " + r.URL.Scheme, nil, nil}
}
info("Retrieve: %s", r.URL.String())
//see https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
//also https://gist.github.com/ijt/950790/fca88967337b9371bb6f7155f3304b3ccbf3946f
connectTimeout := time.Second * time.Duration(*maxConnectTime)
clientTimeout := time.Second * time.Duration(*maxDownloadTime)
//create custom transport with timeout
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: connectTimeout,
}).Dial,
TLSHandshakeTimeout: connectTimeout,
}
//create custom client with timeout
var netClient = &http.Client{
Timeout: clientTimeout,
Transport: netTransport,
}
//fmt.Println("making request")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return &gemini.Response{43, "Could not connect to remote HTTP host", nil, nil}
}
//set user agent if specified
if *userAgent != "" {
req.Header.Add("User-Agent", *userAgent)
}
response, err := netClient.Do(req)
if err != nil {
return &gemini.Response{43, "Remote host did not respond with valid HTTP", nil, nil}
}
defer response.Body.Close()
//final response (may have redirected)
if url != response.Request.URL.String() {
//notify of target location on stderr
//see https://stackoverflow.com/questions/16784419/in-golang-how-to-determine-the-final-url-after-a-series-of-redirects
info("Redirected to: %s", response.Request.URL.String())
//tell the client to get it from a different location otherwise the client
//wont know the baseline for link refs
return &gemini.Response{30, response.Request.URL.String(), nil, nil}
}
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
abandonMsg := fmt.Sprintf("Download abandoned after %d seconds: %s", *maxDownloadTime, response.Request.URL.String())
info(abandonMsg)
return &gemini.Response{43, abandonMsg, nil, nil}
}
if response.StatusCode == 200 {
contentType := response.Header.Get("Content-Type")
info("Content-Type: %s", contentType)
var body io.ReadCloser
if !*unfiltered && strings.Contains(contentType, "text/html") {
info("Converting to text/gemini: %s", r.URL.String())
//translate html to gmi
gmi, err := htmlToGmi(string(contents))
if err != nil {
return &gemini.Response{42, "HTML to GMI conversion failure", nil, nil}
}
//add a footer to communicate that the content was filtered and not original
//also the link provides a clickable link that the user can activate to launch a browser, depending on their client
//behaviour (e.g. Ctrl-Click or similar)
footer := ""
footer += "\n\n──────────────────── 🦆 ──────────────────── 🦆 ──────────────────── \n\n"
footer += "Web page filtered and simplified by Duckling Proxy v" + version + ". To view the original content, open the page in your system web browser.\n"
footer += "=> " + r.URL.String() + " Source page \n"
body = ioutil.NopCloser(strings.NewReader(string(gmi) + footer))
contentType = "text/gemini"
} else {
//let everything else through with the same content type
body = ioutil.NopCloser(strings.NewReader(string(contents)))
}
return &gemini.Response{20, contentType, body, nil}
} else if response.StatusCode == 404 {
return &gemini.Response{51, "Not found", nil, nil}
} else {
return &gemini.Response{50, "Failure: HTTP status: " + response.Status, nil, nil}
}
}
func main() {
flag.Parse()
if *verFlag {
fmt.Println("Duckling Proxy v" + version)
return
}
handler := WebPipeHandler{}
info("Starting Duckling Proxy v%s on %s port: %d", version, *address, *port)
err := gemini.ListenAndServe(*address+":"+strconv.Itoa(*port), *serverCert, *serverKey, handler)
if err != nil {
log.Fatal(err)
}
}