ask auth for headless if there is no cookies, add download logs button for headless
This commit is contained in:
1 parent
08bfbdaed7
commit
870118d5ab
9 files changed
+179
-24
No files matched your search
@@ -13,6 +13,11 @@ export const VK_API_BASE_URL = 'https://api.vk.com/method';
|
||||
export const VK_IM_URL = 'https://vk.com/im';
|
||||
export const TELEMOST_URL = 'https://telemost.yandex.ru/';
|
||||
|
||||
export const VK_LOGIN_URL = 'https://vk.com/';
|
||||
export const YANDEX_LOGIN_URL = 'https://passport.yandex.ru/auth?retpath=https%3A%2F%2Ftelemost.yandex.ru%2F';
|
||||
export const VK_AUTH_COOKIE = 'remixsid';
|
||||
export const YANDEX_AUTH_COOKIE = 'Session_id';
|
||||
|
||||
export const SESSION_PARTITION = 'persist:creator';
|
||||
export const WINDOW_WIDTH = 1200;
|
||||
export const WINDOW_HEIGHT = 800;
|
||||
@@ -52,6 +57,8 @@ export enum IPC {
|
||||
CLOSE_BOT_TAB = 'close-bot-tab',
|
||||
BOT_ERROR = 'bot-error',
|
||||
SEND_BOT_CALL_LINK = 'send-bot-call-link',
|
||||
LOGIN_REQUIRED = 'login-required',
|
||||
LOGIN_DONE = 'login-done',
|
||||
}
|
||||
|
||||
export const LOG_CAPTURE_SNIPPET = [
|
||||
@@ -69,7 +76,6 @@ export const LOG_CAPTURE_SNIPPET = [
|
||||
export enum HeadlessLogMarker {
|
||||
CALL_CREATED = 'CALL CREATED',
|
||||
JOIN_LINK = 'join_link:',
|
||||
SHORT_LINK = 'short:',
|
||||
TURN = 'TURN:',
|
||||
PROTOCOL = 'protocol:',
|
||||
TUNNEL_CONNECTED = 'TUNNEL CONNECTED',
|
||||
|
||||
@@ -12,6 +12,10 @@ import {
|
||||
SESSION_PARTITION,
|
||||
VK_COOKIE_DOMAINS,
|
||||
YANDEX_COOKIE_DOMAINS,
|
||||
VK_LOGIN_URL,
|
||||
YANDEX_LOGIN_URL,
|
||||
VK_AUTH_COOKIE,
|
||||
YANDEX_AUTH_COOKIE,
|
||||
LOG_CAPTURE_SNIPPET,
|
||||
} from '../constants';
|
||||
import { BotManager } from '../bot/bot-manager';
|
||||
@@ -177,7 +181,11 @@ export class TabManager {
|
||||
}
|
||||
}
|
||||
|
||||
private attachProcessOutput(proc: ChildProcess, tabId: string): void {
|
||||
private attachProcessOutput(
|
||||
proc: ChildProcess,
|
||||
tabId: string,
|
||||
inspect?: (msg: string) => void,
|
||||
): void {
|
||||
const onData = (data: Buffer) => {
|
||||
data
|
||||
.toString()
|
||||
@@ -187,6 +195,7 @@ export class TabManager {
|
||||
if (!msg) return;
|
||||
console.log(`[relay:${tabId}]`, msg);
|
||||
this.sendLog(tabId, msg);
|
||||
if (inspect) inspect(msg);
|
||||
});
|
||||
};
|
||||
proc.stdout?.on('data', onData);
|
||||
@@ -240,14 +249,24 @@ export class TabManager {
|
||||
|
||||
const isTelemost = platform === Platform.Telemost;
|
||||
tab.tunnelMode = isTelemost ? TunnelMode.HeadlessTelemost : TunnelMode.HeadlessVK;
|
||||
const cookies = isTelemost
|
||||
? await this.getYandexCookies()
|
||||
: await this.getVKCookies();
|
||||
if (cookies.length === 0) {
|
||||
const name = isTelemost ? 'Yandex' : 'VK';
|
||||
this.sendLog(tabId, `No ${name} cookies found. Please log into ${name} first.`);
|
||||
return;
|
||||
const authCookie = isTelemost ? YANDEX_AUTH_COOKIE : VK_AUTH_COOKIE;
|
||||
const loginUrl = isTelemost ? YANDEX_LOGIN_URL : VK_LOGIN_URL;
|
||||
const cookieDomains = isTelemost ? YANDEX_COOKIE_DOMAINS : VK_COOKIE_DOMAINS;
|
||||
const platformName = isTelemost ? 'Yandex' : 'VK';
|
||||
let cookies = isTelemost ? await this.getYandexCookies() : await this.getVKCookies();
|
||||
if (!cookies.some((c) => c.name === authCookie)) {
|
||||
this.sendLog(tabId, `No ${platformName} session found, opening login.`);
|
||||
if (this._mainWindow && !this._mainWindow.isDestroyed()) {
|
||||
this._mainWindow.webContents.send(IPC.LOGIN_REQUIRED, { tabId, url: loginUrl });
|
||||
}
|
||||
await this.waitForLogin(cookieDomains, authCookie);
|
||||
if (this._mainWindow && !this._mainWindow.isDestroyed()) {
|
||||
this._mainWindow.webContents.send(IPC.LOGIN_DONE, { tabId });
|
||||
}
|
||||
this.sendLog(tabId, `${platformName} login captured.`);
|
||||
cookies = isTelemost ? await this.getYandexCookies() : await this.getVKCookies();
|
||||
}
|
||||
this.sendLog(tabId, `${platformName} cookies (${cookies.length}): ${cookies.map((c) => c.name).join(', ')}`);
|
||||
this.killRelay(tabId, tab);
|
||||
const cookiesPath = path.join(app.getPath('userData'), `cookies-${platform}.json`);
|
||||
await fs.writeFile(cookiesPath, JSON.stringify(cookies));
|
||||
@@ -256,9 +275,63 @@ export class TabManager {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
tab.relay = proc;
|
||||
this.attachProcessOutput(proc, tabId);
|
||||
proc.on('close', (code) => {
|
||||
let sawAuthFailure = false;
|
||||
this.attachProcessOutput(proc, tabId, (msg) => {
|
||||
if (msg.includes('status 401') || msg.includes('"UnauthorizedError"')) {
|
||||
sawAuthFailure = true;
|
||||
}
|
||||
});
|
||||
proc.on('close', async (code) => {
|
||||
this.sendLog(tabId, `Headless exited with code ${code}`);
|
||||
if (sawAuthFailure) {
|
||||
this.sendLog(tabId, `${platformName} session rejected (401), clearing and re-prompting login.`);
|
||||
await this.clearAuthCookies(cookieDomains, authCookie);
|
||||
if (this.tabs.get(tabId) === tab) {
|
||||
this.startHeadless(tabId, platform);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async clearAuthCookies(cookieDomains: string[], authCookieName: string): Promise<void> {
|
||||
const ses = session.fromPartition(SESSION_PARTITION);
|
||||
const matches = await ses.cookies.get({ name: authCookieName });
|
||||
for (const cookie of matches) {
|
||||
if (!cookie.domain || !cookieDomains.some((d) => cookie.domain!.includes(d))) continue;
|
||||
const host = cookie.domain.startsWith('.') ? cookie.domain.slice(1) : cookie.domain;
|
||||
const url = `https://${host}${cookie.path || '/'}`;
|
||||
try {
|
||||
await ses.cookies.remove(url, cookie.name);
|
||||
} catch (err) {
|
||||
console.log(`[COOKIES] failed to remove ${cookie.name} on ${url}:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private waitForLogin(cookieDomains: string[], authCookieName: string): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const ses = session.fromPartition(SESSION_PARTITION);
|
||||
const finish = () => {
|
||||
ses.cookies.removeListener('changed', onChanged);
|
||||
resolve();
|
||||
};
|
||||
const onChanged = (
|
||||
_e: Electron.Event,
|
||||
cookie: Electron.Cookie,
|
||||
_cause: string,
|
||||
removed: boolean,
|
||||
) => {
|
||||
if (removed) return;
|
||||
if (cookie.name !== authCookieName) return;
|
||||
if (!cookie.domain || !cookieDomains.some((d) => cookie.domain!.includes(d))) return;
|
||||
finish();
|
||||
};
|
||||
ses.cookies.on('changed', onChanged);
|
||||
ses.cookies.get({ name: authCookieName }).then((found) => {
|
||||
if (found.some((c) => c.domain && cookieDomains.some((d) => c.domain!.includes(d)))) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -44,4 +44,10 @@ import { IPC } from '../constants';
|
||||
onCloseBotTab(cb: (data: any) => void) {
|
||||
ipcRenderer.on(IPC.CLOSE_BOT_TAB, (_e, data) => cb(data));
|
||||
},
|
||||
onLoginRequired(cb: (tabId: string, url: string) => void) {
|
||||
ipcRenderer.on(IPC.LOGIN_REQUIRED, (_e, data) => cb(data.tabId, data.url));
|
||||
},
|
||||
onLoginDone(cb: (tabId: string) => void) {
|
||||
ipcRenderer.on(IPC.LOGIN_DONE, (_e, data) => cb(data.tabId));
|
||||
},
|
||||
};
|
||||
@@ -27,6 +27,7 @@ export function renderContent(tm: RendererTabManager): void {
|
||||
const toolbar = document.getElementById('toolbar')!;
|
||||
const logsPanel = document.getElementById('logsPanel')!;
|
||||
const headlessInfo = document.getElementById('headlessInfo')!;
|
||||
const hookPanel = document.getElementById('hookPanel')!;
|
||||
|
||||
const webviews = content.querySelectorAll('webview');
|
||||
webviews.forEach((wv) => wv.classList.add('hidden'));
|
||||
@@ -42,7 +43,13 @@ export function renderContent(tm: RendererTabManager): void {
|
||||
logsPanel.style.display = 'flex';
|
||||
|
||||
const activeTab = tm.tabs[tm.activeTabId];
|
||||
if (activeTab.headless) {
|
||||
hookPanel.style.display = activeTab.headless ? 'none' : 'flex';
|
||||
logsPanel.classList.toggle('relay-only', activeTab.headless === true);
|
||||
if (activeTab.loginWebview) {
|
||||
toolbar.style.display = 'none';
|
||||
headlessInfo.style.display = 'none';
|
||||
activeTab.loginWebview.classList.remove('hidden');
|
||||
} else if (activeTab.headless) {
|
||||
toolbar.style.display = 'none';
|
||||
headlessInfo.style.display = 'block';
|
||||
let title = 'Headless VK';
|
||||
@@ -68,7 +75,6 @@ export function renderContent(tm: RendererTabManager): void {
|
||||
} else {
|
||||
callInfoVK.style.display = 'block';
|
||||
document.getElementById('headlessJoinLink')!.textContent = callInfo.joinLink || '';
|
||||
document.getElementById('headlessShortLink')!.textContent = callInfo.shortLink || '';
|
||||
document.getElementById('headlessTurn')!.textContent = callInfo.turn || '';
|
||||
document.getElementById('headlessProtocol')!.textContent = callInfo.protocol || '';
|
||||
}
|
||||
@@ -105,6 +111,29 @@ export function scrollLogs(): void {
|
||||
if (hookEl) hookEl.scrollTop = hookEl.scrollHeight;
|
||||
}
|
||||
|
||||
export function attachLoginWebview(tm: RendererTabManager, tabId: string, url: string): void {
|
||||
const tab = tm.tabs[tabId];
|
||||
if (!tab) return;
|
||||
if (tab.loginWebview) tab.loginWebview.remove();
|
||||
const webview = document.createElement('webview') as unknown as Webview;
|
||||
webview.setAttribute('src', url);
|
||||
webview.setAttribute('partition', SESSION_PARTITION);
|
||||
webview.setAttribute('useragent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36');
|
||||
webview.classList.add('webview-full');
|
||||
webview.dataset.tabId = tabId;
|
||||
document.getElementById('content')!.appendChild(webview);
|
||||
tab.loginWebview = webview;
|
||||
}
|
||||
|
||||
export function detachLoginWebview(tm: RendererTabManager, tabId: string): void {
|
||||
const tab = tm.tabs[tabId];
|
||||
if (!tab) return;
|
||||
if (tab.loginWebview) {
|
||||
tab.loginWebview.remove();
|
||||
tab.loginWebview = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function loadURL(tm: RendererTabManager, url: string): void {
|
||||
if (!tm.activeTabId) return;
|
||||
const activeTab = tm.tabs[tm.activeTabId];
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
exportCookies,
|
||||
copyToClipboard,
|
||||
renameTab,
|
||||
attachLoginWebview,
|
||||
detachLoginWebview,
|
||||
} from './dom';
|
||||
import { VK_IM_URL, TELEMOST_URL } from '../constants';
|
||||
import { Platform, Bridge, BotTabData, LogPanel, TunnelMode } from '../types';
|
||||
@@ -103,15 +105,13 @@ function bindErrorPopup(): void {
|
||||
function bindLogEvents(): void {
|
||||
document.getElementById('btnClearRelay')!.addEventListener('click', () => clearLog(LogPanel.Relay));
|
||||
document.getElementById('btnClearHook')!.addEventListener('click', () => clearLog(LogPanel.Hook));
|
||||
document.getElementById('btnSaveLogsHeadless')!.addEventListener('click', saveLogs);
|
||||
}
|
||||
|
||||
function bindHeadlessEvents(): void {
|
||||
document.getElementById('headlessJoinLink')!.addEventListener('click', (event) => {
|
||||
copyToClipboard((event.target as HTMLElement).textContent || '');
|
||||
});
|
||||
document.getElementById('headlessShortLink')!.addEventListener('click', (event) => {
|
||||
copyToClipboard((event.target as HTMLElement).textContent || '');
|
||||
});
|
||||
document.getElementById('headlessInfo')!.addEventListener('click', (event) => {
|
||||
const target = event.target as HTMLElement;
|
||||
const copyTarget = target.dataset.copy;
|
||||
@@ -172,6 +172,25 @@ function init(): void {
|
||||
tm.closeTab(data.tabId);
|
||||
});
|
||||
|
||||
window.bridge.onLoginRequired((tabId: string, url: string) => {
|
||||
const tab = tm.tabs[tabId];
|
||||
if (!tab) return;
|
||||
tab.headlessStatus = 'Waiting for login...';
|
||||
attachLoginWebview(tm, tabId, url);
|
||||
if (tabId === tm.activeTabId) {
|
||||
renderTabs(tm);
|
||||
renderContent(tm);
|
||||
}
|
||||
});
|
||||
|
||||
window.bridge.onLoginDone((tabId: string) => {
|
||||
const tab = tm.tabs[tabId];
|
||||
if (!tab) return;
|
||||
detachLoginWebview(tm, tabId);
|
||||
tab.headlessStatus = 'Starting...';
|
||||
if (tabId === tm.activeTabId) renderContent(tm);
|
||||
});
|
||||
|
||||
startHookLogPoller(tm);
|
||||
tm.autoStartBot();
|
||||
renderBotButton(tm);
|
||||
|
||||
@@ -87,6 +87,7 @@ export class RendererTabManager {
|
||||
closeTab(tabId: string): void {
|
||||
const tab = this.tabs[tabId];
|
||||
if (tab?.wv) tab.wv.remove();
|
||||
if (tab?.loginWebview) tab.loginWebview.remove();
|
||||
window.bridge.closeTab(tabId);
|
||||
delete this.tabs[tabId];
|
||||
if (this.activeTabId === tabId) {
|
||||
@@ -159,10 +160,6 @@ export class RendererTabManager {
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if (trimmed.includes(HeadlessLogMarker.SHORT_LINK) && tab.callInfo) {
|
||||
tab.callInfo.shortLink = trimmed.split(HeadlessLogMarker.SHORT_LINK)[1].trim();
|
||||
changed = true;
|
||||
}
|
||||
if (trimmed.includes(HeadlessLogMarker.TURN) && tab.callInfo) {
|
||||
tab.callInfo.turn = trimmed.split(HeadlessLogMarker.TURN)[1].trim();
|
||||
changed = true;
|
||||
|
||||
@@ -76,7 +76,6 @@ export interface TabListEntry {
|
||||
|
||||
export interface CallInfo {
|
||||
joinLink?: string;
|
||||
shortLink?: string;
|
||||
turn?: string;
|
||||
protocol?: string;
|
||||
}
|
||||
@@ -102,6 +101,7 @@ export interface RendererTab {
|
||||
headlessStatus?: string;
|
||||
callInfo?: CallInfo;
|
||||
tunnelConnected?: boolean;
|
||||
loginWebview?: Webview;
|
||||
}
|
||||
|
||||
export interface BotTabData {
|
||||
@@ -131,4 +131,6 @@ export interface Bridge {
|
||||
startHeadless(tabId: string, platform: string): Promise<void>;
|
||||
sendBotCallLink(tabId: string, link: string): Promise<void>;
|
||||
onCloseBotTab(cb: (data: { tabId: string }) => void): void;
|
||||
onLoginRequired(cb: (tabId: string, url: string) => void): void;
|
||||
onLoginDone(cb: (tabId: string) => void): void;
|
||||
}
|
||||
Reference in new issue
Block a user