diff --git a/src/clientRPC/ClientClient.ts b/src/clientRPC/ClientClient.ts index d6ffc2f9d1..10fda4ec62 100644 --- a/src/clientRPC/ClientClient.ts +++ b/src/clientRPC/ClientClient.ts @@ -7,6 +7,7 @@ import Logger from '@matrixai/logger'; import WebSocket from 'ws'; import { PromiseCancellable } from '@matrixai/async-cancellable'; import { Timer } from '@matrixai/timer'; +import { Validator } from 'ip-num'; import * as clientRpcUtils from './utils'; import { promise } from '../utils'; @@ -49,18 +50,28 @@ class ClientClient { return clientClient; } + protected host: string; + protected activeConnections: Set> = new Set(); constructor( protected logger: Logger, - protected host: string, + host: string, protected port: number, protected maxReadableStreamBytes: number, protected expectedNodeIds: Array, protected connectionTimeout: number | undefined, protected pingInterval: number, protected pingTimeout: number, - ) {} + ) { + if (Validator.isValidIPv4String(host)[0]) { + this.host = host; + } else if (Validator.isValidIPv6String(host)[0]) { + this.host = `[${host}]`; + } else { + throw Error('TMP Invalid host'); + } + } public async destroy(force: boolean = false) { this.logger.info(`Destroying ${this.constructor.name}`); diff --git a/tests/clientRPC/clientRPC.test.ts b/tests/clientRPC/clientRPC.test.ts index a16bb72f82..3fb847234b 100644 --- a/tests/clientRPC/clientRPC.test.ts +++ b/tests/clientRPC/clientRPC.test.ts @@ -121,6 +121,41 @@ describe('ClientRPC', () => { expect((await reader.read()).done).toBeTrue(); logger.info('ending'); }); + test('makes a connection over IPv6', async () => { + clientServer = await ClientServer.createClientServer({ + connectionCallback: (streamPair) => { + logger.info('inside callback'); + void streamPair.readable + .pipeTo(streamPair.writable) + .catch(() => {}) + .finally(() => loudLogger.info('STREAM HANDLING ENDED')); + }, + basePath: dataDir, + tlsConfig, + host: '::1', + logger: loudLogger.getChild('server'), + }); + logger.info(`Server started on port ${clientServer.port}`); + clientClient = await ClientClient.createClientClient({ + host: '::1', + port: clientServer.port, + expectedNodeIds: [keyRing.getNodeId()], + logger: logger.getChild('clientClient'), + }); + const websocket = await clientClient.startConnection(); + + const writer = websocket.writable.getWriter(); + const reader = websocket.readable.getReader(); + const message1 = Buffer.from('1request1'); + await writer.write(message1); + expect((await reader.read()).value).toStrictEqual(message1); + const message2 = Buffer.from('1request2'); + await writer.write(message2); + expect((await reader.read()).value).toStrictEqual(message2); + await writer.close(); + expect((await reader.read()).done).toBeTrue(); + logger.info('ending'); + }); test('Handles a connection and closes before message', async () => { clientServer = await ClientServer.createClientServer({ connectionCallback: (streamPair) => {