mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
routing: a slow route is not a down relay, and the relay takes whole days
A request to the TREK Valhalla that runs out its deadline (a long bike route) is answered 504 and no longer rests the relay for every user or a tab for ten minutes; that one day goes to OSRM. Only an unreachable host or a server error rests it. A day goes to the relay with up to twenty stops per request and no pause between pieces; a request that ends up at the public FOSSGIS Valhalla is cut to ten and paced, as before. The 1.1 s spacing of the server applies to the public hosts only, not to an admin's own OSRM or Valhalla. The relay keeps its connections open and asks for maneuvers only, which the relay contract now accepts.
This commit is contained in:
@@ -5,13 +5,21 @@
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const built = vi.hoisted(() => ({ agents: [] as unknown[], proxies: [] as Array<Record<string, unknown>> }));
|
||||
const built = vi.hoisted(() => ({
|
||||
agents: [] as unknown[],
|
||||
proxies: [] as Array<Record<string, unknown>>,
|
||||
closed: [] as unknown[],
|
||||
}));
|
||||
|
||||
vi.mock('undici', () => ({
|
||||
Agent: class {
|
||||
constructor(opts: unknown) {
|
||||
constructor(readonly opts: unknown) {
|
||||
built.agents.push(opts);
|
||||
}
|
||||
close() {
|
||||
built.closed.push(this.opts);
|
||||
return Promise.resolve();
|
||||
}
|
||||
},
|
||||
ProxyAgent: class {
|
||||
constructor(opts: Record<string, unknown>) {
|
||||
@@ -33,6 +41,7 @@ afterEach(() => {
|
||||
for (const key of PROXY_KEYS) delete process.env[key];
|
||||
built.agents.length = 0;
|
||||
built.proxies.length = 0;
|
||||
built.closed.length = 0;
|
||||
});
|
||||
|
||||
describe('proxyFor', () => {
|
||||
@@ -87,3 +96,81 @@ describe('createOutboundDispatcher', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createOutboundDispatcher, connections kept (the routing relay)', () => {
|
||||
it('SSRF-KEEP-001: hands the same dispatcher back for the same vetted addresses, held open ten seconds', async () => {
|
||||
const { createOutboundDispatcher } = await loadGuard({});
|
||||
const first = createOutboundDispatcher(
|
||||
'https://valhalla.liketrek.com/route',
|
||||
['203.0.113.7'],
|
||||
true,
|
||||
undefined,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
const again = createOutboundDispatcher(
|
||||
'https://valhalla.liketrek.com/height',
|
||||
['203.0.113.7'],
|
||||
true,
|
||||
undefined,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
expect(again).toBe(first);
|
||||
expect(built.agents).toHaveLength(1);
|
||||
expect(built.agents[0]).toMatchObject({ keepAliveTimeout: 10_000 });
|
||||
});
|
||||
|
||||
it('SSRF-KEEP-002: a name that resolves elsewhere, or other timeouts, get a dispatcher of their own', async () => {
|
||||
const { createOutboundDispatcher } = await loadGuard({});
|
||||
const first = createOutboundDispatcher(
|
||||
'https://valhalla.liketrek.com/',
|
||||
['203.0.113.7'],
|
||||
true,
|
||||
undefined,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
const moved = createOutboundDispatcher(
|
||||
'https://valhalla.liketrek.com/',
|
||||
['203.0.113.8'],
|
||||
true,
|
||||
undefined,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
const slower = createOutboundDispatcher(
|
||||
'https://valhalla.liketrek.com/',
|
||||
['203.0.113.7'],
|
||||
true,
|
||||
60_000,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
expect(new Set([first, moved, slower]).size).toBe(3);
|
||||
// The new address is the one the socket gets, never the old one.
|
||||
const lookup = (
|
||||
built.agents[1] as { connect: { lookup: (h: string, o: object, cb: (...a: unknown[]) => void) => void } }
|
||||
).connect.lookup;
|
||||
const seen: unknown[] = [];
|
||||
lookup('valhalla.liketrek.com', {}, (...args: unknown[]) => seen.push(...args));
|
||||
expect(seen).toContain('203.0.113.8');
|
||||
expect(seen).not.toContain('203.0.113.7');
|
||||
});
|
||||
|
||||
it('SSRF-KEEP-003: keeps at most 32 and closes the oldest; a caller that does not ask keeps none', async () => {
|
||||
const { createOutboundDispatcher } = await loadGuard({});
|
||||
const at = (n: number) =>
|
||||
createOutboundDispatcher('https://h.example/', [`203.0.113.${n}`], true, undefined, undefined, true);
|
||||
const oldest = at(0);
|
||||
for (let n = 1; n <= 32; n++) at(n);
|
||||
expect(built.closed).toEqual([built.agents[0]]);
|
||||
expect(at(0)).not.toBe(oldest);
|
||||
|
||||
built.agents.length = 0;
|
||||
createOutboundDispatcher('https://h.example/', ['203.0.113.1']);
|
||||
createOutboundDispatcher('https://h.example/', ['203.0.113.1']);
|
||||
expect(built.agents).toHaveLength(2);
|
||||
expect(built.agents[0]).not.toHaveProperty('keepAliveTimeout');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user