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'}