Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | // proxyd is a daemon that listens for connections from veyron services |
| 2 | // (typically behind NATs) and proxies these services to the outside world. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "flag" |
| 7 | "net/http" |
| 8 | _ "net/http/pprof" |
| 9 | "time" |
| 10 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2/naming" |
| 12 | "veyron.io/veyron/veyron2/rt" |
| 13 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 14 | |
| 15 | _ "veyron.io/veyron/veyron/profiles" |
| 16 | "veyron.io/veyron/veyron/runtimes/google/ipc/stream/proxy" |
| 17 | "veyron.io/veyron/veyron/runtimes/google/lib/publisher" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
Adam Sadovsky | 5181bdb | 2014-08-13 10:29:11 -0700 | [diff] [blame] | 20 | var ( |
| 21 | // TODO(rthellend): Remove the protocol and address flags when the config |
| 22 | // manager is working. |
| 23 | protocol = flag.String("protocol", "tcp", "protocol to listen on") |
| 24 | address = flag.String("address", ":0", "address to listen on") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | |
Adam Sadovsky | 5181bdb | 2014-08-13 10:29:11 -0700 | [diff] [blame] | 26 | pubAddress = flag.String("published_address", "", "Network address the proxy publishes. If empty, the value of --address will be used") |
| 27 | httpAddr = flag.String("http", ":14142", "Network address on which the HTTP debug server runs") |
| 28 | healthzAddr = flag.String("healthz_address", "", "Network address on which the HTTP healthz server runs. It is intended to be used with a load balancer. The load balancer must be able to reach this address in order to verify that the proxy server is running") |
| 29 | name = flag.String("name", "", "Name to mount the proxy as") |
| 30 | ) |
| 31 | |
| 32 | func main() { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 33 | r := rt.Init() |
Bogdan Caprita | 4258d88 | 2014-07-02 09:15:22 -0700 | [diff] [blame] | 34 | defer r.Cleanup() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 35 | |
| 36 | rid, err := naming.NewRoutingID() |
| 37 | if err != nil { |
| 38 | vlog.Fatal(err) |
| 39 | } |
| 40 | |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 41 | // TODO(ashankar): Set the second argument to r.Principal() once the |
| 42 | // old security model is no longer operational. |
Robin Thellend | 020cd22 | 2014-06-10 14:29:28 -0700 | [diff] [blame] | 43 | proxy, err := proxy.New(rid, nil, *protocol, *address, *pubAddress) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 44 | if err != nil { |
| 45 | vlog.Fatal(err) |
| 46 | } |
| 47 | defer proxy.Shutdown() |
| 48 | |
| 49 | if len(*name) > 0 { |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 50 | publisher := publisher.New(r.NewContext(), r.Namespace(), time.Minute) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | defer publisher.WaitForStop() |
| 52 | defer publisher.Stop() |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 53 | publisher.AddServer(naming.JoinAddressName(proxy.Endpoint().String(), "//"), false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 54 | publisher.AddName(*name) |
| 55 | } |
| 56 | |
Robin Thellend | 2ec843a | 2014-07-30 14:48:54 -0700 | [diff] [blame] | 57 | if len(*healthzAddr) != 0 { |
| 58 | go startHealthzServer(*healthzAddr) |
| 59 | } |
| 60 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 61 | http.Handle("/", proxy) |
| 62 | if err = http.ListenAndServe(*httpAddr, nil); err != nil { |
| 63 | vlog.Fatal(err) |
| 64 | } |
| 65 | } |
Robin Thellend | 2ec843a | 2014-07-30 14:48:54 -0700 | [diff] [blame] | 66 | |
| 67 | // healthzHandler implements net/http.Handler |
| 68 | type healthzHandler struct{} |
| 69 | |
| 70 | func (healthzHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 71 | w.Write([]byte("ok")) |
| 72 | } |
| 73 | |
| 74 | // startHealthzServer starts a HTTP server that simply returns "ok" to every |
| 75 | // request. This is needed to let the load balancer know that the proxy server |
| 76 | // is running. |
| 77 | func startHealthzServer(addr string) { |
| 78 | s := http.Server{ |
| 79 | Addr: addr, |
| 80 | Handler: healthzHandler{}, |
| 81 | ReadTimeout: 10 * time.Second, |
| 82 | WriteTimeout: 10 * time.Second, |
| 83 | } |
| 84 | if err := s.ListenAndServe(); err != nil { |
| 85 | vlog.Fatal(err) |
| 86 | } |
| 87 | } |