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
+41
-11
@@ -25,6 +25,7 @@ import (
|
||||
type agent struct {
|
||||
id string
|
||||
ws *websocket.Conn
|
||||
v2 bool
|
||||
}
|
||||
type lease struct {
|
||||
id string
|
||||
@@ -32,13 +33,15 @@ type lease struct {
|
||||
ws *websocket.Conn
|
||||
}
|
||||
type session struct {
|
||||
id string
|
||||
agent *agent
|
||||
lease *lease
|
||||
ready chan *websocket.Conn
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
claimed bool
|
||||
id string
|
||||
agent *agent
|
||||
lease *lease
|
||||
ready chan *websocket.Conn
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
claimed bool
|
||||
v2 bool
|
||||
signalKey string
|
||||
}
|
||||
type broker struct {
|
||||
mu sync.Mutex
|
||||
@@ -47,6 +50,7 @@ type broker struct {
|
||||
sessions map[string]*session
|
||||
agentToken, clientToken string
|
||||
ctx context.Context
|
||||
cluster *cluster
|
||||
}
|
||||
|
||||
func newBroker(ctx context.Context, agentToken, clientToken string) *broker {
|
||||
@@ -132,7 +136,7 @@ func (b *broker) agentEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
// Reserve before upgrading, so duplicate agents never replace an existing one.
|
||||
a := &agent{id: id}
|
||||
a := &agent{id: id, v2: r.URL.Query().Get("v") == "2"}
|
||||
b.mu.Lock()
|
||||
if b.agents[id] != nil {
|
||||
b.mu.Unlock()
|
||||
@@ -169,7 +173,11 @@ func (b *broker) agentEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
b.mu.Lock()
|
||||
a.ws = ws
|
||||
b.mu.Unlock()
|
||||
if err := writeJSON(b.ctx, ws, map[string]string{"type": "registered", "device": id}); err != nil {
|
||||
registered := map[string]any{"type": "registered", "device": id}
|
||||
if a.v2 {
|
||||
registered["protocol"] = 2
|
||||
}
|
||||
if err := writeJSON(b.ctx, ws, registered); err != nil {
|
||||
return
|
||||
}
|
||||
log.Printf("agent online: %s", id)
|
||||
@@ -220,6 +228,7 @@ func (b *broker) leaseEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
b.mu.Unlock()
|
||||
b.cluster.revoke(l.id)
|
||||
}()
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
@@ -233,7 +242,15 @@ func (b *broker) leaseEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
l.ws = ws
|
||||
b.mu.Unlock()
|
||||
if err := writeJSON(b.ctx, ws, map[string]string{"type": "leased", "lease": l.id}); err != nil {
|
||||
protocol := 1
|
||||
if b.cluster != nil && a.v2 && r.URL.Query().Get("v") == "2" {
|
||||
protocol = 2
|
||||
}
|
||||
leased := map[string]any{"type": "leased", "lease": l.id}
|
||||
if r.URL.Query().Get("v") == "2" {
|
||||
leased["protocol"] = protocol
|
||||
}
|
||||
if err := writeJSON(b.ctx, ws, leased); err != nil {
|
||||
return
|
||||
}
|
||||
hold(b.ctx, ws)
|
||||
@@ -287,7 +304,7 @@ func (b *broker) dataEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
ticket, id := r.URL.Query().Get("session"), r.URL.Query().Get("device")
|
||||
b.mu.Lock()
|
||||
s := b.sessions[ticket]
|
||||
if s == nil || s.agent.id != id || b.agents[id] != s.agent || s.claimed || s.ctx.Err() != nil {
|
||||
if s == nil || s.v2 || s.agent.id != id || b.agents[id] != s.agent || s.claimed || s.ctx.Err() != nil {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "invalid or expired session", 404)
|
||||
return
|
||||
@@ -342,12 +359,17 @@ func (b *broker) handler() http.Handler {
|
||||
mux.HandleFunc("GET /v1/devices", b.auth(b.clientToken, b.devicesEndpoint))
|
||||
mux.HandleFunc("GET /v1/lease", b.auth(b.clientToken, b.leaseEndpoint))
|
||||
mux.HandleFunc("GET /v1/connect", b.auth(b.clientToken, b.connectEndpoint))
|
||||
mux.HandleFunc("GET /v2/nodes", b.auth(b.clientToken, b.nodesEndpoint))
|
||||
mux.HandleFunc("GET /v2/connect", b.auth(b.clientToken, b.clusterConnectEndpoint))
|
||||
mux.HandleFunc("GET /v2/signal", b.auth(b.agentToken, b.signalEndpoint))
|
||||
return mux
|
||||
}
|
||||
func main() {
|
||||
listen := flag.String("listen", "127.0.0.1:8080", "HTTP listen address (use a TLS reverse proxy for public access)")
|
||||
cert := flag.String("tls-cert", "", "TLS certificate PEM path")
|
||||
key := flag.String("tls-key", "", "TLS private key PEM path")
|
||||
clusterFile := flag.String("cluster-config", "", "Cluster configuration JSON; empty preserves standalone v1 mode")
|
||||
insecureNodes := flag.Bool("allow-insecure-nodes", false, "Allow ws relay URLs for isolated integration tests")
|
||||
flag.Parse()
|
||||
agentToken, clientToken := os.Getenv("ADS_AGENT_TOKEN"), os.Getenv("ADS_CLIENT_TOKEN")
|
||||
if len(agentToken) < 32 || len(clientToken) < 32 || agentToken == clientToken {
|
||||
@@ -359,6 +381,14 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
b := newBroker(ctx, agentToken, clientToken)
|
||||
if *clusterFile != "" {
|
||||
configured, err := loadCluster(*clusterFile, os.Getenv("ADS_RELAY_SECRET"), *insecureNodes)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b.cluster = configured
|
||||
go configured.monitor(ctx)
|
||||
}
|
||||
server := &http.Server{Addr: *listen, Handler: b.handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 * 1024}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
Reference in new issue
Block a user