387 lines
6.9 KiB
TypeScript
387 lines
6.9 KiB
TypeScript
import { query } from '../db/connection';
|
|
|
|
import { config } from '../config';
|
|
|
|
import { SystemSettings } from '../models';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repository for system-wide configuration settings.
|
|
|
|
* Reads and updates password policy, token expiry, and runtime ops from the database.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const SETTINGS_CACHE_MS = 5000;
|
|
|
|
let settingsCache: { at: number; data: SystemSettings } | null = null;
|
|
|
|
|
|
|
|
function buildDefaults(): SystemSettings {
|
|
|
|
return {
|
|
|
|
password_min_length: 8,
|
|
|
|
password_require_uppercase: true,
|
|
|
|
password_require_lowercase: true,
|
|
|
|
password_require_number: true,
|
|
|
|
password_require_special: true,
|
|
|
|
access_token_expiry: '15m',
|
|
|
|
refresh_token_expiry: '7d',
|
|
|
|
require_email_verification_for_login: true,
|
|
|
|
allow_withdrawals: config.withdrawal.allowWithdrawals,
|
|
|
|
network_usdt_trc20_deposit_enabled: true,
|
|
|
|
network_usdt_trc20_withdraw_enabled: true,
|
|
|
|
network_usdt_bep20_deposit_enabled: true,
|
|
|
|
network_usdt_bep20_withdraw_enabled: true,
|
|
|
|
rate_limit_window_ms: config.rateLimit.windowMs,
|
|
|
|
rate_limit_max: config.rateLimit.max,
|
|
|
|
login_rate_limit_max: config.rateLimit.loginMax,
|
|
|
|
daily_withdrawal_limit_usd: config.withdrawal.dailyLimitUsd,
|
|
|
|
large_withdrawal_threshold_usd: config.withdrawal.largeThresholdUsd,
|
|
|
|
candle_predict_engine_tick_ms: config.candlePredict.engineTickMs,
|
|
|
|
candle_predict_backfill_max_per_tick: config.candlePredict.backfillMaxPerTick,
|
|
|
|
candle_predict_backfill_interval_ms: config.candlePredict.backfillIntervalMs,
|
|
|
|
candle_predict_backfill_seed_max: config.candlePredict.backfillSeedMax,
|
|
engine_audit_log_retention_days: 7,
|
|
candle_predict_retention_days: 30,
|
|
candle_predict_retention_keep_last: 5000,
|
|
|
|
withdrawal_network_fee_usdt_micro: config.withdrawal.networkFeeUsdtMicro,
|
|
|
|
transfer_min_remaining_usdt_micro: 0,
|
|
|
|
require_admin_2fa: true,
|
|
|
|
require_withdrawal_2fa: true,
|
|
|
|
max_concurrent_sessions: 10,
|
|
|
|
login_max_failed_attempts: 5,
|
|
|
|
login_lockout_minutes: 15,
|
|
|
|
login_captcha_after_failures: 3,
|
|
|
|
sybil_auto_suspend_accounts: true,
|
|
|
|
login_risk_auto_suspend_accounts: false,
|
|
|
|
login_risk_auto_suspend_threshold: 70,
|
|
|
|
support_message_edit_window_minutes: 15,
|
|
|
|
support_message_delete_window_minutes: 15,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const BOOLEAN_KEYS = new Set([
|
|
|
|
'password_require_uppercase',
|
|
|
|
'password_require_lowercase',
|
|
|
|
'password_require_number',
|
|
|
|
'password_require_special',
|
|
|
|
'require_email_verification_for_login',
|
|
|
|
'allow_withdrawals',
|
|
|
|
'network_usdt_trc20_deposit_enabled',
|
|
|
|
'network_usdt_trc20_withdraw_enabled',
|
|
|
|
'network_usdt_bep20_deposit_enabled',
|
|
|
|
'network_usdt_bep20_withdraw_enabled',
|
|
|
|
'require_admin_2fa',
|
|
|
|
'require_withdrawal_2fa',
|
|
|
|
'sybil_auto_suspend_accounts',
|
|
|
|
'login_risk_auto_suspend_accounts',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const INTEGER_KEYS = new Set([
|
|
|
|
'password_min_length',
|
|
|
|
'rate_limit_window_ms',
|
|
|
|
'rate_limit_max',
|
|
|
|
'login_rate_limit_max',
|
|
|
|
'daily_withdrawal_limit_usd',
|
|
|
|
'large_withdrawal_threshold_usd',
|
|
|
|
'candle_predict_engine_tick_ms',
|
|
|
|
'candle_predict_backfill_max_per_tick',
|
|
|
|
'candle_predict_backfill_interval_ms',
|
|
|
|
'candle_predict_backfill_seed_max',
|
|
|
|
'engine_audit_log_retention_days',
|
|
|
|
'candle_predict_retention_days',
|
|
|
|
'candle_predict_retention_keep_last',
|
|
|
|
'withdrawal_network_fee_usdt_micro',
|
|
|
|
'transfer_min_remaining_usdt_micro',
|
|
|
|
'login_max_failed_attempts',
|
|
|
|
'login_lockout_minutes',
|
|
|
|
'login_captcha_after_failures',
|
|
|
|
'max_concurrent_sessions',
|
|
|
|
'login_risk_auto_suspend_threshold',
|
|
|
|
'support_message_edit_window_minutes',
|
|
|
|
'support_message_delete_window_minutes',
|
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a raw string setting value to its typed representation.
|
|
|
|
* @param key - Setting key that determines the target type.
|
|
|
|
* @param value - Raw string value stored in the database.
|
|
|
|
* @returns Parsed value as number, boolean, or string.
|
|
|
|
*/
|
|
|
|
function parseValue(key: string, value: string): number | boolean | string {
|
|
|
|
if (BOOLEAN_KEYS.has(key)) return value === 'true';
|
|
|
|
if (INTEGER_KEYS.has(key)) return parseInt(value, 10) || 0;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clears the in-memory settings cache (call after updates). */
|
|
|
|
export function invalidateSettingsCache(): void {
|
|
|
|
settingsCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads all system settings, merging database values with env-backed defaults.
|
|
|
|
* @returns Complete system settings object with typed values.
|
|
|
|
*/
|
|
|
|
export async function getSettings(): Promise<SystemSettings> {
|
|
|
|
if (settingsCache && Date.now() - settingsCache.at < SETTINGS_CACHE_MS) {
|
|
|
|
return settingsCache.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rows = await query<{ key: string; value: string }[]>(
|
|
|
|
'SELECT key, value FROM system_settings'
|
|
|
|
);
|
|
|
|
const result = buildDefaults();
|
|
|
|
for (const row of rows) {
|
|
(result as unknown as Record<string, unknown>)[row.key] = parseValue(row.key, row.value);
|
|
}
|
|
|
|
|
|
|
|
settingsCache = { at: Date.now(), data: result };
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UPDATABLE_KEYS = [
|
|
|
|
'password_min_length',
|
|
|
|
'password_require_uppercase',
|
|
|
|
'password_require_lowercase',
|
|
|
|
'password_require_number',
|
|
|
|
'password_require_special',
|
|
|
|
'access_token_expiry',
|
|
|
|
'refresh_token_expiry',
|
|
|
|
'require_email_verification_for_login',
|
|
|
|
'allow_withdrawals',
|
|
|
|
'network_usdt_trc20_deposit_enabled',
|
|
|
|
'network_usdt_trc20_withdraw_enabled',
|
|
|
|
'network_usdt_bep20_deposit_enabled',
|
|
|
|
'network_usdt_bep20_withdraw_enabled',
|
|
|
|
'rate_limit_window_ms',
|
|
|
|
'rate_limit_max',
|
|
|
|
'login_rate_limit_max',
|
|
|
|
'daily_withdrawal_limit_usd',
|
|
|
|
'large_withdrawal_threshold_usd',
|
|
|
|
'candle_predict_engine_tick_ms',
|
|
|
|
'candle_predict_backfill_max_per_tick',
|
|
|
|
'candle_predict_backfill_interval_ms',
|
|
|
|
'candle_predict_backfill_seed_max',
|
|
|
|
'engine_audit_log_retention_days',
|
|
|
|
'candle_predict_retention_days',
|
|
|
|
'candle_predict_retention_keep_last',
|
|
|
|
'withdrawal_network_fee_usdt_micro',
|
|
|
|
'transfer_min_remaining_usdt_micro',
|
|
|
|
'require_admin_2fa',
|
|
|
|
'login_max_failed_attempts',
|
|
|
|
'login_lockout_minutes',
|
|
|
|
'login_captcha_after_failures',
|
|
|
|
'require_withdrawal_2fa',
|
|
|
|
'sybil_auto_suspend_accounts',
|
|
|
|
'login_risk_auto_suspend_accounts',
|
|
|
|
'login_risk_auto_suspend_threshold',
|
|
|
|
'max_concurrent_sessions',
|
|
|
|
'support_message_edit_window_minutes',
|
|
|
|
'support_message_delete_window_minutes',
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists one or more system setting updates.
|
|
|
|
* Only keys present in the updates object are written; others are left unchanged.
|
|
|
|
* @param updates - Partial settings object with values to save.
|
|
|
|
* @returns Resolves when all provided settings have been upserted.
|
|
|
|
*/
|
|
|
|
export async function updateSettings(updates: Partial<SystemSettings>): Promise<void> {
|
|
|
|
for (const key of UPDATABLE_KEYS) {
|
|
|
|
const value = updates[key];
|
|
|
|
if (value === undefined) continue;
|
|
|
|
const str = typeof value === 'boolean' ? (value ? 'true' : 'false') : String(value);
|
|
|
|
await query(
|
|
|
|
`INSERT INTO system_settings (key, value, updated_at) VALUES ($1, $2, NOW())
|
|
|
|
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`,
|
|
|
|
[key, str]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
invalidateSettingsCache();
|
|
|
|
}
|
|
|
|
|