From 48c7c256745205a3e183c6a751806ef8dbb549c6 Mon Sep 17 00:00:00 2001 From: thuanle Date: Mon, 4 Aug 2025 15:34:01 +0700 Subject: [PATCH] register session -> send heartbeat --- agent.user.js | 97 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/agent.user.js b/agent.user.js index fa5d052..d3ca1b2 100644 --- a/agent.user.js +++ b/agent.user.js @@ -28,6 +28,18 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); (async () => { 'use strict'; + const uuid = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); + + const KEYS = { + SESSION_ID: 'aRah9OhHeijee6sho3baequu9phoovah', + SESSION_TOKEN: 'ThiegiecohViuZ1Iecio7gahphiechub', + + AGENT_TOKEN: 'baf-agent-token', + } + if (!sessionStorage.getItem(KEYS.SESSION_ID)) { + sessionStorage.setItem(KEYS.SESSION_ID, uuid()); + } + // ====== CONFIGURATION ====== const CONFIG = { HEARTBEAT_INTERVAL: 10000, @@ -35,10 +47,17 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); SERVERS: { local: { label: '🏠 Local', url: 'http://localhost:3000' }, prod: { label: '🌐 Prod', url: 'https://baf.thuanle.me' }, - } + }, }; + const AppSession = { + SESSION_ID: sessionStorage.getItem(KEYS.SESSION_ID), + + getSessionToken: () => sessionStorage.getItem(KEYS.SESSION_TOKEN), + setSessionToken: token => sessionStorage.setItem(KEYS.SESSION_TOKEN, token), + } + // ====== APP ENUMS ====== const AppEnums = { BOT_STATUS: { @@ -57,6 +76,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); this.data = { // Server configuration - Thông tin server hiện tại server_last_seen: null, // Thời gian cuối cùng ping server thành công + server_latency: null, // Thời gian ping server (ms) // Page state - Trạng thái trang hiện tại current_page: null, // Trang hiện tại (detected từ URL) @@ -95,8 +115,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); TL.log('APP-STATE', 'AppState initialized'); } - - /** * Lấy copy của data hiện tại * @returns {Object} Copy của state data @@ -108,6 +126,8 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // ====== GETTER METHODS ====== getServer() { return CONFIG.SERVERS[AppSettings.getServerType()]; } getServerLastSeen() { return this.data.server_last_seen; } + getServerLatency() { return this.data.server_latency; } + /** * Kiểm tra xem server có kết nối không * @returns {boolean} true nếu server kết nối trong 1 phút, false nếu không @@ -193,7 +213,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); * Lưu trữ cài đặt người dùng trong GM storage (vĩnh viễn) */ const AppSettings = { - key_token: 'baf-agent-token', + key_agent_token: 'baf-agent-token', key_server_type: 'baf-server-type', key_bot_status: 'baf-bot-status', key_popup_position: 'baf-popup-position', @@ -201,9 +221,8 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); key_debug: 'baf-debug', key_safety_guard: 'baf-safety-guard', - // Token management - getToken: () => GM_getValue(AppSettings.key_token, ''), - setToken: token => GM_setValue(AppSettings.key_token, String(token || '').trim().replace(/^Bearer\s+/i, '')), + getAgentToken: () => GM_getValue(AppSettings.key_agent_token, ''), + setAgentToken: token => GM_setValue(AppSettings.key_agent_token, String(token || '').trim().replace(/^Bearer\s+/i, '')), // Server configuration getServerType: () => GM_getValue(AppSettings.key_server_type, 'prod'), @@ -351,15 +370,14 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // ====== BAF API MODULE ====== const BAF = { - getHost: () => AppState.getServer()?.url, + _getHost: () => AppState.getServer()?.url, - request: async (method, path, { params, body, headers } = {}) => { - const base = BAF.getHost(); + _request: async (method, path, { params, body, headers, token } = {}) => { + const base = BAF._getHost(); const url = new URL(path, base); if (params) for (const [k, v] of Object.entries(params)) url.searchParams.append(k, v); const h = new Headers(headers || {}); - const token = await AppSettings.getToken(); if (token && !h.has('Authorization')) h.set('Authorization', `Bearer ${token}`); const init = { method, headers: h }; @@ -372,15 +390,20 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); return TL.net.gmRequest(url.toString(), init); }, - get: (path, params, init = {}) => BAF.request('GET', path, { params, headers: init.headers }), - post: (path, body, init = {}) => BAF.request('POST', path, { body, headers: init.headers }), + _agentGet: (path, params, init = {}) => BAF._request('GET', path, { params, headers: init.headers, token: AppSettings.getAgentToken() }), + _agentPost: (path, body, init = {}) => BAF._request('POST', path, { body, headers: init.headers, token: AppSettings.getAgentToken() }), - ping: (status) => BAF.post('/agent/ping', status), + _sessionGet: (path, params, init = {}) => BAF._request('GET', path, { params, headers: init.headers, token: AppSession.getSessionToken() }), + _sessionPost: (path, body, init = {}) => BAF._request('POST', path, { body, headers: init.headers, token: AppSession.getSessionToken() }), + register: (data) => BAF._agentPost('/agent/register', data), + + ping: (data) => BAF._sessionPost('/session/ping', data), + getTask: (data) => BAF._sessionPost('/session/task', data), }; // ====== BINANCE MODULE ====== - + // Binance Pages constants const BINANCE_PAGES = { LOGIN: 'login', @@ -531,7 +554,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); } } }; - + // ====== APP UI ====== const AppUi = { // ====== DASHBOARD OVERLAY ====== @@ -638,7 +661,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Get server info const serverLabel = AppState.getServer()?.label || 'Unknown'; - const serverConnected = AppState.getServerConnected() ? '🟢' : '🔴'; + const serverConnected = AppState.getServerConnected() ? `🟢 (${AppState.getServerLatency()}ms)` : '🔴'; // Get page info const pageDisplay = AppState.getCurrentPage() || 'unknown'; @@ -660,20 +683,24 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); 🛟 Safety: ${safetyStatus.enabled ? '🛡️ Safe' : '🚨 Blocked'}
- 🐛 Debug: ${AppSettings.getDebug() ? '✔️ ON' : '❌ OFF'} + 🆔 Tab: ${AppSession.SESSION_ID.slice(-12)}
🤖 Bot Status: ${statusDisplay}
-
- 🌐 Server: ${serverLabel} ${serverConnected} -
+
👤 Login: ${loginStatus}
Page: ${pageDisplay} -
+ +
+ 🌐 Server: ${serverLabel} ${serverConnected} +
+
+ 🐛 Debug: ${AppSettings.getDebug() ? '✔️ ON' : '❌ OFF'} +
`; // Update content (excluding position button) @@ -732,9 +759,9 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); serverToggleBtn.onclick = async () => { const currentServerType = AppSettings.getServerType(); const newServerType = currentServerType === 'local' ? 'prod' : 'local'; - + // Update server type in settings - AppSettings.setServerType(newServerType); + AppSettings.setServerType(newServerType); await AppState.update({ server_last_seen: null }); // Update the content to reflect the change @@ -807,7 +834,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); await AppSettings.setToken(input); } const s = AppState.getServer(); - const res = await BAF.ping(); + const res = await BAF.register({ session_id: AppSession.SESSION_ID }); const resStr = `Server: ${s.label} (${s.url})\n` + `Status: ${res.ok ? 'Connected ✅' : 'Failed ❌'} (${res.status})`; @@ -843,9 +870,11 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // Setup observers for dashboard overlay updates AppServices.initObservers(); + await AppServices.registerSession(); + // Setup interval services AppServices.initInterval(); - + TL.log('APP-SERVICES', 'AppServices initialized'); }, @@ -868,13 +897,15 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); // } // }); }, + registerSession: async () => { + const msg = await BAF.register({ session_id: AppSession.SESSION_ID }); + AppSession.setSessionToken(msg.data.token); + TL.log('APP-SERVICES', `Session token: ${AppSession.getSessionToken()}`); + }, // Setup interval services initInterval: () => { - // Setup heartbeat interval setInterval(() => AppServices.heartbeat(), CONFIG.HEARTBEAT_INTERVAL); - - // Setup dashboard overlay update interval setInterval(() => AppUi.dashboardOverlay.updateContent(), CONFIG.DASHBOARD_UPDATE_INTERVAL); TL.debug('APP-SERVICES', 'Interval services started'); @@ -895,14 +926,18 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.'); current_page: AppState.getCurrentPage(), bot_status: AppSettings.getBotStatus(), current_task: null, // TODO: Add to AppState if needed - task_data: null, // TODO: Add to AppState if needed + current_task_data: null, // TODO: Add to AppState if needed current_step: null, // TODO: Add to AppState if needed }; // TL.debug(`HEARTBEAT`, `${JSON.stringify(status, null, 2)}`); + const start = performance.now(); const res = await BAF.ping(status); + const end = performance.now(); + const duration = Math.round(end - start); + if (res.ok) { - AppState.update({ server_last_seen: new Date() }); + AppState.update({ server_last_seen: new Date(), server_latency: duration }); } return status;