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. 25
      src/lib/mtproto/transports/websocket.ts
  7. 4
      webpack.common.js

2
src/lib/mtproto/apiFileManager.ts

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

2
src/lib/mtproto/apiManager.ts

@ -162,7 +162,7 @@ export class ApiManager {
/// #if MTPROTO_HTTP_UPLOAD /// #if MTPROTO_HTTP_UPLOAD
// @ts-ignore // @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'; //const transportType: TransportType = connectionType !== 'client' ? 'https' : 'websocket';
/// #else /// #else
// @ts-ignore // @ts-ignore

67
src/lib/mtproto/dcConfigurator.ts

@ -9,6 +9,10 @@ import HTTP from './transports/http';
/// #elif !MTPROTO_HTTP /// #elif !MTPROTO_HTTP
// @ts-ignore // @ts-ignore
import Socket from './transports/websocket'; 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 /// #else
// @ts-ignore // @ts-ignore
import HTTP from './transports/http'; import HTTP from './transports/http';
@ -24,6 +28,9 @@ type Servers = {
} }
}; };
let socketId = 0;
const TEST_SUFFIX = Modes.test ? '_test' : '';
export class DcConfigurator { export class DcConfigurator {
private sslSubdomains = ['pluto', 'venus', 'aurora', 'vesta', 'flora']; private sslSubdomains = ['pluto', 'venus', 'aurora', 'vesta', 'flora'];
@ -46,10 +53,64 @@ export class DcConfigurator {
/// #if !MTPROTO_HTTP /// #if !MTPROTO_HTTP
private transportSocket = (dcId: number, connectionType: ConnectionType) => { private transportSocket = (dcId: number, connectionType: ConnectionType) => {
const subdomain = this.sslSubdomains[dcId - 1]; 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 chosenServer = 'wss://' + subdomain + '.web.telegram.org/' + path;
const suffix = connectionType === 'upload' ? '-U' : connectionType === 'download' ? '-D' : ''; const logSuffix = connectionType === 'upload' ? '-U' : connectionType === 'download' ? '-D' : '';
return new Socket(dcId, chosenServer, suffix, connectionType === 'client' ? 30000 : 10000);
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 /// #endif

21
src/lib/mtproto/mtprotoworker.ts

@ -1,4 +1,5 @@
import MTProtoWorker from 'worker-loader!./mtproto.worker'; import MTProtoWorker from 'worker-loader!./mtproto.worker';
import SocketWorker from 'worker-loader!./transports/websocket';
//import './mtproto.worker'; //import './mtproto.worker';
import { isObject } from '../../helpers/object'; import { isObject } from '../../helpers/object';
import type { MethodDeclMap } from '../../layer'; import type { MethodDeclMap } from '../../layer';
@ -57,6 +58,8 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
private debug = DEBUG; private debug = DEBUG;
private socketsWorkers: Map<number, SocketWorker> = new Map();
constructor() { constructor() {
super(); super();
this.log('constructor'); this.log('constructor');
@ -186,6 +189,24 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
} else { } else {
navigator.serviceWorker.controller.postMessage(task); 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')) { } else if(task.hasOwnProperty('result') || task.hasOwnProperty('error')) {
this.finalizeTask(task.taskId, task.result, task.error); this.finalizeTask(task.taskId, task.result, task.error);
} }

4
src/lib/mtproto/networker.ts

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

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

@ -6,6 +6,8 @@ import MTPNetworker from '../networker';
import { logger, LogLevels } from '../../logger'; import { logger, LogLevels } from '../../logger';
import Obfuscation from './obfuscation'; import Obfuscation from './obfuscation';
import { DEBUG, Modes } from '../mtproto_config'; import { DEBUG, Modes } from '../mtproto_config';
import { isWebWorker } from '../../../helpers/context';
import { ctx } from '../../../helpers/userAgent';
export default class Socket extends MTTransport { export default class Socket extends MTTransport {
public ws: WebSocket; public ws: WebSocket;
@ -254,3 +256,26 @@ 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');
const allowedIPs = ['194.58.97.147', '195.66.140.39', '127.0.0.1', '176.100.8.254']; 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 devMode = process.env.NODE_ENV !== 'production';
const useLocal = true; const useLocal = true;
const useLocalNotLocal = true; const useLocalNotLocal = false;
if(devMode) { if(devMode) {
console.log('DEVMODE IS ON!'); console.log('DEVMODE IS ON!');
@ -18,7 +18,7 @@ if(devMode) {
const opts = { const opts = {
MTPROTO_WORKER: true, MTPROTO_WORKER: true,
MTPROTO_HTTP: false, MTPROTO_HTTP: false,
MTPROTO_HTTP_UPLOAD: true, MTPROTO_HTTP_UPLOAD: false,
DEBUG: devMode, DEBUG: devMode,
version: 3, version: 3,
"ifdef-verbose": devMode, // add this for verbose output "ifdef-verbose": devMode, // add this for verbose output

Loading…
Cancel
Save