Support legacy app sessions

This commit is contained in:
Alexander Zinchuk 2021-06-11 01:19:20 +03:00
parent ab3a00f29a
commit 9f6ba21a53
7 changed files with 49 additions and 13 deletions

View File

@ -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<StateProps & DispatchProps> = ({ authState, disconnect }) => {
}
}
return localStorage.getItem(GRAMJS_SESSION_ID_KEY) ? renderMain() : <Auth />;
const hasSession = localStorage.getItem(GRAMJS_SESSION_ID_KEY);
const hasLegacySession = localStorage.getItem(LEGACY_SESSION_KEY);
return (hasSession || hasLegacySession) ? renderMain() : <Auth />;
};
function renderMain() {

View File

@ -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,

View File

@ -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<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>): MethodResponse<T> {

View File

@ -20,7 +20,7 @@ const requestStatesByCallback = new Map<AnyToVoidFunction, RequestStates>();
// 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],
});
}

View File

@ -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';

View File

@ -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;

View File

@ -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) => {