Browse Source

t.me web sync

master
Eduard Kuzmenko 3 years ago
parent
commit
7c9b5b8d6c
  1. 6
      src/config/app.ts
  2. 2
      src/helpers/date.ts
  3. 8
      src/index.ts
  4. 4
      src/lib/mtproto/apiManager.ts
  5. 79
      src/lib/mtproto/mtproto.ts
  6. 2
      src/lib/mtproto/mtprotoworker.ts
  7. 67
      src/lib/mtproto/telegramMeWebManager.ts
  8. 6
      src/lib/sessionStorage.ts

6
src/config/app.ts

@ -11,6 +11,8 @@ @@ -11,6 +11,8 @@
import type { DcId } from "../types";
export const MAIN_DOMAIN = 'web.telegram.org';
const App = {
id: 1025907,
hash: '452b0359b988148995f22ff0f4229750',
@ -18,9 +20,9 @@ const App = { @@ -18,9 +20,9 @@ const App = {
langPackVersion: '0.2.9',
langPack: 'macos',
langPackCode: 'en',
domains: [] as string[],
domains: [MAIN_DOMAIN] as string[],
baseDcId: 2 as DcId,
isMainDomain: location.hostname === 'web.telegram.org',
isMainDomain: location.hostname === MAIN_DOMAIN,
suffix: 'K'
};

2
src/helpers/date.ts

@ -93,7 +93,7 @@ export const getFullDate = (date: Date, options: Partial<{ @@ -93,7 +93,7 @@ export const getFullDate = (date: Date, options: Partial<{
export function tsNow(seconds?: true) {
const t = Date.now();
return seconds ? Math.floor(t / 1000) : t;
return seconds ? t / 1000 | 0 : t;
}
// https://github.com/DrKLO/Telegram/blob/d52b2c921abd3c1e8d6368858313ad0cb0468c07/TMessagesProj/src/main/java/org/telegram/ui/Adapters/FiltersView.java

8
src/index.ts

@ -326,7 +326,13 @@ console.timeEnd('get storage1'); */ @@ -326,7 +326,13 @@ console.timeEnd('get storage1'); */
}
try {
(await import('./lib/mtproto/webPushApiManager')).default.forceUnsubscribe();
await Promise.all([
import('./lib/mtproto/telegramMeWebManager'),
import('./lib/mtproto/webPushApiManager')
]).then(([meModule, pushModule]) => {
meModule.default.setAuthorized(false);
pushModule.default.forceUnsubscribe();
});
} catch(err) {
}

4
src/lib/mtproto/apiManager.ts

@ -88,7 +88,7 @@ export class ApiManager { @@ -88,7 +88,7 @@ export class ApiManager {
//private lol = false;
constructor() {
// constructor() {
//MtpSingleInstanceService.start();
/* AppStorage.get<number>('dc').then((dcId) => {
@ -96,7 +96,7 @@ export class ApiManager { @@ -96,7 +96,7 @@ export class ApiManager {
this.baseDcId = dcId;
}
}); */
}
// }
/* public telegramMeNotify(newValue: boolean) {
if(this.telegramMeNotified !== newValue) {

79
src/lib/mtproto/mtproto.ts

@ -1,79 +0,0 @@ @@ -1,79 +0,0 @@
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*
* Originally from:
* https://github.com/zhukov/webogram
* Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
* https://github.com/zhukov/webogram/blob/master/LICENSE
*/
/* import PasswordManager from './passwordManager';
import DcConfigurator from './dcConfigurator';
import RSAKeysManager from './rsaKeysManager';
import TimeManager from './timeManager';
import ServerTimeManager from './serverTimeManager';
import Authorizer from './authorizer';
import NetworkerFactory from './networkerFactory';
import ApiManager from './apiManager';
import ApiFileManager from './apiFileManager'; */
/* export class TelegramMeWebService {
public disabled = Modes.test ||
App.domains.indexOf(location.hostname) === -1 ||
location.protocol !== 'http:' && location.protocol !== 'https:' ||
location.protocol === 'https:' && location.hostname !== 'web.telegram.org';
public setAuthorized(canRedirect: boolean) {
if(this.disabled) {
return false;
}
sessionStorage.get('tgme_sync').then((curValue) => {
var ts = Date.now() / 1000;
if(canRedirect &&
curValue &&
curValue.canRedirect === canRedirect &&
curValue.ts + 86400 > ts) {
return false;
}
sessionStorage.set({tgme_sync: {canRedirect: canRedirect, ts: ts}});
var urls = [
'//telegram.me/_websync_?authed=' + (canRedirect ? '1' : '0'),
'//t.me/_websync_?authed=' + (canRedirect ? '1' : '0')
];
urls.forEach(url => {
let script = document.createElement('script');
script.onload = script.onerror = function() {
script.remove();
};
script.src = url;
document.body.appendChild(script);
});
});
}
}
export const telegramMeWebService = new TelegramMeWebService(); */
/* export namespace MTProto {
//$($window).on('click keydown', rng_seed_time); // WARNING!
export const passwordManager = PasswordManager;
export const dcConfigurator = DcConfigurator;
export const rsaKeysManager = RSAKeysManager;
export const timeManager = TimeManager;
export const authorizer = Authorizer;
export const networkerFactory = NetworkerFactory;
export const apiManager = ApiManager;
export const apiFileManager = ApiFileManager;
export const serverTimeManager = ServerTimeManager;
}
//(window as any).MTProto = MTProto; */
export default null;

2
src/lib/mtproto/mtprotoworker.ts

@ -27,6 +27,7 @@ import webPushApiManager from './webPushApiManager'; @@ -27,6 +27,7 @@ import webPushApiManager from './webPushApiManager';
import AppStorage from '../storage';
import appRuntimeManager from '../appManagers/appRuntimeManager';
import { SocketProxyTask } from './transports/socketProxied';
import telegramMeWebManager from './telegramMeWebManager';
type Task = {
taskId: number,
@ -102,6 +103,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -102,6 +103,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
Promise.all([
AppStorage.toggleStorage(false),
sessionStorage.clear(),
telegramMeWebManager.setAuthorized(false),
webPushApiManager.forceUnsubscribe()
]).finally(() => {
appRuntimeManager.reload();

67
src/lib/mtproto/telegramMeWebManager.ts

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*
* Originally from:
* https://github.com/zhukov/webogram
* Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
* https://github.com/zhukov/webogram/blob/master/LICENSE
*/
import App from "../../config/app";
import { MOUNT_CLASS_TO } from "../../config/debug";
import Modes from "../../config/modes";
import { tsNow } from "../../helpers/date";
import sessionStorage from '../sessionStorage';
export class TelegramMeWebManager {
private disabled = /* false && */(Modes.test || App.domains.indexOf(location.hostname) === -1);
public async setAuthorized(canRedirect: boolean) {
if(this.disabled) {
return;
}
sessionStorage.get('tgme_sync').then((curValue) => {
const ts = tsNow(true);
if(canRedirect &&
curValue &&
curValue.canRedirect === canRedirect &&
(curValue.ts + 86400) > ts) {
return;
}
sessionStorage.set({
tgme_sync: {
canRedirect,
ts
}
});
const urls = [
'//telegram.me/_websync_?authed=' + (canRedirect ? '1' : '0'),
'//t.me/_websync_?authed=' + (canRedirect ? '1' : '0')
];
const promises = urls.map(url => {
const script = document.createElement('script');
const promise = new Promise<void>((resolve) => {
script.onload = script.onerror = () => {
script.remove();
resolve();
};
});
script.src = url;
document.body.appendChild(script);
return promise;
});
return Promise.all(promises);
});
}
}
const telegramMeWebManager = new TelegramMeWebManager();
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.telegramMeWebManager = telegramMeWebManager);
export default telegramMeWebManager;

6
src/lib/sessionStorage.ts

@ -25,7 +25,11 @@ const sessionStorage = new LocalStorageController<{ @@ -25,7 +25,11 @@ const sessionStorage = new LocalStorageController<{
dc5_server_salt: string,
server_time_offset: number,
xt_instance: AppInstance,
kz_version: 'K' | 'Z'
kz_version: 'K' | 'Z',
tgme_sync: {
canRedirect: boolean,
ts: number
}
}>(/* ['kz_version'] */);
MOUNT_CLASS_TO.appStorage = sessionStorage;
export default sessionStorage;

Loading…
Cancel
Save