// 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); }); });