-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp-settings.js
More file actions
95 lines (81 loc) · 3.66 KB
/
Copy pathapp-settings.js
File metadata and controls
95 lines (81 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Sentinel SDK — App Settings Persistence
*
* Typed settings object with defaults, disk persistence, and atomic writes.
* Covers: DNS, tunnel, session defaults, polling intervals.
* Every setting has a sane default — apps can use this out of the box.
*
* Usage:
* import { loadAppSettings, saveAppSettings } from './app-settings.js';
* const settings = loadAppSettings();
* settings.dnsPreset = 'google';
* saveAppSettings(settings);
*/
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from 'fs';
import path from 'path';
import os from 'os';
const SETTINGS_DIR = path.join(os.homedir(), '.sentinel-sdk');
const SETTINGS_FILE = path.join(SETTINGS_DIR, 'app-settings.json');
// ─── Default Settings ───────────────────────────────────────────────────────
/** All settings with their defaults. */
export const APP_SETTINGS_DEFAULTS = Object.freeze({
// Network
dnsPreset: 'handshake', // 'handshake' | 'google' | 'cloudflare' | 'custom'
customDns: '', // Custom DNS IPs (comma-separated)
// Tunnel
fullTunnel: true, // Route all traffic through VPN
systemProxy: false, // Set OS SOCKS proxy for V2Ray
killSwitch: false, // Block all traffic if tunnel drops
wgMtu: 1420, // WireGuard MTU (1280-1500)
wgKeepalive: 25, // WireGuard keepalive seconds (15-60)
// Session
defaultGigabytes: 1, // Default GB amount for per-GB sessions
defaultHours: 1, // Default hour amount for hourly sessions
preferHourly: false, // Prefer hourly pricing when available
protocolPreference: 'auto', // 'auto' | 'wireguard' | 'v2ray'
// Polling (seconds)
statusPollSec: 3, // Connection status check
ipCheckSec: 60, // Public IP check
balanceCheckSec: 300, // Wallet balance refresh (5 min)
allocationCheckSec: 120, // Session allocation refresh (2 min)
// Plan
planProbeMax: 500, // Max plan ID to probe in discoverPlans
// Favorites
favoriteNodes: [], // Array of sentnode1... addresses
// Last connection
lastNodeAddress: null, // For quick reconnect
lastServiceType: null, // 'wireguard' | 'v2ray'
});
// ─── Load / Save ────────────────────────────────────────────────────────────
/**
* Load app settings from disk. Returns defaults for missing/corrupt files.
* @returns {object} Settings object (mutate + pass to saveAppSettings)
*/
export function loadAppSettings() {
try {
if (existsSync(SETTINGS_FILE)) {
const raw = JSON.parse(readFileSync(SETTINGS_FILE, 'utf8'));
// Merge with defaults — new settings get defaults, removed settings get dropped
return { ...APP_SETTINGS_DEFAULTS, ...raw };
}
} catch { /* corrupt file — return defaults */ }
return { ...APP_SETTINGS_DEFAULTS };
}
/**
* Save app settings to disk (atomic write).
* @param {object} settings - Settings object to save
*/
export function saveAppSettings(settings) {
try {
if (!existsSync(SETTINGS_DIR)) mkdirSync(SETTINGS_DIR, { recursive: true });
const tmpFile = SETTINGS_FILE + '.tmp';
writeFileSync(tmpFile, JSON.stringify(settings, null, 2));
renameSync(tmpFile, SETTINGS_FILE);
} catch { /* non-fatal */ }
}
/**
* Reset all settings to defaults.
*/
export function resetAppSettings() {
saveAppSettings({ ...APP_SETTINGS_DEFAULTS });
}