From 7c9b5b8d6c77d1c3941338ee5cb2aad2652b9a33 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Mon, 28 Jun 2021 16:41:42 +0300 Subject: [PATCH] t.me web sync --- src/config/app.ts | 6 +- src/helpers/date.ts | 2 +- src/index.ts | 8 ++- src/lib/mtproto/apiManager.ts | 4 +- src/lib/mtproto/mtproto.ts | 79 ------------------------- src/lib/mtproto/mtprotoworker.ts | 2 + src/lib/mtproto/telegramMeWebManager.ts | 67 +++++++++++++++++++++ src/lib/sessionStorage.ts | 6 +- 8 files changed, 88 insertions(+), 86 deletions(-) delete mode 100644 src/lib/mtproto/mtproto.ts create mode 100644 src/lib/mtproto/telegramMeWebManager.ts diff --git a/src/config/app.ts b/src/config/app.ts index ffd854ad..ac6c8e34 100644 --- a/src/config/app.ts +++ b/src/config/app.ts @@ -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 = { 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' }; diff --git a/src/helpers/date.ts b/src/helpers/date.ts index cbcef992..129efc71 100644 --- a/src/helpers/date.ts +++ b/src/helpers/date.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index 8244ea69..804b32e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { } diff --git a/src/lib/mtproto/apiManager.ts b/src/lib/mtproto/apiManager.ts index 66fd949a..660104df 100644 --- a/src/lib/mtproto/apiManager.ts +++ b/src/lib/mtproto/apiManager.ts @@ -88,7 +88,7 @@ export class ApiManager { //private lol = false; - constructor() { + // constructor() { //MtpSingleInstanceService.start(); /* AppStorage.get('dc').then((dcId) => { @@ -96,7 +96,7 @@ export class ApiManager { this.baseDcId = dcId; } }); */ - } + // } /* public telegramMeNotify(newValue: boolean) { if(this.telegramMeNotified !== newValue) { diff --git a/src/lib/mtproto/mtproto.ts b/src/lib/mtproto/mtproto.ts deleted file mode 100644 index 12dc6ba5..00000000 --- a/src/lib/mtproto/mtproto.ts +++ /dev/null @@ -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 - * 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; diff --git a/src/lib/mtproto/mtprotoworker.ts b/src/lib/mtproto/mtprotoworker.ts index 4d580891..eb29b51a 100644 --- a/src/lib/mtproto/mtprotoworker.ts +++ b/src/lib/mtproto/mtprotoworker.ts @@ -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 { Promise.all([ AppStorage.toggleStorage(false), sessionStorage.clear(), + telegramMeWebManager.setAuthorized(false), webPushApiManager.forceUnsubscribe() ]).finally(() => { appRuntimeManager.reload(); diff --git a/src/lib/mtproto/telegramMeWebManager.ts b/src/lib/mtproto/telegramMeWebManager.ts new file mode 100644 index 00000000..3e8c4408 --- /dev/null +++ b/src/lib/mtproto/telegramMeWebManager.ts @@ -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 + * 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((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; diff --git a/src/lib/sessionStorage.ts b/src/lib/sessionStorage.ts index 99798310..788c9cec 100644 --- a/src/lib/sessionStorage.ts +++ b/src/lib/sessionStorage.ts @@ -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;