Use new libsignal chat connection types

This commit is contained in:
Alex Konradi 2024-12-03 17:46:10 -05:00 committed by GitHub
parent 76c7e35da4
commit 4c75c6875a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,7 +34,6 @@ import net from 'net';
import { z } from 'zod'; import { z } from 'zod';
import { clearInterval } from 'timers'; import { clearInterval } from 'timers';
import { random } from 'lodash'; import { random } from 'lodash';
import type { ChatServiceDebugInfo } from '@signalapp/libsignal-client/Native';
import type { LibSignalError, Net } from '@signalapp/libsignal-client'; import type { LibSignalError, Net } from '@signalapp/libsignal-client';
import { Buffer } from 'node:buffer'; import { Buffer } from 'node:buffer';
@ -77,19 +76,6 @@ export enum IpVersion {
IPv6 = 'ipv6', IPv6 = 'ipv6',
} }
export namespace IpVersion {
export function fromDebugInfoCode(ipType: number): IpVersion | undefined {
switch (ipType) {
case 1:
return IpVersion.IPv4;
case 2:
return IpVersion.IPv6;
default:
return undefined;
}
}
}
const AggregatedStatsSchema = z.object({ const AggregatedStatsSchema = z.object({
connectionFailures: z.number(), connectionFailures: z.number(),
requestsCompared: z.number(), requestsCompared: z.number(),
@ -361,7 +347,10 @@ export function connectUnauthenticatedLibsignal({
}, },
}; };
return connectLibsignal( return connectLibsignal(
libsignalNet.newUnauthenticatedChatService(listener), abortSignal =>
libsignalNet.connectUnauthenticatedChat(listener, {
abortSignal,
}),
listener, listener,
logId, logId,
keepalive keepalive
@ -423,11 +412,13 @@ export function connectAuthenticatedLibsignal({
}, },
}; };
return connectLibsignal( return connectLibsignal(
libsignalNet.newAuthenticatedChatService( (abortSignal: AbortSignal) =>
libsignalNet.connectAuthenticatedChat(
credentials.username, credentials.username,
credentials.password, credentials.password,
receiveStories, receiveStories,
listener listener,
{ abortSignal }
), ),
listener, listener,
logId, logId,
@ -440,18 +431,25 @@ function logDisconnectedListenerWarn(logId: string, method: string): void {
} }
function connectLibsignal( function connectLibsignal(
chatService: Net.ChatService, makeConnection: (
abortSignal: AbortSignal
) => Promise<
Net.UnauthenticatedChatConnection | Net.AuthenticatedChatConnection
>,
resourceHolder: LibsignalWebSocketResourceHolder, resourceHolder: LibsignalWebSocketResourceHolder,
logId: string, logId: string,
keepalive: KeepAliveOptionsType keepalive: KeepAliveOptionsType
): AbortableProcess<LibsignalWebSocketResource> { ): AbortableProcess<LibsignalWebSocketResource> {
const abortController = new AbortController();
const connectAsync = async () => { const connectAsync = async () => {
try { try {
const debugInfo = await chatService.connect(); const service = await makeConnection(abortController.signal);
log.info(`${logId} connected`, debugInfo); log.info(`${logId} connected`);
const connectionInfo = service.connectionInfo();
const resource = new LibsignalWebSocketResource( const resource = new LibsignalWebSocketResource(
chatService, service,
IpVersion.fromDebugInfoCode(debugInfo.ipType), IpVersion[connectionInfo.ipVersion],
connectionInfo.localPort,
logId, logId,
keepalive keepalive
); );
@ -466,12 +464,7 @@ function connectLibsignal(
}; };
return new AbortableProcess<LibsignalWebSocketResource>( return new AbortableProcess<LibsignalWebSocketResource>(
`${logId}.connect`, `${logId}.connect`,
{ abortController,
abort() {
// if interrupted, trying to disconnect
drop(chatService.disconnect());
},
},
connectAsync() connectAsync()
); );
} }
@ -489,8 +482,9 @@ export class LibsignalWebSocketResource
private keepalive: KeepAliveSender; private keepalive: KeepAliveSender;
constructor( constructor(
private readonly chatService: Net.ChatService, private readonly chatService: Net.ChatConnection,
private readonly socketIpVersion: IpVersion | undefined, private readonly socketIpVersion: IpVersion,
private readonly localPortNumber: number,
private readonly logId: string, private readonly logId: string,
keepalive: KeepAliveOptionsType keepalive: KeepAliveOptionsType
) { ) {
@ -499,11 +493,11 @@ export class LibsignalWebSocketResource
this.keepalive = new KeepAliveSender(this, this.logId, keepalive); this.keepalive = new KeepAliveSender(this, this.logId, keepalive);
} }
public localPort(): number | undefined { public localPort(): number {
return undefined; return this.localPortNumber;
} }
public ipVersion(): IpVersion | undefined { public ipVersion(): IpVersion {
return this.socketIpVersion; return this.socketIpVersion;
} }
@ -561,28 +555,25 @@ export class LibsignalWebSocketResource
} }
public async sendRequest(options: SendRequestOptions): Promise<Response> { public async sendRequest(options: SendRequestOptions): Promise<Response> {
const [response] = await this.sendRequestGetDebugInfo(options); const response = await this.sendRequestGetDebugInfo(options);
return response; return response;
} }
public async sendRequestGetDebugInfo( public async sendRequestGetDebugInfo(
options: SendRequestOptions options: SendRequestOptions
): Promise<[Response, ChatServiceDebugInfo]> { ): Promise<Response> {
const { response, debugInfo } = await this.chatService.fetchAndDebug({ const response = await this.chatService.fetch({
verb: options.verb, verb: options.verb,
path: options.path, path: options.path,
headers: options.headers ? options.headers : [], headers: options.headers ? options.headers : [],
body: options.body, body: options.body,
timeoutMillis: options.timeout, timeoutMillis: options.timeout,
}); });
return [ return new Response(response.body, {
new Response(response.body, {
status: response.status, status: response.status,
statusText: response.message, statusText: response.message,
headers: [...response.headers], headers: [...response.headers],
}), });
debugInfo,
];
} }
} }