diff options
author | James Mills <prologic@shortcircuit.net.au> | 2016-09-22 11:22:10 +1000 |
---|---|---|
committer | James Mills <prologic@shortcircuit.net.au> | 2016-09-22 11:22:10 +1000 |
commit | 773606bd45e283d05a4aaa06787289ad322ca7b7 (patch) | |
tree | f82287ca30e93aa5b30c8cc258e01b0ce851891a /main.go | |
download | gopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.tar.gz gopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.tar.bz2 gopherproxy-773606bd45e283d05a4aaa06787289ad322ca7b7.zip |
Initial Commit
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "flag" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "log" | ||
8 | "net/http" | ||
9 | "strings" | ||
10 | |||
11 | "github.com/prologic/go-gopher" | ||
12 | ) | ||
13 | |||
14 | var ( | ||
15 | bind = flag.String("bind", ":80", "[int]:port to bind to") | ||
16 | host = flag.String("host", "localhost", "host to proxy to") | ||
17 | port = flag.Int("port", 70, "port to proxy to") | ||
18 | ) | ||
19 | |||
20 | func proxy(res http.ResponseWriter, req *http.Request) { | ||
21 | path := strings.TrimPrefix(req.URL.Path, "/") | ||
22 | |||
23 | gr, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) | ||
24 | if err != nil { | ||
25 | io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | ||
26 | return | ||
27 | } | ||
28 | |||
29 | if gr.Body != nil { | ||
30 | io.Copy(res, gr.Body) | ||
31 | } else { | ||
32 | bytes, err := gr.Dir.ToText() | ||
33 | if err != nil { | ||
34 | io.WriteString(res, fmt.Sprintf("<b>Error:</b><pre>%s</pre>", err)) | ||
35 | return | ||
36 | } | ||
37 | |||
38 | io.WriteString(res, string(bytes)) | ||
39 | } | ||
40 | } | ||
41 | |||
42 | func main() { | ||
43 | flag.Parse() | ||
44 | |||
45 | http.HandleFunc("/", proxy) | ||
46 | log.Fatal(http.ListenAndServe(*bind, nil)) | ||
47 | } | ||