-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-cli.js
More file actions
executable file
·224 lines (191 loc) · 6.04 KB
/
session-cli.js
File metadata and controls
executable file
·224 lines (191 loc) · 6.04 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
'use strict';
const sessions = require('./sessions');
const args = process.argv.slice(2);
const command = args[0];
function usage() {
console.log(`
Session Manager CLI
Usage:
node session-cli.js <command> [options]
Commands:
list List all sessions
show <session_id> Show session details
set <session_id> <key=value> Update session setting (e.g., darkMode=true)
delete <session_id> Delete a session
cleanup Run cleanup of expired sessions
purge <time> Delete sessions older than <time>
Time format: Nsecs, Nmins, Nhours, Ndays
(e.g., 10secs, 30mins, 2hours, 7days)
Examples:
node session-cli.js list
node session-cli.js show abc-123-def
node session-cli.js set abc-123-def darkMode=false
node session-cli.js delete abc-123-def
node session-cli.js purge 7days
`);
}
function formatDate(timestamp) {
if (!timestamp) return 'N/A';
return new Date(timestamp * 1000).toISOString().replace('T', ' ').substring(0, 19);
}
function listSessions() {
const db = sessions.initDb();
const rows = db.prepare('SELECT * FROM sessions ORDER BY last_seen DESC').all();
if (rows.length === 0) {
console.log('No sessions found.');
return;
}
console.log(`\nFound ${rows.length} session(s):\n`);
console.log('ID'.padEnd(40) + 'Username'.padEnd(15) + 'Last IP'.padEnd(18) + 'Last Seen'.padEnd(22) + 'Settings');
console.log('-'.repeat(115));
for (const row of rows) {
const id = (row.client_id || '').substring(0, 36).padEnd(40);
const user = (row.username || '-').substring(0, 12).padEnd(15);
const lastIp = (row.last_ip || '-').padEnd(18);
const lastSeen = formatDate(row.last_seen).padEnd(22);
const settings = row.settings || '{}';
console.log(`${id}${user}${lastIp}${lastSeen}${settings}`);
}
console.log('');
}
function showSession(sessionId) {
if (!sessionId) {
console.error('Error: Session ID required');
usage();
process.exit(1);
}
const session = sessions.getSession(sessionId);
if (!session) {
console.error(`Session not found: ${sessionId}`);
process.exit(1);
}
console.log('\nSession Details:');
console.log('-'.repeat(50));
console.log(` ID: ${session.clientId}`);
console.log(` Username: ${session.username || '-'}`);
console.log(` Created: ${formatDate(session.createdAt)}`);
console.log(` Created IP: ${session.createdIp || '-'}`);
console.log(` Last Seen: ${formatDate(session.lastSeen)}`);
console.log(` Last IP: ${session.lastIp || '-'}`);
console.log(` Settings: ${JSON.stringify(session.settings, null, 2)}`);
console.log('');
}
function updateSetting(sessionId, setting) {
if (!sessionId || !setting) {
console.error('Error: Session ID and setting required');
usage();
process.exit(1);
}
const [key, value] = setting.split('=');
if (!key || value === undefined) {
console.error('Error: Setting must be in format key=value');
process.exit(1);
}
const session = sessions.getSession(sessionId);
if (!session) {
console.error(`Session not found: ${sessionId}`);
process.exit(1);
}
// Parse value (handle booleans and numbers)
let parsedValue = value;
if (value === 'true') parsedValue = true;
else if (value === 'false') parsedValue = false;
else if (!isNaN(value) && value !== '') parsedValue = Number(value);
const newSettings = { ...session.settings, [key]: parsedValue };
const updated = sessions.updateSettings(sessionId, newSettings);
if (updated) {
console.log(`Updated ${key} = ${parsedValue}`);
console.log(`New settings: ${JSON.stringify(newSettings)}`);
} else {
console.error('Failed to update settings');
process.exit(1);
}
}
function deleteSession(sessionId) {
if (!sessionId) {
console.error('Error: Session ID required');
usage();
process.exit(1);
}
const session = sessions.getSession(sessionId);
if (!session) {
console.error(`Session not found: ${sessionId}`);
process.exit(1);
}
const db = sessions.initDb();
const result = db.prepare('DELETE FROM sessions WHERE client_id = ?').run(sessionId);
if (result.changes > 0) {
console.log(`Deleted session: ${sessionId}`);
} else {
console.error('Failed to delete session');
process.exit(1);
}
}
function runCleanup() {
const deleted = sessions.cleanupSessions();
console.log(`Cleanup complete. Deleted ${deleted} expired session(s).`);
}
function parseTimeString(timeStr) {
if (!timeStr || typeof timeStr !== 'string') return null;
const match = timeStr.match(/^(\d+)(secs?|mins?|hours?|days?)$/i);
if (!match) return null;
const value = parseInt(match[1], 10);
const unit = match[2].toLowerCase();
if (unit.startsWith('sec')) return value;
if (unit.startsWith('min')) return value * 60;
if (unit.startsWith('hour')) return value * 60 * 60;
if (unit.startsWith('day')) return value * 24 * 60 * 60;
return null;
}
function purgeSessions(timeStr) {
if (!timeStr) {
console.error('Error: Time parameter required (e.g., 10secs, 30mins, 2hours, 7days)');
process.exit(1);
}
const seconds = parseTimeString(timeStr);
if (seconds === null) {
console.error(`Error: Invalid time format: ${timeStr}`);
console.error('Valid formats: Nsecs, Nmins, Nhours, Ndays (e.g., 10secs, 30mins, 2hours, 7days)');
process.exit(1);
}
const db = sessions.initDb();
const cutoff = Math.floor(Date.now() / 1000) - seconds;
const result = db.prepare('DELETE FROM sessions WHERE last_seen < ?').run(cutoff);
console.log(`Purged ${result.changes} session(s) older than ${timeStr}.`);
}
// Initialize DB
sessions.initDb();
// Route commands
switch (command) {
case 'list':
listSessions();
break;
case 'show':
showSession(args[1]);
break;
case 'set':
updateSetting(args[1], args[2]);
break;
case 'delete':
deleteSession(args[1]);
break;
case 'cleanup':
runCleanup();
break;
case 'purge':
purgeSessions(args[1]);
break;
case 'help':
case '--help':
case '-h':
usage();
break;
default:
if (command) {
console.error(`Unknown command: ${command}`);
}
usage();
process.exit(command ? 1 : 0);
}
sessions.closeDb();