From 773606bd45e283d05a4aaa06787289ad322ca7b7 Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 22 Sep 2016 11:22:10 +1000 Subject: Initial Commit --- main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..be625c6 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "flag" + "fmt" + "io" + "log" + "net/http" + "strings" + + "github.com/prologic/go-gopher" +) + +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") +) + +func proxy(res http.ResponseWriter, req *http.Request) { + path := strings.TrimPrefix(req.URL.Path, "/") + + gr, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/%s", *host, *port, path)) + if err != nil { + io.WriteString(res, fmt.Sprintf("Error:
%s
", err)) + return + } + + if gr.Body != nil { + io.Copy(res, gr.Body) + } else { + bytes, err := gr.Dir.ToText() + if err != nil { + io.WriteString(res, fmt.Sprintf("Error:
%s
", err)) + return + } + + io.WriteString(res, string(bytes)) + } +} + +func main() { + flag.Parse() + + http.HandleFunc("/", proxy) + log.Fatal(http.ListenAndServe(*bind, nil)) +} -- cgit v1.2.3-54-g00ecf