aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: be625c66ff06e20cd28ae205ffbf4b67797dfbdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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("<b>Error:</b><pre>%s</pre>", 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("<b>Error:</b><pre>%s</pre>", err))
			return
		}

		io.WriteString(res, string(bytes))
	}
}

func main() {
	flag.Parse()

	http.HandleFunc("/", proxy)
	log.Fatal(http.ListenAndServe(*bind, nil))
}