Hub: node signature before the WebSocket upgrade
The hub accepted a WebSocket from anyone on /node/v1, a path in the open code, and replied with a hello nonce: one connection was enough to tell a scanner what it had found. The agent now signs the upgrade request itself (X-Node-Id, X-Node-Ts, X-Node-Sig; on first binding also the join secret and its key), and the hub checks it before Accept. Without a valid signature it answers Go's plain "404 page not found". Inside the socket the protocol is unchanged, and the auth frame must come from the node that signed the request. Older agents send no signature. The hub lets them in while some node has never connected the new way (nodes.preauth), logs them as outdated, and closes that door by itself once every node is updated; HUB_LEGACY=on/off overrides. The timestamp window is a day: node clocks drift, and the headers travel inside pinned TLS. The agent treats a 404 at upgrade as a rejection and backs off for a minute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013bNGYiDst62wH3ZGTRPFLJ
This commit is contained in:
+28
-1
@@ -198,6 +198,26 @@ func authMsg(nonce string, nodeID int) []byte {
|
||||
return []byte("awg-node-v1\n" + nonce + "\n" + strconv.Itoa(nodeID))
|
||||
}
|
||||
|
||||
// preauthMsg — подпись в заголовке запроса на WebSocket: хаб проверяет её до апгрейда и без неё
|
||||
// отвечает 404, как пустой сервер (internal/ui/hub_preauth.go, там же копия).
|
||||
func preauthMsg(nodeID int, ts int64) []byte {
|
||||
return []byte("awg-node-pre-v1\n" + strconv.Itoa(nodeID) + "\n" + strconv.FormatInt(ts, 10))
|
||||
}
|
||||
|
||||
// preauthHeader — подпись ноды (и при первой привязке — секрет с ключом) для запроса на WebSocket.
|
||||
func (a *Agent) preauthHeader() http.Header {
|
||||
ts := time.Now().Unix()
|
||||
h := http.Header{}
|
||||
h.Set("X-Node-Id", strconv.Itoa(a.c.NodeID))
|
||||
h.Set("X-Node-Ts", strconv.FormatInt(ts, 10))
|
||||
h.Set("X-Node-Sig", b64.EncodeToString(ed25519.Sign(a.nodeKey, preauthMsg(a.c.NodeID, ts))))
|
||||
if s := a.secret(); s != "" {
|
||||
h.Set("X-Node-Join", s)
|
||||
h.Set("X-Node-Pub", a.nodePubRaw)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// ── Запросы core → awg-ctrl ────────────────────────────────────────────────
|
||||
|
||||
var methods = map[string]bool{"GET": true, "POST": true, "PUT": true, "DELETE": true}
|
||||
@@ -476,13 +496,20 @@ func (a *Agent) connectOnce(ctx context.Context, onReady func()) (authFailed boo
|
||||
|
||||
tr := &http.Transport{Proxy: nil, TLSClientConfig: a.tlsConfig(), TLSHandshakeTimeout: handshakeTimeout}
|
||||
dctx, dcancel := context.WithTimeout(ctx, handshakeTimeout)
|
||||
ws, _, err := websocket.Dial(dctx, fmt.Sprintf("wss://%s/node/v1", a.addr()), &websocket.DialOptions{
|
||||
ws, resp, err := websocket.Dial(dctx, fmt.Sprintf("wss://%s/node/v1", a.addr()), &websocket.DialOptions{
|
||||
HTTPClient: &http.Client{Transport: tr},
|
||||
HTTPHeader: a.preauthHeader(),
|
||||
CompressionMode: websocket.CompressionDisabled,
|
||||
OnPingReceived: func(context.Context, []byte) bool { armDead(); return true },
|
||||
})
|
||||
dcancel()
|
||||
if err != nil {
|
||||
// 404 до апгрейда — хаб не принял подпись (или IP временно заблокирован за неудачи):
|
||||
// как отказ в auth, переподключение — не чаще раза в минуту.
|
||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||
a.c.Errorf("agent: core отклонил ноду до подключения (неверный ключ, просроченная или использованная ссылка, часы ноды сильно расходятся)")
|
||||
return true
|
||||
}
|
||||
if errors.Is(err, errPin) {
|
||||
a.c.Errorf("agent: %v", errPin)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user