From aa001ce2142080e65a9e6499ea0d2aa86574f081 Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 31 Mar 2026 05:18:20 +0000 Subject: [PATCH] refactor(node): rename websocket handler and clarify UEPS comments Co-Authored-By: Virgil --- node/controller_test.go | 2 +- node/integration_test.go | 6 +++--- node/transport.go | 6 +++--- node/transport_test.go | 8 ++++---- ueps/packet.go | 18 ++++++++++-------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/node/controller_test.go b/node/controller_test.go index aa6ede8..c0881ef 100644 --- a/node/controller_test.go +++ b/node/controller_test.go @@ -50,7 +50,7 @@ func makeWorkerServer(t *testing.T) (*NodeManager, string, *Transport) { srv := NewTransport(nm, reg, cfg) mux := http.NewServeMux() - mux.HandleFunc(cfg.WebSocketPath, srv.handleWSUpgrade) + mux.HandleFunc(cfg.WebSocketPath, srv.handleWebSocketUpgrade) ts := httptest.NewServer(mux) u, _ := url.Parse(ts.URL) diff --git a/node/integration_test.go b/node/integration_test.go index f523457..4fd8f2e 100644 --- a/node/integration_test.go +++ b/node/integration_test.go @@ -82,7 +82,7 @@ func TestIntegration_FullNodeLifecycle_Good(t *testing.T) { // Start the worker transport behind httptest. mux := http.NewServeMux() - mux.HandleFunc(workerCfg.WebSocketPath, workerTransport.handleWSUpgrade) + mux.HandleFunc(workerCfg.WebSocketPath, workerTransport.handleWebSocketUpgrade) ts := httptest.NewServer(mux) t.Cleanup(func() { controllerTransport.Stop() @@ -485,7 +485,7 @@ func TestIntegration_AllowlistHandshakeRejection_Bad(t *testing.T) { workerTransport := NewTransport(workerNM, workerReg, DefaultTransportConfig()) mux := http.NewServeMux() - mux.HandleFunc("/ws", workerTransport.handleWSUpgrade) + mux.HandleFunc("/ws", workerTransport.handleWebSocketUpgrade) ts := httptest.NewServer(mux) t.Cleanup(func() { workerTransport.Stop() @@ -529,7 +529,7 @@ func TestIntegration_AllowlistHandshakeAccepted_Good(t *testing.T) { worker.RegisterOnTransport() mux := http.NewServeMux() - mux.HandleFunc("/ws", workerTransport.handleWSUpgrade) + mux.HandleFunc("/ws", workerTransport.handleWebSocketUpgrade) ts := httptest.NewServer(mux) t.Cleanup(func() { workerTransport.Stop() diff --git a/node/transport.go b/node/transport.go index d92dc29..a0755d0 100644 --- a/node/transport.go +++ b/node/transport.go @@ -313,7 +313,7 @@ func (t *Transport) agentUserAgent() string { // Start opens the WebSocket listener and background maintenance loops. func (t *Transport) Start() error { mux := http.NewServeMux() - mux.HandleFunc(t.config.webSocketPath(), t.handleWSUpgrade) + mux.HandleFunc(t.config.webSocketPath(), t.handleWebSocketUpgrade) listenAddress := t.config.listenAddress() @@ -533,8 +533,8 @@ func (t *Transport) GetConnection(peerID string) *PeerConnection { return t.connections[peerID] } -// handleWSUpgrade handles incoming WebSocket connections. -func (t *Transport) handleWSUpgrade(w http.ResponseWriter, r *http.Request) { +// handleWebSocketUpgrade handles incoming WebSocket connections. +func (t *Transport) handleWebSocketUpgrade(w http.ResponseWriter, r *http.Request) { userAgent := r.Header.Get("User-Agent") // Enforce the maximum connection limit, including pending handshakes. diff --git a/node/transport_test.go b/node/transport_test.go index 424da51..24969b1 100644 --- a/node/transport_test.go +++ b/node/transport_test.go @@ -72,7 +72,7 @@ func setupTestTransportPairWithConfig(t *testing.T, serverCfg, clientCfg Transpo // Use httptest.Server with the transport's WebSocket handler mux := http.NewServeMux() - mux.HandleFunc(serverCfg.WebSocketPath, serverTransport.handleWSUpgrade) + mux.HandleFunc(serverCfg.WebSocketPath, serverTransport.handleWebSocketUpgrade) ts := httptest.NewServer(mux) u, _ := url.Parse(ts.URL) @@ -253,7 +253,7 @@ func TestTransport_ConnectSendsAgentUserAgent_Good(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc(serverCfg.WebSocketPath, func(w http.ResponseWriter, r *http.Request) { capturedUserAgent.Store(r.Header.Get("User-Agent")) - serverTransport.handleWSUpgrade(w, r) + serverTransport.handleWebSocketUpgrade(w, r) }) ts := httptest.NewServer(mux) @@ -482,7 +482,7 @@ func TestTransport_MaxConnectionsEnforcement_Good(t *testing.T) { serverTransport := NewTransport(serverNM, serverReg, serverCfg) mux := http.NewServeMux() - mux.HandleFunc(serverCfg.WebSocketPath, serverTransport.handleWSUpgrade) + mux.HandleFunc(serverCfg.WebSocketPath, serverTransport.handleWebSocketUpgrade) ts := httptest.NewServer(mux) t.Cleanup(func() { serverTransport.Stop() @@ -746,7 +746,7 @@ func TestTransport_NewTransport_DefaultMaxMessageSize_Good(t *testing.T) { if tr.config.MaxMessageSize != 0 { t.Errorf("config should preserve 0 value, got %d", tr.config.MaxMessageSize) } - // The actual default is applied at usage time (readLoop, handleWSUpgrade) + // The actual default is applied at usage time (readLoop, handleWebSocketUpgrade) } func TestTransport_ConnectedPeerCount_Good(t *testing.T) { diff --git a/ueps/packet.go b/ueps/packet.go index be9d440..45ed113 100644 --- a/ueps/packet.go +++ b/ueps/packet.go @@ -21,7 +21,7 @@ const ( TagPayload = 0xFF // The Data ) -// UEPSHeader represents the conscious routing metadata +// UEPSHeader represents the conscious routing metadata. // // header := UEPSHeader{IntentID: 0x01} type UEPSHeader struct { @@ -32,17 +32,17 @@ type UEPSHeader struct { ThreatScore uint16 // 0-65535 } -// PacketBuilder helps construct a signed UEPS frame +// PacketBuilder builds a signed UEPS frame from a concrete intent and payload. // -// builder := NewBuilder(0x01, []byte("hello")) +// builder := NewBuilder(0x20, []byte("hello")) type PacketBuilder struct { Header UEPSHeader Payload []byte } -// NewBuilder creates a packet context for a specific intent +// NewBuilder creates a packet builder for a specific intent and payload. // -// builder := NewBuilder(0x01, []byte("hello")) +// builder := NewBuilder(0x20, []byte("hello")) func NewBuilder(intentID uint8, payload []byte) *PacketBuilder { return &PacketBuilder{ Header: UEPSHeader{ @@ -56,7 +56,9 @@ func NewBuilder(intentID uint8, payload []byte) *PacketBuilder { } } -// MarshalAndSign generates the final byte stream using the shared secret +// MarshalAndSign signs a packet with a shared secret. +// +// frame, err := builder.MarshalAndSign(sharedSecret) func (p *PacketBuilder) MarshalAndSign(sharedSecret []byte) ([]byte, error) { buf := new(bytes.Buffer) @@ -106,8 +108,8 @@ func (p *PacketBuilder) MarshalAndSign(sharedSecret []byte) ([]byte, error) { return buf.Bytes(), nil } -// Helper to write a simple TLV. -// Now uses 2-byte big-endian length (uint16) to support up to 64KB payloads. +// writeTLV writes a single tag-length-value record with a 2-byte length prefix. +// It supports payloads up to 64KB. func writeTLV(w io.Writer, tag uint8, value []byte) error { // Check length constraint (2 byte length = max 65535 bytes) if len(value) > 65535 { -- 2.45.3