-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding-detector.js
More file actions
85 lines (69 loc) · 2.21 KB
/
Copy pathencoding-detector.js
File metadata and controls
85 lines (69 loc) · 2.21 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
/**
* BBSFirewall - Encoding detection (UTF-8 vs CP437)
* Routes clients to the appropriate backend port based on their terminal encoding.
* https://github.com/SysopNetwork/BBSFirewall
*/
const logger = require('./logger');
function detectFromSSHEnvironment(env) {
if (!env) return 'cp437';
const langVars = ['LANG', 'LC_ALL', 'LC_CTYPE'];
for (const varName of langVars) {
const value = env[varName];
if (value && typeof value === 'string') {
const upper = value.toUpperCase();
if (upper.includes('UTF-8') || upper.includes('UTF8')) {
logger.debug(`Detected UTF-8 from ${varName}=${value}`);
return 'utf8';
}
}
}
logger.debug('No UTF-8 indicators in SSH environment, defaulting to CP437');
return 'cp437';
}
function detectFromTerminalType(termType) {
if (!termType || typeof termType !== 'string') return 'cp437';
const term = termType.toLowerCase();
const utf8Terminals = [
'xterm-256color', 'xterm-color', 'xterm',
'screen-256color', 'screen',
'rxvt-unicode', 'konsole', 'gnome',
'linux', 'vt220', 'vt100',
];
const cp437Terminals = [
'ansi', 'ansi-bbs', 'ansi-mono', 'ansi-color',
'pcansi', 'scoansi',
];
for (const t of utf8Terminals) {
if (term.includes(t)) {
logger.debug(`Detected UTF-8 from terminal type: ${termType}`);
return 'utf8';
}
}
for (const t of cp437Terminals) {
if (term.includes(t)) {
logger.debug(`Detected CP437 from terminal type: ${termType}`);
return 'cp437';
}
}
logger.debug(`Unknown terminal type: ${termType}, defaulting to CP437`);
return 'cp437';
}
function detectFromTelnetNegotiation(termType) {
return detectFromTerminalType(termType);
}
function getBackendPortForEncoding(encoding, config) {
if (!config.encodingDetection) return config.backendPort;
if (encoding === 'utf8') {
logger.debug(`Using UTF-8 backend port: ${config.backendPortUTF8}`);
return config.backendPortUTF8;
} else {
logger.debug(`Using CP437 backend port: ${config.backendPortCP437}`);
return config.backendPortCP437;
}
}
module.exports = {
detectFromSSHEnvironment,
detectFromTerminalType,
detectFromTelnetNegotiation,
getBackendPortForEncoding,
};