| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/json" |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"io/ioutil" |
| 10 |
"log" |
| 11 |
"net/http" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"path/filepath" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"github.com/cretz/bine/tor" |
| 18 |
"github.com/cretz/bine/torutil" |
| 19 |
) |
| 20 |
|
| 21 |
var verbose bool |
| 22 |
|
| 23 |
func main() { |
| 24 |
flag.BoolVar(&verbose, "verbose", false, "Whether to have verbose logging") |
| 25 |
flag.Parse() |
| 26 |
if flag.NArg() != 1 { |
| 27 |
log.Fatal("Expecting single domain arg") |
| 28 |
} else if err := run(flag.Arg(0)); err != nil { |
| 29 |
log.Fatal(err) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func run(domain string) error { |
| 34 |
fmt.Println("Please wait while generating services") |
| 35 |
ctx, cancelFn := context.WithCancel(context.Background()) |
| 36 |
defer cancelFn() |
| 37 |
|
| 38 |
if _, err := exec.LookPath("mkcert"); err != nil { |
| 39 |
return fmt.Errorf("Unable to find mkcert on PATH: %v", err) |
| 40 |
} |
| 41 |
|
| 42 |
srv, err := start(ctx, domain, ":80") |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
defer srv.Close() |
| 47 |
fmt.Printf("Listening on all IPs on port 80, so http://%v will use second onion as alt-svc\n", domain) |
| 48 |
fmt.Printf("Listening on onion http://%v.onion that will use second onion as alt-svc\n", srv.onion1.ID) |
| 49 |
fmt.Printf("Created secure second onion at https://%v.onion\n", srv.onion2.ID) |
| 50 |
fmt.Println("Press enter to exit") |
| 51 |
|
| 52 |
go func() { |
| 53 |
fmt.Scanln() |
| 54 |
cancelFn() |
| 55 |
}() |
| 56 |
select { |
| 57 |
case err := <-srv.Err(): |
| 58 |
return err |
| 59 |
case <-ctx.Done(): |
| 60 |
return nil |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
type server struct { |
| 65 |
exitAddrs map[string]bool |
| 66 |
t *tor.Tor |
| 67 |
onion1 *tor.OnionService |
| 68 |
onion2 *tor.OnionService |
| 69 |
httpSrv *http.Server |
| 70 |
httpSrvErrCh chan error |
| 71 |
} |
| 72 |
|
| 73 |
func start(ctx context.Context, domain string, httpAddr string) (srv *server, err error) { |
| 74 |
srv = &server{} |
| 75 |
|
| 76 |
if srv.exitAddrs, err = getExitAddresses(); err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
|
| 80 |
startConf := &tor.StartConf{DataDir: "tor-data"} |
| 81 |
if verbose { |
| 82 |
startConf.DebugWriter = os.Stdout |
| 83 |
} else { |
| 84 |
startConf.ExtraArgs = []string{"--quiet"} |
| 85 |
} |
| 86 |
if srv.t, err = tor.Start(ctx, startConf); err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if srv.onion1, err = srv.t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{80}, Version3: true}); err != nil { |
| 92 |
srv.Close() |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
|
| 96 |
if srv.onion2, err = srv.t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{443}, Version3: true}); err != nil { |
| 97 |
srv.Close() |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
|
| 101 |
cmd := exec.CommandContext(ctx, "mkcert", domain, srv.onion1.ID+".onion", srv.onion2.ID+".onion") |
| 102 |
cmd.Dir = "tor-data" |
| 103 |
output, err := cmd.CombinedOutput() |
| 104 |
if verbose { |
| 105 |
fmt.Printf("Output from mkcert:\n%v\n", string(output)) |
| 106 |
} |
| 107 |
if err != nil { |
| 108 |
srv.Close() |
| 109 |
return nil, fmt.Errorf("Failed running mkcert: %v", err) |
| 110 |
} |
| 111 |
cert := filepath.Join("tor-data", domain+"+2.pem") |
| 112 |
key := filepath.Join("tor-data", domain+"+2-key.pem") |
| 113 |
|
| 114 |
srv.httpSrvErrCh = make(chan error, 3) |
| 115 |
go func(errCh chan error) { |
| 116 |
errCh <- http.Serve(srv.onion1, |
| 117 |
srv.NewHandler(srv.onion1.ID+".onion", srv.onion2.ID+".onion:443")) |
| 118 |
}(srv.httpSrvErrCh) |
| 119 |
go func(errCh chan error) { |
| 120 |
errCh <- http.ServeTLS(srv.onion2, |
| 121 |
srv.NewHandler(srv.onion2.ID+".onion", "", "http://"+srv.onion1.ID+".onion", "https://"+domain, |
| 122 |
"http://"+domain), cert, key) |
| 123 |
}(srv.httpSrvErrCh) |
| 124 |
|
| 125 |
srv.httpSrv = &http.Server{Addr: httpAddr, Handler: srv.NewHandler(httpAddr, srv.onion2.ID+".onion:443")} |
| 126 |
go func(httpSrv *http.Server, errCh chan error) { errCh <- httpSrv.ListenAndServe() }(srv.httpSrv, srv.httpSrvErrCh) |
| 127 |
return |
| 128 |
} |
| 129 |
|
| 130 |
func (s *server) Err() <-chan error { return s.httpSrvErrCh } |
| 131 |
|
| 132 |
func (s *server) NewHandler(siteAddr string, altSvc string, origins ...string) http.Handler { |
| 133 |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
s.handleRegularRequest(siteAddr, altSvc, w, r) |
| 139 |
|
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
func (s *server) handleOpportunistic(siteAddr string, origins []string, w http.ResponseWriter, r *http.Request) { |
| 144 |
if verbose { |
| 145 |
fmt.Printf("-------\nAccessed %v, responding with origins %v, site info:\n%v-------\n", |
| 146 |
siteAddr, origins, string(s.requestInfo(siteAddr, r))) |
| 147 |
} |
| 148 |
w.Header().Add("Content-Type", "application/json") |
| 149 |
byts, _ := json.Marshal(origins) |
| 150 |
w.Write(byts) |
| 151 |
} |
| 152 |
|
| 153 |
func (s *server) handleRegularRequest(siteAddr string, altSvc string, w http.ResponseWriter, r *http.Request) { |
| 154 |
|
| 155 |
if altSvc != "" { |
| 156 |
w.Header().Add("Alt-Svc", "h2=\""+altSvc+"\"; ma=600") |
| 157 |
} |
| 158 |
|
| 159 |
resp := s.requestInfo(siteAddr, r) |
| 160 |
if verbose { |
| 161 |
fmt.Printf("-------\nAccessed %v, responding with:\n%v-------\n", siteAddr, string(resp)) |
| 162 |
} |
| 163 |
w.Write(resp) |
| 164 |
} |
| 165 |
|
| 166 |
func (s *server) requestInfo(siteAddr string, r *http.Request) []byte { |
| 167 |
remoteAddr := r.Header.Get("X-Forwarded-For") |
| 168 |
if remoteAddr == "" { |
| 169 |
remoteAddr, _, _ = torutil.PartitionString(r.RemoteAddr, ':') |
| 170 |
} |
| 171 |
exit := "" |
| 172 |
if !s.exitAddrs[remoteAddr] { |
| 173 |
exit = " NOT" |
| 174 |
} |
| 175 |
buf := &bytes.Buffer{} |
| 176 |
fmt.Fprintf(buf, "Server-side site addr: %v\n", siteAddr) |
| 177 |
fmt.Fprintf(buf, "You accessed %v on %v from %v which is%v an exit node\n", |
| 178 |
r.URL.Path, r.Host, remoteAddr, exit) |
| 179 |
fmt.Fprintf(buf, "Headers:\n") |
| 180 |
for h, vals := range r.Header { |
| 181 |
for _, val := range vals { |
| 182 |
fmt.Fprintf(buf, " %v: %v\n", h, val) |
| 183 |
} |
| 184 |
} |
| 185 |
if verbose { |
| 186 |
fmt.Printf("-------\nAccessed %v, responding with:\n%v-------\n", siteAddr, string(buf.Bytes())) |
| 187 |
} |
| 188 |
return buf.Bytes() |
| 189 |
} |
| 190 |
|
| 191 |
func (s *server) Close() { |
| 192 |
if s.httpSrv != nil { |
| 193 |
s.httpSrv.Close() |
| 194 |
} |
| 195 |
if s.onion1 != nil { |
| 196 |
s.onion1.Close() |
| 197 |
} |
| 198 |
if s.onion2 != nil { |
| 199 |
s.onion2.Close() |
| 200 |
} |
| 201 |
if s.t != nil { |
| 202 |
s.t.Close() |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func getExitAddresses() (map[string]bool, error) { |
| 207 |
resp, err := http.Get("https://check.torproject.org/exit-addresses") |
| 208 |
if err != nil { |
| 209 |
return nil, err |
| 210 |
} |
| 211 |
body, err := ioutil.ReadAll(resp.Body) |
| 212 |
resp.Body.Close() |
| 213 |
if err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
ret := map[string]bool{} |
| 217 |
for _, line := range strings.Split(string(body), "\n") { |
| 218 |
pieces := strings.Split(strings.TrimSpace(line), " ") |
| 219 |
if len(pieces) >= 2 && pieces[0] == "ExitAddress" { |
| 220 |
ret[pieces[1]] = true |
| 221 |
} |
| 222 |
} |
| 223 |
return ret, nil |
| 224 |
} |