Files
BetterDesk/web-nodejs/tests/keyService.test.js
T
Knienartowicz 95f8ea6964 fix(signal): advertise rendezvous servers, never relay, in ConfigUpdate
handleTestNat() built ConfigUpdate.rendezvous_servers from
GetRelayServers(), so every TestNatResponse told the client that its *ID
server* was the relay endpoint. On this instance RENDEZVOUS_SERVERS is unset,
so the list contained exactly `85.14.70.12:21117` and was sent 113 times
today.

Newer RustDesk clients honour ConfigUpdate. Reconfiguring the signal socket to
the relay port makes the host stop answering RequestRelay altogether: the panel
connected as initiator, hbbr logged `Connection ... for UUID` and then
`Pair timeout` 30 s later, and the browser saw "Relay connection closed
(WebSocket 1005)". Peer 275129227 (RustDesk 1.4.9) showed exactly that
signature: TestNatRequest, heartbeats, and no relay connection ever.

- config: add GetRendezvousServers() which parses RENDEZVOUS_SERVERS and
  appends SignalPort, with the same validation as GetRelayServers().
- signal: handleTestNat() now advertises only configured rendezvous servers and
  omits ConfigUpdate entirely when none are configured.
- web: relay endpoints are always emitted as host:port (resolveRustDeskEndpoints,
  keyService, customTxtBuilder, agentBundleService, generator config). A bare
  relay host made clients fall back to the API port, which hbbr cannot pair.

Refs #329

Thanks: INSOLVE (Honorary); Marco Jakobs (@jacotec); MyNameisStitch (@MyNameisStitch); Redspin (@playerumpknow); muopc (@muopc)
2026-11-17 09:54:48 +01:00

140 lines
6.0 KiB
JavaScript

'use strict';
const keyService = require('../services/keyService');
describe('keyService RustDesk config encoding', () => {
const samplePayload = {
host: '203.0.113.10',
relay: '203.0.113.10',
api: 'http://203.0.113.10:21114',
key: 'AbCdEfGhIjKlMnOpQrStUvWxYz0123456789+/=',
};
it('encodeRustDeskCliConfigString reverses base64 without padding', () => {
const json = JSON.stringify(samplePayload);
let b64 = Buffer.from(json).toString('base64').replace(/=+$/, '');
const expected = b64.split('').reverse().join('');
expect(keyService.encodeRustDeskCliConfigString(samplePayload)).toBe(expected);
});
it('encodeRustDeskConfigUri path matches deploy string (#368)', () => {
const uri = keyService.encodeRustDeskConfigUri(samplePayload);
const deploy = keyService.encodeRustDeskCliConfigString(samplePayload);
expect(uri).toBe(`rustdesk://config/${deploy}`);
});
it('encodeRustDeskConfigUri round-trips like RustDesk ServerConfig.decode (#368)', () => {
const uri = keyService.encodeRustDeskConfigUri(samplePayload);
const path = uri.slice('rustdesk://config/'.length);
// RustDesk: reverse path → normalize base64 → decode → JSON
const reversed = path.split('').reverse().join('');
const decoded = JSON.parse(Buffer.from(reversed, 'base64').toString('utf8'));
expect(decoded).toEqual(samplePayload);
});
it('buildRustDeskConfigPayload normalizes host input', () => {
const payload = keyService.buildRustDeskConfigPayload('https://desk.example.com:8443/path', {
publicKey: '',
});
expect(payload.host).toBe('desk.example.com');
// Relay always carries an explicit port — a bare host makes RustDesk
// 1.4.8/1.4.9 request relay on the API port and never pair.
expect(payload.relay).toBe('desk.example.com:21117');
});
it('buildRustDeskConfigPayload accepts split endpoints object', () => {
const payload = keyService.buildRustDeskConfigPayload({
host: 'remote.example.com',
relay: 'relay.example.com',
api: 'https://api.example.com',
}, { publicKey: '' });
expect(payload.host).toBe('remote.example.com');
expect(payload.relay).toBe('relay.example.com:21117');
expect(payload.api).toBe('https://api.example.com');
});
it('buildRustDeskConfigPayload keeps an explicit relay port', () => {
const payload = keyService.buildRustDeskConfigPayload({
host: 'remote.example.com',
relay: 'relay.example.com:21119',
}, { publicKey: '' });
expect(payload.relay).toBe('relay.example.com:21119');
});
});
describe('keyService public key validation (#340)', () => {
// 32 zero bytes → canonical base64 (44 chars with padding)
const validKey = Buffer.alloc(32, 0).toString('base64');
afterEach(() => {
keyService._resetGoKeyCacheForTests();
jest.restoreAllMocks();
});
it('isValidRustDeskPublicKey accepts canonical 32-byte base64', () => {
expect(keyService.isValidRustDeskPublicKey(validKey)).toBe(true);
});
it('isValidRustDeskPublicKey rejects placeholders and junk', () => {
expect(keyService.isValidRustDeskPublicKey('')).toBe(false);
expect(keyService.isValidRustDeskPublicKey('v1.4.9_public_key_placeholder...')).toBe(false);
expect(keyService.isValidRustDeskPublicKey('YOUR_PUBLIC_KEY')).toBe(false);
expect(keyService.isValidRustDeskPublicKey('__PUB_KEY_PATH__')).toBe(false);
expect(keyService.isValidRustDeskPublicKey('not-base64!!!')).toBe(false);
expect(keyService.isValidRustDeskPublicKey(Buffer.alloc(16).toString('base64'))).toBe(false);
});
it('getClientConfig embeds valid public key in deploy string', async () => {
const payload = keyService.buildRustDeskConfigPayload('desk.example.com', { publicKey: validKey });
const deploy = keyService.encodeRustDeskCliConfigString(payload);
const json = Buffer.from(deploy.split('').reverse().join(''), 'base64').toString('utf8');
const decoded = JSON.parse(json);
expect(decoded.key).toBe(validKey);
expect(decoded.host).toBe('desk.example.com');
expect(payload.key).toBe(validKey);
});
it('buildRustDeskConfigPayload drops invalid publicKey override', () => {
const payload = keyService.buildRustDeskConfigPayload('desk.example.com', {
publicKey: 'v1.4.9_public_key_placeholder...',
});
expect(payload.key).toBe('');
});
it('getClientConfig with invalid file key and Go fallback fills deploy string', async () => {
jest.spyOn(keyService, 'getPublicKey').mockReturnValue(null);
const betterdeskApi = require('../services/betterdeskApi');
jest.spyOn(betterdeskApi.apiClient, 'get').mockResolvedValue({ data: { key: validKey } });
const config = await keyService.getClientConfig({
host: '203.0.113.10',
relay: '203.0.113.10',
api: 'http://203.0.113.10:21114',
});
expect(config.has_public_key).toBe(true);
expect(config.public_key).toBe(validKey);
expect(config.deploy_config_string).toBeTruthy();
const json = Buffer.from(
config.deploy_config_string.split('').reverse().join(''),
'base64'
).toString('utf8');
expect(JSON.parse(json).key).toBe(validKey);
});
it('getClientConfig leaves deploy string empty when no valid key exists', async () => {
jest.spyOn(keyService, 'getPublicKey').mockReturnValue(null);
const betterdeskApi = require('../services/betterdeskApi');
jest.spyOn(betterdeskApi.apiClient, 'get').mockResolvedValue({
data: { key: 'v1.4.9_public_key_placeholder...' },
});
const config = await keyService.getClientConfig('desk.example.com');
expect(config.has_public_key).toBe(false);
expect(config.public_key).toBe('');
expect(config.deploy_config_string).toBe('');
});
});