| index 89ce1f350..fc89e1ed5 100644 |
| --- a/internal/edge/tunnel_ws_e2e_test.go |
| +++ b/internal/edge/tunnel_ws_e2e_test.go |
| @@ -68,7 +68,9 @@ import ( |
| |
| const ( |
| wsHost = "shop.example.com" // forward row on home, gated on the box |
| - wsTunnelHost = "t.example.com" // the hub's -tunnel row, channel t1 |
| + wsTunnelHost = "t.example.com" // the hub's -tunnel row, channel t1, gated on the box |
| + wsHubGateHost = "tg.example.com" // a -tunnel row on t1 gated on the hub instead |
| + wsPlainHost = "plain.example.com" // an ungated socket row the hub serves itself |
| wsLimitedHost = "limited.example.com" // forward row with the hub's burst 2 |
| wsRawHost = "raw.example.com" // forward row the box answers by hand |
| wsOldHost = "old.example.com" // forward row on away, for the old box |
| @@ -98,11 +100,11 @@ type seenReq struct { |
| |
| // cableApp is the WebSocket app behind the box, Action Cable shaped. |
| type cableApp struct { |
| - mu sync.Mutex |
| - seen []seenReq // every request that reached the app, in order |
| - conns map[*appConn]struct{} |
| - opened chan *appConn |
| - srv *websocket.Server |
| + mu sync.Mutex |
| + seen []seenReq // every request that reached the app, in order |
| + conns map[*appConn]struct{} |
| + byID map[string]chan *appConn // each socket, by its handshake's tws id |
| + srv *websocket.Server |
| } |
| |
| // appConn is the app's end of one accepted socket. |
| @@ -120,7 +122,7 @@ type appConn struct { |
| } |
| |
| func newCableApp() *cableApp { |
| - a := &cableApp{conns: make(map[*appConn]struct{}), opened: make(chan *appConn, 1024)} |
| + a := &cableApp{conns: make(map[*appConn]struct{}), byID: make(map[string]chan *appConn)} |
| a.srv = &websocket.Server{ |
| // Action Cable's same-origin rule and its one subprotocol. |
| Handshake: func(cfg *websocket.Config, r *http.Request) error { |
| @@ -187,9 +189,11 @@ func (a *cableApp) serve(ws *websocket.Conn) { |
| a.mu.Unlock() |
| close(c.closed) |
| }() |
| - select { |
| - case a.opened <- c: |
| - default: |
| + if id := r.URL.Query().Get(wsIDParam); id != "" { |
| + select { |
| + case a.slot(id) <- c: |
| + default: |
| + } |
| } |
| a.send(c, map[string]any{"type": "welcome"}) |
| go func() { |
| @@ -280,27 +284,40 @@ func (a *cableApp) closeAll() { |
| } |
| } |
| |
| -// drainOpened forgets sockets opened earlier, so the next one read is new. |
| -func (a *cableApp) drainOpened() { |
| - for { |
| - select { |
| - case <-a.opened: |
| - default: |
| - return |
| - } |
| +// wsIDParam is the query parameter that names one handshake, so the app's |
| +// end of a socket is matched to the client's by identity, not by order. |
| +const wsIDParam = "tws" |
| + |
| +// newHandshakeID is a fresh tws value. |
| +func newHandshakeID() string { |
| + b := make([]byte, 8) |
| + rand.Read(b) |
| + return fmt.Sprintf("%x", b) |
| +} |
| + |
| +// slot is the one-socket channel for handshake id. |
| +func (a *cableApp) slot(id string) chan *appConn { |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + ch := a.byID[id] |
| + if ch == nil { |
| + ch = make(chan *appConn, 1) |
| + a.byID[id] = ch |
| } |
| + return ch |
| } |
| |
| -func (a *cableApp) nextOpened(t *testing.T, host string) *appConn { |
| +// opened is the app's end of the socket that handshake id opened. |
| +func (a *cableApp) opened(t *testing.T, id, host string) *appConn { |
| t.Helper() |
| select { |
| - case c := <-a.opened: |
| + case c := <-a.slot(id): |
| if c.host != host { |
| - t.Fatalf("the app opened a socket for %s, want %s", c.host, host) |
| + t.Fatalf("handshake %s opened a socket for %s, want %s", id, c.host, host) |
| } |
| return c |
| case <-time.After(5 * time.Second): |
| - t.Fatalf("the app opened no socket for %s within 5 s", host) |
| + t.Fatalf("the app opened no socket for handshake %s (%s) within 5 s", id, host) |
| return nil |
| } |
| } |
| @@ -539,12 +556,19 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene { |
| t.Fatalf("PutForward(%s): %v", rt.Host, err) |
| } |
| } |
| - if err := s.hubReg.Upsert(wsTunnelRow("t1")); err != nil { |
| - t.Fatal(err) |
| + for _, rt := range []registry.Route{ |
| + wsTunnelRow(wsTunnelHost, "t1"), |
| + wsTunnelRow(wsHubGateHost, "t1"), |
| + {Host: wsPlainHost, Kind: "instance", Status: "ready", Socket: sock, Account: fwdOwner, App: "plain", RateLimit: wsWideRate}, |
| + } { |
| + if err := s.hubReg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| - // A -tunnel row is a platform row on the hub: its gate is the hub's |
| - // (the hub 404s a login POST to an ungated non-forward row). |
| - if err := s.hubReg.SetGate(wsTunnelHost, s.hash); err != nil { |
| + // t.example.com is gated on the box only (spec §4.5: the hub passes the |
| + // login and the cookie, the box judges them); tg.example.com on the hub |
| + // only, whose gate judges and strips before the tunnel. |
| + if err := s.hubReg.SetGate(wsHubGateHost, s.hash); err != nil { |
| t.Fatal(err) |
| } |
| s.hub, err = New(s.hubReg, nil) |
| @@ -572,6 +596,7 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene { |
| for _, rt := range []registry.Route{ |
| wsBoxRow(wsHost, sock, ""), |
| wsBoxRow(wsTunnelHost, sock, ""), |
| + wsBoxRow(wsHubGateHost, sock, ""), |
| wsBoxRow(wsLimitedHost, sock, wsBoxRate), |
| wsBoxRow(wsOldHost, sock, ""), |
| } { |
| @@ -579,8 +604,10 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene { |
| t.Fatal(err) |
| } |
| } |
| - if err := s.boxReg.SetGate(wsHost, s.hash); err != nil { |
| - t.Fatal(err) |
| + for _, h := range []string{wsHost, wsTunnelHost} { |
| + if err := s.boxReg.SetGate(h, s.hash); err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| s.box, err = New(s.boxReg, nil) |
| if err != nil { |
| @@ -653,8 +680,8 @@ func wsForward(host, fleet, rate string) registry.Route { |
| return rt |
| } |
| |
| -func wsTunnelRow(channel string) registry.Route { |
| - return registry.Route{Host: wsTunnelHost, Kind: "instance", Status: "ready", Tunnel: channel, |
| +func wsTunnelRow(host, channel string) registry.Route { |
| + return registry.Route{Host: host, Kind: "instance", Status: "ready", Tunnel: channel, |
| Account: fwdOwner, App: "shop", RateLimit: wsWideRate} |
| } |
| |
| @@ -973,9 +1000,9 @@ type cableClient struct { |
| // and returns it with the app's end. |
| func (s *wsScene) openCable(t *testing.T, host, cookie string) (*cableClient, *appConn) { |
| t.Helper() |
| - s.app.drainOpened() |
| + id := newHandshakeID() |
| conn := s.dialTLS(t, s.addr, host) |
| - cfg, err := websocket.NewConfig("wss://"+host+"/cable", "https://"+host) |
| + cfg, err := websocket.NewConfig("wss://"+host+"/cable?"+wsIDParam+"="+id, "https://"+host) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -1012,7 +1039,7 @@ func (s *wsScene) openCable(t *testing.T, host, cookie string) (*cableClient, *a |
| } |
| }() |
| t.Cleanup(func() { ws.Close() }) |
| - return c, s.app.nextOpened(t, host) |
| + return c, s.app.opened(t, id, host) |
| } |
| |
| func (c *cableClient) next(t *testing.T) string { |
| @@ -1055,14 +1082,41 @@ func (c *cableClient) echo(t *testing.T, text string) { |
| } |
| } |
| |
| -// waitClosed waits for ch to close within d. |
| -func waitClosed(t *testing.T, ch <-chan struct{}, d time.Duration, what string) { |
| +// closedBy requires the client's end c and the app's end ac (either may be |
| +// nil) to have closed before deadline: one bound, taken at the trigger, for |
| +// both. |
| +func closedBy(t *testing.T, deadline time.Time, what string, c *cableClient, ac *appConn) { |
| t.Helper() |
| - select { |
| - case <-ch: |
| - case <-time.After(d): |
| - t.Fatalf("%s: not closed within %s", what, d) |
| + timer := time.NewTimer(time.Until(deadline)) |
| + defer timer.Stop() |
| + for _, e := range []struct { |
| + name string |
| + ch <-chan struct{} |
| + set bool |
| + }{{"the client's socket", chOf(c), c != nil}, {"the app's socket", chOfApp(ac), ac != nil}} { |
| + if !e.set { |
| + continue |
| + } |
| + select { |
| + case <-e.ch: |
| + case <-timer.C: |
| + t.Fatalf("%s: %s was not closed by the deadline", what, e.name) |
| + } |
| + } |
| +} |
| + |
| +func chOf(c *cableClient) <-chan struct{} { |
| + if c == nil { |
| + return nil |
| + } |
| + return c.closed |
| +} |
| + |
| +func chOfApp(ac *appConn) <-chan struct{} { |
| + if ac == nil { |
| + return nil |
| } |
| + return ac.closed |
| } |
| |
| // notClosed checks ch has not closed. |
| @@ -1179,8 +1233,9 @@ func (s *wsScene) fullExchange(t *testing.T, host string) { |
| if got := ac.hdr.Get("X-Forwarded-Proto"); got != "https" { |
| t.Fatalf("the app saw X-Forwarded-Proto %q, want https", got) |
| } |
| + closeBy := time.Now().Add(2 * time.Second) |
| c.ws.Close() |
| - waitClosed(t, ac.closed, 2*time.Second, "the app's end after the client closed") |
| + closedBy(t, closeBy, "after the client closed", nil, ac) |
| } |
| |
| // ---- the witness ------------------------------------------------------------ |
| @@ -1240,24 +1295,63 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) { |
| }) |
| |
| t.Run("case02b_tunnel_arm", func(t *testing.T) { |
| - ten, ok := tenancyAt(s.hub, wsTunnelHost) |
| - if !ok || ten.Arm != ArmTunnel || ten.Tunnel != "t1" { |
| - t.Fatalf("%s is served %v under %+v, want the -tunnel arm on t1", wsTunnelHost, ok, ten) |
| + // The brief's shape: the hub's -tunnel row is ungated and the box's |
| + // row carries the gate, so the login and the cookie cross the |
| + // tunnel and the box judges both. |
| + for _, host := range []string{wsTunnelHost, wsHubGateHost} { |
| + ten, ok := tenancyAt(s.hub, host) |
| + if !ok || ten.Arm != ArmTunnel || ten.Tunnel != "t1" { |
| + t.Fatalf("%s is served %v under %+v, want the -tunnel arm on t1", host, ok, ten) |
| + } |
| + } |
| + if g := s.hub.current()[wsTunnelHost].route.Gate; g != "" { |
| + t.Fatalf("the hub's %s row has a gate", wsTunnelHost) |
| + } |
| + if g := s.box.current()[wsTunnelHost].route.Gate; g != s.hash { |
| + t.Fatalf("the box's %s row has gate %q, want the scene's", wsTunnelHost, g) |
| } |
| s.fullExchange(t, wsTunnelHost) |
| waitOpen(t, s.hub, s.channel(t, "t1"), 0, "after case 2b") |
| }) |
| |
| + t.Run("case02b_hub_gated_tunnel_row", func(t *testing.T) { |
| + // Extra coverage: a -tunnel row with its own gate at the hub. The |
| + // hub renders, judges and strips; the box's row is ungated. |
| + if g := s.box.current()[wsHubGateHost].route.Gate; g != "" { |
| + t.Fatalf("the box's %s row has a gate", wsHubGateHost) |
| + } |
| + s.fullExchange(t, wsHubGateHost) |
| + waitOpen(t, s.hub, s.channel(t, "t1"), 0, "after the hub-gated exchange") |
| + }) |
| + |
| + t.Run("case02b_ungated_row_login_404", func(t *testing.T) { |
| + // Unchanged: a login POST to an ungated row that is neither a |
| + // forward row nor a -tunnel row is a 404 and reaches no app. |
| + before := len(s.app.seenFor(wsPlainHost)) |
| + resp, _ := s.do(t, http.MethodPost, wsPlainHost, gate.LoginPath, "", |
| + http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, "password=right&next=%2Fcable") |
| + if resp.StatusCode != http.StatusNotFound { |
| + t.Fatalf("a login POST to the ungated %s: %d, want 404", wsPlainHost, resp.StatusCode) |
| + } |
| + if n := len(s.app.seenFor(wsPlainHost)); n != before { |
| + t.Fatalf("the app saw %d requests for %s, want none", n-before, wsPlainHost) |
| + } |
| + // Control: the row serves. |
| + if resp, body := s.do(t, "GET", wsPlainHost, "/x", "", nil, ""); resp.StatusCode != 200 || body != "plain:"+wsPlainHost+"/x" { |
| + t.Fatalf("a GET to %s: %d %q", wsPlainHost, resp.StatusCode, body) |
| + } |
| + }) |
| + |
| t.Run("case03_close_account", func(t *testing.T) { |
| s.hub.SetPlanGovernedFor(func(rt registry.Route) bool { return rt.Account == fwdOwner }) |
| defer s.hub.SetPlanGovernedFor(nil) |
| c, ac := s.openShop(t) |
| c.echo(t, "before the close") |
| + deadline := time.Now().Add(2 * time.Second) |
| if n := s.hub.CloseAccountUpgrades(fwdOwner); n != 1 { |
| t.Fatalf("CloseAccountUpgrades(%s) with one socket open closed %d, want 1", fwdOwner, n) |
| } |
| - waitClosed(t, c.closed, 2*time.Second, "the client's socket") |
| - waitClosed(t, ac.closed, 2*time.Second, "the app's socket") |
| + closedBy(t, deadline, "CloseAccountUpgrades", c, ac) |
| if n := s.hub.CloseAccountUpgrades(fwdOwner); n != 0 { |
| t.Fatalf("a second CloseAccountUpgrades closed %d, want 0", n) |
| } |
| @@ -1314,13 +1408,13 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) { |
| for _, drop := range []bool{false, true} { |
| c, ac := s.openShop(t) |
| c.echo(t, "before the app closes") |
| + deadline := time.Now().Add(2 * time.Second) |
| if drop { |
| ac.raw.Close() |
| } else { |
| ac.ws.Close() |
| } |
| - waitClosed(t, ac.closed, 2*time.Second, "the app's socket") |
| - waitClosed(t, c.closed, 2*time.Second, fmt.Sprintf("the client's socket (dropped %v)", drop)) |
| + closedBy(t, deadline, fmt.Sprintf("the app closed (dropped %v)", drop), c, ac) |
| // The hub holds the slot until the client closes its side, as |
| // a browser does once it reads the close. |
| c.ws.Close() |
| @@ -1444,9 +1538,9 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) { |
| old := s.channel(t, wsBox1) |
| c, ac := s.openShop(t) |
| c.echo(t, "before a second loop registers") |
| + deadline := time.Now().Add(2 * time.Second) |
| s.startLoop("second", wsBox1, fwdBox1Tok) // a second loop as home/box-1 |
| - waitClosed(t, c.closed, 2*time.Second, "the client's socket") |
| - waitClosed(t, ac.closed, 2*time.Second, "the app's socket") |
| + closedBy(t, deadline, "re-registration", c, ac) |
| s.stopLoop("second") |
| waitOpen(t, s.hub, old, 0, "the replaced connection") |
| s.waitServing(t, 15*time.Second) |
| @@ -1457,8 +1551,7 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) { |
| if err := s.hub.Reload(); err != nil { |
| t.Fatal(err) |
| } |
| - waitClosed(t, c.closed, 2*time.Second, "the client's socket") |
| - waitClosed(t, ac.closed, 2*time.Second, "the app's socket") |
| + closedBy(t, time.Now().Add(2*time.Second), "detach", c, ac) |
| setBoxStatus(t, s.hubReg, "home", "box-1", "active") |
| if err := s.hub.Reload(); err != nil { |
| t.Fatal(err) |
| @@ -1491,9 +1584,9 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) { |
| old := s.channel(t, wsBox1) |
| c, ac := s.openShop(t) |
| c.echo(t, "before the tunnel goes") |
| + deadline := time.Now().Add(10 * time.Second) |
| wait := s.cancelLoop(wsBox1) |
| - waitClosed(t, c.closed, 10*time.Second, "the client's socket") |
| - waitClosed(t, ac.closed, 10*time.Second, "the app's socket") |
| + closedBy(t, deadline, "tunnel loss", c, ac) |
| wait() |
| waitOpen(t, s.hub, old, 0, "the lost connection") |
| s.startLoop(wsBox1, wsBox1, fwdBox1Tok) |
| @@ -1700,18 +1793,18 @@ func (s *wsScene) modifyResponseFails(t *testing.T, cookie, what string) { |
| func (s *wsScene) openWrapped(t *testing.T, cookie string, mode int32, what string) (hs, chan struct{}, *upgradeReservation, *appConn) { |
| t.Helper() |
| s.drainInserted() |
| - s.app.drainOpened() |
| + id := newHandshakeID() |
| barrier := make(chan struct{}) |
| s.wrapBarrier.Store(&barrier) |
| s.wrapMode.Store(mode) |
| - h := s.sendHandshake(t, s.wrapAddr, wsHost, "/cable", cookie, "websocket") |
| + h := s.sendHandshake(t, s.wrapAddr, wsHost, "/cable?"+wsIDParam+"="+id, cookie, "websocket") |
| res := s.nextInserted(t) |
| select { |
| case <-s.wrapReturned: |
| case <-time.After(5 * time.Second): |
| t.Fatalf("%s: the hub did not return within 5 s", what) |
| } |
| - return h, barrier, res, s.app.nextOpened(t, wsHost) |
| + return h, barrier, res, s.app.opened(t, id, wsHost) |
| } |
| |
| // hijackNotSupported: the hub behind a writer whose Hijack answers |
| @@ -1728,6 +1821,7 @@ func (s *wsScene) hijackNotSupported(t *testing.T, cookie, what string) { |
| if got := res.firstReleaser(); got != relNone { |
| t.Fatalf("%s: with the request live the reservation was released by %d", what, got) |
| } |
| + deadline := time.Now().Add(2 * time.Second) |
| close(barrier) |
| h.read(t) |
| wantStatus(t, h, http.StatusBadGateway, what) |
| @@ -1735,7 +1829,7 @@ func (s *wsScene) hijackNotSupported(t *testing.T, cookie, what string) { |
| if got := res.firstReleaser(); got != relBackstop { |
| t.Fatalf("%s: the first releaser is %d, want relBackstop (%d)", what, got, relBackstop) |
| } |
| - waitClosed(t, ac.closed, 2*time.Second, what+": the app's socket") |
| + closedBy(t, deadline, what, nil, ac) |
| } |
| |
| // noHijack: a writer with no Hijack at all. slotWriter's Hijack then |
| @@ -1756,11 +1850,12 @@ func (s *wsScene) noHijack(t *testing.T, cookie, what string) { |
| if got := res.firstReleaser(); got != relBody { |
| t.Fatalf("%s: the first releaser is %d, want relBody (%d)", what, got, relBody) |
| } |
| + deadline = time.Now().Add(2 * time.Second) |
| close(barrier) |
| h.read(t) |
| wantStatus(t, h, http.StatusBadGateway, what) |
| waitOpen(t, s.hub, tc, 0, what) |
| - waitClosed(t, ac.closed, 2*time.Second, what+": the app's socket") |
| + closedBy(t, deadline, what, nil, ac) |
| } |
| |
| // revocations is spec §8.2 case 15: on an account plans do not govern, a |
| @@ -1814,16 +1909,17 @@ func (s *wsScene) revocations(t *testing.T) { |
| } |
| }() |
| } |
| + // One deadline for both ends, from the trigger: the registry |
| + // write under Watch, Reload's return when called directly. |
| + deadline := time.Now().Add(5 * time.Second) |
| rv.revoke(t) |
| - bound := 5 * time.Second |
| if !watch { |
| if err := s.hub.Reload(); err != nil { |
| t.Fatal(err) |
| } |
| - bound = 2 * time.Second |
| + deadline = time.Now().Add(2 * time.Second) |
| } |
| - waitClosed(t, c.closed, bound, "the client's socket") |
| - waitClosed(t, ac.closed, bound, "the app's socket") |
| + closedBy(t, deadline, name, c, ac) |
| before := len(s.app.seenFor(rv.host)) |
| h := s.handshake(t, rv.host, "/cable", cookie) |
| if h.resp.StatusCode == http.StatusSwitchingProtocols { |
| @@ -1865,7 +1961,7 @@ func (s *wsScene) putForward(t *testing.T, rt registry.Route) { |
| |
| func (s *wsScene) pointTunnel(t *testing.T, channel string) { |
| t.Helper() |
| - if err := s.hubReg.Upsert(wsTunnelRow(channel)); err != nil { |
| + if err := s.hubReg.Upsert(wsTunnelRow(wsTunnelHost, channel)); err != nil { |
| t.Fatal(err) |
| } |
| got, err := s.hubReg.Get(wsTunnelHost) |