2020-03-31 20:03:38 +00:00
|
|
|
import { w3cwebsocket as WebSocket } from 'websocket';
|
|
|
|
import fetch, { Response } from 'node-fetch';
|
|
|
|
import ProxyAgent from 'proxy-agent';
|
|
|
|
import { Agent } from 'https';
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
import is from '@sindresorhus/is';
|
2020-04-13 17:37:29 +00:00
|
|
|
import { redactPackId } from '../../js/modules/stickers';
|
|
|
|
import { getRandomValue } from '../Crypto';
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
import { v4 as getGuid } from 'uuid';
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable no-bitwise
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _btoa(str: any) {
|
2018-05-26 01:01:56 +00:00
|
|
|
let buffer;
|
|
|
|
|
|
|
|
if (str instanceof Buffer) {
|
|
|
|
buffer = str;
|
|
|
|
} else {
|
|
|
|
buffer = Buffer.from(str.toString(), 'binary');
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.toString('base64');
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const _call = (object: any) => Object.prototype.toString.call(object);
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const ArrayBufferToString = _call(new ArrayBuffer(0));
|
2018-05-26 01:01:56 +00:00
|
|
|
const Uint8ArrayToString = _call(new Uint8Array());
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _getString(thing: any): string {
|
2018-05-26 01:01:56 +00:00
|
|
|
if (typeof thing !== 'string') {
|
2020-03-31 20:03:38 +00:00
|
|
|
if (_call(thing) === Uint8ArrayToString) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return String.fromCharCode.apply(null, thing);
|
2020-03-31 20:03:38 +00:00
|
|
|
}
|
|
|
|
if (_call(thing) === ArrayBufferToString) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _getString(new Uint8Array(thing));
|
2020-03-31 20:03:38 +00:00
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return thing;
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
// prettier-ignore
|
2020-03-31 20:03:38 +00:00
|
|
|
function _b64ToUint6(nChr: number) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return nChr > 64 && nChr < 91
|
|
|
|
? nChr - 65
|
|
|
|
: nChr > 96 && nChr < 123
|
|
|
|
? nChr - 71
|
|
|
|
: nChr > 47 && nChr < 58
|
|
|
|
? nChr + 4
|
|
|
|
: nChr === 43
|
|
|
|
? 62
|
|
|
|
: nChr === 47
|
|
|
|
? 63
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _getStringable(thing: any) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return (
|
|
|
|
typeof thing === 'string' ||
|
|
|
|
typeof thing === 'number' ||
|
|
|
|
typeof thing === 'boolean' ||
|
|
|
|
(thing === Object(thing) &&
|
|
|
|
(_call(thing) === ArrayBufferToString ||
|
|
|
|
_call(thing) === Uint8ArrayToString))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _ensureStringed(thing: any): any {
|
2018-05-26 01:01:56 +00:00
|
|
|
if (_getStringable(thing)) {
|
|
|
|
return _getString(thing);
|
|
|
|
} else if (thing instanceof Array) {
|
|
|
|
const res = [];
|
|
|
|
for (let i = 0; i < thing.length; i += 1) {
|
|
|
|
res[i] = _ensureStringed(thing[i]);
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return res;
|
|
|
|
} else if (thing === Object(thing)) {
|
2020-03-31 20:03:38 +00:00
|
|
|
const res: any = {};
|
|
|
|
// tslint:disable-next-line forin no-for-in
|
2018-05-26 01:01:56 +00:00
|
|
|
for (const key in thing) {
|
|
|
|
res[key] = _ensureStringed(thing[key]);
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return res;
|
|
|
|
} else if (thing === null) {
|
|
|
|
return null;
|
2018-10-18 01:01:21 +00:00
|
|
|
} else if (thing === undefined) {
|
|
|
|
return undefined;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
throw new Error(`unsure of how to jsonify object of type ${typeof thing}`);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _jsonThing(thing: any) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return JSON.stringify(_ensureStringed(thing));
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _base64ToBytes(sBase64: string, nBlocksSize?: number) {
|
2018-05-26 01:01:56 +00:00
|
|
|
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, '');
|
|
|
|
const nInLen = sB64Enc.length;
|
|
|
|
const nOutLen = nBlocksSize
|
|
|
|
? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize
|
|
|
|
: (nInLen * 3 + 1) >> 2;
|
|
|
|
const aBBytes = new ArrayBuffer(nOutLen);
|
|
|
|
const taBytes = new Uint8Array(aBBytes);
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
let nMod3 = 0;
|
|
|
|
let nMod4 = 0;
|
|
|
|
let nUint24 = 0;
|
|
|
|
let nOutIdx = 0;
|
|
|
|
|
|
|
|
for (let nInIdx = 0; nInIdx < nInLen; nInIdx += 1) {
|
2018-05-26 01:01:56 +00:00
|
|
|
nMod4 = nInIdx & 3;
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line binary-expression-operand-order
|
2018-05-26 01:01:56 +00:00
|
|
|
nUint24 |= _b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (18 - 6 * nMod4);
|
|
|
|
if (nMod4 === 3 || nInLen - nInIdx === 1) {
|
|
|
|
for (
|
|
|
|
nMod3 = 0;
|
|
|
|
nMod3 < 3 && nOutIdx < nOutLen;
|
|
|
|
nMod3 += 1, nOutIdx += 1
|
|
|
|
) {
|
|
|
|
taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
|
|
|
|
}
|
|
|
|
nUint24 = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return aBBytes;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _validateResponse(response: any, schema: any) {
|
2018-05-26 01:01:56 +00:00
|
|
|
try {
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line forin no-for-in
|
2018-05-26 01:01:56 +00:00
|
|
|
for (const i in schema) {
|
|
|
|
switch (schema[i]) {
|
|
|
|
case 'object':
|
|
|
|
case 'string':
|
|
|
|
case 'number':
|
|
|
|
if (typeof response[i] !== schema[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function _createSocket(
|
|
|
|
url: string,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
}: { certificateAuthority: string; proxyUrl?: string }
|
|
|
|
) {
|
2018-05-26 01:01:56 +00:00
|
|
|
let requestOptions;
|
|
|
|
if (proxyUrl) {
|
|
|
|
requestOptions = {
|
|
|
|
ca: certificateAuthority,
|
|
|
|
agent: new ProxyAgent(proxyUrl),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
requestOptions = {
|
|
|
|
ca: certificateAuthority,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:51:12 +00:00
|
|
|
return new WebSocket(url, undefined, undefined, undefined, requestOptions, {
|
|
|
|
maxReceivedFrameSize: 0x210000,
|
|
|
|
});
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 01:23:07 +00:00
|
|
|
const FIVE_MINUTES = 1000 * 60 * 5;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
type AgentCacheType = {
|
|
|
|
[name: string]: {
|
|
|
|
timestamp: number;
|
|
|
|
agent: ProxyAgent | Agent;
|
|
|
|
};
|
2018-11-07 19:20:43 +00:00
|
|
|
};
|
2020-03-31 20:03:38 +00:00
|
|
|
const agents: AgentCacheType = {};
|
2018-11-07 19:20:43 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function getContentType(response: Response) {
|
2019-01-16 03:03:56 +00:00
|
|
|
if (response.headers && response.headers.get) {
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-backbone-get-set-outside-model
|
2019-01-16 03:03:56 +00:00
|
|
|
return response.headers.get('content-type');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type HeaderListType = { [name: string]: string };
|
|
|
|
|
|
|
|
type PromiseAjaxOptionsType = {
|
|
|
|
accessKey?: string;
|
|
|
|
certificateAuthority?: string;
|
|
|
|
contentType?: string;
|
|
|
|
data?: ArrayBuffer | Buffer | string;
|
|
|
|
headers?: HeaderListType;
|
|
|
|
host?: string;
|
|
|
|
password?: string;
|
|
|
|
path?: string;
|
|
|
|
proxyUrl?: string;
|
|
|
|
redactUrl?: (url: string) => string;
|
|
|
|
redirect?: 'error' | 'follow' | 'manual';
|
|
|
|
responseType?: 'json' | 'arraybuffer' | 'arraybufferwithdetails';
|
|
|
|
stack?: string;
|
|
|
|
timeout?: number;
|
|
|
|
type: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
|
|
unauthenticated?: boolean;
|
|
|
|
user?: string;
|
|
|
|
validateResponse?: any;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// tslint:disable-next-line max-func-body-length
|
|
|
|
async function _promiseAjax(
|
|
|
|
providedUrl: string | null,
|
|
|
|
options: PromiseAjaxOptionsType
|
|
|
|
): Promise<any> {
|
|
|
|
// tslint:disable-next-line max-func-body-length
|
2018-05-26 01:01:56 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const url = providedUrl || `${options.host}/${options.path}`;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const unauthLabel = options.unauthenticated ? ' (unauth)' : '';
|
|
|
|
if (options.redactUrl) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(
|
|
|
|
`${options.type} ${options.redactUrl(url)}${unauthLabel}`
|
|
|
|
);
|
2019-01-16 03:03:56 +00:00
|
|
|
} else {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(`${options.type} ${url}${unauthLabel}`);
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
2019-02-20 22:40:32 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
const timeout =
|
2020-03-31 20:03:38 +00:00
|
|
|
typeof options.timeout === 'number' ? options.timeout : 10000;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
const { proxyUrl } = options;
|
2018-11-09 01:23:07 +00:00
|
|
|
const agentType = options.unauthenticated ? 'unauth' : 'auth';
|
2019-02-20 22:40:32 +00:00
|
|
|
const cacheKey = `${proxyUrl}-${agentType}`;
|
2018-11-07 19:20:43 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const { timestamp } = agents[cacheKey] || { timestamp: null };
|
2018-11-09 01:23:07 +00:00
|
|
|
if (!timestamp || timestamp + FIVE_MINUTES < Date.now()) {
|
|
|
|
if (timestamp) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(`Cycling agent for type ${cacheKey}`);
|
2018-11-07 19:20:43 +00:00
|
|
|
}
|
2019-02-20 22:40:32 +00:00
|
|
|
agents[cacheKey] = {
|
2018-11-09 01:23:07 +00:00
|
|
|
agent: proxyUrl
|
|
|
|
? new ProxyAgent(proxyUrl)
|
|
|
|
: new Agent({ keepAlive: true }),
|
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2019-02-20 22:40:32 +00:00
|
|
|
const { agent } = agents[cacheKey];
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
const fetchOptions = {
|
|
|
|
method: options.type,
|
2020-03-31 20:03:38 +00:00
|
|
|
body: options.data,
|
2019-01-16 03:03:56 +00:00
|
|
|
headers: {
|
2020-01-17 00:53:35 +00:00
|
|
|
'User-Agent': `Signal Desktop ${options.version}`,
|
2019-01-16 03:03:56 +00:00
|
|
|
'X-Signal-Agent': 'OWD',
|
|
|
|
...options.headers,
|
2020-03-31 20:03:38 +00:00
|
|
|
} as HeaderListType,
|
2019-01-16 03:03:56 +00:00
|
|
|
redirect: options.redirect,
|
2018-05-26 01:01:56 +00:00
|
|
|
agent,
|
2020-03-31 20:03:38 +00:00
|
|
|
// We patched node-fetch to add the ca param; its type definitions don't have it
|
|
|
|
// @ts-ignore
|
2018-05-26 01:01:56 +00:00
|
|
|
ca: options.certificateAuthority,
|
|
|
|
timeout,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fetchOptions.body instanceof ArrayBuffer) {
|
|
|
|
// node-fetch doesn't support ArrayBuffer, only node Buffer
|
|
|
|
const contentLength = fetchOptions.body.byteLength;
|
|
|
|
fetchOptions.body = Buffer.from(fetchOptions.body);
|
|
|
|
|
|
|
|
// node-fetch doesn't set content-length like S3 requires
|
2020-03-31 20:03:38 +00:00
|
|
|
fetchOptions.headers['Content-Length'] = contentLength.toString();
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const { accessKey, unauthenticated } = options;
|
|
|
|
if (unauthenticated) {
|
|
|
|
if (!accessKey) {
|
|
|
|
throw new Error(
|
|
|
|
'_promiseAjax: mode is aunathenticated, but accessKey was not provided'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Access key is already a Base64 string
|
|
|
|
fetchOptions.headers['Unidentified-Access-Key'] = accessKey;
|
|
|
|
} else if (options.user && options.password) {
|
2018-05-26 01:01:56 +00:00
|
|
|
const user = _getString(options.user);
|
|
|
|
const password = _getString(options.password);
|
|
|
|
const auth = _btoa(`${user}:${password}`);
|
|
|
|
fetchOptions.headers.Authorization = `Basic ${auth}`;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
if (options.contentType) {
|
|
|
|
fetchOptions.headers['Content-Type'] = options.contentType;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
fetch(url, fetchOptions)
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line max-func-body-length
|
|
|
|
.then(async response => {
|
2018-05-26 01:01:56 +00:00
|
|
|
let resultPromise;
|
|
|
|
if (
|
|
|
|
options.responseType === 'json' &&
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-backbone-get-set-outside-model
|
2018-05-26 01:01:56 +00:00
|
|
|
response.headers.get('Content-Type') === 'application/json'
|
|
|
|
) {
|
|
|
|
resultPromise = response.json();
|
2019-01-16 03:03:56 +00:00
|
|
|
} else if (
|
|
|
|
options.responseType === 'arraybuffer' ||
|
|
|
|
options.responseType === 'arraybufferwithdetails'
|
|
|
|
) {
|
2018-05-26 01:01:56 +00:00
|
|
|
resultPromise = response.buffer();
|
|
|
|
} else {
|
|
|
|
resultPromise = response.text();
|
|
|
|
}
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return resultPromise.then(result => {
|
2019-01-16 03:03:56 +00:00
|
|
|
if (
|
|
|
|
options.responseType === 'arraybuffer' ||
|
|
|
|
options.responseType === 'arraybufferwithdetails'
|
|
|
|
) {
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-parameter-reassignment
|
2018-05-26 01:01:56 +00:00
|
|
|
result = result.buffer.slice(
|
|
|
|
result.byteOffset,
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line: restrict-plus-operands
|
2018-05-26 01:01:56 +00:00
|
|
|
result.byteOffset + result.byteLength
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (options.responseType === 'json') {
|
|
|
|
if (options.validateResponse) {
|
|
|
|
if (!_validateResponse(result, options.validateResponse)) {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (options.redactUrl) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(
|
2019-01-16 03:03:56 +00:00
|
|
|
options.type,
|
2019-05-16 22:32:11 +00:00
|
|
|
options.redactUrl(url),
|
2019-01-16 03:03:56 +00:00
|
|
|
response.status,
|
|
|
|
'Error'
|
|
|
|
);
|
|
|
|
} else {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.error(options.type, url, response.status, 'Error');
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
reject(
|
|
|
|
makeHTTPError(
|
2018-05-26 01:01:56 +00:00
|
|
|
'promiseAjax: invalid response',
|
|
|
|
response.status,
|
|
|
|
result,
|
|
|
|
options.stack
|
|
|
|
)
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (response.status >= 0 && response.status < 400) {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (options.redactUrl) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(
|
2019-01-16 03:03:56 +00:00
|
|
|
options.type,
|
2019-05-16 22:32:11 +00:00
|
|
|
options.redactUrl(url),
|
2019-01-16 03:03:56 +00:00
|
|
|
response.status,
|
|
|
|
'Success'
|
|
|
|
);
|
|
|
|
} else {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(options.type, url, response.status, 'Success');
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
if (options.responseType === 'arraybufferwithdetails') {
|
2020-03-31 20:03:38 +00:00
|
|
|
resolve({
|
2019-01-16 03:03:56 +00:00
|
|
|
data: result,
|
|
|
|
contentType: getContentType(response),
|
|
|
|
response,
|
|
|
|
});
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
resolve(result);
|
|
|
|
|
|
|
|
return;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
if (options.redactUrl) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info(
|
2019-05-16 22:32:11 +00:00
|
|
|
options.type,
|
|
|
|
options.redactUrl(url),
|
|
|
|
response.status,
|
|
|
|
'Error'
|
|
|
|
);
|
2018-05-26 01:01:56 +00:00
|
|
|
} else {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.error(options.type, url, response.status, 'Error');
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
reject(
|
|
|
|
makeHTTPError(
|
2019-01-16 03:03:56 +00:00
|
|
|
'promiseAjax: error response',
|
|
|
|
response.status,
|
|
|
|
result,
|
|
|
|
options.stack
|
|
|
|
)
|
|
|
|
);
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return;
|
2018-05-26 01:01:56 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (options.redactUrl) {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.error(options.type, options.redactUrl(url), 0, 'Error');
|
2019-01-16 03:03:56 +00:00
|
|
|
} else {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.error(options.type, url, 0, 'Error');
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
|
2020-03-31 20:03:38 +00:00
|
|
|
reject(makeHTTPError('promiseAjax catch', 0, e.toString(), stack));
|
2018-05-26 01:01:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function _retryAjax(
|
|
|
|
url: string | null,
|
|
|
|
options: PromiseAjaxOptionsType,
|
|
|
|
providedLimit?: number,
|
|
|
|
providedCount?: number
|
|
|
|
) {
|
2018-05-26 01:01:56 +00:00
|
|
|
const count = (providedCount || 0) + 1;
|
|
|
|
const limit = providedLimit || 3;
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return _promiseAjax(url, options).catch(async (e: Error) => {
|
2018-05-26 01:01:56 +00:00
|
|
|
if (e.name === 'HTTPError' && e.code === -1 && count < limit) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(_retryAjax(url, options, limit, count));
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function _outerAjax(url: string | null, options: PromiseAjaxOptionsType) {
|
2018-05-26 01:01:56 +00:00
|
|
|
options.stack = new Error().stack; // just in case, save stack here.
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return _retryAjax(url, options);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
declare global {
|
|
|
|
interface Error {
|
|
|
|
code?: number | string;
|
|
|
|
response?: any;
|
2020-04-13 17:37:29 +00:00
|
|
|
warn?: boolean;
|
2020-03-31 20:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeHTTPError(
|
|
|
|
message: string,
|
|
|
|
providedCode: number,
|
|
|
|
response: any,
|
|
|
|
stack?: string
|
|
|
|
) {
|
2018-05-26 01:01:56 +00:00
|
|
|
const code = providedCode > 999 || providedCode < 100 ? -1 : providedCode;
|
|
|
|
const e = new Error(`${message}; code: ${code}`);
|
|
|
|
e.name = 'HTTPError';
|
|
|
|
e.code = code;
|
|
|
|
e.stack += `\nOriginal stack:\n${stack}`;
|
|
|
|
if (response) {
|
|
|
|
e.response = response;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
const URL_CALLS = {
|
|
|
|
accounts: 'v1/accounts',
|
2018-12-13 19:12:33 +00:00
|
|
|
updateDeviceName: 'v1/accounts/name',
|
2019-01-11 16:53:35 +00:00
|
|
|
removeSignalingKey: 'v1/accounts/signaling_key',
|
2019-05-08 20:14:52 +00:00
|
|
|
attachmentId: 'v2/attachments/form/upload',
|
2018-10-18 01:01:21 +00:00
|
|
|
deliveryCert: 'v1/certificate/delivery',
|
|
|
|
supportUnauthenticatedDelivery: 'v1/devices/unauthenticated_delivery',
|
2020-03-05 21:14:58 +00:00
|
|
|
registerCapabilities: 'v1/devices/capabilities',
|
2018-05-26 01:01:56 +00:00
|
|
|
devices: 'v1/devices',
|
|
|
|
keys: 'v2/keys',
|
|
|
|
messages: 'v1/messages',
|
|
|
|
profile: 'v1/profile',
|
2018-10-18 01:01:21 +00:00
|
|
|
signed: 'v2/keys/signed',
|
2019-12-17 20:25:57 +00:00
|
|
|
getStickerPackUpload: 'v1/sticker/pack/form',
|
2020-03-05 21:14:58 +00:00
|
|
|
whoami: 'v1/accounts/whoami',
|
2020-05-27 21:37:06 +00:00
|
|
|
config: 'v1/config',
|
2018-05-26 01:01:56 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type InitializeOptionsType = {
|
|
|
|
url: string;
|
2020-04-17 22:51:39 +00:00
|
|
|
cdnUrlObject: {
|
|
|
|
readonly '0': string;
|
|
|
|
readonly [propName: string]: string;
|
|
|
|
};
|
2020-03-31 20:03:38 +00:00
|
|
|
certificateAuthority: string;
|
|
|
|
contentProxyUrl: string;
|
|
|
|
proxyUrl: string;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ConnectParametersType = {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type MessageType = any;
|
|
|
|
|
|
|
|
type AjaxOptionsType = {
|
|
|
|
accessKey?: string;
|
|
|
|
call: keyof typeof URL_CALLS;
|
|
|
|
httpType: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
|
|
jsonData?: any;
|
|
|
|
responseType?: 'json' | 'arraybuffer' | 'arraybufferwithdetails';
|
|
|
|
timeout?: number;
|
|
|
|
unauthenticated?: boolean;
|
|
|
|
urlParameters?: string;
|
|
|
|
validateResponse?: any;
|
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export type WebAPIConnectType = {
|
|
|
|
connect: (options: ConnectParametersType) => WebAPIType;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StickerPackManifestType = any;
|
|
|
|
|
|
|
|
export type WebAPIType = {
|
|
|
|
confirmCode: (
|
|
|
|
number: string,
|
|
|
|
code: string,
|
|
|
|
newPassword: string,
|
|
|
|
registrationId: number,
|
|
|
|
deviceName?: string | null,
|
|
|
|
options?: { accessKey?: ArrayBuffer }
|
|
|
|
) => Promise<any>;
|
2020-04-17 22:51:39 +00:00
|
|
|
getAttachment: (cdnKey: string, cdnNumber: number) => Promise<any>;
|
2020-04-13 17:37:29 +00:00
|
|
|
getAvatar: (path: string) => Promise<any>;
|
|
|
|
getDevices: () => Promise<any>;
|
|
|
|
getKeysForIdentifier: (
|
|
|
|
identifier: string,
|
|
|
|
deviceId?: number
|
|
|
|
) => Promise<ServerKeysType>;
|
|
|
|
getKeysForIdentifierUnauth: (
|
|
|
|
identifier: string,
|
|
|
|
deviceId?: number,
|
|
|
|
options?: { accessKey?: string }
|
|
|
|
) => Promise<ServerKeysType>;
|
|
|
|
getMessageSocket: () => WebSocket;
|
|
|
|
getMyKeys: () => Promise<number>;
|
2020-04-15 23:12:28 +00:00
|
|
|
getProfile: (
|
|
|
|
identifier: string,
|
|
|
|
options?: {
|
|
|
|
profileKeyVersion?: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
|
|
|
}
|
|
|
|
) => Promise<any>;
|
2020-04-13 17:37:29 +00:00
|
|
|
getProfileUnauth: (
|
|
|
|
identifier: string,
|
2020-04-15 23:12:28 +00:00
|
|
|
options: {
|
|
|
|
accessKey: string;
|
|
|
|
profileKeyVersion?: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
) => Promise<any>;
|
|
|
|
getProvisioningSocket: () => WebSocket;
|
|
|
|
getSenderCertificate: (withUuid?: boolean) => Promise<any>;
|
|
|
|
getSticker: (packId: string, stickerId: string) => Promise<any>;
|
|
|
|
getStickerPackManifest: (packId: string) => Promise<StickerPackManifestType>;
|
|
|
|
makeProxiedRequest: (
|
|
|
|
targetUrl: string,
|
|
|
|
options?: ProxiedRequestOptionsType
|
|
|
|
) => Promise<any>;
|
|
|
|
putAttachment: (encryptedBin: ArrayBuffer) => Promise<any>;
|
|
|
|
registerCapabilities: (capabilities: any) => Promise<void>;
|
|
|
|
putStickers: (
|
|
|
|
encryptedManifest: ArrayBuffer,
|
|
|
|
encryptedStickers: Array<ArrayBuffer>,
|
|
|
|
onProgress?: () => void
|
|
|
|
) => Promise<string>;
|
|
|
|
registerKeys: (genKeys: KeysType) => Promise<void>;
|
|
|
|
registerSupportForUnauthenticatedDelivery: () => Promise<any>;
|
|
|
|
removeSignalingKey: () => Promise<void>;
|
|
|
|
requestVerificationSMS: (number: string) => Promise<any>;
|
|
|
|
requestVerificationVoice: (number: string) => Promise<any>;
|
|
|
|
sendMessages: (
|
|
|
|
destination: string,
|
|
|
|
messageArray: Array<MessageType>,
|
|
|
|
timestamp: number,
|
|
|
|
silent?: boolean,
|
|
|
|
online?: boolean
|
|
|
|
) => Promise<void>;
|
|
|
|
sendMessagesUnauth: (
|
|
|
|
destination: string,
|
|
|
|
messageArray: Array<MessageType>,
|
|
|
|
timestamp: number,
|
|
|
|
silent?: boolean,
|
|
|
|
online?: boolean,
|
|
|
|
options?: { accessKey?: string }
|
|
|
|
) => Promise<void>;
|
|
|
|
setSignedPreKey: (signedPreKey: SignedPreKeyType) => Promise<void>;
|
|
|
|
updateDeviceName: (deviceName: string) => Promise<void>;
|
|
|
|
whoami: () => Promise<any>;
|
2020-05-27 21:37:06 +00:00
|
|
|
getConfig: () => Promise<Array<{ name: string; enabled: boolean }>>;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type SignedPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: ArrayBuffer;
|
|
|
|
signature: ArrayBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type KeysType = {
|
|
|
|
identityKey: ArrayBuffer;
|
|
|
|
signedPreKey: SignedPreKeyType;
|
|
|
|
preKeys: Array<{
|
|
|
|
keyId: number;
|
|
|
|
publicKey: ArrayBuffer;
|
|
|
|
}>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ServerKeysType = {
|
|
|
|
devices: Array<{
|
|
|
|
deviceId: number;
|
|
|
|
registrationId: number;
|
|
|
|
signedPreKey: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: ArrayBuffer;
|
|
|
|
signature: ArrayBuffer;
|
|
|
|
};
|
|
|
|
preKey?: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: ArrayBuffer;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
identityKey: ArrayBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ProxiedRequestOptionsType = {
|
|
|
|
returnArrayBuffer?: boolean;
|
|
|
|
start?: number;
|
|
|
|
end?: number;
|
|
|
|
};
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
// We first set up the data that won't change during this session of the app
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line max-func-body-length
|
2020-04-13 17:37:29 +00:00
|
|
|
export function initialize({
|
2019-01-16 03:03:56 +00:00
|
|
|
url,
|
2020-04-17 22:51:39 +00:00
|
|
|
cdnUrlObject,
|
2019-01-16 03:03:56 +00:00
|
|
|
certificateAuthority,
|
|
|
|
contentProxyUrl,
|
|
|
|
proxyUrl,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2020-04-13 17:37:29 +00:00
|
|
|
}: InitializeOptionsType): WebAPIConnectType {
|
2018-05-26 01:01:56 +00:00
|
|
|
if (!is.string(url)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid server url');
|
|
|
|
}
|
2020-04-17 22:51:39 +00:00
|
|
|
if (!is.object(cdnUrlObject)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid cdnUrlObject');
|
|
|
|
}
|
|
|
|
if (!is.string(cdnUrlObject['0'])) {
|
|
|
|
throw new Error('WebAPI.initialize: Missing CDN 0 configuration');
|
|
|
|
}
|
|
|
|
if (!is.string(cdnUrlObject['2'])) {
|
|
|
|
throw new Error('WebAPI.initialize: Missing CDN 2 configuration');
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
if (!is.string(certificateAuthority)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid certificateAuthority');
|
|
|
|
}
|
2019-01-16 03:03:56 +00:00
|
|
|
if (!is.string(contentProxyUrl)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid contentProxyUrl');
|
|
|
|
}
|
2020-01-17 00:53:35 +00:00
|
|
|
if (!is.string(version)) {
|
|
|
|
throw new Error('WebAPI.initialize: Invalid version');
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
// Thanks to function-hoisting, we can put this return statement before all of the
|
|
|
|
// below function definitions.
|
|
|
|
return {
|
|
|
|
connect,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Then we connect to the server with user-specific information. This is the only API
|
|
|
|
// exposed to the browser context, ensuring that it can't connect to arbitrary
|
|
|
|
// locations.
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line max-func-body-length
|
|
|
|
function connect({
|
|
|
|
username: initialUsername,
|
|
|
|
password: initialPassword,
|
|
|
|
}: ConnectParametersType) {
|
2018-05-26 01:01:56 +00:00
|
|
|
let username = initialUsername;
|
|
|
|
let password = initialPassword;
|
2019-05-16 22:32:11 +00:00
|
|
|
const PARSE_RANGE_HEADER = /\/(\d+)$/;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
// Thanks, function hoisting!
|
|
|
|
return {
|
|
|
|
confirmCode,
|
|
|
|
getAttachment,
|
|
|
|
getAvatar,
|
|
|
|
getDevices,
|
2020-03-05 21:14:58 +00:00
|
|
|
getKeysForIdentifier,
|
|
|
|
getKeysForIdentifierUnauth,
|
2018-05-26 01:01:56 +00:00
|
|
|
getMessageSocket,
|
|
|
|
getMyKeys,
|
|
|
|
getProfile,
|
2018-10-18 01:01:21 +00:00
|
|
|
getProfileUnauth,
|
2018-05-26 01:01:56 +00:00
|
|
|
getProvisioningSocket,
|
2019-01-16 03:03:56 +00:00
|
|
|
getSenderCertificate,
|
2019-05-16 22:32:11 +00:00
|
|
|
getSticker,
|
|
|
|
getStickerPackManifest,
|
2019-01-16 03:03:56 +00:00
|
|
|
makeProxiedRequest,
|
2018-05-26 01:01:56 +00:00
|
|
|
putAttachment,
|
2020-03-05 21:14:58 +00:00
|
|
|
registerCapabilities,
|
2019-12-17 20:25:57 +00:00
|
|
|
putStickers,
|
2018-05-26 01:01:56 +00:00
|
|
|
registerKeys,
|
2019-01-16 03:03:56 +00:00
|
|
|
registerSupportForUnauthenticatedDelivery,
|
|
|
|
removeSignalingKey,
|
2018-05-26 01:01:56 +00:00
|
|
|
requestVerificationSMS,
|
|
|
|
requestVerificationVoice,
|
|
|
|
sendMessages,
|
2018-10-18 01:01:21 +00:00
|
|
|
sendMessagesUnauth,
|
2018-05-26 01:01:56 +00:00
|
|
|
setSignedPreKey,
|
2018-12-13 19:12:33 +00:00
|
|
|
updateDeviceName,
|
2020-03-05 21:14:58 +00:00
|
|
|
whoami,
|
2020-05-27 21:37:06 +00:00
|
|
|
getConfig,
|
2018-05-26 01:01:56 +00:00
|
|
|
};
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
async function _ajax(param: AjaxOptionsType): Promise<any> {
|
2018-05-26 01:01:56 +00:00
|
|
|
if (!param.urlParameters) {
|
|
|
|
param.urlParameters = '';
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return _outerAjax(null, {
|
|
|
|
certificateAuthority,
|
|
|
|
contentType: 'application/json; charset=utf-8',
|
|
|
|
data: param.jsonData && _jsonThing(param.jsonData),
|
|
|
|
host: url,
|
|
|
|
password,
|
|
|
|
path: URL_CALLS[param.call] + param.urlParameters,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: param.responseType,
|
|
|
|
timeout: param.timeout,
|
|
|
|
type: param.httpType,
|
|
|
|
user: username,
|
|
|
|
validateResponse: param.validateResponse,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2018-10-18 01:01:21 +00:00
|
|
|
unauthenticated: param.unauthenticated,
|
|
|
|
accessKey: param.accessKey,
|
2020-03-31 20:03:38 +00:00
|
|
|
}).catch((e: Error) => {
|
2018-05-26 01:01:56 +00:00
|
|
|
const { code } = e;
|
|
|
|
if (code === 200) {
|
2020-03-31 20:03:38 +00:00
|
|
|
// Happens sometimes when we get no response. Might be nice to get 204 instead.
|
2018-05-26 01:01:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let message;
|
|
|
|
switch (code) {
|
|
|
|
case -1:
|
|
|
|
message =
|
|
|
|
'Failed to connect to the server, please check your network connection.';
|
|
|
|
break;
|
|
|
|
case 413:
|
|
|
|
message = 'Rate limit exceeded, please try again later.';
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
message = 'Invalid code, please try again.';
|
|
|
|
break;
|
|
|
|
case 417:
|
|
|
|
message = 'Number already registered.';
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
message =
|
|
|
|
'Invalid authentication, most likely someone re-registered and invalidated our registration.';
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
message = 'Number is not registered.';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message =
|
|
|
|
'The server rejected our query, please file a bug report.';
|
|
|
|
}
|
2018-11-09 01:23:07 +00:00
|
|
|
e.message = `${message} (original: ${e.message})`;
|
2018-05-26 01:01:56 +00:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function whoami() {
|
2020-03-05 21:14:58 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'whoami',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
async function getConfig() {
|
|
|
|
type ResType = {
|
|
|
|
config: Array<{ name: string; enabled: boolean }>;
|
|
|
|
};
|
|
|
|
const res: ResType = await _ajax({
|
|
|
|
call: 'config',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.config.filter(({ name }: { name: string }) =>
|
|
|
|
name.startsWith('desktop.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:51:37 +00:00
|
|
|
async function getSenderCertificate() {
|
2018-10-18 01:01:21 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'deliveryCert',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
2020-03-31 20:03:38 +00:00
|
|
|
validateResponse: { certificate: 'string' },
|
2020-05-07 20:51:37 +00:00
|
|
|
urlParameters: '?includeUuid=true',
|
2018-10-18 01:01:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function registerSupportForUnauthenticatedDelivery() {
|
2018-10-18 01:01:21 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'supportUnauthenticatedDelivery',
|
|
|
|
httpType: 'PUT',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function registerCapabilities(capabilities: any) {
|
2020-03-05 21:14:58 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'registerCapabilities',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: { capabilities },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:12:28 +00:00
|
|
|
function getProfileUrl(
|
|
|
|
identifier: string,
|
|
|
|
profileKeyVersion?: string,
|
|
|
|
profileKeyCredentialRequest?: string
|
|
|
|
) {
|
|
|
|
if (profileKeyVersion && profileKeyCredentialRequest) {
|
|
|
|
return `/${identifier}/${profileKeyVersion}/${profileKeyCredentialRequest}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `/${identifier}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getProfile(
|
|
|
|
identifier: string,
|
|
|
|
options: {
|
|
|
|
profileKeyVersion?: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
|
|
|
} = {}
|
|
|
|
) {
|
|
|
|
const { profileKeyVersion, profileKeyCredentialRequest } = options;
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'profile',
|
|
|
|
httpType: 'GET',
|
2020-04-15 23:12:28 +00:00
|
|
|
urlParameters: getProfileUrl(
|
|
|
|
identifier,
|
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest
|
|
|
|
),
|
2018-05-26 01:01:56 +00:00
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
2020-04-15 23:12:28 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getProfileUnauth(
|
|
|
|
identifier: string,
|
2020-04-15 23:12:28 +00:00
|
|
|
options: {
|
|
|
|
accessKey: string;
|
|
|
|
profileKeyVersion?: string;
|
|
|
|
profileKeyCredentialRequest?: string;
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
) {
|
2020-04-15 23:12:28 +00:00
|
|
|
const {
|
|
|
|
accessKey,
|
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest,
|
|
|
|
} = options;
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'profile',
|
|
|
|
httpType: 'GET',
|
2020-04-15 23:12:28 +00:00
|
|
|
urlParameters: getProfileUrl(
|
|
|
|
identifier,
|
|
|
|
profileKeyVersion,
|
|
|
|
profileKeyCredentialRequest
|
|
|
|
),
|
2018-10-18 01:01:21 +00:00
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
|
|
|
});
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getAvatar(path: string) {
|
2018-05-26 01:01:56 +00:00
|
|
|
// Using _outerAJAX, since it's not hardcoded to the Signal Server. Unlike our
|
|
|
|
// attachment CDN, it uses our self-signed certificate, so we pass it in.
|
2020-04-17 22:51:39 +00:00
|
|
|
return _outerAjax(`${cdnUrlObject['0']}/${path}`, {
|
2018-05-26 01:01:56 +00:00
|
|
|
certificateAuthority,
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2018-05-26 01:01:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function requestVerificationSMS(number: string) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'accounts',
|
|
|
|
httpType: 'GET',
|
|
|
|
urlParameters: `/sms/code/${number}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function requestVerificationVoice(number: string) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'accounts',
|
|
|
|
httpType: 'GET',
|
|
|
|
urlParameters: `/voice/code/${number}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function confirmCode(
|
2020-03-31 20:03:38 +00:00
|
|
|
number: string,
|
|
|
|
code: string,
|
|
|
|
newPassword: string,
|
2020-04-13 17:37:29 +00:00
|
|
|
registrationId: number,
|
|
|
|
deviceName?: string | null,
|
|
|
|
options: { accessKey?: ArrayBuffer } = {}
|
2018-05-26 01:01:56 +00:00
|
|
|
) {
|
2018-10-18 01:01:21 +00:00
|
|
|
const { accessKey } = options;
|
2020-03-31 20:03:38 +00:00
|
|
|
const jsonData: any = {
|
2020-05-07 20:51:37 +00:00
|
|
|
// tslint:disable-next-line: no-suspicious-comment
|
|
|
|
// TODO: uncomment this once we want to start registering UUID support
|
|
|
|
// capabilities: {
|
|
|
|
// uuid: true,
|
|
|
|
// },
|
2018-05-26 01:01:56 +00:00
|
|
|
fetchesMessages: true,
|
2020-05-05 19:46:42 +00:00
|
|
|
name: deviceName ? deviceName : undefined,
|
2018-05-26 01:01:56 +00:00
|
|
|
registrationId,
|
2020-05-05 19:46:42 +00:00
|
|
|
supportsSms: false,
|
2018-10-18 01:01:21 +00:00
|
|
|
unidentifiedAccessKey: accessKey
|
|
|
|
? _btoa(_getString(accessKey))
|
|
|
|
: undefined,
|
|
|
|
unrestrictedUnidentifiedAccess: false,
|
2018-05-26 01:01:56 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const call = deviceName ? 'devices' : 'accounts';
|
|
|
|
const urlPrefix = deviceName ? '/' : '/code/';
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
// We update our saved username and password, since we're creating a new account
|
|
|
|
username = number;
|
|
|
|
password = newPassword;
|
|
|
|
|
|
|
|
const response = await _ajax({
|
|
|
|
call,
|
|
|
|
httpType: 'PUT',
|
2020-03-05 21:14:58 +00:00
|
|
|
responseType: 'json',
|
2018-05-26 01:01:56 +00:00
|
|
|
urlParameters: urlPrefix + code,
|
|
|
|
jsonData,
|
|
|
|
});
|
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
// From here on out, our username will be our UUID or E164 combined with device
|
|
|
|
username = `${response.uuid || number}.${response.deviceId || 1}`;
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function updateDeviceName(deviceName: string) {
|
2018-12-13 19:12:33 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'updateDeviceName',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: {
|
|
|
|
deviceName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function removeSignalingKey() {
|
2019-01-11 16:53:35 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'removeSignalingKey',
|
|
|
|
httpType: 'DELETE',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getDevices() {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'devices',
|
|
|
|
httpType: 'GET',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type JSONSignedPreKeyType = {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type JSONKeysType = {
|
|
|
|
identityKey: string;
|
|
|
|
signedPreKey: JSONSignedPreKeyType;
|
|
|
|
preKeys: Array<{
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
}>;
|
|
|
|
lastResortKey: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
async function registerKeys(genKeys: KeysType) {
|
|
|
|
const preKeys = genKeys.preKeys.map(key => ({
|
|
|
|
keyId: key.keyId,
|
|
|
|
publicKey: _btoa(_getString(key.publicKey)),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const keys: JSONKeysType = {
|
|
|
|
identityKey: _btoa(_getString(genKeys.identityKey)),
|
|
|
|
signedPreKey: {
|
|
|
|
keyId: genKeys.signedPreKey.keyId,
|
|
|
|
publicKey: _btoa(_getString(genKeys.signedPreKey.publicKey)),
|
|
|
|
signature: _btoa(_getString(genKeys.signedPreKey.signature)),
|
|
|
|
},
|
|
|
|
preKeys,
|
|
|
|
// This is just to make the server happy (v2 clients should choke on publicKey)
|
|
|
|
lastResortKey: {
|
|
|
|
keyId: 0x7fffffff,
|
|
|
|
publicKey: _btoa('42'),
|
|
|
|
},
|
|
|
|
};
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
return _ajax({
|
|
|
|
call: 'keys',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: keys,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function setSignedPreKey(signedPreKey: SignedPreKeyType) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'signed',
|
|
|
|
httpType: 'PUT',
|
|
|
|
jsonData: {
|
|
|
|
keyId: signedPreKey.keyId,
|
|
|
|
publicKey: _btoa(_getString(signedPreKey.publicKey)),
|
|
|
|
signature: _btoa(_getString(signedPreKey.signature)),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type ServerKeyCountType = {
|
|
|
|
count: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getMyKeys(): Promise<number> {
|
|
|
|
const result: ServerKeyCountType = await _ajax({
|
2018-05-26 01:01:56 +00:00
|
|
|
call: 'keys',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
validateResponse: { count: 'number' },
|
2020-03-31 20:03:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return result.count;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type ServerKeyResponseType = {
|
|
|
|
devices: Array<{
|
|
|
|
deviceId: number;
|
|
|
|
registrationId: number;
|
|
|
|
signedPreKey: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
|
|
|
preKey?: {
|
|
|
|
keyId: number;
|
|
|
|
publicKey: string;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
identityKey: string;
|
|
|
|
};
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
function handleKeys(res: ServerKeyResponseType): ServerKeysType {
|
2018-10-18 01:01:21 +00:00
|
|
|
if (!Array.isArray(res.devices)) {
|
|
|
|
throw new Error('Invalid response');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
const devices = res.devices.map(device => {
|
2018-10-18 01:01:21 +00:00
|
|
|
if (
|
|
|
|
!_validateResponse(device, { signedPreKey: 'object' }) ||
|
|
|
|
!_validateResponse(device.signedPreKey, {
|
|
|
|
publicKey: 'string',
|
|
|
|
signature: 'string',
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid signedPreKey');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
let preKey;
|
2018-10-18 01:01:21 +00:00
|
|
|
if (device.preKey) {
|
|
|
|
if (
|
|
|
|
!_validateResponse(device, { preKey: 'object' }) ||
|
|
|
|
!_validateResponse(device.preKey, { publicKey: 'string' })
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid preKey');
|
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
preKey = {
|
|
|
|
keyId: device.preKey.keyId,
|
|
|
|
publicKey: _base64ToBytes(device.preKey.publicKey),
|
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
deviceId: device.deviceId,
|
|
|
|
registrationId: device.registrationId,
|
|
|
|
preKey,
|
|
|
|
signedPreKey: {
|
|
|
|
keyId: device.signedPreKey.keyId,
|
|
|
|
publicKey: _base64ToBytes(device.signedPreKey.publicKey),
|
|
|
|
signature: _base64ToBytes(device.signedPreKey.signature),
|
|
|
|
},
|
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
});
|
2020-03-31 20:03:38 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
devices,
|
|
|
|
identityKey: _base64ToBytes(res.identityKey),
|
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
async function getKeysForIdentifier(identifier: string, deviceId?: number) {
|
2018-05-26 01:01:56 +00:00
|
|
|
return _ajax({
|
|
|
|
call: 'keys',
|
|
|
|
httpType: 'GET',
|
2020-04-13 17:37:29 +00:00
|
|
|
urlParameters: `/${identifier}/${deviceId || '*'}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
responseType: 'json',
|
|
|
|
validateResponse: { identityKey: 'string', devices: 'object' },
|
2018-10-18 01:01:21 +00:00
|
|
|
}).then(handleKeys);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getKeysForIdentifierUnauth(
|
|
|
|
identifier: string,
|
2020-04-13 17:37:29 +00:00
|
|
|
deviceId?: number,
|
2020-03-31 20:03:38 +00:00
|
|
|
{ accessKey }: { accessKey?: string } = {}
|
2018-10-18 01:01:21 +00:00
|
|
|
) {
|
|
|
|
return _ajax({
|
|
|
|
call: 'keys',
|
|
|
|
httpType: 'GET',
|
2020-04-13 17:37:29 +00:00
|
|
|
urlParameters: `/${identifier}/${deviceId || '*'}`,
|
2018-10-18 01:01:21 +00:00
|
|
|
responseType: 'json',
|
|
|
|
validateResponse: { identityKey: 'string', devices: 'object' },
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
|
|
|
}).then(handleKeys);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function sendMessagesUnauth(
|
|
|
|
destination: string,
|
|
|
|
messageArray: Array<MessageType>,
|
|
|
|
timestamp: number,
|
2020-04-13 17:37:29 +00:00
|
|
|
silent?: boolean,
|
|
|
|
online?: boolean,
|
2020-03-31 20:03:38 +00:00
|
|
|
{ accessKey }: { accessKey?: string } = {}
|
2018-10-18 01:01:21 +00:00
|
|
|
) {
|
2020-03-31 20:03:38 +00:00
|
|
|
const jsonData: any = { messages: messageArray, timestamp };
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
if (silent) {
|
|
|
|
jsonData.silent = true;
|
|
|
|
}
|
2018-11-14 19:10:32 +00:00
|
|
|
if (online) {
|
|
|
|
jsonData.online = true;
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
return _ajax({
|
|
|
|
call: 'messages',
|
|
|
|
httpType: 'PUT',
|
|
|
|
urlParameters: `/${destination}`,
|
|
|
|
jsonData,
|
|
|
|
responseType: 'json',
|
|
|
|
unauthenticated: true,
|
|
|
|
accessKey,
|
2018-05-26 01:01:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function sendMessages(
|
|
|
|
destination: string,
|
|
|
|
messageArray: Array<MessageType>,
|
|
|
|
timestamp: number,
|
2020-04-13 17:37:29 +00:00
|
|
|
silent?: boolean,
|
|
|
|
online?: boolean
|
2018-11-14 19:10:32 +00:00
|
|
|
) {
|
2020-03-31 20:03:38 +00:00
|
|
|
const jsonData: any = { messages: messageArray, timestamp };
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
if (silent) {
|
|
|
|
jsonData.silent = true;
|
|
|
|
}
|
2018-11-14 19:10:32 +00:00
|
|
|
if (online) {
|
|
|
|
jsonData.online = true;
|
|
|
|
}
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
return _ajax({
|
|
|
|
call: 'messages',
|
|
|
|
httpType: 'PUT',
|
|
|
|
urlParameters: `/${destination}`,
|
|
|
|
jsonData,
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
function redactStickerUrl(stickerUrl: string) {
|
2019-05-16 22:32:11 +00:00
|
|
|
return stickerUrl.replace(
|
|
|
|
/(\/stickers\/)([^/]+)(\/)/,
|
2020-03-31 20:03:38 +00:00
|
|
|
(_, begin: string, packId: string, end: string) =>
|
|
|
|
`${begin}${redactPackId(packId)}${end}`
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getSticker(packId: string, stickerId: string) {
|
2020-04-17 22:51:39 +00:00
|
|
|
return _outerAjax(
|
|
|
|
`${cdnUrlObject['0']}/stickers/${packId}/full/${stickerId}`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
type: 'GET',
|
|
|
|
redactUrl: redactStickerUrl,
|
|
|
|
version,
|
|
|
|
}
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function getStickerPackManifest(packId: string) {
|
2020-04-17 22:51:39 +00:00
|
|
|
return _outerAjax(
|
|
|
|
`${cdnUrlObject['0']}/stickers/${packId}/manifest.proto`,
|
|
|
|
{
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
type: 'GET',
|
|
|
|
redactUrl: redactStickerUrl,
|
|
|
|
version,
|
|
|
|
}
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
type ServerAttachmentType = {
|
|
|
|
key: string;
|
|
|
|
credential: string;
|
|
|
|
acl: string;
|
|
|
|
algorithm: string;
|
|
|
|
date: string;
|
|
|
|
policy: string;
|
|
|
|
signature: string;
|
|
|
|
};
|
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
function makePutParams(
|
2020-03-31 20:03:38 +00:00
|
|
|
{
|
|
|
|
key,
|
|
|
|
credential,
|
|
|
|
acl,
|
|
|
|
algorithm,
|
|
|
|
date,
|
|
|
|
policy,
|
|
|
|
signature,
|
|
|
|
}: ServerAttachmentType,
|
|
|
|
encryptedBin: ArrayBuffer
|
2019-12-17 20:25:57 +00:00
|
|
|
) {
|
2019-05-08 20:14:52 +00:00
|
|
|
// Note: when using the boundary string in the POST body, it needs to be prefixed by
|
|
|
|
// an extra --, and the final boundary string at the end gets a -- prefix and a --
|
|
|
|
// suffix.
|
|
|
|
const boundaryString = `----------------${getGuid().replace(/-/g, '')}`;
|
|
|
|
const CRLF = '\r\n';
|
2020-03-31 20:03:38 +00:00
|
|
|
const getSection = (name: string, value: string) =>
|
2019-05-08 20:14:52 +00:00
|
|
|
[
|
|
|
|
`--${boundaryString}`,
|
|
|
|
`Content-Disposition: form-data; name="${name}"${CRLF}`,
|
|
|
|
value,
|
|
|
|
].join(CRLF);
|
|
|
|
|
|
|
|
const start = [
|
|
|
|
getSection('key', key),
|
|
|
|
getSection('x-amz-credential', credential),
|
|
|
|
getSection('acl', acl),
|
|
|
|
getSection('x-amz-algorithm', algorithm),
|
|
|
|
getSection('x-amz-date', date),
|
|
|
|
getSection('policy', policy),
|
|
|
|
getSection('x-amz-signature', signature),
|
|
|
|
getSection('Content-Type', 'application/octet-stream'),
|
|
|
|
`--${boundaryString}`,
|
|
|
|
'Content-Disposition: form-data; name="file"',
|
|
|
|
`Content-Type: application/octet-stream${CRLF}${CRLF}`,
|
|
|
|
].join(CRLF);
|
|
|
|
const end = `${CRLF}--${boundaryString}--${CRLF}`;
|
|
|
|
|
|
|
|
const startBuffer = Buffer.from(start, 'utf8');
|
|
|
|
const attachmentBuffer = Buffer.from(encryptedBin);
|
|
|
|
const endBuffer = Buffer.from(end, 'utf8');
|
|
|
|
|
|
|
|
const contentLength =
|
|
|
|
startBuffer.length + attachmentBuffer.length + endBuffer.length;
|
|
|
|
const data = Buffer.concat(
|
|
|
|
[startBuffer, attachmentBuffer, endBuffer],
|
|
|
|
contentLength
|
2018-05-26 01:01:56 +00:00
|
|
|
);
|
2019-05-08 20:14:52 +00:00
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
return {
|
|
|
|
data,
|
|
|
|
contentType: `multipart/form-data; boundary=${boundaryString}`,
|
|
|
|
headers: {
|
2020-03-31 20:03:38 +00:00
|
|
|
'Content-Length': contentLength.toString(),
|
2019-12-17 20:25:57 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function putStickers(
|
2020-03-31 20:03:38 +00:00
|
|
|
encryptedManifest: ArrayBuffer,
|
|
|
|
encryptedStickers: Array<ArrayBuffer>,
|
|
|
|
onProgress?: () => void
|
2019-12-17 20:25:57 +00:00
|
|
|
) {
|
|
|
|
// Get manifest and sticker upload parameters
|
|
|
|
const { packId, manifest, stickers } = await _ajax({
|
|
|
|
call: 'getStickerPackUpload',
|
|
|
|
responseType: 'json',
|
2020-03-31 20:03:38 +00:00
|
|
|
httpType: 'GET',
|
2019-12-17 20:25:57 +00:00
|
|
|
urlParameters: `/${encryptedStickers.length}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Upload manifest
|
|
|
|
const manifestParams = makePutParams(manifest, encryptedManifest);
|
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2020-04-17 22:51:39 +00:00
|
|
|
await _outerAjax(`${cdnUrlObject['0']}/`, {
|
2019-12-17 20:25:57 +00:00
|
|
|
...manifestParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-12-17 20:25:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Upload stickers
|
2019-12-19 23:27:02 +00:00
|
|
|
const queue = new PQueue({ concurrency: 3 });
|
2019-12-17 20:25:57 +00:00
|
|
|
await Promise.all(
|
2020-03-31 20:03:38 +00:00
|
|
|
stickers.map(async (sticker: ServerAttachmentType, index: number) => {
|
|
|
|
const stickerParams = makePutParams(
|
|
|
|
sticker,
|
|
|
|
encryptedStickers[index]
|
|
|
|
);
|
2019-12-19 23:27:02 +00:00
|
|
|
await queue.add(async () =>
|
2020-04-17 22:51:39 +00:00
|
|
|
_outerAjax(`${cdnUrlObject['0']}/`, {
|
2019-12-19 23:27:02 +00:00
|
|
|
...stickerParams,
|
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-12-19 23:27:02 +00:00
|
|
|
})
|
|
|
|
);
|
2019-12-17 20:25:57 +00:00
|
|
|
if (onProgress) {
|
|
|
|
onProgress();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Done!
|
|
|
|
return packId;
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:51:39 +00:00
|
|
|
async function getAttachment(cdnKey: string, cdnNumber: number) {
|
|
|
|
const cdnUrl = cdnUrlObject[cdnNumber] || cdnUrlObject['0'];
|
2019-12-17 20:25:57 +00:00
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2020-04-17 22:51:39 +00:00
|
|
|
return _outerAjax(`${cdnUrl}/attachments/${cdnKey}`, {
|
2019-12-17 20:25:57 +00:00
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
timeout: 0,
|
|
|
|
type: 'GET',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-12-17 20:25:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function putAttachment(encryptedBin: ArrayBuffer) {
|
2019-12-17 20:25:57 +00:00
|
|
|
const response = await _ajax({
|
|
|
|
call: 'attachmentId',
|
|
|
|
httpType: 'GET',
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
|
|
|
const { attachmentIdString } = response;
|
|
|
|
|
|
|
|
const params = makePutParams(response, encryptedBin);
|
|
|
|
|
2019-05-08 20:14:52 +00:00
|
|
|
// This is going to the CDN, not the service, so we use _outerAjax
|
2020-04-17 22:51:39 +00:00
|
|
|
await _outerAjax(`${cdnUrlObject['0']}/attachments/`, {
|
2019-12-17 20:25:57 +00:00
|
|
|
...params,
|
2019-05-08 20:14:52 +00:00
|
|
|
certificateAuthority,
|
|
|
|
proxyUrl,
|
|
|
|
timeout: 0,
|
|
|
|
type: 'POST',
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-05-08 20:14:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return attachmentIdString;
|
2018-05-26 01:01:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function getHeaderPadding() {
|
2020-03-31 20:03:38 +00:00
|
|
|
const max = getRandomValue(1, 64);
|
2019-05-16 22:32:11 +00:00
|
|
|
let characters = '';
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
for (let i = 0; i < max; i += 1) {
|
|
|
|
characters += String.fromCharCode(getRandomValue(65, 122));
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return characters;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
async function makeProxiedRequest(
|
|
|
|
targetUrl: string,
|
|
|
|
options: ProxiedRequestOptionsType = {}
|
|
|
|
) {
|
2019-01-16 03:03:56 +00:00
|
|
|
const { returnArrayBuffer, start, end } = options;
|
2020-03-31 20:03:38 +00:00
|
|
|
const headers: HeaderListType = {
|
2019-05-16 22:32:11 +00:00
|
|
|
'X-SignalPadding': getHeaderPadding(),
|
|
|
|
};
|
2019-01-16 03:03:56 +00:00
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
if (is.number(start) && is.number(end)) {
|
2019-05-16 22:32:11 +00:00
|
|
|
headers.Range = `bytes=${start}-${end}`;
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
const result = await _outerAjax(targetUrl, {
|
|
|
|
responseType: returnArrayBuffer ? 'arraybufferwithdetails' : undefined,
|
2019-01-16 03:03:56 +00:00
|
|
|
proxyUrl: contentProxyUrl,
|
|
|
|
type: 'GET',
|
|
|
|
redirect: 'follow',
|
2019-05-16 22:32:11 +00:00
|
|
|
redactUrl: () => '[REDACTED_URL]',
|
2019-01-16 03:03:56 +00:00
|
|
|
headers,
|
2020-01-17 00:53:35 +00:00
|
|
|
version,
|
2019-01-16 03:03:56 +00:00
|
|
|
});
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
if (!returnArrayBuffer) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { response } = result;
|
|
|
|
if (!response.headers || !response.headers.get) {
|
|
|
|
throw new Error('makeProxiedRequest: Problem retrieving header value');
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-backbone-get-set-outside-model
|
2019-05-16 22:32:11 +00:00
|
|
|
const range = response.headers.get('content-range');
|
|
|
|
const match = PARSE_RANGE_HEADER.exec(range);
|
|
|
|
|
|
|
|
if (!match || !match[1]) {
|
|
|
|
throw new Error(
|
|
|
|
`makeProxiedRequest: Unable to parse total size from ${range}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const totalSize = parseInt(match[1], 10);
|
|
|
|
|
|
|
|
return {
|
|
|
|
totalSize,
|
|
|
|
result,
|
|
|
|
};
|
2019-01-16 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-26 01:01:56 +00:00
|
|
|
function getMessageSocket() {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info('opening message socket', url);
|
2018-05-26 01:01:56 +00:00
|
|
|
const fixedScheme = url
|
|
|
|
.replace('https://', 'wss://')
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-http-string
|
2018-05-26 01:01:56 +00:00
|
|
|
.replace('http://', 'ws://');
|
|
|
|
const login = encodeURIComponent(username);
|
|
|
|
const pass = encodeURIComponent(password);
|
2020-01-17 00:53:35 +00:00
|
|
|
const clientVersion = encodeURIComponent(version);
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
return _createSocket(
|
2020-01-17 00:53:35 +00:00
|
|
|
`${fixedScheme}/v1/websocket/?login=${login}&password=${pass}&agent=OWD&version=${clientVersion}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
{ certificateAuthority, proxyUrl }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProvisioningSocket() {
|
2020-03-31 20:03:38 +00:00
|
|
|
window.log.info('opening provisioning socket', url);
|
2018-05-26 01:01:56 +00:00
|
|
|
const fixedScheme = url
|
|
|
|
.replace('https://', 'wss://')
|
2020-03-31 20:03:38 +00:00
|
|
|
// tslint:disable-next-line no-http-string
|
2018-05-26 01:01:56 +00:00
|
|
|
.replace('http://', 'ws://');
|
2020-01-17 00:53:35 +00:00
|
|
|
const clientVersion = encodeURIComponent(version);
|
2018-05-26 01:01:56 +00:00
|
|
|
|
|
|
|
return _createSocket(
|
2020-01-17 00:53:35 +00:00
|
|
|
`${fixedScheme}/v1/websocket/provisioning/?agent=OWD&version=${clientVersion}`,
|
2018-05-26 01:01:56 +00:00
|
|
|
{ certificateAuthority, proxyUrl }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|