feat: add control center, multiple relay nodes and P2P-first transport
This commit is contained in:
1 parent
0988e9c363
commit
2abfb63da9
44 files changed
+3477
-67
No files matched your search
@@ -0,0 +1,3 @@
|
||||
# Relay internals
|
||||
|
||||
Go package implementing short-lived HMAC route tickets, replay/revocation checks, bounded WebSocket stream pairing node health/load counters and optional rate-bounded STUN Binding. Shared by the controller and the standalone node binary. This directory contains Go source; no credentials are stored here.
|
||||
@@ -0,0 +1,204 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
type pair struct {
|
||||
lease string
|
||||
peers map[string]*websocket.Conn
|
||||
ready chan struct{}
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
expires int64
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
ID, Secret string
|
||||
Capacity int
|
||||
mu sync.Mutex
|
||||
pairs map[string]*pair
|
||||
used map[string]int64
|
||||
revoked map[string]int64
|
||||
bytes atomic.Int64
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewNode(ctx context.Context, id, secret string, capacity int) *Node {
|
||||
return &Node{ID: id, Secret: secret, Capacity: capacity, ctx: ctx, pairs: make(map[string]*pair), used: make(map[string]int64), revoked: make(map[string]int64)}
|
||||
}
|
||||
|
||||
func (n *Node) cleanupLocked() {
|
||||
now := time.Now().Unix()
|
||||
for id, expiry := range n.used {
|
||||
if expiry <= now {
|
||||
delete(n.used, id)
|
||||
}
|
||||
}
|
||||
for id, expiry := range n.revoked {
|
||||
if expiry <= now {
|
||||
delete(n.revoked, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
n.mu.Lock()
|
||||
count := len(n.pairs)
|
||||
n.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": n.ID, "sessions": count, "capacity": n.Capacity, "bytes": n.bytes.Load()})
|
||||
})
|
||||
mux.HandleFunc("POST /internal/revoke", n.revoke)
|
||||
mux.HandleFunc("GET /v2/tunnel", n.tunnel)
|
||||
return mux
|
||||
}
|
||||
|
||||
func (n *Node) revoke(w http.ResponseWriter, r *http.Request) {
|
||||
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte("Bearer "+n.Secret)) != 1 {
|
||||
http.Error(w, "unauthorized", 401)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Lease string `json:"lease"`
|
||||
}
|
||||
if json.NewDecoder(io.LimitReader(r.Body, 4096)).Decode(&body) != nil || body.Lease == "" {
|
||||
http.Error(w, "bad request", 400)
|
||||
return
|
||||
}
|
||||
n.mu.Lock()
|
||||
n.cleanupLocked()
|
||||
n.revoked[body.Lease] = time.Now().Add(2 * time.Minute).Unix()
|
||||
for _, p := range n.pairs {
|
||||
if p.lease == body.Lease {
|
||||
p.cancel()
|
||||
}
|
||||
}
|
||||
n.mu.Unlock()
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
func (n *Node) tunnel(w http.ResponseWriter, r *http.Request) {
|
||||
ticket, err := Verify(n.Secret, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "), n.ID, time.Now())
|
||||
if err != nil {
|
||||
http.Error(w, "unauthorized", 401)
|
||||
return
|
||||
}
|
||||
n.mu.Lock()
|
||||
n.cleanupLocked()
|
||||
key := ticket.Session + ":" + ticket.Side
|
||||
if n.revoked[ticket.Lease] > 0 || n.used[key] > 0 {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "revoked or replayed ticket", 409)
|
||||
return
|
||||
}
|
||||
p := n.pairs[ticket.Session]
|
||||
if p == nil {
|
||||
if len(n.pairs) >= n.Capacity {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "node full", 429)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(n.ctx)
|
||||
p = &pair{lease: ticket.Lease, peers: make(map[string]*websocket.Conn), ready: make(chan struct{}), ctx: ctx, cancel: cancel, expires: ticket.Expires}
|
||||
n.pairs[ticket.Session] = p
|
||||
}
|
||||
if p.lease != ticket.Lease || p.ctx.Err() != nil {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "session closed", 409)
|
||||
return
|
||||
}
|
||||
n.used[key] = ticket.Expires
|
||||
n.mu.Unlock()
|
||||
defer func() {
|
||||
p.cancel()
|
||||
n.mu.Lock()
|
||||
if n.pairs[ticket.Session] == p {
|
||||
delete(n.pairs, ticket.Session)
|
||||
}
|
||||
n.mu.Unlock()
|
||||
}()
|
||||
ws, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
p.cancel()
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
ws.SetReadLimit(128 * 1024)
|
||||
n.mu.Lock()
|
||||
if p.ctx.Err() != nil {
|
||||
n.mu.Unlock()
|
||||
return
|
||||
}
|
||||
p.peers[ticket.Side] = ws
|
||||
if len(p.peers) == 2 {
|
||||
close(p.ready)
|
||||
}
|
||||
n.mu.Unlock()
|
||||
timer := time.NewTimer(15 * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-p.ready:
|
||||
case <-p.ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
return
|
||||
}
|
||||
// Only the client handler owns copying; both handlers remain until it ends.
|
||||
if ticket.Side == "client" {
|
||||
n.mu.Lock()
|
||||
agent := p.peers["agent"]
|
||||
n.mu.Unlock()
|
||||
n.copy(p.ctx, ws, agent)
|
||||
p.cancel()
|
||||
} else {
|
||||
<-p.ctx.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) copy(parent context.Context, a, b *websocket.Conn) {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
defer cancel()
|
||||
var wg sync.WaitGroup
|
||||
copyOne := func(dst, src *websocket.Conn) {
|
||||
defer wg.Done()
|
||||
defer cancel()
|
||||
for {
|
||||
typ, data, err := src.Read(ctx)
|
||||
if err != nil || typ != websocket.MessageBinary {
|
||||
return
|
||||
}
|
||||
deadline, stop := context.WithTimeout(ctx, 30*time.Second)
|
||||
err = dst.Write(deadline, typ, data)
|
||||
stop()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n.bytes.Add(int64(len(data)))
|
||||
}
|
||||
}
|
||||
wg.Add(2)
|
||||
go copyOne(a, b)
|
||||
go copyOne(b, a)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (n *Node) Close() {
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
for _, p := range n.pairs {
|
||||
p.cancel()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coder/websocket"
|
||||
"github.com/pion/stun/v3"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const secret = "test-cluster-secret-at-least-thirty-two-bytes"
|
||||
|
||||
func TestTicketScope(t *testing.T) {
|
||||
now := time.Now()
|
||||
good := Ticket{"s", "l", "n", "client", now.Add(time.Minute).Unix()}
|
||||
encoded := Sign(secret, good)
|
||||
if _, err := Verify(secret, encoded, "n", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
token, node string
|
||||
when time.Time
|
||||
}{{encoded, "other", now}, {encoded, "n", now.Add(2 * time.Minute)}, {encoded + "x", "n", now}, {Sign(secret, Ticket{"s", "l", "n", "admin", good.Expires}), "n", now}, {Sign(secret, Ticket{"s", "l", "n", "agent", now.Add(time.Hour).Unix()}), "n", now}}
|
||||
for _, c := range cases {
|
||||
if _, err := Verify(secret, c.token, c.node, c.when); err == nil {
|
||||
t.Fatal("accepted invalid ticket")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestNodePairReplayAndRevocation(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
n := NewNode(ctx, "n", secret, 2)
|
||||
defer n.Close()
|
||||
s := httptest.NewServer(n.Handler())
|
||||
defer s.Close()
|
||||
ticket := func(side string) string {
|
||||
return Sign(secret, Ticket{"s", "l", "n", side, time.Now().Add(time.Minute).Unix()})
|
||||
}
|
||||
dial := func(token string) (*websocket.Conn, *http.Response, error) {
|
||||
return websocket.Dial(ctx, strings.Replace(s.URL, "http:", "ws:", 1)+"/v2/tunnel", &websocket.DialOptions{HTTPHeader: http.Header{"Authorization": []string{"Bearer " + token}}})
|
||||
}
|
||||
if c, r, e := dial(secret); e == nil {
|
||||
c.CloseNow()
|
||||
t.Fatal("accepted master secret as ticket")
|
||||
} else if r.StatusCode != 401 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
a, _, err := dial(ticket("agent"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.CloseNow()
|
||||
c, _, err := dial(ticket("client"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.CloseNow()
|
||||
if other, r, e := dial(ticket("client")); e == nil {
|
||||
other.CloseNow()
|
||||
t.Fatal("replay accepted")
|
||||
} else if r.StatusCode != 409 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
if err = c.Write(ctx, websocket.MessageBinary, []byte("adb")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kind, data, err := a.Read(ctx)
|
||||
if err != nil || kind != websocket.MessageBinary || string(data) != "adb" {
|
||||
t.Fatal(kind, string(data), err)
|
||||
}
|
||||
req, _ := http.NewRequest("POST", s.URL+"/internal/revoke", bytes.NewBufferString(`{"lease":"l"}`))
|
||||
req.Header.Set("Authorization", "Bearer "+secret)
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 204 {
|
||||
t.Fatal(response.StatusCode)
|
||||
}
|
||||
if _, _, err := c.Read(ctx); err == nil {
|
||||
t.Fatal("revocation did not close stream")
|
||||
}
|
||||
if other, r, e := dial(ticket("agent")); e == nil {
|
||||
other.CloseNow()
|
||||
t.Fatal("revoked ticket accepted")
|
||||
} else if r.StatusCode != 409 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
}
|
||||
func TestFailedUpgradeReleasesCapacity(t *testing.T) {
|
||||
n := NewNode(context.Background(), "n", secret, 1)
|
||||
defer n.Close()
|
||||
req := httptest.NewRequest("GET", "/v2/tunnel", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+Sign(secret, Ticket{"s", "l", "n", "agent", time.Now().Add(time.Minute).Unix()}))
|
||||
n.Handler().ServeHTTP(httptest.NewRecorder(), req)
|
||||
recorder := httptest.NewRecorder()
|
||||
n.Handler().ServeHTTP(recorder, httptest.NewRequest("GET", "/healthz", nil))
|
||||
var value struct{ Sessions int }
|
||||
json.Unmarshal(recorder.Body.Bytes(), &value)
|
||||
if value.Sessions != 0 {
|
||||
t.Fatal(value)
|
||||
}
|
||||
}
|
||||
func TestSTUNBinding(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go ServeSTUN(ctx, conn)
|
||||
client, err := net.DialUDP("udp", nil, conn.LocalAddr().(*net.UDPAddr))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
client.SetDeadline(time.Now().Add(time.Second))
|
||||
request := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
|
||||
client.Write(request.Raw)
|
||||
buffer := make([]byte, 1500)
|
||||
size, err := client.Read(buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := &stun.Message{Raw: buffer[:size]}
|
||||
if err = response.Decode(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var mapped stun.XORMappedAddress
|
||||
if err = mapped.GetFrom(response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mapped.Port != client.LocalAddr().(*net.UDPAddr).Port || !mapped.IP.Equal(net.IPv4(127, 0, 0, 1)) || response.TransactionID != request.TransactionID {
|
||||
t.Fatal(mapped)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pion/stun/v3"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ServeSTUN handles binding only; no TURN allocations or arbitrary destinations.
|
||||
// A fixed global budget bounds work and reflection traffic without a per-IP map.
|
||||
func ServeSTUN(ctx context.Context, conn *net.UDPConn) error {
|
||||
go func() { <-ctx.Done(); conn.Close() }()
|
||||
buffer := make([]byte, 1500)
|
||||
budget, reset := 100, time.Now()
|
||||
for {
|
||||
size, address, err := conn.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if time.Since(reset) >= time.Second {
|
||||
budget = 100
|
||||
reset = time.Now()
|
||||
}
|
||||
if budget == 0 || size < 20 || size > 1200 {
|
||||
continue
|
||||
}
|
||||
budget--
|
||||
message := &stun.Message{Raw: buffer[:size]}
|
||||
if message.Decode() != nil || message.Type != stun.BindingRequest {
|
||||
continue
|
||||
}
|
||||
response, err := stun.Build(stun.NewTransactionIDSetter(message.TransactionID), stun.BindingSuccess,
|
||||
&stun.XORMappedAddress{IP: address.IP, Port: address.Port})
|
||||
if err == nil {
|
||||
conn.WriteToUDP(response.Raw, address)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ticket struct {
|
||||
Session string `json:"session"`
|
||||
Lease string `json:"lease"`
|
||||
Node string `json:"node"`
|
||||
Side string `json:"side"`
|
||||
Expires int64 `json:"expires"`
|
||||
}
|
||||
|
||||
func Sign(secret string, ticket Ticket) string {
|
||||
payload, _ := json.Marshal(ticket)
|
||||
encoded := base64.RawURLEncoding.EncodeToString(payload)
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(encoded))
|
||||
return encoded + "." + base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func Verify(secret, encoded, node string, now time.Time) (Ticket, error) {
|
||||
var ticket Ticket
|
||||
parts := strings.Split(encoded, ".")
|
||||
if len(parts) != 2 {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(parts[0]))
|
||||
if !hmac.Equal(signature, mac.Sum(nil)) {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
payload, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil || json.Unmarshal(payload, &ticket) != nil {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
if ticket.Node != node || ticket.Expires <= now.Unix() || ticket.Expires > now.Add(2*time.Minute).Unix() || ticket.Session == "" || ticket.Lease == "" || (ticket.Side != "client" && ticket.Side != "agent") {
|
||||
return ticket, errors.New("expired or invalid ticket")
|
||||
}
|
||||
return ticket, nil
|
||||
}
|
||||
Reference in new issue
Block a user