Browse Source

no comments [2]

master
morethanwords 4 years ago
parent
commit
4ea2844144
  1. 2
      src/lib/mtproto/apiFileManager.ts
  2. 2
      src/lib/mtproto/apiManager.ts
  3. 67
      src/lib/mtproto/dcConfigurator.ts
  4. 21
      src/lib/mtproto/mtprotoworker.ts
  5. 4
      src/lib/mtproto/networker.ts
  6. 27
      src/lib/mtproto/transports/websocket.ts
  7. 4
      webpack.common.js

2
src/lib/mtproto/apiFileManager.ts

@ -87,7 +87,7 @@ export class ApiFileManager { @@ -87,7 +87,7 @@ export class ApiFileManager {
public downloadCheck(dcId: string | number) {
const downloadPull = this.downloadPulls[dcId];
const downloadLimit = dcId === 'upload' ? 24 : 48;
const downloadLimit = dcId === 'upload' ? 256 : 48;
//const downloadLimit = Infinity;
if(this.downloadActives[dcId] >= downloadLimit || !downloadPull || !downloadPull.length) {

2
src/lib/mtproto/apiManager.ts

@ -162,7 +162,7 @@ export class ApiManager { @@ -162,7 +162,7 @@ export class ApiManager {
/// #if MTPROTO_HTTP_UPLOAD
// @ts-ignore
const transportType: TransportType = connectionType === 'upload' && isSafari && false ? 'https' : 'websocket';
const transportType: TransportType = connectionType === 'upload' && isSafari ? 'https' : 'websocket';
//const transportType: TransportType = connectionType !== 'client' ? 'https' : 'websocket';
/// #else
// @ts-ignore

67
src/lib/mtproto/dcConfigurator.ts

@ -9,6 +9,10 @@ import HTTP from './transports/http'; @@ -9,6 +9,10 @@ import HTTP from './transports/http';
/// #elif !MTPROTO_HTTP
// @ts-ignore
import Socket from './transports/websocket';
import { isSafari } from '../../helpers/userAgent';
import type MTPNetworker from './networker';
import { notifyAll, isWebWorker } from '../../helpers/context';
import { CancellablePromise, deferredPromise } from '../../helpers/cancellablePromise';
/// #else
// @ts-ignore
import HTTP from './transports/http';
@ -24,6 +28,9 @@ type Servers = { @@ -24,6 +28,9 @@ type Servers = {
}
};
let socketId = 0;
const TEST_SUFFIX = Modes.test ? '_test' : '';
export class DcConfigurator {
private sslSubdomains = ['pluto', 'venus', 'aurora', 'vesta', 'flora'];
@ -46,10 +53,64 @@ export class DcConfigurator { @@ -46,10 +53,64 @@ export class DcConfigurator {
/// #if !MTPROTO_HTTP
private transportSocket = (dcId: number, connectionType: ConnectionType) => {
const subdomain = this.sslSubdomains[dcId - 1];
const path = Modes.test ? 'apiws_test' : 'apiws';
const path = 'apiws' + TEST_SUFFIX;
const chosenServer = 'wss://' + subdomain + '.web.telegram.org/' + path;
const suffix = connectionType === 'upload' ? '-U' : connectionType === 'download' ? '-D' : '';
return new Socket(dcId, chosenServer, suffix, connectionType === 'client' ? 30000 : 10000);
const logSuffix = connectionType === 'upload' ? '-U' : connectionType === 'download' ? '-D' : '';
const retryTimeout = connectionType === 'client' ? 30000 : 10000;
if(isSafari && isWebWorker && false) {
class P extends MTTransport {
private id: number;
private taskId = 0;
public networker: MTPNetworker;
public promises: Map<number, CancellablePromise<Uint8Array>> = new Map();
constructor(dcId: number, url: string) {
super(dcId, url);
this.id = ++socketId;
notifyAll({
type: 'socketProxy',
payload: {
type: 'setup',
payload: {
dcId,
url,
logSuffix,
retryTimeout
},
id: this.id
}
});
}
send = (payload: Uint8Array) => {
const task: any = {
type: 'socketProxy',
payload: {
type: 'send',
payload,
id: this.id
}
};
if(this.networker) {
notifyAll(task);
return null;
}
task.payload.taskId = ++this.taskId;
const deferred = deferredPromise<Uint8Array>();
this.promises.set(task.id, deferred);
notifyAll(task);
return deferred;
};
}
return new P(dcId, chosenServer);
} else {
return new Socket(dcId, chosenServer, logSuffix, retryTimeout);
}
};
/// #endif

21
src/lib/mtproto/mtprotoworker.ts

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
import MTProtoWorker from 'worker-loader!./mtproto.worker';
import SocketWorker from 'worker-loader!./transports/websocket';
//import './mtproto.worker';
import { isObject } from '../../helpers/object';
import type { MethodDeclMap } from '../../layer';
@ -57,6 +58,8 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -57,6 +58,8 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
private debug = DEBUG;
private socketsWorkers: Map<number, SocketWorker> = new Map();
constructor() {
super();
this.log('constructor');
@ -186,6 +189,24 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -186,6 +189,24 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
} else {
navigator.serviceWorker.controller.postMessage(task);
}
} else if(task.type === 'socketProxy') {
const socketTask = task.payload;
const id = socketTask.id;
console.log('socketProxy', socketTask, id);
if(socketTask.type === 'send') {
const socketWorker = this.socketsWorkers.get(id);
socketWorker.postMessage(socketTask);
} else if(socketTask.type === 'setup') {
const socketWorker = new SocketWorker();
socketWorker.postMessage(socketTask);
socketWorker.addEventListener('message', (e) => {
const task = e.data;
});
this.socketsWorkers.set(id, socketWorker);
}
} else if(task.hasOwnProperty('result') || task.hasOwnProperty('error')) {
this.finalizeTask(task.taskId, task.result, task.error);
}

4
src/lib/mtproto/networker.ts

@ -20,13 +20,13 @@ import { isSafari } from '../../helpers/userAgent'; @@ -20,13 +20,13 @@ import { isSafari } from '../../helpers/userAgent';
// @ts-ignore
import HTTP from './transports/http';
// @ts-ignore
import Socket from './transports/websocket';
import type Socket from './transports/websocket';
/// #elif MTPROTO_HTTP
// @ts-ignore
import HTTP from './transports/http';
/// #else
// @ts-ignore
import Socket from './transports/websocket';
import type Socket from './transports/websocket';
/// #endif
//console.error('networker included!', new Error().stack);

27
src/lib/mtproto/transports/websocket.ts

@ -6,6 +6,8 @@ import MTPNetworker from '../networker'; @@ -6,6 +6,8 @@ import MTPNetworker from '../networker';
import { logger, LogLevels } from '../../logger';
import Obfuscation from './obfuscation';
import { DEBUG, Modes } from '../mtproto_config';
import { isWebWorker } from '../../../helpers/context';
import { ctx } from '../../../helpers/userAgent';
export default class Socket extends MTTransport {
public ws: WebSocket;
@ -253,4 +255,27 @@ export default class Socket extends MTTransport { @@ -253,4 +255,27 @@ export default class Socket extends MTTransport {
}
}
}
/* const setupSafariFix = () => {
};
if(isWebWorker) {
import('../../polyfill').then(() => {
//ctx.postMessage('ready');
let socket: Socket;
ctx.addEventListener('message', (e) => {
console.log('websocket worker message', e);
const task = e.data;
if(task.type === 'send') {
// const promise = socket.send(task.payload);
// if(task.taskId) {
// promise
// }
} else if(task.type === 'setup') {
socket = new Socket(task.dcId, task.url, task.logSuffix, task.retryTimeout);
}
});
});
} */

4
webpack.common.js

@ -9,7 +9,7 @@ const fs = require('fs'); @@ -9,7 +9,7 @@ const fs = require('fs');
const allowedIPs = ['194.58.97.147', '195.66.140.39', '127.0.0.1', '176.100.8.254'];
const devMode = process.env.NODE_ENV !== 'production';
const useLocal = true;
const useLocalNotLocal = true;
const useLocalNotLocal = false;
if(devMode) {
console.log('DEVMODE IS ON!');
@ -18,7 +18,7 @@ if(devMode) { @@ -18,7 +18,7 @@ if(devMode) {
const opts = {
MTPROTO_WORKER: true,
MTPROTO_HTTP: false,
MTPROTO_HTTP_UPLOAD: true,
MTPROTO_HTTP_UPLOAD: false,
DEBUG: devMode,
version: 3,
"ifdef-verbose": devMode, // add this for verbose output

Loading…
Cancel
Save