diff options
Diffstat (limited to 'gopherproxy.go')
| -rw-r--r-- | gopherproxy.go | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/gopherproxy.go b/gopherproxy.go index 11c08d5..62b7446 100644 --- a/gopherproxy.go +++ b/gopherproxy.go | |||
| @@ -10,6 +10,7 @@ import ( | |||
| 10 | "log" | 10 | "log" |
| 11 | "net/http" | 11 | "net/http" |
| 12 | "net/url" | 12 | "net/url" |
| 13 | "regexp" | ||
| 13 | "strings" | 14 | "strings" |
| 14 | 15 | ||
| 15 | "github.com/temoto/robotstxt" | 16 | "github.com/temoto/robotstxt" |
| @@ -17,6 +18,8 @@ import ( | |||
| 17 | "github.com/prologic/go-gopher" | 18 | "github.com/prologic/go-gopher" |
| 18 | 19 | ||
| 19 | "github.com/gobuffalo/packr" | 20 | "github.com/gobuffalo/packr" |
| 21 | |||
| 22 | "github.com/davidbyttow/govips/pkg/vips" | ||
| 20 | ) | 23 | ) |
| 21 | 24 | ||
| 22 | type Item struct { | 25 | type Item struct { |
| @@ -25,7 +28,7 @@ type Item struct { | |||
| 25 | Text string | 28 | Text string |
| 26 | } | 29 | } |
| 27 | 30 | ||
| 28 | func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, uri string, hostport string, d gopher.Directory) error { | 31 | func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext string, jstext string, uri string, hostport string, d gopher.Directory) error { |
| 29 | var title string | 32 | var title string |
| 30 | 33 | ||
| 31 | out := make([]Item, len(d.Items)) | 34 | out := make([]Item, len(d.Items)) |
| @@ -78,9 +81,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st | |||
| 78 | Title string | 81 | Title string |
| 79 | URI string | 82 | URI string |
| 80 | Style string | 83 | Style string |
| 84 | Script string | ||
| 81 | Lines []Item | 85 | Lines []Item |
| 82 | RawText string | 86 | RawText string |
| 83 | }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, out, ""}) | 87 | }{title, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, out, ""}) |
| 84 | } | 88 | } |
| 85 | 89 | ||
| 86 | // GopherHandler returns a Handler that proxies requests | 90 | // GopherHandler returns a Handler that proxies requests |
| @@ -88,7 +92,7 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, styletext st | |||
| 88 | // to the request path and renders the content using the provided template. | 92 | // to the request path and renders the content using the provided template. |
| 89 | // The optional robots parameters points to a robotstxt.RobotsData struct | 93 | // The optional robots parameters points to a robotstxt.RobotsData struct |
| 90 | // to test user agents against a configurable robotst.txt file. | 94 | // to test user agents against a configurable robotst.txt file. |
| 91 | func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, uri string) http.HandlerFunc { | 95 | func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, robotsdebug bool, styletext string, jstext string, uri string) http.HandlerFunc { |
| 92 | return func(w http.ResponseWriter, req *http.Request) { | 96 | return func(w http.ResponseWriter, req *http.Request) { |
| 93 | agent := req.UserAgent() | 97 | agent := req.UserAgent() |
| 94 | path := strings.TrimPrefix(req.URL.Path, "/") | 98 | path := strings.TrimPrefix(req.URL.Path, "/") |
| @@ -140,14 +144,23 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, rob | |||
| 140 | Title string | 144 | Title string |
| 141 | URI string | 145 | URI string |
| 142 | Style string | 146 | Style string |
| 147 | Script string | ||
| 143 | RawText string | 148 | RawText string |
| 144 | Lines []Item | 149 | Lines []Item |
| 145 | }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, buf.String(), nil}) | 150 | }{uri, fmt.Sprintf("%s/%s", hostport, uri), styletext, jstext, buf.String(), nil}) |
| 151 | } else if parts[1] == "T" { | ||
| 152 | _, _, err = vips.NewTransform(). | ||
| 153 | Load(res.Body). | ||
| 154 | ResizeStrategy(vips.ResizeStrategyAuto). | ||
| 155 | ResizeWidth(160). | ||
| 156 | Quality(75). | ||
| 157 | Output(w). | ||
| 158 | Apply() | ||
| 146 | } else { | 159 | } else { |
| 147 | io.Copy(w, res.Body) | 160 | io.Copy(w, res.Body) |
| 148 | } | 161 | } |
| 149 | } else { | 162 | } else { |
| 150 | if err := renderDirectory(w, tpl, styletext, uri, hostport, res.Dir); err != nil { | 163 | if err := renderDirectory(w, tpl, styletext, jstext, uri, hostport, res.Dir); err != nil { |
| 151 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | 164 | io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) |
| 152 | return | 165 | return |
| 153 | } | 166 | } |
| @@ -240,6 +253,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error | |||
| 240 | styletext = "" | 253 | styletext = "" |
| 241 | } | 254 | } |
| 242 | 255 | ||
| 256 | jstext, err := box.FindString("main.js") | ||
| 257 | if err != nil { | ||
| 258 | jstext = "" | ||
| 259 | } | ||
| 260 | |||
| 243 | favicondata, err := box.Find("favicon.ico") | 261 | favicondata, err := box.Find("favicon.ico") |
| 244 | if err != nil { | 262 | if err != nil { |
| 245 | favicondata = []byte{} | 263 | favicondata = []byte{} |
| @@ -257,6 +275,9 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error | |||
| 257 | "safeCss": func(s string) template.CSS { | 275 | "safeCss": func(s string) template.CSS { |
| 258 | return template.CSS(s) | 276 | return template.CSS(s) |
| 259 | }, | 277 | }, |
| 278 | "safeJs": func(s string) template.JS { | ||
| 279 | return template.JS(s) | ||
| 280 | }, | ||
| 260 | "HTMLEscape": func(s string) string { | 281 | "HTMLEscape": func(s string) string { |
| 261 | return html.EscapeString(s) | 282 | return html.EscapeString(s) |
| 262 | }, | 283 | }, |
| @@ -267,6 +288,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error | |||
| 267 | "pop": func(s []string) []string { | 288 | "pop": func(s []string) []string { |
| 268 | return s[:len(s)-1] | 289 | return s[:len(s)-1] |
| 269 | }, | 290 | }, |
| 291 | "replace": func(pattern, output string, input interface{}) string { | ||
| 292 | var re = regexp.MustCompile(pattern) | ||
| 293 | var inputStr = fmt.Sprintf("%v", input) | ||
| 294 | return re.ReplaceAllString(inputStr, output) | ||
| 295 | }, | ||
| 270 | } | 296 | } |
| 271 | 297 | ||
| 272 | tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) | 298 | tpl, err = template.New("gophermenu").Funcs(funcMap).Parse(tpltext) |
| @@ -274,7 +300,11 @@ func ListenAndServe(bind, robotsfile string, robotsdebug bool, uri string) error | |||
| 274 | log.Fatal(err) | 300 | log.Fatal(err) |
| 275 | } | 301 | } |
| 276 | 302 | ||
| 277 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, uri)) | 303 | vips.Startup(&vips.Config{ |
| 304 | ConcurrencyLevel: 2, | ||
| 305 | }) | ||
| 306 | |||
| 307 | http.HandleFunc("/", GopherHandler(tpl, robotsdata, robotsdebug, styletext, jstext, uri)) | ||
| 278 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) | 308 | http.HandleFunc("/robots.txt", RobotsTxtHandler(robotstxtdata)) |
| 279 | http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) | 309 | http.HandleFunc("/favicon.ico", FaviconHandler(favicondata)) |
| 280 | http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) | 310 | http.HandleFunc("/iosevka-term-ss03-regular.woff", FontHandler(false, fontdataw)) |
