signal-desktop/ts/RemoteConfig.ts

254 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2020-05-27 21:37:06 +00:00
import { get, throttle } from 'lodash';
import type { WebAPIType } from './textsecure/WebAPI';
import * as log from './logging/log';
import type { AciString } from './types/ServiceId';
import { parseIntOrThrow } from './util/parseIntOrThrow';
import { SECOND, HOUR } from './util/durations';
import * as Bytes from './Bytes';
2023-08-09 00:53:06 +00:00
import { uuidToBytes } from './util/uuidToBytes';
2023-11-02 18:14:38 +00:00
import { dropNull } from './util/dropNull';
import { HashType } from './types/Crypto';
import { getCountryCode } from './types/PhoneNumber';
2020-05-27 21:37:06 +00:00
2021-03-03 20:09:58 +00:00
export type ConfigKeyType =
2024-02-22 21:19:50 +00:00
| 'desktop.calling.adhoc'
2024-10-16 18:08:47 +00:00
| 'desktop.calling.adhoc.beta'
2024-06-10 15:23:43 +00:00
| 'desktop.calling.adhoc.create'
2024-10-16 18:08:47 +00:00
| 'desktop.calling.adhoc.create.beta'
| 'desktop.calling.ringrtcAdm'
| 'desktop.clientExpiration'
2024-04-22 21:25:56 +00:00
| 'desktop.backup.credentialFetch'
| 'desktop.internalUser'
2021-06-25 16:08:16 +00:00
| 'desktop.mediaQuality.levels'
| 'desktop.messageCleanup'
2021-06-08 21:51:58 +00:00
| 'desktop.retryRespondMaxAge'
| 'desktop.senderKey.retry'
| 'desktop.senderKeyMaxAge'
2024-08-06 21:21:15 +00:00
| 'desktop.experimentalTransport.enableAuth'
| 'desktop.experimentalTransportEnabled.alpha'
| 'desktop.experimentalTransportEnabled.beta'
| 'desktop.experimentalTransportEnabled.prod'
| 'desktop.cdsiViaLibsignal'
| 'global.attachments.maxBytes'
| 'global.attachments.maxReceiveBytes'
| 'global.calling.maxGroupCallRingSize'
| 'global.groupsv2.groupSizeHardLimit'
2022-10-18 17:12:02 +00:00
| 'global.groupsv2.maxGroupSize'
| 'global.messageQueueTimeInSeconds'
2022-10-18 17:12:02 +00:00
| 'global.nicknames.max'
| 'global.nicknames.min'
| 'global.textAttachmentLimitBytes';
2022-10-07 18:34:27 +00:00
2020-05-27 21:37:06 +00:00
type ConfigValueType = {
name: ConfigKeyType;
enabled: boolean;
enabledAt?: number;
2023-11-02 18:14:38 +00:00
value?: string;
2020-05-27 21:37:06 +00:00
};
export type ConfigMapType = {
[key in ConfigKeyType]?: ConfigValueType;
};
2020-05-27 21:37:06 +00:00
type ConfigListenerType = (value: ConfigValueType) => unknown;
type ConfigListenersMapType = {
[key: string]: Array<ConfigListenerType>;
};
let config: ConfigMapType = {};
const listeners: ConfigListenersMapType = {};
export function restoreRemoteConfigFromStorage(): void {
2020-05-27 21:37:06 +00:00
config = window.storage.get('remoteConfig') || {};
}
export async function initRemoteConfig(server: WebAPIType): Promise<void> {
restoreRemoteConfigFromStorage();
await maybeRefreshRemoteConfig(server);
2020-05-27 21:37:06 +00:00
}
2020-09-11 19:37:01 +00:00
export function onChange(
key: ConfigKeyType,
fn: ConfigListenerType
): () => void {
2020-05-27 21:37:06 +00:00
const keyListeners: Array<ConfigListenerType> = get(listeners, key, []);
keyListeners.push(fn);
listeners[key] = keyListeners;
return () => {
listeners[key] = listeners[key].filter(l => l !== fn);
};
}
export const refreshRemoteConfig = async (
server: WebAPIType
): Promise<void> => {
2020-05-27 21:37:06 +00:00
const now = Date.now();
const { config: newConfig, serverEpochTime } = await server.getConfig();
const serverTimeSkew = serverEpochTime * SECOND - now;
if (Math.abs(serverTimeSkew) > HOUR) {
log.warn(
'Remote Config: sever clock skew detected. ' +
`Server time ${serverEpochTime * SECOND}, local time ${now}`
);
}
2020-05-27 21:37:06 +00:00
// Process new configuration in light of the old configuration
// The old configuration is not set as the initial value in reduce because
// flags may have been deleted
const oldConfig = config;
config = newConfig.reduce((acc, { name, enabled, value }) => {
2020-05-27 21:37:06 +00:00
const previouslyEnabled: boolean = get(oldConfig, [name, 'enabled'], false);
2023-11-02 18:14:38 +00:00
const previousValue: string | undefined = get(
oldConfig,
[name, 'value'],
undefined
);
2020-09-11 19:37:01 +00:00
// If a flag was previously not enabled and is now enabled,
// record the time it was enabled
2020-05-27 21:37:06 +00:00
const enabledAt: number | undefined =
previouslyEnabled && enabled ? now : get(oldConfig, [name, 'enabledAt']);
const configValue = {
2020-05-27 21:37:06 +00:00
name: name as ConfigKeyType,
enabled,
enabledAt,
2023-11-02 18:14:38 +00:00
value: dropNull(value),
2020-05-27 21:37:06 +00:00
};
const hasChanged =
previouslyEnabled !== enabled || previousValue !== configValue.value;
2020-05-27 21:37:06 +00:00
// If enablement changes at all, notify listeners
const currentListeners = listeners[name] || [];
if (hasChanged) {
log.info(`Remote Config: Flag ${name} has changed`);
2020-05-27 21:37:06 +00:00
currentListeners.forEach(listener => {
listener(configValue);
2020-05-27 21:37:06 +00:00
});
}
// Return new configuration object
return {
...acc,
[name]: configValue,
2020-05-27 21:37:06 +00:00
};
}, {});
// If remote configuration fetch worked - we are not expired anymore.
if (
!getValue('desktop.clientExpiration') &&
window.storage.get('remoteBuildExpiration') != null
) {
log.warn('Remote Config: clearing remote expiration on successful fetch');
await window.storage.remove('remoteBuildExpiration');
}
await window.storage.put('remoteConfig', config);
await window.storage.put('serverTimeSkew', serverTimeSkew);
2020-05-27 21:37:06 +00:00
};
export const maybeRefreshRemoteConfig = throttle(
refreshRemoteConfig,
// Only fetch remote configuration if the last fetch was more than two hours ago
2 * 60 * 60 * 1000,
{ trailing: false }
);
export function isEnabled(name: ConfigKeyType): boolean {
return get(config, [name, 'enabled'], false);
}
export function getValue(name: ConfigKeyType): string | undefined {
return get(config, [name, 'value'], undefined);
}
// See isRemoteConfigBucketEnabled in selectors/items.ts
export function isBucketValueEnabled(
name: ConfigKeyType,
e164: string | undefined,
aci: AciString | undefined
): boolean {
return innerIsBucketValueEnabled(name, getValue(name), e164, aci);
}
export function innerIsBucketValueEnabled(
name: ConfigKeyType,
flagValue: unknown,
e164: string | undefined,
aci: AciString | undefined
): boolean {
if (e164 == null || aci == null) {
return false;
}
const countryCode = getCountryCode(e164);
if (countryCode == null) {
return false;
}
if (typeof flagValue !== 'string') {
return false;
}
const remoteConfigValue = getCountryCodeValue(countryCode, flagValue, name);
if (remoteConfigValue == null) {
return false;
}
const bucketValue = getBucketValue(aci, name);
return bucketValue < remoteConfigValue;
}
export function getCountryCodeValue(
countryCode: number,
flagValue: string,
flagName: string
): number | undefined {
const logId = `getCountryCodeValue/${flagName}`;
if (flagValue.length === 0) {
return undefined;
}
const countryCodeString = countryCode.toString();
const items = flagValue.split(',');
let wildcard: number | undefined;
for (const item of items) {
const [code, value] = item.split(':');
if (code == null || value == null) {
log.warn(`${logId}: '${code}:${value}' entry was invalid`);
continue;
}
const parsedValue = parseIntOrThrow(
value,
`${logId}: Country code '${code}' had an invalid number '${value}'`
);
if (code === '*') {
wildcard = parsedValue;
} else if (countryCodeString === code) {
return parsedValue;
}
}
return wildcard;
}
export function getBucketValue(aci: AciString, flagName: string): number {
const hashInput = Bytes.concatenate([
Bytes.fromString(`${flagName}.`),
uuidToBytes(aci),
]);
2023-07-18 23:57:38 +00:00
const hashResult = window.SignalContext.crypto.hash(
HashType.size256,
hashInput
);
2023-07-18 23:57:38 +00:00
return Number(Bytes.readBigUint64BE(hashResult.slice(0, 8)) % 1_000_000n);
}