fix: use more withs for onion.ts file

pull/3281/head
Audric Ackermann 4 months ago
parent a8372f1b8c
commit 3e34b0f77f
No known key found for this signature in database

@ -212,7 +212,7 @@ export function useIsKickedFromGroup(convoId?: string) {
export function useIsGroupDestroyed(convoId?: string) { export function useIsGroupDestroyed(convoId?: string) {
const libIsDestroyed = useLibGroupDestroyed(convoId); const libIsDestroyed = useLibGroupDestroyed(convoId);
if (convoId && PubKey.is03Pubkey(convoId)) { if (convoId && PubKey.is03Pubkey(convoId)) {
return libIsDestroyed || false; return !!libIsDestroyed;
} }
return false; return false;
} }

@ -2,7 +2,6 @@ import { isArray } from 'lodash';
import { AbortController } from 'abort-controller'; import { AbortController } from 'abort-controller';
import { MessageSender } from '../../sending'; import { MessageSender } from '../../sending';
import { Snode } from '../../../data/types';
import { SnodeResponseError } from '../../utils/errors'; import { SnodeResponseError } from '../../utils/errors';
import { processOnionRequestErrorAtDestination, SnodeResponse } from './onions'; import { processOnionRequestErrorAtDestination, SnodeResponse } from './onions';
import { SessionRpc } from './sessionRpc'; import { SessionRpc } from './sessionRpc';
@ -15,15 +14,12 @@ import {
} from './SnodeRequestTypes'; } from './SnodeRequestTypes';
import { NotEmptyArrayOfBatchResults } from './BatchResultEntry'; import { NotEmptyArrayOfBatchResults } from './BatchResultEntry';
import { MergedAbortSignal, WithTimeoutMs } from './requestWith'; import { MergedAbortSignal, WithTimeoutMs } from './requestWith';
import { WithAllow401s } from '../../types/with'; import { WithAllow401s, WithAssociatedWith, WithTargetNode } from '../../types/with';
function logSubRequests(requests: Array<BuiltSnodeSubRequests>) { function logSubRequests(requests: Array<BuiltSnodeSubRequests>) {
return `[${requests.map(builtRequestToLoggingId).join(', ')}]`; return `[${requests.map(builtRequestToLoggingId).join(', ')}]`;
} }
type WithTargetNode = { targetNode: Snode };
type WithAssociatedWith = { associatedWith: string | null };
/** /**
* This is the equivalent to the batch send on sogs. The target node runs each sub request and returns a list of all the sub status and bodies. * This is the equivalent to the batch send on sogs. The target node runs each sub request and returns a list of all the sub status and bodies.
* If the global status code is not 200, an exception is thrown. * If the global status code is not 200, an exception is thrown.

@ -21,7 +21,13 @@ import { fileServerHost } from '../file_server_api/FileServerApi';
import { hrefPnServerProd } from '../push_notification_api/PnServer'; import { hrefPnServerProd } from '../push_notification_api/PnServer';
import { ERROR_CODE_NO_CONNECT } from './SNodeAPI'; import { ERROR_CODE_NO_CONNECT } from './SNodeAPI';
import { MergedAbortSignal, WithAbortSignal, WithTimeoutMs } from './requestWith'; import { MergedAbortSignal, WithAbortSignal, WithTimeoutMs } from './requestWith';
import { WithAllow401s } from '../../types/with'; import {
WithAllow401s,
WithAssociatedWith,
WithDestinationEd25519,
WithGuardNode,
WithSymmetricKey,
} from '../../types/with';
// hold the ed25519 key of a snode against the time it fails. Used to remove a snode only after a few failures (snodeFailureThreshold failures) // hold the ed25519 key of a snode against the time it fails. Used to remove a snode only after a few failures (snodeFailureThreshold failures)
let snodeFailureCount: Record<string, number> = {}; let snodeFailureCount: Record<string, number> = {};
@ -311,11 +317,10 @@ export async function processOnionRequestErrorAtDestination({
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith, associatedWith,
allow401s, allow401s,
}: WithAllow401s & { }: WithAllow401s &
Partial<WithDestinationEd25519 & WithAssociatedWith> & {
statusCode: number; statusCode: number;
body: string; body: string;
destinationSnodeEd25519?: string;
associatedWith?: string;
}) { }) {
if (statusCode === 200) { if (statusCode === 200) {
return; return;
@ -328,13 +333,13 @@ export async function processOnionRequestErrorAtDestination({
process401Error(statusCode); process401Error(statusCode);
} }
processOxenServerError(statusCode, body); processOxenServerError(statusCode, body);
await process421Error(statusCode, body, associatedWith, destinationSnodeEd25519); await process421Error(statusCode, body, associatedWith || undefined, destinationSnodeEd25519);
if (destinationSnodeEd25519) { if (destinationSnodeEd25519) {
await processAnyOtherErrorAtDestination( await processAnyOtherErrorAtDestination(
statusCode, statusCode,
body, body,
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith associatedWith || undefined
); );
} }
} }
@ -342,9 +347,8 @@ export async function processOnionRequestErrorAtDestination({
async function handleNodeNotFound({ async function handleNodeNotFound({
ed25519NotFound, ed25519NotFound,
associatedWith, associatedWith,
}: { }: Partial<WithAssociatedWith> & {
ed25519NotFound: string; ed25519NotFound: string;
associatedWith?: string;
}) { }) {
const shortNodeNotFound = ed25519Str(ed25519NotFound); const shortNodeNotFound = ed25519Str(ed25519NotFound);
window?.log?.warn('Handling NODE NOT FOUND with: ', shortNodeNotFound); window?.log?.warn('Handling NODE NOT FOUND with: ', shortNodeNotFound);
@ -526,13 +530,10 @@ async function processOnionResponse({
associatedWith, associatedWith,
destinationSnodeEd25519, destinationSnodeEd25519,
allow401s, allow401s,
}: Partial<WithAbortSignal> & }: Partial<WithAbortSignal & WithDestinationEd25519 & WithAssociatedWith & WithSymmetricKey> &
WithAllow401s & { WithAllow401s &
WithGuardNode & {
response?: { text: () => Promise<string>; status: number }; response?: { text: () => Promise<string>; status: number };
symmetricKey?: ArrayBuffer;
guardNode: Snode;
destinationSnodeEd25519?: string;
associatedWith?: string;
}): Promise<SnodeResponse> { }): Promise<SnodeResponse> {
let ciphertext = ''; let ciphertext = '';
@ -549,7 +550,7 @@ async function processOnionResponse({
ciphertext, ciphertext,
guardNode.pubkey_ed25519, guardNode.pubkey_ed25519,
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith associatedWith || undefined
); );
if (!ciphertext) { if (!ciphertext) {
@ -598,7 +599,7 @@ async function processOnionResponse({
} }
return value; return value;
}) as Record<string, any>; }) as Record<string, any>;
// TODO: type those status
const status = jsonRes.status_code || jsonRes.status; const status = jsonRes.status_code || jsonRes.status;
await processOnionRequestErrorAtDestination({ await processOnionRequestErrorAtDestination({
@ -642,12 +643,9 @@ async function processOnionResponseV4({
guardNode, guardNode,
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith, associatedWith,
}: Partial<WithAbortSignal> & { }: Partial<WithAbortSignal & WithDestinationEd25519 & WithAssociatedWith & WithSymmetricKey> &
WithGuardNode & {
response?: Response; response?: Response;
symmetricKey?: ArrayBuffer;
guardNode: Snode;
destinationSnodeEd25519?: string;
associatedWith?: string;
}): Promise<SnodeResponseV4 | undefined> { }): Promise<SnodeResponseV4 | undefined> {
processAbortedRequest(abortSignal); processAbortedRequest(abortSignal);
const validSymmetricKey = await processNoSymmetricKeyError(guardNode, symmetricKey); const validSymmetricKey = await processNoSymmetricKeyError(guardNode, symmetricKey);
@ -669,7 +667,7 @@ async function processOnionResponseV4({
cipherText, cipherText,
guardNode.pubkey_ed25519, guardNode.pubkey_ed25519,
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith associatedWith || undefined
); );
const plaintextBuffer = await callUtilsWorker( const plaintextBuffer = await callUtilsWorker(
@ -705,9 +703,8 @@ export type FinalRelayOptions = {
port?: number; // default to 443 port?: number; // default to 443
}; };
export type DestinationContext = { export type DestinationContext = WithSymmetricKey & {
ciphertext: Uint8Array; ciphertext: Uint8Array;
symmetricKey: ArrayBuffer;
ephemeralKey: ArrayBuffer; ephemeralKey: ArrayBuffer;
}; };
@ -721,10 +718,8 @@ async function handle421InvalidSwarm({
body, body,
destinationSnodeEd25519, destinationSnodeEd25519,
associatedWith, associatedWith,
}: { }: Partial<WithDestinationEd25519 & WithAssociatedWith> & {
body: string; body: string;
destinationSnodeEd25519?: string;
associatedWith?: string;
}) { }) {
if (!destinationSnodeEd25519 || !associatedWith) { if (!destinationSnodeEd25519 || !associatedWith) {
// The snode isn't associated with the given public key anymore // The snode isn't associated with the given public key anymore
@ -784,9 +779,8 @@ async function handle421InvalidSwarm({
async function incrementBadSnodeCountOrDrop({ async function incrementBadSnodeCountOrDrop({
snodeEd25519, snodeEd25519,
associatedWith, associatedWith,
}: { }: Partial<WithAssociatedWith> & {
snodeEd25519: string; snodeEd25519: string;
associatedWith?: string;
}) { }) {
const oldFailureCount = snodeFailureCount[snodeEd25519] || 0; const oldFailureCount = snodeFailureCount[snodeEd25519] || 0;
const newFailureCount = oldFailureCount + 1; const newFailureCount = oldFailureCount + 1;
@ -829,12 +823,12 @@ async function sendOnionRequestHandlingSnodeEjectNoRetries({
timeoutMs, timeoutMs,
}: WithAbortSignal & }: WithAbortSignal &
WithTimeoutMs & WithTimeoutMs &
WithAllow401s & { WithAllow401s &
Partial<WithAssociatedWith> & {
nodePath: Array<Snode>; nodePath: Array<Snode>;
destSnodeX25519: string; destSnodeX25519: string;
finalDestOptions: FinalDestOptions; finalDestOptions: FinalDestOptions;
finalRelayOptions?: FinalRelayOptions; finalRelayOptions?: FinalRelayOptions;
associatedWith?: string;
useV4: boolean; useV4: boolean;
throwErrors: boolean; throwErrors: boolean;
}): Promise<SnodeResponse | SnodeResponseV4 | undefined> { }): Promise<SnodeResponse | SnodeResponseV4 | undefined> {
@ -1119,12 +1113,12 @@ async function sendOnionRequestSnodeDestNoRetries({
associatedWith, associatedWith,
}: WithTimeoutMs & }: WithTimeoutMs &
WithAbortSignal & WithAbortSignal &
WithAllow401s & { WithAllow401s &
Partial<WithAssociatedWith> & {
onionPath: Array<Snode>; onionPath: Array<Snode>;
targetNode: Snode; targetNode: Snode;
headers: Record<string, any>; headers: Record<string, any>;
plaintext: string | null; plaintext: string | null;
associatedWith?: string;
}) { }) {
return Onions.sendOnionRequestHandlingSnodeEjectNoRetries({ return Onions.sendOnionRequestHandlingSnodeEjectNoRetries({
nodePath: onionPath, nodePath: onionPath,
@ -1156,11 +1150,11 @@ async function lokiOnionFetchNoRetries({
timeoutMs, timeoutMs,
}: WithTimeoutMs & }: WithTimeoutMs &
WithAbortSignal & WithAbortSignal &
WithAllow401s & { WithAllow401s &
Partial<WithAssociatedWith> & {
targetNode: Snode; targetNode: Snode;
headers: Record<string, any>; headers: Record<string, any>;
body: string | null; body: string | null;
associatedWith?: string;
}): Promise<SnodeResponse | undefined> { }): Promise<SnodeResponse | undefined> {
try { try {
// Get a path excluding `targetNode`: // Get a path excluding `targetNode`:

@ -1,4 +1,5 @@
import { PubkeyType } from 'libsession_util_nodejs'; import { PubkeyType } from 'libsession_util_nodejs';
import { Snode } from '../../data/types';
export type WithMessageHash = { messageHash: string }; export type WithMessageHash = { messageHash: string };
export type WithTimestamp = { timestamp: number }; export type WithTimestamp = { timestamp: number };
@ -26,3 +27,10 @@ export type ShortenOrExtend = 'extend' | 'shorten' | '';
export type WithShortenOrExtend = { shortenOrExtend: ShortenOrExtend }; export type WithShortenOrExtend = { shortenOrExtend: ShortenOrExtend };
export type WithMessagesHashes = { messagesHashes: Array<string> }; export type WithMessagesHashes = { messagesHashes: Array<string> };
export type WithAllow401s = { allow401s: boolean }; export type WithAllow401s = { allow401s: boolean };
export type WithDestinationEd25519 = { destinationSnodeEd25519: string };
export type WithAssociatedWith = { associatedWith: string | null };
export type WithTargetNode = { targetNode: Snode };
export type WithGuardNode = { guardNode: Snode };
export type WithSymmetricKey = { symmetricKey: ArrayBuffer };

Loading…
Cancel
Save