-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.ts
More file actions
331 lines (294 loc) · 12 KB
/
Copy pathstate.ts
File metadata and controls
331 lines (294 loc) · 12 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { Socket } from 'net';
import { Connection } from 'ssh2';
import { Terminal } from '@xterm/headless';
import { SerializeAddon } from '@xterm/addon-serialize';
import { networkInterfaces, homedir } from 'os';
import { get } from 'https';
import fs from 'fs';
import path from 'path';
const PRIVATE_RE = [
/^10\./,
/^172\.(1[6-9]|2\d|3[01])\./,
/^192\.168\./,
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./,
/^127\./,
];
function getPublicIpFromWeb(): Promise<string> {
return new Promise((resolve, reject) => {
const req = get('https://ip.sb', { headers: { 'User-Agent': 'curl/8.0' } }, (res) => {
let data = '';
res.on('data', (chunk: Buffer) => { data += chunk; });
res.on('end', () => resolve(data.trim()));
res.on('error', reject);
});
req.on('error', reject);
req.setTimeout(5000, () => { req.destroy(); reject(new Error('timeout')); });
});
}
async function resolveServerIp(): Promise<string> {
if (process.env.SERVER_PUBLIC_IP) return process.env.SERVER_PUBLIC_IP;
const ifaces = networkInterfaces();
for (const name in ifaces) {
for (const iface of ifaces[name] ?? []) {
if (iface.internal || iface.family !== 'IPv4') continue;
if (!PRIVATE_RE.some(r => r.test(iface.address))) return iface.address;
}
}
try { return await getPublicIpFromWeb(); } catch {}
return '127.0.0.1';
}
export let serverIp = '127.0.0.1';
export const serverIpReady = resolveServerIp().then(ip => {
serverIp = ip;
console.log(`[*] Server IP: ${serverIp}`);
});
// Persistent aliases keyed by stable client id (client.py derives it from /etc/machine-id).
const aliasesPath = path.join(homedir(), '.ssh', 'aliases.json');
export const aliasesByClientId = new Map<string, string>();
export function loadAliases() {
try {
const obj = JSON.parse(fs.readFileSync(aliasesPath, 'utf-8'));
if (obj && typeof obj === 'object') {
for (const [k, v] of Object.entries(obj as Record<string, unknown>)) {
if (typeof v === 'string' && v) aliasesByClientId.set(k, v);
}
}
console.log(`[*] Loaded ${aliasesByClientId.size} alias(es) from ${aliasesPath}`);
} catch (e: any) {
if (e?.code !== 'ENOENT') console.error(`[!] Failed to load aliases: ${e.message}`);
}
}
export function setAlias(clientId: string, alias: string) {
if (!clientId) return;
if (alias) aliasesByClientId.set(clientId, alias);
else aliasesByClientId.delete(clientId);
try {
const obj: Record<string, string> = {};
for (const [k, v] of aliasesByClientId) obj[k] = v;
fs.writeFileSync(aliasesPath, JSON.stringify(obj, null, 2));
} catch (e: any) {
console.error(`[!] Failed to save aliases: ${e.message}`);
}
}
loadAliases();
const visibleLength = (str: string): number => {
return str.replace(/\x1b\[[0-9;]*m/g, '').length;
};
export type PanelTarget =
| { type: 'settings' }
| { type: 'connection'; id: string };
type PanelHitArea = {
startColumn: number;
endColumn: number;
target: PanelTarget;
};
export class SSHConnection {
selectedId: string | null = null;
settingsPanel: boolean = false;
commandMode: boolean = true;
commandInputMode: boolean = false;
commandInputBuffer: string = '';
statusMessage: string = '';
tmuxInterceptMode: boolean = false;
deleteConfirmMode: boolean = false;
rows?: number;
cols?: number;
private panelHitAreas: PanelHitArea[] = [];
constructor(public stream: any) { }
getPanelAtColumn(column: number): PanelTarget | null {
return this.panelHitAreas.find((area) => (
column >= area.startColumn && column <= area.endColumn
))?.target ?? null;
}
showSettingsPanel() {
this.selectedId = null;
this.settingsPanel = true;
this.commandMode = true;
this.commandInputMode = false;
this.commandInputBuffer = '';
this.statusMessage = '';
this.tmuxInterceptMode = false;
this.deleteConfirmMode = false;
this.renderSettingsPanel();
this.drawBottomBar();
}
renderSettingsPanel() {
if (!this.rows || !this.cols || !this.stream) return;
const contentRows = Math.max(1, this.rows - 1);
const panelWidth = Math.max(4, Math.min(72, this.cols - 2));
const innerWidth = panelWidth - 2;
const fit = (text: string) => text.slice(0, innerWidth).padEnd(innerWidth);
const center = (text: string) => {
if (text.length >= innerWidth) return text.slice(0, innerWidth);
const left = Math.floor((innerWidth - text.length) / 2);
return ' '.repeat(left) + text + ' '.repeat(innerWidth - text.length - left);
};
const border = `+${'-'.repeat(innerWidth)}+`;
const lines = [
border,
`|${center('Settings')}|`,
`|${fit('')}|`,
`|${fit(' Reserved for future settings.')}|`,
`|${fit(' No settings are available yet.')}|`,
`|${fit('')}|`,
`|${fit(' Controls')}|`,
`|${fit(' Mouse Click a status bar tab')}|`,
`|${fit(' 0 Open this settings panel')}|`,
`|${fit(' 1-9 Switch to a connection panel')}|`,
`|${fit(' : Open the command prompt')}|`,
border,
];
const topRow = Math.max(1, Math.floor((contentRows - lines.length) / 2) + 1);
const leftCol = Math.max(1, Math.floor((this.cols - panelWidth) / 2) + 1);
this.stream.write('\x1b[2J');
this.stream.write(`\x1b[1;${contentRows}r`);
lines.forEach((line, index) => {
const row = topRow + index;
if (row <= contentRows) this.stream.write(`\x1b[${row};${leftCol}H${line}`);
});
this.stream.write('\x1b[H');
}
drawBottomBar() {
if (!this.rows || !this.cols || !this.stream) return;
this.panelHitAreas = [];
// Command line input takes over the bottom bar (vim-like): no echo into the content area.
if (this.commandInputMode) {
this.writeStatusBar(': ' + this.commandInputBuffer, '42');
return;
}
let status: string;
if (this.tmuxInterceptMode) {
status = '[Tmux >_]';
} else if (this.deleteConfirmMode) {
status = '[Delete? x=confirm]';
} else if (this.settingsPanel) {
status = '[Settings]';
} else if (this.commandMode) {
status = '[Command Mode]';
} else if (this.selectedId) {
const info = activeConnections.get(this.selectedId);
const label = info?.alias || (info && info.user && info.os ? `${info.user}@${info.os}` : this.selectedId) || 'None';
status = `[Connected: ${label}]`;
} else {
status = '[No Connection]';
}
// One-shot command result replaces the bottom bar so the main content stays clean.
if (this.statusMessage) {
this.writeStatusBar(this.statusMessage, '42');
return;
}
const tabs: Array<{ text: string; active: boolean; target: PanelTarget }> = [{
text: ' 0:Settings ',
active: this.settingsPanel,
target: { type: 'settings' },
}];
let index = 1;
activeConnections.forEach((info, id) => {
const isActive = id === this.selectedId;
const label = info.alias || (info.user && info.os ? `${info.user}@${info.os}` : id);
const dcTag = info.disconnected ? ' [DC]' : '';
const prefix = info.tmuxEnabled
? (isActive ? `[<${info.tmuxCurrentWindow}>,${info.tmuxWindowCount}]` : `[${index}]`)
: `${index}`;
tabs.push({
text: ` ${prefix}:${label}${dcTag} `,
active: isActive,
target: { type: 'connection', id },
});
index++;
});
const statusVisLength = visibleLength(status);
const availableForTabs = Math.max(0, this.cols - statusVisLength - 2);
let tabContent = '';
let tabVisLength = 0;
for (const tab of tabs) {
const remaining = availableForTabs - tabVisLength;
if (remaining <= 0) break;
const isTruncated = tab.text.length > remaining;
const visibleText = isTruncated
? (remaining > 3 ? tab.text.slice(0, remaining - 3) + '...' : tab.text.slice(0, remaining))
: tab.text;
if (!visibleText) break;
const startColumn = tabVisLength + 1;
tabVisLength += visibleText.length;
this.panelHitAreas.push({
startColumn,
endColumn: tabVisLength,
target: tab.target,
});
tabContent += (tab.active ? '\x1b[1m' : '') + visibleText + (tab.active ? '\x1b[22m' : '');
if (isTruncated) break;
}
// Calculate visible padding to right-align status: total visible space between tabs and status
const paddingVisLength = this.cols - tabVisLength - statusVisLength;
const padding = ' '.repeat(Math.max(0, paddingVisLength));
// Full line: set color bg based on mode (41=red deleteConfirm, 42=green commandMode, 43=yellow tmuxIntercept, 44=blue passthrough)
const bgColor = this.deleteConfirmMode ? '41' : (this.commandMode ? '42' : (this.tmuxInterceptMode ? '43' : '44'));
const fullLine = `\x1b[${bgColor};37m` + tabContent + padding + status + '\x1b[0m';
this.writeRawStatusBar(fullLine);
}
// Paint a full bottom-bar line (save/restore cursor, clear the line).
private writeRawStatusBar(fullLine: string) {
// Enable basic click tracking with SGR coordinates for terminals wider than 223 columns.
this.stream.write('\x1b[?1000h\x1b[?1006h');
this.stream.write('\x1b[s');
this.stream.write(`\x1b[${this.rows};0H`);
this.stream.write('\x1b[2K');
this.stream.write(fullLine);
this.stream.write('\x1b[u');
}
// Paint plain text on the bottom bar with a solid background, truncating to cols.
private writeStatusBar(text: string, bgColor: string) {
const max = this.cols!;
let line = text;
const visLen = visibleLength(line);
if (visLen > max) {
// Truncate approximately; for simplicity, slice string and adjust
line = line.slice(0, Math.floor(line.length * (max - 3) / visLen)) + '...';
}
const pad = ' '.repeat(Math.max(0, max - visibleLength(line)));
this.writeRawStatusBar(`\x1b[${bgColor};37m${line}${pad}\x1b[0m`);
}
}
export const writeSocketSafe = (socket: Socket, data: string | Buffer): boolean => {
if (socket.destroyed || !socket.writable || socket.writableEnded) return false;
try {
return socket.write(data);
} catch {
try { socket.destroy(); } catch { }
return false;
}
};
export class ConnectionInfo {
socket: Socket;
user: string;
os: string;
terminal: Terminal;
serializeAddon: SerializeAddon;
rows = 24;
cols = 80;
tmuxEnabled: boolean = false;
tmuxCurrentWindow: number = 1;
tmuxWindowCount: number = 1;
disconnected: boolean = false;
disconnectedAt: number = 0;
collectingInfo: boolean = false;
alias: string = '';
clientId: string = '';
constructor(socket: Socket, user: string, os: string) {
this.socket = socket;
this.user = user;
this.os = os;
this.terminal = new Terminal({ rows: 24, cols: 80, allowProposedApi: true });
this.serializeAddon = new SerializeAddon();
this.terminal.loadAddon(this.serializeAddon);
}
resize(rows: number, cols: number) {
this.terminal.resize(cols, rows);
this.rows = rows;
this.cols = cols;
writeSocketSafe(this.socket, `\x1b[8;${rows};${cols}t`);
}
}
export const activeConnections = new Map<string, ConnectionInfo>();
export const activeSSHConnections = new Map<Connection, SSHConnection>();