WebSocket மறைகுறியாக்கம்
GraphQL சந்தாக்கள் மறைகுறியாக்கப்பட்ட பைனரி சட்டகங்களைப் பயன்படுத்துகின்றன.
இந்தப் பக்கம் WSS /graphql-ஐக் காட்டுகிறது: அமர்வு விசை init, ack மற்றும் செய்திச் சட்டகங்களை XChaCha20-Poly1305 குறிச்சொற்களால் அங்கீகரிக்கிறது. இச்சட்டகங்களில் Ed25519 கையொப்பமோ SignaturePurpose-ஓ இல்லை. கடவுச்சொல் இல்லாத உள்நுழைவு WSS /auth-இல் ServerHello-ஐக் கையொப்பமிடுகிறது; அங்கீகாரத்தைப் பார்க்கவும்.
wss://<host>:<port>/graphql?cid=706c6169-3b80-8f42-9a61-4e312e54d001Init அனுப்பவும்
சேவையகம் Init-ஐச் சரிபார்த்து clientNonce-ஐப் படிக்கிறது.
மீளனுப்பல் தாக்குதல் தடுப்பு
…/graphql?cid=<clientId>binding = SHA256(fields(initFrame, ackFrame))client → server: seq 1, 2, 3…server → client: seq 1, 2, 3…seq = lastReceived + 1lastReceived ← seqcloseமீண்டும் இணைக்கும்போது: புதிய binding, எண்ணிகள் மீட்டமைப்பு
பிற WebSocket வழிகள்
key: தற்போதைய அமர்வின் token
wss://<host>:<port>/screen?cid=<clientId>Init · clientNonce
சேவையகம் Init-ஐச் சரிபார்த்து clientNonce-ஐப் படிக்கிறது.
செயல்பாடுகள்
1. இணைக்கவும்
socket திறந்த பிறகு Init அனுப்பவும். cid கிளையன்டை அடையாளம் காண்கிறது.
const cid = '706c6169-3b80-8f42-9a61-4e312e54d001' // Your existing session's client ID
const socket = new WebSocket(
'wss://<device-host>:<https-port>/graphql?cid=' + encodeURIComponent(cid)
)
socket.binaryType = 'arraybuffer'
let binding: Uint8Array
let lastSent = 0n
let lastReceived = 0n2. கிளையன்ட் Init
10 வினாடிகளுக்குள் அனுப்பவும். clientNonce: 32 சீரற்ற பைட்டுகள், 64 hex எழுத்துகளாக. AEAD nonce: ஒவ்வொரு சட்டகத்திற்கும் 24 சீரற்ற பைட்டுகள்.
// Run immediately on socket.open.
const init = {
clientNonce: Array.from(randomBytes(32), n => n.toString(16).padStart(2, '0')).join(''),
}
const aad = fields([utf8.encode('WS_INIT'), utf8.encode('/graphql'), utf8.encode(cid)])
const initFrame = seal(key, utf8.encode(JSON.stringify(init)), aad)
socket.send(initFrame) // Retain these exact bytes for Ack.3. சேவையக Ack
Ack-ஐச் சரிபார்த்து binding-ஐக் கணக்கிட்டு, பின்னர் கிளையன்ட் sequence 1-இல் உடனடியாக மறைகுறியாக்கப்பட்ட வெற்று உறுதிப்படுத்தலை அனுப்பவும்.
socket.addEventListener('message', event => {
const ackFrame = new Uint8Array(event.data)
if (ackFrame.length < 40) throw new Error('Invalid Ack')
const aad = fields([utf8.encode('WS_ACK'), utf8.encode('/graphql'), utf8.encode(cid), sha256(initFrame)])
const ack: { serverNonce: string } = JSON.parse(decoder.decode(open(key, ackFrame, aad)))
if (!/^[0-9a-f]{64}$/.test(ack.serverNonce)) throw new Error('Invalid serverNonce')
binding = sha256(fields([initFrame, ackFrame]))
lastSent = lastReceived = 0n
const counter = uint64be(1n)
const confirmationAad = fields([utf8.encode('WS_FRAME'), utf8.encode('/graphql'), utf8.encode(cid), binding, utf8.encode('client'), counter])
const encrypted = seal(key, new Uint8Array(), confirmationAad)
lastSent = 1n
socket.send(concatBytes(counter, encrypted))
}, { once: true })4. மறைகுறியாக்கப்பட்ட செய்தியை அனுப்பவும்
direction client மற்றும் புதிய nonce பயன்படுத்தவும். வெற்று உறுதிப்படுத்தல் sequence 1-ஐப் பயன்படுத்தும்; செயல்பாட்டுச் செய்திகள் sequence 2-இல் தொடங்கி கண்டிப்பாக அதிகரிக்கும்.
const message = {
type: 'subscribe', id: 'prefs',
payload: { query: 'subscription { prefsChanged }' },
}
const next = lastSent + 1n
const counter = uint64be(next) // Reject overflow; never wrap.
const aad = fields([utf8.encode('WS_FRAME'), utf8.encode('/graphql'), utf8.encode(cid), binding, utf8.encode('client'), counter])
const encrypted = seal(key, utf8.encode(JSON.stringify(message)), aad)
lastSent = next // Advance only after successful encryption.
socket.send(concatBytes(counter, encrypted))5. மறைகுறியாக்கப்பட்ட செய்தியைப் பெறவும்
lastReceived + 1 (முதலில்: 1) மற்றும் direction server-ஐக் கட்டாயப்படுத்தவும். அங்கீகரிக்கப்பட்ட மறைகுறி நீக்கத்திற்குப் பிறகே முன்னேற்றவும்.
// For each binary message after Ack, in arrival order:
const frame = new Uint8Array(event.data)
if (frame.length < 48) throw new Error('Invalid frame')
const sequence = new DataView(frame.buffer).getBigUint64(0, false)
if (sequence !== lastReceived + 1n) throw new Error('Invalid sequence')
const aad = fields([utf8.encode('WS_FRAME'), utf8.encode('/graphql'), utf8.encode(cid), binding, utf8.encode('server'), frame.subarray(0, 8)])
const plaintext = open(key, frame.subarray(8), aad) // Throws on invalid tag.
lastReceived = sequence // Advance only after authentication.
const message = JSON.parse(decoder.decode(plaintext))6. செய்தி வகைகள்
ping/pong உள்ளிட்ட பயன்பாட்டு JSON செய்திகள் மறைகுறியாக்கப்படும். WebSocket-இன் இயல்பான Ping/Pong கட்டுப்பாட்டு ஃபிரேம்களுக்கு இந்த மறைகுறியாக்கம் பொருந்தாது; ஆனால் TLS அவற்றைப் பாதுகாக்கலாம். சந்தா தயாரானதை subscriptionReady குறிக்கும்.
const subscribe = {
type: 'subscribe', id: 'prefs',
payload: { query: 'subscription { prefsChanged }' },
}
const ready = {
type: 'next', id: 'prefs',
payload: { data: null, extensions: { subscriptionReady: true } },
}
const event = {
type: 'next', id: 'prefs',
payload: { data: { prefsChanged: true } },
}
const complete = { type: 'complete', id: 'prefs' }
const ping = { type: 'ping', payload: null }
const pong = { type: 'pong', payload: null }
const error = { type: 'error', id: 'prefs', payload: [{ message: '...' }] }7. வரம்புகளும் தோல்விகளும்
அங்கீகாரம் அல்லது வரிசை எண் தோல்வியுற்றால் இணைப்பை மூடவும். புதிய கைகுலுக்கலுடன் மீண்டும் இணைக்கவும்.
Server Init wait: timeout(10 seconds, socket.next()), after upgrade and session-key lookup
Server confirmation: timeout(10 seconds, socket.next()), after Ack send completes
Confirmation: send encrypted empty payload immediately after Ack, client sequence 1
Business messages: client starts at sequence 2; server starts at sequence 1
Init replay: may receive a fresh Ack; old bound frames fail
Replay state: connection counters, no timestamp or global cache
Message sequence: last + 1, independently in each direction
Sequence overflow: u64 max once; next frame fails, never wrap to 0
Frame size: <= 1,048,576 bytes
Timeout/auth failure: close 1008, reason "invalid_subscription"
Reconnect: fresh Init / Ack, new binding, last counters reset to 0; no legacy fallback