// dawnline.awg: disguise presets against their own validation, the parser // of `awg show all dump` on the output formats of AmneziaWG 1.0, 2.0 and 3.1 // (the stand-in awg in fixtures/bin prints the fixture put at AWG_DUMP), // Reconnect, and a device's configuration from the fixture model. import { readfile, writefile, unlink } from 'fs'; import * as awg from 'dawnline.awg'; import * as core from 'dawnline.core'; const AWG_DUMP = '/tmp/dawnline-tests/awg-dump'; let fixtures = sourcepath(0, true) + '/fixtures'; test('obf_preset off', () => { let o = awg.obf_preset('off'); eq(o, { jc: '0', jmin: '0', jmax: '0', s1: '0', s2: '0', h1: '1', h2: '2', h3: '3', h4: '4' }, 'plain WireGuard headers'); eq(awg.obf_errors(o), [], 'valid'); }); test('obf_preset balanced', () => { for (let i = 0; i < 25; i++) { let o = awg.obf_preset('balanced'); eq(awg.obf_errors(o), [], 'balanced is valid'); ok(+o.jc >= 4 && +o.jc <= 8, `jc=${o.jc}`); let jmin = +o.jmin, jmax = +o.jmax; ok(jmin >= 64 && jmax > jmin && jmax <= 1024, `junk sizes ${o.jmin}-${o.jmax}`); ok(+o.s1 + 56 != +o.s2, 's1 + 56 != s2'); eq(length(uniq([ o.h1, o.h2, o.h3, o.h4 ])), 4, 'distinct headers'); for (let k in [ 'h1', 'h2', 'h3', 'h4' ]) ok(+o[k] >= 5, `${k} above the reserved values`); ok(+o.s3 >= 8 && +o.s3 <= 48 && +o.s4 >= 4 && +o.s4 <= 16, `padding ${o.s3}/${o.s4}`); ok(substr(o.i1, 0, 3) == ' { let o = awg.obf_preset('max'); eq(o.header_protection_key, 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=', 'key from awg genkey'); ok(+o.jc >= 4 && +o.jc <= 6, 'jc'); ok(substr(o.i1, 0, 3) == ' 1, 'signature packets differ between servers'); // The preset passes the server's own validation (jmin/jmax may be small: 0.5.2). eq(awg.obf_errors(o), [], 'max is valid'); }); test('obf_errors', () => { eq(awg.obf_errors({}), [], 'nothing set'); eq(awg.obf_errors({ jc: '11' }), [ 'jc=11 (0-10)' ], 'jc range'); eq(awg.obf_errors({ jmin: '0', jmax: '0' }), [], 'zero junk sizes are allowed'); eq(awg.obf_errors({ jmin: '2000' }), [ 'jmin=2000 (0-1024)' ], 'jmin range'); eq(awg.obf_errors({ jmin: '200', jmax: '100' }), [ 'jmin > jmax' ], 'order'); eq(awg.obf_errors({ s1: '5', s2: '61' }), [ 's1 + 56 = s2' ], 'the handshake size clash'); eq(awg.obf_errors({ s4: '33' }), [ 's4=33 (0-32)' ], 's4 range'); eq(awg.obf_errors({ jc: 'x' }), [ 'jc=x (0-10)' ], 'not a number'); eq(awg.obf_errors({ h1: '7', h2: '7' }), [ 'h1-h4 must be distinct' ], 'duplicate headers'); eq(awg.obf_errors({ h1: '7-9', h2: 'abc' }), [ 'h2 must be a number or range' ], 'header ranges'); eq(awg.obf_neutral({ jc: '0', jmin: '0', jmax: '0', s1: '0', s2: '0', h1: '1', h2: '2', h3: '3', h4: '4' }), true, 'off is neutral'); eq(awg.obf_neutral({ jc: '4', jmin: '64', jmax: '128' }), false, 'junk is not neutral'); }); function dump_of(name) { writefile(AWG_DUMP, readfile(`${fixtures}/awg-dump-${name}.txt`)); return awg.dump(); } test('dump: AmneziaWG 1.0 (14 interface fields)', () => { let d = dump_of('1.0'); eq(d.ok, true, 'ok'); eq(d.ifaces, { awgmain: { public_key: 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=', port: 51820 } }, 'interface'); eq(d.peers['HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw='], { ifname: 'awgmain', endpoint: '203.0.113.7:41821', handshake: 1760000000, rx: 123456, tx: 654321 }, 'peer with a session'); eq(d.peers['TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0='], { ifname: 'awgmain', endpoint: '', handshake: 0, rx: 0, tx: 0 }, 'peer that never connected'); }); test('dump: AmneziaWG 2.0 (33 interface fields)', () => { let d = dump_of('2.0'); eq(d.ifaces, { awgmain: { public_key: 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=', port: 51820 } }, 'interface'); eq(keys(d.peers), [ 'HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=' ], 'one peer'); }); test('dump: AmneziaWG 3.1 (33 interface fields, protection on)', () => { let d = dump_of('3.1'); eq(d.ifaces, { awgmain: { public_key: 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=', port: 51820 } }, 'interface'); eq(length(d.peers), 2, 'two peers'); }); test('dump: the tool fails', () => { unlink(AWG_DUMP); let d = awg.dump(); eq(d, { ok: false, ifaces: {}, peers: {} }, 'nothing known'); }); test('handshake_since: Reconnect hides older handshakes', () => { eq(awg.handshake_since(1000, null), 1000, 'never set up again'); eq(awg.handshake_since(1000, 0), 1000, 'no time'); eq(awg.handshake_since(1000, 1500), 0, 'connected before: not set up yet'); eq(awg.handshake_since(1500, 1500), 0, 'the same second does not count'); eq(awg.handshake_since(1600, 1500), 1600, 'connected after'); eq(awg.handshake_since(0, 1500), 0, 'never connected'); }); test('blocked reasons', () => { let srv = (b) => ({ name: 'Home', blocked: b }); eq(awg.blocked_text(srv(null)), null, 'not kept down'); eq(awg.blocked_text(srv({ code: 'ifname', ifname: 'bad name' })), 'Home has an invalid interface name "bad name".', 'interface name'); eq(awg.blocked_text(srv({ code: 'taken', ifname: 'wg0' })), "Home can't use the network interface wg0: it exists and isn't Dawnline's.", 'foreign interface'); eq(awg.blocked_text(srv({ code: 'disguise', need: '3.1', installed: '2.0' })), 'Home is kept down: its disguise needs AmneziaWG 3.1 (installed: 2.0).', 'disguise'); eq(awg.blocked_text(srv({ code: 'disguise', need: '3.1' })), 'Home is kept down: its disguise needs AmneziaWG 3.1 (installed: none).', 'disguise, none installed'); // Files of older versions hold the English fragment after the name. eq(awg.blocked_text(srv('has an invalid interface name "x"')), 'Home has an invalid interface name "x".', 'old fragment'); eq(awg.blocked_note({ code: 'ifname', ifname: 'x' }), 'has an invalid interface name "x"', 'the log keeps the fragment'); }); test('client_config: an IPv6 endpoint in brackets, without a regex', () => { let m = core.load(); let srv = m.servers.main, dev = m.devices.alice_phone; let ep = (h) => { srv.endpoint = h; return match(awg.client_config(m, dev).conf, /\nEndpoint = ([^\n]*)\n/)?.[1]; }; eq(ep('vpn.example.com'), 'vpn.example.com:51820', 'a name'); eq(ep('203.0.113.7'), '203.0.113.7:51820', 'IPv4'); eq(ep('2001:db8::1'), '[2001:db8::1]:51820', 'IPv6'); eq(ep('[2001:db8::1]'), '[2001:db8::1]:51820', 'IPv6 in brackets already'); eq(ep(''), 'vpn.example.com:51820', 'the portal name when the server has none'); // The regexes it stands for, on hosts changed at each character. for (let h in [ '2001:db8::1', '[::1]', 'a.b' ]) for (let i = 0; i <= length(h); i++) for (let c in [ ':', '[', ']', 'a', '' ]) { let s = `${substr(h, 0, i)}${c}${substr(h, i + 1)}`; let want = match(s, /:/) && !match(s, /^\[/) ? `[${s}]` : s; eq(ep(s), `${want || 'vpn.example.com'}:51820`, s); } }); test('config_fingerprint leaves out the private key and the junk', () => { let m = core.load(); let dev = m.devices.alice_phone; let f = awg.config_fingerprint(m, dev); ok(match(f, /^[0-9a-f]{8}$/), 'eight hex digits'); dev.private_key = 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='; eq(awg.config_fingerprint(m, dev), f, 'a private key'); m.servers.main.obf.jc = '7'; eq(awg.config_fingerprint(m, dev), f, 'another junk count'); m.servers.main.port = 51821; ok(awg.config_fingerprint(m, dev) != f, 'another port'); eq(awg.config_fingerprint(m, null), null, 'no device'); });