Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 5 | // Package wsprlib implements utilities for the wspr web socket proxy, which |
| 6 | // converts between the Vanadium RPC protocol and a custom web socket based |
| 7 | // protocol. |
Todd Wang | 5b77a34 | 2015-04-06 18:31:37 -0700 | [diff] [blame] | 8 | package wsprlib |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 9 | |
| 10 | import ( |
| 11 | "bytes" |
| 12 | "crypto/tls" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 13 | "fmt" |
| 14 | "io" |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 15 | "net" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 16 | "net/http" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 17 | "sync" |
| 18 | "time" |
| 19 | |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 20 | "v.io/v23" |
| 21 | "v.io/v23/context" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 22 | "v.io/v23/rpc" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 23 | |
Todd Wang | 5b77a34 | 2015-04-06 18:31:37 -0700 | [diff] [blame] | 24 | "v.io/x/ref/services/wspr/internal/account" |
| 25 | "v.io/x/ref/services/wspr/internal/principal" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | pingInterval = 50 * time.Second // how often the server pings the client. |
| 30 | pongTimeout = pingInterval + 10*time.Second // maximum wait for pong. |
| 31 | ) |
| 32 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 33 | type WSPR struct { |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 34 | mu sync.Mutex |
| 35 | tlsCert *tls.Certificate |
Matt Rosencrantz | c90eb7b | 2015-01-09 08:32:01 -0800 | [diff] [blame] | 36 | ctx *context.T |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 37 | // HTTP port for WSPR to serve on. Note, WSPR always serves on localhost. |
| 38 | httpPort int |
| 39 | ln *net.TCPListener // HTTP listener |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 40 | listenSpec *rpc.ListenSpec |
Benjamin Prosnitz | 3c73850 | 2014-11-04 14:51:38 -0800 | [diff] [blame] | 41 | namespaceRoots []string |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 42 | principalManager *principal.PrincipalManager |
Benjamin Prosnitz | 10fab58 | 2014-11-11 13:28:15 -0800 | [diff] [blame] | 43 | accountManager *account.AccountManager |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 44 | pipes map[*http.Request]*pipe |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 47 | func readFromRequest(r *http.Request) (*bytes.Buffer, error) { |
| 48 | var buf bytes.Buffer |
| 49 | if readBytes, err := io.Copy(&buf, r.Body); err != nil { |
| 50 | return nil, fmt.Errorf("error copying message out of request: %v", err) |
| 51 | } else if wantBytes := r.ContentLength; readBytes != wantBytes { |
| 52 | return nil, fmt.Errorf("read %d bytes, wanted %d", readBytes, wantBytes) |
| 53 | } |
| 54 | return &buf, nil |
| 55 | } |
| 56 | |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 57 | // Starts listening for requests and returns the network endpoint address. |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 58 | func (wspr *WSPR) Listen() net.Addr { |
| 59 | addr := fmt.Sprintf("127.0.0.1:%d", wspr.httpPort) |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 60 | ln, err := net.Listen("tcp", addr) |
| 61 | if err != nil { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 62 | wspr.ctx.Fatalf("Listen failed: %s", err) |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 63 | } |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 64 | wspr.ln = ln.(*net.TCPListener) |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 65 | wspr.ctx.VI(1).Infof("Listening at %s", ln.Addr().String()) |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 66 | return ln.Addr() |
| 67 | } |
| 68 | |
| 69 | // tcpKeepAliveListener sets TCP keep-alive timeouts on accepted connections. |
| 70 | // It's used by ListenAndServe and ListenAndServeTLS so dead TCP connections |
| 71 | // (e.g. closing laptop mid-download) eventually go away. |
| 72 | // Copied from http/server.go, since it's not exported. |
| 73 | type tcpKeepAliveListener struct { |
| 74 | *net.TCPListener |
| 75 | } |
| 76 | |
| 77 | func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { |
| 78 | tc, err := ln.AcceptTCP() |
| 79 | if err != nil { |
| 80 | return |
| 81 | } |
| 82 | tc.SetKeepAlive(true) |
| 83 | tc.SetKeepAlivePeriod(3 * time.Minute) |
| 84 | return tc, nil |
| 85 | } |
| 86 | |
| 87 | // Starts serving http requests. This method is blocking. |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 88 | func (wspr *WSPR) Serve() { |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 89 | // Configure HTTP routes. |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 90 | http.HandleFunc("/ws", wspr.handleWS) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 91 | // Everything else is a 404. |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 92 | // Note: the pattern "/" matches all paths not matched by other registered |
| 93 | // patterns, not just the URL with Path == "/". |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 94 | // (http://golang.org/pkg/net/http/#ServeMux) |
| 95 | http.Handle("/", http.NotFoundHandler()) |
Adam Sadovsky | df33b67 | 2014-10-27 15:50:22 -0700 | [diff] [blame] | 96 | |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 97 | if err := http.Serve(tcpKeepAliveListener{wspr.ln}, nil); err != nil { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 98 | wspr.ctx.Fatalf("Serve failed: %s", err) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 102 | func (wspr *WSPR) Shutdown() { |
Ankur | 7a47701 | 2014-12-09 10:29:29 -0800 | [diff] [blame] | 103 | // TODO(ataly, bprosnitz): Get rid of this method if possible. |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 106 | func (wspr *WSPR) CleanUpPipe(req *http.Request) { |
| 107 | wspr.mu.Lock() |
| 108 | defer wspr.mu.Unlock() |
| 109 | delete(wspr.pipes, req) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Creates a new WebSocket Proxy object. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 113 | func NewWSPR(ctx *context.T, httpPort int, listenSpec *rpc.ListenSpec, identdEP string, namespaceRoots []string) *WSPR { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 114 | if listenSpec.Proxy == "" { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 115 | ctx.Fatalf("a vanadium proxy must be set") |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 116 | } |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 117 | |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 118 | wspr := &WSPR{ |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 119 | ctx: ctx, |
Benjamin Prosnitz | 3c73850 | 2014-11-04 14:51:38 -0800 | [diff] [blame] | 120 | httpPort: httpPort, |
| 121 | listenSpec: listenSpec, |
Benjamin Prosnitz | 3c73850 | 2014-11-04 14:51:38 -0800 | [diff] [blame] | 122 | namespaceRoots: namespaceRoots, |
Benjamin Prosnitz | 3c73850 | 2014-11-04 14:51:38 -0800 | [diff] [blame] | 123 | pipes: map[*http.Request]*pipe{}, |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 126 | p := v23.GetPrincipal(ctx) |
Ankur | 7a47701 | 2014-12-09 10:29:29 -0800 | [diff] [blame] | 127 | var err error |
Nicolas Lacasse | 83aa855 | 2015-07-06 11:51:11 -0700 | [diff] [blame] | 128 | // TODO(nlacasse): Use a serializer that can actually persist, as we do in browspr. |
| 129 | if wspr.principalManager, err = principal.NewPrincipalManager(p, principal.NewInMemorySerializer()); err != nil { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 130 | ctx.Fatalf("principal.NewPrincipalManager failed: %s", err) |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Matt Rosencrantz | c90eb7b | 2015-01-09 08:32:01 -0800 | [diff] [blame] | 133 | wspr.accountManager = account.NewAccountManager(identdEP, wspr.principalManager) |
Benjamin Prosnitz | 10fab58 | 2014-11-11 13:28:15 -0800 | [diff] [blame] | 134 | |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 135 | return wspr |
| 136 | } |
| 137 | |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 138 | func (wspr *WSPR) logAndSendBadReqErr(w http.ResponseWriter, msg string) { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 139 | wspr.ctx.Error(msg) |
Ankur | 78bc418 | 2014-10-13 17:34:27 -0700 | [diff] [blame] | 140 | http.Error(w, msg, http.StatusBadRequest) |
| 141 | return |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // HTTP Handlers |
| 145 | |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 146 | func (wspr *WSPR) handleWS(w http.ResponseWriter, r *http.Request) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 147 | if r.Method != "GET" { |
| 148 | http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed) |
| 149 | return |
| 150 | } |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 151 | wspr.ctx.VI(0).Info("Creating a new websocket") |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 152 | p := newPipe(w, r, wspr, nil) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 153 | |
| 154 | if p == nil { |
| 155 | return |
| 156 | } |
Suharsh Sivakumar | 94d0066 | 2015-01-21 14:31:30 -0800 | [diff] [blame] | 157 | wspr.mu.Lock() |
| 158 | defer wspr.mu.Unlock() |
| 159 | wspr.pipes[r] = p |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 160 | } |