@ecubelabs/mysql-mcp 1.1.0 → 2.0.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +245 -452
- package/dist/index.js +11 -11
- package/dist/src/config/index.js +265 -131
- package/dist/src/db/index.js +30 -13
- package/dist/src/db/permissions.js +4 -1
- package/dist/src/ssh/config.js +0 -135
- package/dist/src/ssh/known-hosts.js +1 -4
- package/dist/src/ssh/tunnel.js +26 -97
- package/package.json +1 -1
package/dist/src/ssh/config.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
1
|
import * as os from 'os';
|
|
3
2
|
import * as path from 'path';
|
|
4
3
|
export function expandHome(p) {
|
|
@@ -10,137 +9,3 @@ export function expandHome(p) {
|
|
|
10
9
|
}
|
|
11
10
|
return p;
|
|
12
11
|
}
|
|
13
|
-
function splitDirective(line) {
|
|
14
|
-
const trimmed = line.trim();
|
|
15
|
-
if (!trimmed || trimmed.startsWith('#')) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
const eq = trimmed.indexOf('=');
|
|
19
|
-
const ws = trimmed.search(/\s/);
|
|
20
|
-
let sepIndex;
|
|
21
|
-
if (eq !== -1 && (ws === -1 || eq < ws)) {
|
|
22
|
-
sepIndex = eq;
|
|
23
|
-
}
|
|
24
|
-
else if (ws !== -1) {
|
|
25
|
-
sepIndex = ws;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
const keyword = trimmed.slice(0, sepIndex).trim().toLowerCase();
|
|
31
|
-
const value = trimmed
|
|
32
|
-
.slice(sepIndex + 1)
|
|
33
|
-
.trim()
|
|
34
|
-
.replace(/^=/, '')
|
|
35
|
-
.trim();
|
|
36
|
-
if (!keyword || !value) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return [keyword, value];
|
|
40
|
-
}
|
|
41
|
-
function parseLocalForward(value) {
|
|
42
|
-
const parts = value.split(/\s+/).filter(Boolean);
|
|
43
|
-
if (parts.length < 2) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const localRaw = parts[0];
|
|
47
|
-
const localPortStr = localRaw.includes(':') ? localRaw.slice(localRaw.lastIndexOf(':') + 1) : localRaw;
|
|
48
|
-
const localPort = Number(localPortStr);
|
|
49
|
-
if (!Number.isInteger(localPort) || localPort < 0 || localPort > 65535) {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
let remoteHost;
|
|
53
|
-
let remotePortStr;
|
|
54
|
-
const remoteRaw = parts[1];
|
|
55
|
-
const lastColon = remoteRaw.lastIndexOf(':');
|
|
56
|
-
if (lastColon > 0) {
|
|
57
|
-
remoteHost = remoteRaw.slice(0, lastColon);
|
|
58
|
-
remotePortStr = remoteRaw.slice(lastColon + 1);
|
|
59
|
-
}
|
|
60
|
-
else if (parts.length >= 3) {
|
|
61
|
-
remoteHost = remoteRaw;
|
|
62
|
-
remotePortStr = parts[2];
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const remotePort = Number(remotePortStr);
|
|
68
|
-
if (!remoteHost || !Number.isInteger(remotePort) || remotePort <= 0) {
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
return { localPort, remoteHost, remotePort };
|
|
72
|
-
}
|
|
73
|
-
export function readSSHConfigHost(alias, configPath = path.join(os.homedir(), '.ssh', 'config')) {
|
|
74
|
-
if (!fs.existsSync(configPath)) {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
const wanted = alias.trim().toLowerCase();
|
|
78
|
-
let raw;
|
|
79
|
-
try {
|
|
80
|
-
raw = fs.readFileSync(configPath, 'utf8');
|
|
81
|
-
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
throw new Error(`Failed to read SSH config at ${configPath}: ${err.message}`);
|
|
84
|
-
}
|
|
85
|
-
let inBlock = false;
|
|
86
|
-
const entry = {};
|
|
87
|
-
let found = false;
|
|
88
|
-
for (const line of raw.split(/\r?\n/)) {
|
|
89
|
-
const directive = splitDirective(line);
|
|
90
|
-
if (!directive) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const [keyword, value] = directive;
|
|
94
|
-
if (keyword === 'host') {
|
|
95
|
-
inBlock = value.split(/\s+/).some((token) => token.toLowerCase() === wanted);
|
|
96
|
-
if (inBlock) {
|
|
97
|
-
found = true;
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (keyword === 'match') {
|
|
102
|
-
inBlock = false;
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
if (!inBlock) {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
switch (keyword) {
|
|
109
|
-
case 'hostname':
|
|
110
|
-
entry.hostName ??= value;
|
|
111
|
-
break;
|
|
112
|
-
case 'user':
|
|
113
|
-
entry.user ??= value;
|
|
114
|
-
break;
|
|
115
|
-
case 'port': {
|
|
116
|
-
const port = Number(value);
|
|
117
|
-
if (Number.isInteger(port) && port > 0 && port <= 65535) {
|
|
118
|
-
entry.port ??= port;
|
|
119
|
-
}
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
case 'identityfile':
|
|
123
|
-
entry.identityFile ??= expandHome(value.replace(/^"|"$/g, ''));
|
|
124
|
-
break;
|
|
125
|
-
case 'userknownhostsfile':
|
|
126
|
-
entry.userKnownHostsFiles ??=
|
|
127
|
-
value.toLowerCase() === 'none'
|
|
128
|
-
? []
|
|
129
|
-
: value.split(/\s+/).map((file) => expandHome(file.replace(/^"|"$/g, '')));
|
|
130
|
-
break;
|
|
131
|
-
case 'hostkeyalias':
|
|
132
|
-
entry.hostKeyAlias ??= value;
|
|
133
|
-
break;
|
|
134
|
-
case 'localforward': {
|
|
135
|
-
const forward = parseLocalForward(value);
|
|
136
|
-
if (forward && !entry.localForward) {
|
|
137
|
-
entry.localForward = forward;
|
|
138
|
-
}
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
default:
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return found ? entry : null;
|
|
146
|
-
}
|
|
@@ -3,10 +3,7 @@ import * as fs from 'fs';
|
|
|
3
3
|
import { expandHome } from './config.js';
|
|
4
4
|
export const DEFAULT_USER_KNOWN_HOSTS_FILES = ['~/.ssh/known_hosts', '~/.ssh/known_hosts2'];
|
|
5
5
|
const GLOBAL_KNOWN_HOSTS_FILES = ['/etc/ssh/ssh_known_hosts', '/etc/ssh/ssh_known_hosts2'];
|
|
6
|
-
export function knownHostsName(host, port
|
|
7
|
-
if (hostKeyAlias) {
|
|
8
|
-
return hostKeyAlias;
|
|
9
|
-
}
|
|
6
|
+
export function knownHostsName(host, port) {
|
|
10
7
|
const lowered = host.toLowerCase();
|
|
11
8
|
return port === 22 ? lowered : `[${lowered}]:${port}`;
|
|
12
9
|
}
|
package/dist/src/ssh/tunnel.js
CHANGED
|
@@ -1,95 +1,45 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as net from 'net';
|
|
3
3
|
import { Client as SSHClient } from 'ssh2';
|
|
4
|
-
import { MYSQL_HOST, MYSQL_PORT,
|
|
4
|
+
import { MYSQL_HOST, MYSQL_PORT, SSH_ENABLED, SSH_HOST, SSH_PASSPHRASE, SSH_PORT, SSH_PRIVATE_KEY_PATH, SSH_USER, } from '../config/index.js';
|
|
5
5
|
import { log } from '../utils/index.js';
|
|
6
|
-
import { expandHome
|
|
6
|
+
import { expandHome } from './config.js';
|
|
7
7
|
import { DEFAULT_USER_KNOWN_HOSTS_FILES, hostKeyAlgorithms, isKnownHostKey, knownHostsName, lookupKnownHosts, } from './known-hosts.js';
|
|
8
|
-
class LocalPortTakenError extends Error {
|
|
9
|
-
}
|
|
10
8
|
const RECONNECT_MAX_ATTEMPTS = 3;
|
|
11
9
|
const RECONNECT_BASE_DELAY_MS = 1000;
|
|
12
|
-
const PROBE_TIMEOUT_MS = 750;
|
|
13
10
|
const SSH_READY_TIMEOUT_MS = 20000;
|
|
14
11
|
let resolvedTunnelConfig = null;
|
|
15
12
|
export function resolveTunnelConfig() {
|
|
16
13
|
if (resolvedTunnelConfig) {
|
|
17
14
|
return resolvedTunnelConfig;
|
|
18
15
|
}
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
`Either add the alias or set MYSQL_SSH_HOST / MYSQL_SSH_USER explicitly.`);
|
|
26
|
-
}
|
|
27
|
-
log('info', `[ssh] resolved alias "${configHost}" from ~/.ssh/config: ` +
|
|
28
|
-
JSON.stringify({
|
|
29
|
-
hostName: fromConfig.hostName,
|
|
30
|
-
user: fromConfig.user,
|
|
31
|
-
port: fromConfig.port,
|
|
32
|
-
localForward: fromConfig.localForward,
|
|
33
|
-
}));
|
|
34
|
-
}
|
|
35
|
-
const sshHost = SSH_HOST ?? fromConfig?.hostName;
|
|
36
|
-
const sshUser = SSH_USER ?? fromConfig?.user;
|
|
37
|
-
const sshPort = SSH_PORT ?? fromConfig?.port ?? 22;
|
|
38
|
-
const privateKeyPath = expandHome(SSH_PRIVATE_KEY_PATH ?? fromConfig?.identityFile ?? '~/.ssh/id_rsa');
|
|
39
|
-
const localPort = SSH_LOCAL_PORT ?? fromConfig?.localForward?.localPort ?? 0;
|
|
40
|
-
const remoteHost = fromConfig?.localForward?.remoteHost ?? MYSQL_HOST;
|
|
41
|
-
const remotePort = fromConfig?.localForward?.remotePort ?? MYSQL_PORT;
|
|
42
|
-
const missing = [];
|
|
43
|
-
if (!sshHost) {
|
|
44
|
-
missing.push('MYSQL_SSH_HOST (or a HostName in the alias)');
|
|
45
|
-
}
|
|
46
|
-
if (!sshUser) {
|
|
47
|
-
missing.push('MYSQL_SSH_USER (or a User in the alias)');
|
|
48
|
-
}
|
|
49
|
-
if (!remoteHost) {
|
|
50
|
-
missing.push('MYSQL_HOST (or a LocalForward target in the alias)');
|
|
51
|
-
}
|
|
52
|
-
if (missing.length > 0) {
|
|
53
|
-
throw new Error(`SSH tunnel is enabled but underspecified. Missing: ${missing.join(', ')}.`);
|
|
16
|
+
const sshHost = SSH_HOST;
|
|
17
|
+
const sshUser = SSH_USER;
|
|
18
|
+
const remoteHost = MYSQL_HOST;
|
|
19
|
+
const remotePort = MYSQL_PORT;
|
|
20
|
+
if (!sshHost || !sshUser || !remoteHost || remotePort === undefined) {
|
|
21
|
+
throw new Error('SSH tunnel is enabled but underspecified. Pass --ssh-host, --ssh-user, --host and --port together.');
|
|
54
22
|
}
|
|
23
|
+
const sshPort = SSH_PORT ?? 22;
|
|
24
|
+
const privateKeyPath = expandHome(SSH_PRIVATE_KEY_PATH ?? '~/.ssh/id_rsa');
|
|
55
25
|
if (!fs.existsSync(privateKeyPath)) {
|
|
56
26
|
throw new Error(`SSH private key not found at "${privateKeyPath}". ` +
|
|
57
|
-
`
|
|
27
|
+
`Pass --ssh-private-key-path with the key that authenticates ${sshUser}@${sshHost}.`);
|
|
58
28
|
}
|
|
59
29
|
resolvedTunnelConfig = {
|
|
60
|
-
sshHost
|
|
30
|
+
sshHost,
|
|
61
31
|
sshPort,
|
|
62
|
-
sshUser
|
|
32
|
+
sshUser,
|
|
63
33
|
privateKeyPath,
|
|
64
34
|
passphrase: SSH_PASSPHRASE,
|
|
65
|
-
knownHostsFiles:
|
|
66
|
-
hostKeyName: knownHostsName(sshHost, sshPort
|
|
67
|
-
sshTarget:
|
|
68
|
-
|
|
69
|
-
remoteHost: remoteHost,
|
|
35
|
+
knownHostsFiles: DEFAULT_USER_KNOWN_HOSTS_FILES,
|
|
36
|
+
hostKeyName: knownHostsName(sshHost, sshPort),
|
|
37
|
+
sshTarget: `${sshPort === 22 ? '' : `-p ${sshPort} `}${sshUser}@${sshHost}`,
|
|
38
|
+
remoteHost,
|
|
70
39
|
remotePort,
|
|
71
40
|
};
|
|
72
41
|
return resolvedTunnelConfig;
|
|
73
42
|
}
|
|
74
|
-
function probeLocalPort(port) {
|
|
75
|
-
return new Promise((resolve) => {
|
|
76
|
-
const socket = new net.Socket();
|
|
77
|
-
let settled = false;
|
|
78
|
-
const finish = (result) => {
|
|
79
|
-
if (settled) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
settled = true;
|
|
83
|
-
socket.destroy();
|
|
84
|
-
resolve(result);
|
|
85
|
-
};
|
|
86
|
-
socket.setTimeout(PROBE_TIMEOUT_MS);
|
|
87
|
-
socket.once('connect', () => finish(true));
|
|
88
|
-
socket.once('timeout', () => finish(false));
|
|
89
|
-
socket.once('error', () => finish(false));
|
|
90
|
-
socket.connect(port, '127.0.0.1');
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
43
|
let endpointPromise = null;
|
|
94
44
|
let localServer = null;
|
|
95
45
|
let sshClient = null;
|
|
@@ -241,17 +191,13 @@ export function forwardSocket(socket, client, cfg, unavailable = null) {
|
|
|
241
191
|
stream.once('close', () => socket.destroy());
|
|
242
192
|
});
|
|
243
193
|
}
|
|
244
|
-
function startLocalServer(cfg
|
|
194
|
+
function startLocalServer(cfg) {
|
|
245
195
|
return new Promise((resolve, reject) => {
|
|
246
196
|
const server = net.createServer((socket) => forwardSocket(socket, sshClient, cfg, fatalError));
|
|
247
197
|
server.once('error', (err) => {
|
|
248
|
-
|
|
249
|
-
reject(new LocalPortTakenError(`Local port ${requestedPort} was claimed by another process.`));
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
reject(new Error(`Failed to open local tunnel listener on port ${requestedPort}: ${err.message}`));
|
|
198
|
+
reject(new Error(`Failed to open local tunnel listener: ${err.message}`));
|
|
253
199
|
});
|
|
254
|
-
server.listen(
|
|
200
|
+
server.listen(0, '127.0.0.1', () => {
|
|
255
201
|
const address = server.address();
|
|
256
202
|
if (!address || typeof address === 'string') {
|
|
257
203
|
reject(new Error('Local tunnel listener reported no TCP address.'));
|
|
@@ -270,38 +216,21 @@ export function ensureTunnel() {
|
|
|
270
216
|
endpointPromise = (async () => {
|
|
271
217
|
const cfg = resolveTunnelConfig();
|
|
272
218
|
activeConfig = cfg;
|
|
273
|
-
|
|
274
|
-
log('error', `[ssh] 127.0.0.1:${cfg.localPort} is already accepting connections; reusing it (MYSQL_SSH_REUSE_EXISTING=true).`);
|
|
275
|
-
activeEndpoint = { host: '127.0.0.1', port: cfg.localPort, reused: true };
|
|
276
|
-
return activeEndpoint;
|
|
277
|
-
}
|
|
278
|
-
log('info', `[ssh] opening tunnel ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} -> ${cfg.remoteHost}:${cfg.remotePort} ` +
|
|
279
|
-
`(local port ${cfg.localPort === 0 ? 'auto' : cfg.localPort})`);
|
|
219
|
+
log('info', `[ssh] opening tunnel ${cfg.sshUser}@${cfg.sshHost}:${cfg.sshPort} -> ${cfg.remoteHost}:${cfg.remotePort}`);
|
|
280
220
|
const client = await connectSSH(cfg);
|
|
281
221
|
sshClient = client;
|
|
282
222
|
let port;
|
|
283
223
|
try {
|
|
284
|
-
port = await startLocalServer(cfg
|
|
224
|
+
port = await startLocalServer(cfg);
|
|
285
225
|
}
|
|
286
226
|
catch (err) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
throw err;
|
|
291
|
-
}
|
|
292
|
-
log('error', `[ssh] local port ${cfg.localPort} is taken; opening our own tunnel on an auto-assigned port instead.`);
|
|
293
|
-
try {
|
|
294
|
-
port = await startLocalServer(cfg, 0);
|
|
295
|
-
}
|
|
296
|
-
catch (fallbackErr) {
|
|
297
|
-
client.destroy();
|
|
298
|
-
sshClient = null;
|
|
299
|
-
throw fallbackErr;
|
|
300
|
-
}
|
|
227
|
+
client.destroy();
|
|
228
|
+
sshClient = null;
|
|
229
|
+
throw err;
|
|
301
230
|
}
|
|
302
231
|
attachDropHandler(client, cfg);
|
|
303
232
|
log('info', `[ssh] tunnel listening on 127.0.0.1:${port}`);
|
|
304
|
-
activeEndpoint = { host: '127.0.0.1', port
|
|
233
|
+
activeEndpoint = { host: '127.0.0.1', port };
|
|
305
234
|
return activeEndpoint;
|
|
306
235
|
})();
|
|
307
236
|
endpointPromise.catch(() => {
|
package/package.json
CHANGED