tlsproxy.go (2033B)
1 // A TLS front end for the simulator's local services. 2 // 3 // The wallet hard-codes https://taldir.gnunet.org for the directory and 4 // https://mailbox.gnunet.org as the mailbox default, so the only way to 5 // exercise those code paths is to answer for those names: this listens with a 6 // certificate for both and forwards by Host header to the plain-HTTP service 7 // behind. The app is not modified; the device is told the names resolve here. 8 package main 9 10 import ( 11 "crypto/tls" 12 "flag" 13 "log" 14 "net/http" 15 "net/http/httputil" 16 "net/url" 17 "strings" 18 ) 19 20 func proxyTo(target string) *httputil.ReverseProxy { 21 u, err := url.Parse(target) 22 if err != nil { 23 log.Fatalf("bad target %q: %v", target, err) 24 } 25 p := httputil.NewSingleHostReverseProxy(u) 26 p.Director = func(r *http.Request) { 27 r.URL.Scheme = u.Scheme 28 r.URL.Host = u.Host 29 r.Host = u.Host 30 } 31 return p 32 } 33 34 func main() { 35 listen := flag.String("listen", "127.0.0.1:8443", "address to listen on") 36 taldir := flag.String("taldir", "http://127.0.0.1:11001", "directory service") 37 mailbox := flag.String("mailbox", "http://127.0.0.1:11000", "mailbox service") 38 cert := flag.String("cert", "srv.crt", "server certificate") 39 key := flag.String("key", "srv.key", "server key") 40 flag.Parse() 41 42 routes := map[string]*httputil.ReverseProxy{ 43 "taldir.gnunet.org": proxyTo(*taldir), 44 "mailbox.gnunet.org": proxyTo(*mailbox), 45 } 46 mux := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 47 host := r.Host 48 if i := strings.IndexByte(host, ':'); i >= 0 { 49 host = host[:i] 50 } 51 p, ok := routes[host] 52 if !ok { 53 log.Printf("no route for %q (%s)", host, r.URL.Path) 54 http.Error(w, "no route for "+host, http.StatusNotFound) 55 return 56 } 57 log.Printf("%s %s%s", r.Method, host, r.URL.Path) 58 p.ServeHTTP(w, r) 59 }) 60 srv := &http.Server{ 61 Addr: *listen, 62 Handler: mux, 63 TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12}, 64 } 65 log.Printf("TLS front end on %s: taldir=%s mailbox=%s", *listen, *taldir, *mailbox) 66 log.Fatal(srv.ListenAndServeTLS(*cert, *key)) 67 }