Persist one browser user agent for Axios and WebSockets

This commit is contained in:
Brandon Li
2026-10-19 06:08:23 -05:00
parent 13e03c3d0f
commit ed56e306d1
21 changed files with 380 additions and 64 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ import { getClients, resetClients } from '@/lib/clients';
import { getTradableFirms } from '@/lib/firm-catalog';
import { getAccountConfig } from '@/lib/account-type-resolver';
import { combineCmeSessions, computeDayPlan, resolveEffectiveConfig, currentTradeDateCT } from '@/lib/trading-logic';
import axios from 'axios';
import axios from '@/lib/http-client';
/**
* Per-account snapshot of everything computeDayPlan sees, plus the book-balance
+1 -1
View File
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { getClients } from '@/lib/clients';
import axios from 'axios';
import axios from '@/lib/http-client';
export async function GET(
req: NextRequest,
+3
View File
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { getSetting, setSetting } from '@/lib/db';
import { scheduleBackupPush } from '@/lib/backup-sync';
import { getRequestUserAgent } from '@/lib/request-user-agent';
const VALID_KEYS = ['max_concurrent_accounts', 'tick_interval_seconds', 'master_dashboard_url', 'instance_name', 'trading_hours', 'twocaptcha_api_key', 'twocaptcha_proxy', 'master_backup_token', 'proxy_url'] as const;
type SettingKey = typeof VALID_KEYS[number];
@@ -13,6 +14,8 @@ export async function GET() {
// Read-only on purpose (not in VALID_KEYS): the master issues this uuid,
// and editing it by hand would detach the instance from its person.
result.instance_id = getSetting('instance_id');
// Generated once per instance. Callers cannot rotate an active identity.
result.user_agent = getRequestUserAgent();
result.backup_last_push_at = getSetting('backup_last_push_at');
result.backup_last_error = getSetting('backup_last_error');
return NextResponse.json(result);
+13
View File
@@ -47,6 +47,7 @@ interface AppSettings {
trading_hours: string | null;
master_backup_token: string | null;
proxy_url: string | null;
user_agent: string | null;
backup_last_push_at: string | null;
backup_last_error: string | null;
}
@@ -77,6 +78,7 @@ export default function SettingsPage() {
const [saved, setSaved] = useState(false);
const [backupToken, setBackupToken] = useState<string>('');
const [proxyUrl, setProxyUrl] = useState<string>('');
const [userAgent, setUserAgent] = useState<string>('');
const [backupStatus, setBackupStatus] = useState<{ at: string | null; error: string | null }>({ at: null, error: null });
const [restoreList, setRestoreList] = useState<BackupMeta[] | null>(null);
const [restoreBusy, setRestoreBusy] = useState(false);
@@ -130,6 +132,7 @@ export default function SettingsPage() {
if (s.proxy_url != null) {
setProxyUrl(s.proxy_url);
}
setUserAgent(s.user_agent ?? '');
setBackupStatus({ at: s.backup_last_push_at, error: s.backup_last_error || null });
});
@@ -440,6 +443,16 @@ export default function SettingsPage() {
className="w-72 rounded-lg border border-slate-200 bg-slate-50 px-3 py-1.5 text-sm font-mono text-slate-800 focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="border-t border-slate-100 px-4 py-3">
<p className="text-sm font-medium text-slate-800">User agent</p>
<p className="text-xs text-slate-400 mt-0.5">
Chrome, Firefox, or Edge is randomly selected on first load and saved.
All server Axios requests and WebSocket connections reuse it, including after restarts.
</p>
<p className="mt-2 break-all rounded-lg bg-slate-50 px-3 py-2 text-xs font-mono text-slate-700">
{userAgent || 'Loading…'}
</p>
</div>
</div>
{/* ── Instruments ── */}