mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
ISS telemetry relay: cap frames on the socket, time out the handshake, slower ladder for flapping sessions
- The feed socket is opened with maxPayload at the 64 KB frame cap and a 10 s handshake timeout: ws would otherwise buffer up to 100 MiB before the cap applied. - Only a session that has run a minute brings the retry ladder back to its first step, so a feed that sends a snapshot and then END keeps backing off; the ladder's later steps are tested. - The rate limit keys IPv6 clients by /64, and any path under the route is a JSON 404.
This commit is contained in:
1 parent
14814b4dab
commit
2c2e9de14d
2 files changed
+76
-9
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { createRequire } from 'node:module';
|
||||
import { clientKey, makeRateLimiter } from '../common/rate-limit.js';
|
||||
import { clientNetworkKey, makeRateLimiter } from '../common/rate-limit.js';
|
||||
import { admitSameSiteKeyless } from '../common/same-site.js';
|
||||
|
||||
/**
|
||||
@@ -76,6 +76,14 @@ const SILENCE_MS = 30_000;
|
||||
const IDLE_MS = 120_000;
|
||||
/** Waits between connection attempts, the last one repeating. */
|
||||
const RETRY_LADDER_MS = Object.freeze([5_000, 15_000, 60_000, 300_000]);
|
||||
/**
|
||||
* A session that has run this long has proved itself: its updates bring the
|
||||
* retry ladder back to its first step. One that ends sooner (a snapshot and
|
||||
* then `END`, over and over) keeps climbing the ladder.
|
||||
*/
|
||||
const STABLE_SESSION_MS = 60_000;
|
||||
/** The opening handshake with the feed gives up after this. */
|
||||
const HANDSHAKE_TIMEOUT_MS = 10_000;
|
||||
/** A frame larger than this is not telemetry; it is dropped unread. */
|
||||
const MAX_FRAME_BYTES = 64 * 1024;
|
||||
const TICK_MS = 5_000;
|
||||
@@ -173,11 +181,14 @@ export function createIssTelemetryRelay({ createSocket, now = Date.now }) {
|
||||
let failures = 0;
|
||||
let retryAt = 0;
|
||||
let unavailable = false;
|
||||
/** When the live socket was opened, or null without one. */
|
||||
let connectedAt = null;
|
||||
|
||||
function drop() {
|
||||
generation += 1;
|
||||
const old = socket;
|
||||
socket = null;
|
||||
connectedAt = null;
|
||||
fields.clear();
|
||||
// A replacement session must rebuild both its joint snapshot and signal
|
||||
// credit; a new clock tick cannot make the old session's angles live.
|
||||
@@ -223,7 +234,8 @@ export function createIssTelemetryRelay({ createSocket, now = Date.now }) {
|
||||
const name = ISS_TELEMETRY_ITEMS[update.item - 1];
|
||||
if (!name) return;
|
||||
fields.set(update.item, update.fields);
|
||||
failures = 0;
|
||||
if (connectedAt !== null && now() - connectedAt >= STABLE_SESSION_MS)
|
||||
failures = 0;
|
||||
const [stamp, raw, statusClass] = update.fields;
|
||||
const at = now();
|
||||
if (name === SIGNAL_ITEM) {
|
||||
@@ -257,6 +269,7 @@ export function createIssTelemetryRelay({ createSocket, now = Date.now }) {
|
||||
generation += 1;
|
||||
const mine = generation;
|
||||
socket = opened;
|
||||
connectedAt = now();
|
||||
lastFrameAt = now();
|
||||
opened.on('open', () => {
|
||||
if (mine !== generation) return;
|
||||
@@ -380,7 +393,14 @@ export function issTelemetryProxy({
|
||||
env,
|
||||
createSocket = () => {
|
||||
const WebSocket = loadWebSocket();
|
||||
return WebSocket ? new WebSocket(FEED_URL, FEED_PROTOCOL) : null;
|
||||
// `ws` buffers a whole frame before handing it over, up to 100 MiB by
|
||||
// default: the frame cap applies on the socket, not only after it.
|
||||
return WebSocket
|
||||
? new WebSocket(FEED_URL, FEED_PROTOCOL, {
|
||||
maxPayload: MAX_FRAME_BYTES,
|
||||
handshakeTimeout: HANDSHAKE_TIMEOUT_MS,
|
||||
})
|
||||
: null;
|
||||
},
|
||||
now = Date.now,
|
||||
} = {}) {
|
||||
@@ -407,6 +427,12 @@ export function issTelemetryProxy({
|
||||
|
||||
function handle(req, res) {
|
||||
if (admitSameSiteKeyless(req, res)) return;
|
||||
// The route is the one path: anything under it is not found, as JSON.
|
||||
const path = String(req.url ?? '/').split('?')[0];
|
||||
if (path !== '/' && path !== '') {
|
||||
json(res, 404, { error: 'Not Found' });
|
||||
return;
|
||||
}
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
json(res, 405, { error: 'Method Not Allowed' });
|
||||
return;
|
||||
@@ -416,7 +442,8 @@ export function issTelemetryProxy({
|
||||
json(res, 200, { configured: false });
|
||||
return;
|
||||
}
|
||||
if (!allow(clientKey(req))) {
|
||||
// IPv6 clients fold to their /64, so one host cannot spread across it.
|
||||
if (!allow(clientNetworkKey(req))) {
|
||||
json(res, 429, { error: 'Too Many Requests' });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ function install(plugin, preview = false) {
|
||||
plugin[preview ? 'configurePreviewServer' : 'configureServer']({
|
||||
middlewares: { use: (route, handler) => routes.set(route, handler) },
|
||||
});
|
||||
return (method = 'GET', headers = {}) => {
|
||||
return (method = 'GET', headers = {}, url = '/') => {
|
||||
const res = {
|
||||
headers: {},
|
||||
writeHead(status, headers) {
|
||||
@@ -329,10 +329,7 @@ function install(plugin, preview = false) {
|
||||
this.status ??= this.statusCode;
|
||||
},
|
||||
};
|
||||
routes.get(ISS_TELEMETRY_ROUTE)(
|
||||
{ url: '/', method, headers, socket: {} },
|
||||
res,
|
||||
);
|
||||
routes.get(ISS_TELEMETRY_ROUTE)({ url, method, headers, socket: {} }, res);
|
||||
return res;
|
||||
};
|
||||
}
|
||||
@@ -375,6 +372,10 @@ test('switched on, the route opens the feed once and serves the snapshot', () =>
|
||||
request();
|
||||
assert.equal(sockets.length, 1, 'one connection for every request');
|
||||
assert.equal(request('POST').status, 405);
|
||||
const unknown = request('GET', {}, '/anything');
|
||||
assert.equal(unknown.status, 404);
|
||||
assert.deepEqual(JSON.parse(unknown.body), { error: 'Not Found' });
|
||||
assert.equal(request('GET', {}, '/?t=1').status, 200, 'a query is fine');
|
||||
assert.equal(
|
||||
request('GET', { 'sec-fetch-site': 'cross-site' }).status,
|
||||
403,
|
||||
@@ -388,3 +389,42 @@ test('switched on, the route opens the feed once and serves the snapshot', () =>
|
||||
}
|
||||
assert.equal(sockets[0].terminated, true);
|
||||
});
|
||||
|
||||
test('a session that keeps ending soon after its snapshot climbs the retry ladder', () => {
|
||||
const { relay, sockets, clock } = relayAt(NOV_13);
|
||||
relay.demand();
|
||||
/** The current socket opens, sends a snapshot update, then ends. */
|
||||
const shortSession = () => {
|
||||
const socket = sockets.at(-1);
|
||||
socket.emit('open');
|
||||
socket.emit('message', 'CONOK,S1,50000,5000,*');
|
||||
socket.emit(
|
||||
'message',
|
||||
`U,1,${index('S0000004')},${stampOf(clock.ms)}|17|24`,
|
||||
);
|
||||
socket.emit('message', 'END,41,session closed');
|
||||
};
|
||||
// Each wait is the next step: 5 s, 15 s, 60 s, then 5 minutes.
|
||||
for (const wait of [5_000, 15_000, 60_000, 300_000, 300_000]) {
|
||||
shortSession();
|
||||
const before = sockets.length;
|
||||
clock.ms += wait - 1_000;
|
||||
relay.demand();
|
||||
assert.equal(sockets.length, before, `not before ${wait / 1000} s`);
|
||||
clock.ms += 1_000;
|
||||
relay.demand();
|
||||
assert.equal(sockets.length, before + 1, `again after ${wait / 1000} s`);
|
||||
}
|
||||
// A session that runs a minute before its updates brings the ladder down.
|
||||
const socket = sockets.at(-1);
|
||||
socket.emit('open');
|
||||
socket.emit('message', 'CONOK,S1,50000,5000,*');
|
||||
clock.ms += 60_000;
|
||||
relay.demand();
|
||||
socket.emit('message', `U,1,${index('S0000004')},${stampOf(clock.ms)}|17|24`);
|
||||
socket.emit('message', 'END,41,session closed');
|
||||
const before = sockets.length;
|
||||
clock.ms += 5_000;
|
||||
relay.demand();
|
||||
assert.equal(sockets.length, before + 1, 'back to 5 s');
|
||||
});
|
||||
Reference in new issue
Block a user