blob: 74ad181b9f30e846b14601721136e846ac7a21c8 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001// proxyd is a daemon that listens for connections from veyron services
2// (typically behind NATs) and proxies these services to the outside world.
3package main
4
5import (
6 "flag"
7 "net/http"
8 _ "net/http/pprof"
9 "time"
10
Jiri Simsa519c5072014-09-17 21:37:57 -070011 "veyron.io/veyron/veyron2/naming"
12 "veyron.io/veyron/veyron2/rt"
13 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaouf889c732014-10-16 20:46:54 -070014
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 Simsa5293dcb2014-05-10 09:56:38 -070018)
19
Adam Sadovsky5181bdb2014-08-13 10:29:11 -070020var (
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 Simsa5293dcb2014-05-10 09:56:38 -070025
Adam Sadovsky5181bdb2014-08-13 10:29:11 -070026 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
32func main() {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070033 r := rt.Init()
Bogdan Caprita4258d882014-07-02 09:15:22 -070034 defer r.Cleanup()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070035
36 rid, err := naming.NewRoutingID()
37 if err != nil {
38 vlog.Fatal(err)
39 }
40
Asim Shankar8f05c222014-10-06 22:08:19 -070041 // TODO(ashankar): Set the second argument to r.Principal() once the
42 // old security model is no longer operational.
Robin Thellend020cd222014-06-10 14:29:28 -070043 proxy, err := proxy.New(rid, nil, *protocol, *address, *pubAddress)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070044 if err != nil {
45 vlog.Fatal(err)
46 }
47 defer proxy.Shutdown()
48
49 if len(*name) > 0 {
Cosmos Nicolaou4e029972014-06-13 14:53:08 -070050 publisher := publisher.New(r.NewContext(), r.Namespace(), time.Minute)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070051 defer publisher.WaitForStop()
52 defer publisher.Stop()
David Why Use Two When One Will Do Presotto3da1c792014-10-03 11:15:53 -070053 publisher.AddServer(naming.JoinAddressName(proxy.Endpoint().String(), "//"), false)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070054 publisher.AddName(*name)
55 }
56
Robin Thellend2ec843a2014-07-30 14:48:54 -070057 if len(*healthzAddr) != 0 {
58 go startHealthzServer(*healthzAddr)
59 }
60
Jiri Simsa5293dcb2014-05-10 09:56:38 -070061 http.Handle("/", proxy)
62 if err = http.ListenAndServe(*httpAddr, nil); err != nil {
63 vlog.Fatal(err)
64 }
65}
Robin Thellend2ec843a2014-07-30 14:48:54 -070066
67// healthzHandler implements net/http.Handler
68type healthzHandler struct{}
69
70func (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.
77func 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}