aboutsummaryrefslogtreecommitdiff
path: root/cmd/tor2web/main.go
blob: b6a03e613257daa1d21104d6c8055cdfcccfc75d (plain)
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
48
49
50
51
52
53
54
55
56
57
58
// Command tor2web is a simple caching proxy to tor onion sites.
package main

import (
	"context"
	"flag"
	"log"
	"net/http"
	"net/http/httputil"

	"github.com/birkelund/boltdbcache"
	"github.com/gregjones/httpcache"
	"golang.org/x/net/proxy"
	"within.website/ln"
	"within.website/ln/opname"
)

var (
	dbLoc        = flag.String("db-loc", "./cache.db", "cache location on disk (boltdb)")
	torSocksAddr = flag.String("tor-socks-addr", "127.0.0.1:9050", "tor socks address")
	httpPort     = flag.String("port", "80", "HTTP port")
	httpsPort    = flag.String("https-port", "443", "HTTPS port")
	tlsCert      = flag.String("tls-cert", "/etc/within/star.onion.cert.pem", "tls cert location on disk")
	tlsKey       = flag.String("tls-key", "/etc/within/star.onion.key.pem", "tls key location on disk")
)

func main() {
	ctx := opname.With(context.Background(), "main")
	// Create a socks5 dialer
	dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
	if err != nil {
		log.Fatal(err)
	}

	// Setup HTTP transport
	tr := &http.Transport{
		Dial: dialer.Dial,
	}

	c, err := boltdbcache.New(*dbLoc, boltdbcache.WithBucketName("darkweb"))
	if err != nil {
		ln.FatalErr(ctx, err)
	}

	ttr := httpcache.NewTransport(c)
	ttr.Transport = tr

	rp := &httputil.ReverseProxy{
		Transport: ttr,
		Director: func(r *http.Request) {
			r.URL.Scheme = "http"
			r.URL.Host = r.Host
		},
	}

	go ln.FatalErr(ctx, http.ListenAndServe(":"+*httpPort, rp))
	ln.FatalErr(ctx, http.ListenAndServeTLS(":"+*httpsPort, *tlsCert, *tlsKey, rp))
}