From 8c53aa278f839e33f0ccdca355b562419317be15 Mon Sep 17 00:00:00 2001 From: James Mills Date: Fri, 23 Sep 2016 00:35:54 +1000 Subject: Add support for proxying to anywhere in Gopherspace --- README.md | 7 +------ main.go | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9e4831a..057500c 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,7 @@ gopherproxy is written in Go (#golang) using the $ gopherproxy ``` -By default gopherproxy will proxy requests to a locally running Gopher server -on gopher://localhost:70/ -- To change where to proxy to: - -```#!bash -$ gopherproxy -host gopher.floodgap.com -``` +Then simply visit: http://localhost/gopher.floodgap.com ## License diff --git a/main.go b/main.go index 32ae98c..766ece3 100644 --- a/main.go +++ b/main.go @@ -16,8 +16,6 @@ import ( var ( bind = flag.String("bind", ":80", "[int]:port to bind to") - host = flag.String("host", "localhost", "host to proxy to") - port = flag.Int("port", 70, "port to proxy to") tpl *template.Template ) @@ -28,7 +26,7 @@ type tplRow struct { Text string } -func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Directory) error { +func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error { out := make([]tplRow, len(d)) for i, x := range d { @@ -47,7 +45,8 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir } else { tr.Link = template.URL( fmt.Sprintf( - "/%s%s", + "/%s/%s%s", + fmt.Sprintf("%s:%d", x.Host, x.Port), string(byte(x.Type)), url.QueryEscape(x.Selector), ), @@ -60,17 +59,18 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir return tpl.Execute(w, struct { Title string Lines []tplRow - }{*host, out}) + }{hostport, out}) } func proxy(w http.ResponseWriter, req *http.Request) { - path := strings.TrimPrefix(req.URL.Path, "/") + parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/") + hostport := parts[0] + path := strings.Join(parts[1:], "/") res, err := gopher.Get( fmt.Sprintf( - "gopher://%s:%d/%s", - *host, - *port, + "gopher://%s/%s", + hostport, url.QueryEscape(path), ), ) @@ -82,7 +82,7 @@ func proxy(w http.ResponseWriter, req *http.Request) { if res.Body != nil { io.Copy(w, res.Body) } else { - if err := renderDirectory(w, tpl, res.Dir); err != nil { + if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil { io.WriteString(w, fmt.Sprintf("Error:
%s
", err)) return } -- cgit v1.2.3-70-g09d2