diff --git a/src/App.tsx b/src/App.tsx index 9615e22cb..01668300e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,7 +3,9 @@ import React, { withGlobal } from './lib/teact/teactn'; import { GlobalActions, GlobalState } from './global/types'; -import { GRAMJS_SESSION_ID_KEY, INACTIVE_MARKER, PAGE_TITLE } from './config'; +import { + GRAMJS_SESSION_ID_KEY, INACTIVE_MARKER, LEGACY_SESSION_KEY, PAGE_TITLE, +} from './config'; import { pick } from './util/iteratees'; import { updateSizes } from './util/windowSize'; import { addActiveTabChangeListener } from './util/activeTabMonitor'; @@ -53,7 +55,10 @@ const App: FC = ({ authState, disconnect }) => { } } - return localStorage.getItem(GRAMJS_SESSION_ID_KEY) ? renderMain() : ; + const hasSession = localStorage.getItem(GRAMJS_SESSION_ID_KEY); + const hasLegacySession = localStorage.getItem(LEGACY_SESSION_KEY); + + return (hasSession || hasLegacySession) ? renderMain() : ; }; function renderMain() { diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 901b01723..b426c5b3b 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -27,7 +27,7 @@ let onUpdate: OnApiUpdate; let client: TelegramClient; let isConnected = false; -export async function init(sessionId: string, _onUpdate: OnApiUpdate) { +export async function init(sessionInfo: string, _onUpdate: OnApiUpdate) { onUpdate = _onUpdate; if (DEBUG) { @@ -36,8 +36,8 @@ export async function init(sessionId: string, _onUpdate: OnApiUpdate) { } const session = IS_TEST - ? new sessions.LocalStorageSession(sessionId) - : new sessions.IdbSession(sessionId); + ? new sessions.LocalStorageSession(sessionInfo) + : new sessions.IdbSession(sessionInfo); client = new TelegramClient( session, diff --git a/src/api/gramjs/provider.ts b/src/api/gramjs/provider.ts index 42d50e7b5..e40ee106d 100644 --- a/src/api/gramjs/provider.ts +++ b/src/api/gramjs/provider.ts @@ -16,7 +16,7 @@ import * as methods from './methods'; let onUpdate: OnApiUpdate; -export async function initApi(_onUpdate: OnApiUpdate, sessionId = '') { +export async function initApi(_onUpdate: OnApiUpdate, sessionInfo = '') { onUpdate = _onUpdate; initUpdater(handleUpdate); @@ -28,7 +28,7 @@ export async function initApi(_onUpdate: OnApiUpdate, sessionId = '') { initManagement(handleUpdate); initTwoFaSettings(handleUpdate); - await initClient(sessionId, handleUpdate); + await initClient(sessionInfo, handleUpdate); } export function callApi(fnName: T, ...args: MethodArgs): MethodResponse { diff --git a/src/api/gramjs/worker/provider.ts b/src/api/gramjs/worker/provider.ts index 01ca0d518..40060e42f 100644 --- a/src/api/gramjs/worker/provider.ts +++ b/src/api/gramjs/worker/provider.ts @@ -20,7 +20,7 @@ const requestStatesByCallback = new Map(); // TODO Re-use `util/WorkerConnector.ts` -export function initApi(onUpdate: OnApiUpdate, sessionId = '') { +export function initApi(onUpdate: OnApiUpdate, sessionInfo = '') { if (!worker) { if (DEBUG) { // eslint-disable-next-line no-console @@ -33,7 +33,7 @@ export function initApi(onUpdate: OnApiUpdate, sessionId = '') { return makeRequest({ type: 'initApi', - args: [sessionId], + args: [sessionInfo], }); } diff --git a/src/config.ts b/src/config.ts index ad1fd39fc..b83426110 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,7 @@ export const DEBUG_ALERT_MSG = 'Shoot!\nSomething went wrong, please see the err export const DEBUG_GRAMJS = false; export const GRAMJS_SESSION_ID_KEY = 'GramJs:sessionId'; +export const LEGACY_SESSION_KEY = 'dc'; export const GLOBAL_STATE_CACHE_DISABLED = false; export const GLOBAL_STATE_CACHE_KEY = 'tt-global-state'; diff --git a/src/lib/gramjs/sessions/StorageSession.js b/src/lib/gramjs/sessions/StorageSession.js index 4956cfd4b..bb7db3355 100644 --- a/src/lib/gramjs/sessions/StorageSession.js +++ b/src/lib/gramjs/sessions/StorageSession.js @@ -3,12 +3,19 @@ const AuthKey = require('../crypto/AuthKey'); const utils = require('../Utils'); const STORAGE_KEY_BASE = 'GramJs-session-'; +const SESSION_DATA_PREFIX = 'session:'; class StorageSession extends MemorySession { - constructor(sessionId) { + constructor(sessionInfo) { super(); - this._storageKey = sessionId; + this._authKeys = {}; + + if (sessionInfo && sessionInfo.startsWith(SESSION_DATA_PREFIX)) { + void this._initFromSessionData(sessionInfo); + } else if (sessionInfo) { + this._storageKey = sessionInfo; + } } get authKey() { @@ -87,6 +94,19 @@ class StorageSession extends MemorySession { void this._updateStorage(); } + async _initFromSessionData(sessionData) { + const [, mainDcIdStr, mainDcKey] = sessionData.split(':'); + const mainDcId = Number(mainDcIdStr); + const { + ipAddress, + port, + } = utils.getDC(mainDcId); + this.setDC(mainDcId, ipAddress, port); + const authKey = new AuthKey(); + await authKey.setKey(Buffer.from(mainDcKey, 'hex'), true); + this.setAuthKey(authKey, mainDcId); + } + async _updateStorage() { if (!this._storageKey) { return; diff --git a/src/modules/actions/api/initial.ts b/src/modules/actions/api/initial.ts index 294137f7b..0f4c2a415 100644 --- a/src/modules/actions/api/initial.ts +++ b/src/modules/actions/api/initial.ts @@ -11,6 +11,7 @@ import { MEDIA_CACHE_NAME, MEDIA_CACHE_NAME_AVATARS, MEDIA_PROGRESSIVE_CACHE_NAME, + LEGACY_SESSION_KEY, } from '../../../config'; import { initApi, callApi } from '../../../api/gramjs'; import { unsubscribe } from '../../../util/notifications'; @@ -18,9 +19,18 @@ import * as cacheApi from '../../../util/cacheApi'; import { updateAppBadge } from '../../../util/appBadge'; addReducer('initApi', (global: GlobalState, actions) => { - const sessionId = localStorage.getItem(GRAMJS_SESSION_ID_KEY) || undefined; + let sessionInfo = localStorage.getItem(GRAMJS_SESSION_ID_KEY) || undefined; - void initApi(actions.apiUpdate, sessionId); + if (!sessionInfo) { + const legacySessionMainDc = localStorage.getItem(LEGACY_SESSION_KEY); + const legacySessionMainDcKey = localStorage.getItem(`dc${legacySessionMainDc}_auth_key`); + + if (legacySessionMainDc && legacySessionMainDcKey) { + sessionInfo = `session:${legacySessionMainDc}:${legacySessionMainDcKey}`; + } + } + + void initApi(actions.apiUpdate, sessionInfo); }); addReducer('setAuthPhoneNumber', (global, actions, payload) => {