Hub 2.20.0: port list labels; frps allow_ports checked against it
- GET /api/admin/ports names each port's install (church, product, web/API, domain); the port list shows it, linked to the install, and says which ports are held for a port check. - A church server's hand-written frpc tunnels are listed as taken (note: frpc tunnel "<name>" on <server>) on each frp-read/frp-write, and taken off when they leave the file. - Central servers: allow_ports must cover the port range and every listed port outside it. An allow_ports alert names what's missing and the value to set; the frp card warns, and Edit has "Use the Hub's allow_ports" (INI or TOML). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// Hub 2.20.0: frps's allow_ports against the port list, and frpc's hand-written tunnels.
|
||||
const f = require('../src/utils/frpPorts');
|
||||
|
||||
describe('ranges', () => {
|
||||
test('parses, merges and formats', () => {
|
||||
expect(f.parseRanges('3000, 20000-29999,3001,bad,5-4')).toEqual([[3000, 3001], [20000, 29999]]);
|
||||
expect(f.formatRanges([[5, 5], [1, 3], [4, 4], [10, 12]])).toBe('1-5,10-12');
|
||||
});
|
||||
|
||||
test('needed: the range and listed ports outside it (not freed ones)', () => {
|
||||
const rows = [{ port: 3000 }, { port: 20005 }, { port: 8182 }, { port: 5000, releasedAt: new Date() }];
|
||||
expect(f.formatRanges(f.neededRanges(rows, { from: 20000, to: 29999 }))).toBe('3000,8182,20000-29999');
|
||||
});
|
||||
|
||||
test('missing: what allow_ports leaves out; none when every port is allowed', () => {
|
||||
const needed = f.parseRanges('3000,8182,20000-29999');
|
||||
expect(f.formatRanges(f.missingRanges('3000,20000-20007', needed))).toBe('8182,20008-29999');
|
||||
expect(f.formatRanges(f.missingRanges('1-65535', needed))).toBe('');
|
||||
expect(f.missingRanges(null, needed)).toEqual([]);
|
||||
expect(f.missingRanges(' ', needed)).toEqual([]);
|
||||
expect(f.formatRanges(f.missingRanges('20100-20200', f.parseRanges('20000-20300')))).toBe('20000-20099,20201-20300');
|
||||
});
|
||||
|
||||
test('suggested keeps what is allowed now and adds what is missing', () => {
|
||||
expect(f.suggestedAllowPorts('3000,7000,20000-20007', f.parseRanges('3000,8182,20000-29999'))).toBe('3000,7000,8182,20000-29999');
|
||||
});
|
||||
});
|
||||
|
||||
describe('withAllowPorts', () => {
|
||||
test('INI: replaces it in [common], or adds it there', () => {
|
||||
const ini = '[common]\r\nbind_port = 7000\r\nallow_ports = 3000\r\n\r\n[x]\r\nallow_ports = 1\r\n';
|
||||
expect(f.withAllowPorts(ini, 'ini', '3000,20000-29999')).toBe('[common]\r\nbind_port = 7000\r\nallow_ports = 3000,20000-29999\r\n\r\n[x]\r\nallow_ports = 1\r\n');
|
||||
expect(f.withAllowPorts('[common]\nbind_port = 7000\n', 'ini', '1-2')).toBe('[common]\nallow_ports = 1-2\nbind_port = 7000\n');
|
||||
});
|
||||
|
||||
test('TOML: replaces a one-line or several-line allowPorts, or adds it before the first table', () => {
|
||||
expect(f.withAllowPorts('bindPort = 7000\nallowPorts = [{ single = 3000 }]\n', 'toml', '3000,20000-29999'))
|
||||
.toBe('bindPort = 7000\nallowPorts = [{ single = 3000 }, { start = 20000, end = 29999 }]\n');
|
||||
expect(f.withAllowPorts('bindPort = 7000\nallowPorts = [\n { single = 3000 },\n { start = 1, end = 2 }\n]\nproxyBindAddr = "127.0.0.1"\n', 'toml', '5'))
|
||||
.toBe('bindPort = 7000\nallowPorts = [{ single = 5 }]\nproxyBindAddr = "127.0.0.1"\n');
|
||||
expect(f.withAllowPorts('bindPort = 7000\n[auth]\ntoken = "x"\n', 'toml', '5'))
|
||||
.toBe('bindPort = 7000\nallowPorts = [{ single = 5 }]\n[auth]\ntoken = "x"\n');
|
||||
});
|
||||
});
|
||||
|
||||
describe('frpcTunnels', () => {
|
||||
test('INI: each section but [common], by its remote_port', () => {
|
||||
const ini = '[common]\nserver_port = 7000\nadmin_port = 7400\n\n[events-frontend]\ntype = tcp\nlocal_port = 3000\nremote_port = 3000\n\n# >>> crosscode:hope:events\n[hope-events-web]\nremote_port = 20000\n[no-port]\ntype = tcp\n';
|
||||
expect(f.frpcTunnels(ini, 'ini')).toEqual([{ name: 'events-frontend', port: 3000 }, { name: 'hope-events-web', port: 20000 }]);
|
||||
});
|
||||
|
||||
test('TOML: [[proxies]] by name and remotePort', () => {
|
||||
const toml = 'serverPort = 7000\n[auth]\ntoken = "x"\n\n[[proxies]]\nname = "events-frontend"\ntype = "tcp"\nlocalPort = 3000\nremotePort = 3000 # web\n\n[[visitors]]\nname = "v"\nremotePort = 9\n';
|
||||
expect(f.frpcTunnels(toml, 'toml')).toEqual([{ name: 'events-frontend', port: 3000 }]);
|
||||
});
|
||||
|
||||
test('tunnel notes', () => {
|
||||
expect(f.tunnelNote('games-night', 'church-1')).toBe('frpc tunnel "games-night" on church-1');
|
||||
expect(f.isTunnelNote(f.tunnelNote('a', 'b'))).toBe(true);
|
||||
expect(f.isTunnelNote('by hand')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1115,3 +1115,53 @@ describe('a second product while another is being uninstalled (2.12.0)', () => {
|
||||
expect(mockMails.some(m => m.subject.includes('Uninstalled: hope') && m.text.includes('folder and app user'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('frps allow_ports and port labels (Hub 2.20.0)', () => {
|
||||
const frpcIni = [
|
||||
'[common]', 'server_port = 7000', 'token = ••••••', '',
|
||||
'[events-frontend]', 'type = tcp', 'local_port = 3000', 'remote_port = 3000', '',
|
||||
'[games-night]', 'type = tcp', 'local_port = 3201', 'remote_port = 3201', '',
|
||||
'# >>> crosscode:hope:events', '[hope-events-web]', 'type = tcp', 'local_port = 20000', 'remote_port = 20000', '# <<< crosscode:hope:events', '',
|
||||
].join('\n');
|
||||
const read = content => ({ service: 'frpc', path: '/etc/frp/frpc.ini', source: 'systemd', found: true, format: 'ini', content, hidden: ['token'], proxyBindAddr: null, allowPorts: null, serviceRunning: true, readAt: NOW.toISOString() });
|
||||
|
||||
test("a church server's hand-written tunnels are listed as taken, and taken off when they go", async () => {
|
||||
const { server } = await joinServer();
|
||||
await db.port.create({ data: { port: 20000, serverId: server.id, role: 'web', deploymentId: 'dep-1' } });
|
||||
await db.port.create({ data: { port: 3201, role: 'taken', note: 'mine' } });
|
||||
await jobs.listFrpcTunnels(server.id, read(frpcIni), NOW);
|
||||
const rows = Object.fromEntries((await db.port.findMany({})).map(r => [r.port, r]));
|
||||
expect(rows[3000]).toMatchObject({ role: 'taken', serverId: server.id, note: 'frpc tunnel "events-frontend" on church-1' });
|
||||
expect(rows[3201].note).toBe('mine'); // marked by hand: left as it is
|
||||
expect(rows[20000]).toMatchObject({ role: 'web', deploymentId: 'dep-1' }); // an install's
|
||||
|
||||
await jobs.listFrpcTunnels(server.id, read(frpcIni.replace('remote_port = 3000', 'remote_port = 3005')), NOW);
|
||||
const after = (await db.port.findMany({})).map(r => r.port).sort((a, b) => a - b);
|
||||
expect(after).toEqual([3005, 3201, 20000]);
|
||||
|
||||
// A central server's frps config lists nothing.
|
||||
const central = (await joinServer('central', 'central-1')).server;
|
||||
await jobs.listFrpcTunnels(central.id, read('[common]\n[x]\nremote_port = 4444\n'), NOW);
|
||||
expect(await db.port.findUnique({ where: { port: 4444 } })).toBeNull();
|
||||
});
|
||||
|
||||
test('the central server is warned when allow_ports leaves out the range or a listed port', async () => {
|
||||
const central = (await joinServer('central', 'central-1')).server;
|
||||
await db.port.create({ data: { port: 3000, role: 'taken', note: 'frpc tunnel "events-frontend" on church-1' } });
|
||||
await db.port.create({ data: { port: 5000, role: 'taken', releasedAt: NOW } });
|
||||
const check = await servers.allowPortsCheck('3000,20000-20007');
|
||||
expect(check).toMatchObject({ needed: '3000,20000-29999', missing: '20008-29999', suggested: '3000,20000-29999' });
|
||||
expect((await servers.allowPortsCheck('3000,20000-29999')).missing).toBe('');
|
||||
expect((await servers.allowPortsCheck(null)).missing).toBe('');
|
||||
|
||||
await db.server.update({ where: { id: central.id }, data: { lastSeenAt: NOW, lastStats: { disks: [], frpConfigPath: '/etc/frp/frps.ini', central: { proxyBindAddr: '127.0.0.1', allowPorts: '20000-29999', frpsRunning: true } } } });
|
||||
await servers.runServerAlerts(NOW);
|
||||
const alert = await db.serverAlert.findFirst({ where: { serverId: central.id, type: 'allow_ports' } });
|
||||
expect(alert.message).toContain("doesn't allow 3000");
|
||||
expect(alert.message).toContain('Set it to 3000,20000-29999');
|
||||
|
||||
await db.server.update({ where: { id: central.id }, data: { lastStats: { disks: [], frpConfigPath: '/etc/frp/frps.ini', central: { proxyBindAddr: '127.0.0.1', allowPorts: '3000,20000-29999', frpsRunning: true } } } });
|
||||
await servers.runServerAlerts(NOW);
|
||||
expect((await db.serverAlert.findFirst({ where: { serverId: central.id, type: 'allow_ports' } })).resolvedAt).toEqual(NOW);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user