aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2016-09-23 00:35:54 +1000
committerJames Mills <prologic@shortcircuit.net.au>2016-09-23 00:35:54 +1000
commit8c53aa278f839e33f0ccdca355b562419317be15 (patch)
tree886d68b731d0c7e02c5935a4815b01c01676dde8
parentAdd query support (diff)
downloadgopherproxy-8c53aa278f839e33f0ccdca355b562419317be15.tar.gz
gopherproxy-8c53aa278f839e33f0ccdca355b562419317be15.tar.bz2
gopherproxy-8c53aa278f839e33f0ccdca355b562419317be15.zip
Add support for proxying to anywhere in Gopherspace
-rw-r--r--README.md7
-rw-r--r--main.go20
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
18$ gopherproxy 18$ gopherproxy
19``` 19```
20 20
21By default gopherproxy will proxy requests to a locally running Gopher server 21Then simply visit: http://localhost/gopher.floodgap.com
22on gopher://localhost:70/ -- To change where to proxy to:
23
24```#!bash
25$ gopherproxy -host gopher.floodgap.com
26```
27 22
28## License 23## License
29 24
diff --git a/main.go b/main.go
index 32ae98c..766ece3 100644
--- a/main.go
+++ b/main.go
@@ -16,8 +16,6 @@ import (
16 16
17var ( 17var (
18 bind = flag.String("bind", ":80", "[int]:port to bind to") 18 bind = flag.String("bind", ":80", "[int]:port to bind to")
19 host = flag.String("host", "localhost", "host to proxy to")
20 port = flag.Int("port", 70, "port to proxy to")
21 19
22 tpl *template.Template 20 tpl *template.Template
23) 21)
@@ -28,7 +26,7 @@ type tplRow struct {
28 Text string 26 Text string
29} 27}
30 28
31func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Directory) error { 29func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport string, d gopher.Directory) error {
32 out := make([]tplRow, len(d)) 30 out := make([]tplRow, len(d))
33 31
34 for i, x := range d { 32 for i, x := range d {
@@ -47,7 +45,8 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
47 } else { 45 } else {
48 tr.Link = template.URL( 46 tr.Link = template.URL(
49 fmt.Sprintf( 47 fmt.Sprintf(
50 "/%s%s", 48 "/%s/%s%s",
49 fmt.Sprintf("%s:%d", x.Host, x.Port),
51 string(byte(x.Type)), 50 string(byte(x.Type)),
52 url.QueryEscape(x.Selector), 51 url.QueryEscape(x.Selector),
53 ), 52 ),
@@ -60,17 +59,18 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, d gopher.Dir
60 return tpl.Execute(w, struct { 59 return tpl.Execute(w, struct {
61 Title string 60 Title string
62 Lines []tplRow 61 Lines []tplRow
63 }{*host, out}) 62 }{hostport, out})
64} 63}
65 64
66func proxy(w http.ResponseWriter, req *http.Request) { 65func proxy(w http.ResponseWriter, req *http.Request) {
67 path := strings.TrimPrefix(req.URL.Path, "/") 66 parts := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/")
67 hostport := parts[0]
68 path := strings.Join(parts[1:], "/")
68 69
69 res, err := gopher.Get( 70 res, err := gopher.Get(
70 fmt.Sprintf( 71 fmt.Sprintf(
71 "gopher://%s:%d/%s", 72 "gopher://%s/%s",
72 *host, 73 hostport,
73 *port,
74 url.QueryEscape(path), 74 url.QueryEscape(path),
75 ), 75 ),
76 ) 76 )
@@ -82,7 +82,7 @@ func proxy(w http.ResponseWriter, req *http.Request) {
82 if res.Body != nil { 82 if res.Body != nil {
83 io.Copy(w, res.Body) 83 io.Copy(w, res.Body)
84 } else { 84 } else {
85 if err := renderDirectory(w, tpl, res.Dir); err != nil { 85 if err := renderDirectory(w, tpl, hostport, res.Dir); err != nil {
86 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) 86 io.WriteString(w, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err))
87 return 87 return
88 } 88 }