dbgate-api 5.0.6 → 5.0.7

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dbgate-api",
3
3
  "main": "src/index.js",
4
- "version": "5.0.6",
4
+ "version": "5.0.7",
5
5
  "homepage": "https://dbgate.org/",
6
6
  "repository": {
7
7
  "type": "git",
@@ -26,8 +26,9 @@
26
26
  "cors": "^2.8.5",
27
27
  "cross-env": "^6.0.3",
28
28
  "dbgate-query-splitter": "^4.9.0",
29
- "dbgate-sqltree": "^5.0.6",
30
- "dbgate-tools": "^5.0.6",
29
+ "dbgate-sqltree": "^5.0.7",
30
+ "dbgate-tools": "^5.0.7",
31
+ "debug": "^4.3.4",
31
32
  "diff": "^5.0.0",
32
33
  "diff2html": "^3.4.13",
33
34
  "eslint": "^6.8.0",
@@ -45,9 +46,9 @@
45
46
  "lodash": "^4.17.21",
46
47
  "ncp": "^2.0.0",
47
48
  "node-cron": "^2.0.3",
48
- "node-ssh-forward": "^0.7.2",
49
49
  "portfinder": "^1.0.28",
50
50
  "simple-encryptor": "^4.0.0",
51
+ "ssh2": "^1.11.0",
51
52
  "tar": "^6.0.5",
52
53
  "uuid": "^3.4.0"
53
54
  },
@@ -63,7 +64,7 @@
63
64
  "devDependencies": {
64
65
  "@types/fs-extra": "^9.0.11",
65
66
  "@types/lodash": "^4.14.149",
66
- "dbgate-types": "^5.0.6",
67
+ "dbgate-types": "^5.0.7",
67
68
  "env-cmd": "^10.1.0",
68
69
  "node-loader": "^1.0.2",
69
70
  "nodemon": "^2.0.2",
@@ -59,13 +59,10 @@ module.exports = {
59
59
 
60
60
  getSettings_meta: true,
61
61
  async getSettings() {
62
- try {
63
- return this.fillMissingSettings(
64
- JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' }))
65
- );
66
- } catch (err) {
67
- return this.fillMissingSettings({});
68
- }
62
+ const res = await lock.acquire('settings', async () => {
63
+ return await this.loadSettings();
64
+ });
65
+ return res;
69
66
  },
70
67
 
71
68
  fillMissingSettings(value) {
@@ -79,12 +76,21 @@ module.exports = {
79
76
  return res;
80
77
  },
81
78
 
79
+ async loadSettings() {
80
+ try {
81
+ const settingsText = await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' });
82
+ return this.fillMissingSettings(JSON.parse(settingsText));
83
+ } catch (err) {
84
+ return this.fillMissingSettings({});
85
+ }
86
+ },
87
+
82
88
  updateSettings_meta: true,
83
89
  async updateSettings(values, req) {
84
90
  if (!hasPermission(`settings/change`, req)) return false;
85
91
 
86
- const res = await lock.acquire('update', async () => {
87
- const currentValue = await this.getSettings();
92
+ const res = await lock.acquire('settings', async () => {
93
+ const currentValue = await this.loadSettings();
88
94
  try {
89
95
  const updated = {
90
96
  ...currentValue,
@@ -1,5 +1,5 @@
1
1
 
2
2
  module.exports = {
3
- version: '5.0.6',
4
- buildTime: '2022-06-27T18:24:19.090Z'
3
+ version: '5.0.7',
4
+ buildTime: '2022-07-16T05:23:26.751Z'
5
5
  };
@@ -1,8 +1,8 @@
1
1
  const fs = require('fs-extra');
2
2
  const platformInfo = require('../utility/platformInfo');
3
3
  const childProcessChecker = require('../utility/childProcessChecker');
4
- const { SSHConnection } = require('node-ssh-forward');
5
4
  const { handleProcessCommunication } = require('../utility/processComm');
5
+ const { SSHConnection } = require('../utility/SSHConnection');
6
6
 
7
7
  async function getSshConnection(connection) {
8
8
  const sshConfig = {
@@ -35,6 +35,8 @@ async function handleStart({ connection, tunnelConfig }) {
35
35
  tunnelConfig,
36
36
  });
37
37
  } catch (err) {
38
+ console.log('Error creating SSH tunnel connection:', err.message);
39
+
38
40
  process.send({
39
41
  msgtype: 'error',
40
42
  connection,
@@ -0,0 +1,251 @@
1
+ /*
2
+ * Copyright 2018 Stocard GmbH.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ const { Client } = require('ssh2');
18
+ const net = require('net');
19
+ const fs = require('fs');
20
+ const os = require('os');
21
+ const path = require('path');
22
+ const debug = require('debug');
23
+
24
+ // interface Options {
25
+ // username?: string;
26
+ // password?: string;
27
+ // privateKey?: string | Buffer;
28
+ // agentForward?: boolean;
29
+ // bastionHost?: string;
30
+ // passphrase?: string;
31
+ // endPort?: number;
32
+ // endHost: string;
33
+ // agentSocket?: string;
34
+ // skipAutoPrivateKey?: boolean;
35
+ // noReadline?: boolean;
36
+ // }
37
+
38
+ // interface ForwardingOptions {
39
+ // fromPort: number;
40
+ // toPort: number;
41
+ // toHost?: string;
42
+ // }
43
+
44
+ class SSHConnection {
45
+ constructor(options) {
46
+ this.options = options;
47
+ this.debug = debug('ssh');
48
+ this.connections = [];
49
+ this.isWindows = process.platform === 'win32';
50
+ if (!options.username) {
51
+ this.options.username = process.env['SSH_USERNAME'] || process.env['USER'];
52
+ }
53
+ if (!options.endPort) {
54
+ this.options.endPort = 22;
55
+ }
56
+ if (!options.privateKey && !options.agentForward && !options.skipAutoPrivateKey) {
57
+ const defaultFilePath = path.join(os.homedir(), '.ssh', 'id_rsa');
58
+ if (fs.existsSync(defaultFilePath)) {
59
+ this.options.privateKey = fs.readFileSync(defaultFilePath);
60
+ }
61
+ }
62
+ }
63
+
64
+ async shutdown() {
65
+ this.debug('Shutdown connections');
66
+ for (const connection of this.connections) {
67
+ connection.removeAllListeners();
68
+ connection.end();
69
+ }
70
+ return new Promise(resolve => {
71
+ if (this.server) {
72
+ this.server.close(resolve);
73
+ }
74
+ return resolve();
75
+ });
76
+ }
77
+
78
+ async tty() {
79
+ const connection = await this.establish();
80
+ this.debug('Opening tty');
81
+ await this.shell(connection);
82
+ }
83
+
84
+ async executeCommand(command) {
85
+ const connection = await this.establish();
86
+ this.debug('Executing command "%s"', command);
87
+ await this.shell(connection, command);
88
+ }
89
+
90
+ async shell(connection, command) {
91
+ return new Promise((resolve, reject) => {
92
+ connection.shell((err, stream) => {
93
+ if (err) {
94
+ return reject(err);
95
+ }
96
+ stream
97
+ .on('close', async () => {
98
+ stream.end();
99
+ process.stdin.unpipe(stream);
100
+ process.stdin.destroy();
101
+ connection.end();
102
+ await this.shutdown();
103
+ return resolve();
104
+ })
105
+ .stderr.on('data', data => {
106
+ return reject(data);
107
+ });
108
+ stream.pipe(process.stdout);
109
+
110
+ if (command) {
111
+ stream.end(`${command}\nexit\n`);
112
+ } else {
113
+ process.stdin.pipe(stream);
114
+ }
115
+ });
116
+ });
117
+ }
118
+
119
+ async establish() {
120
+ let connection;
121
+ if (this.options.bastionHost) {
122
+ connection = await this.connectViaBastion(this.options.bastionHost);
123
+ } else {
124
+ connection = await this.connect(this.options.endHost);
125
+ }
126
+ return connection;
127
+ }
128
+
129
+ async connectViaBastion(bastionHost) {
130
+ this.debug('Connecting to bastion host "%s"', bastionHost);
131
+ const connectionToBastion = await this.connect(bastionHost);
132
+ return new Promise((resolve, reject) => {
133
+ connectionToBastion.forwardOut(
134
+ '127.0.0.1',
135
+ 22,
136
+ this.options.endHost,
137
+ this.options.endPort || 22,
138
+ async (err, stream) => {
139
+ if (err) {
140
+ return reject(err);
141
+ }
142
+ const connection = await this.connect(this.options.endHost, stream);
143
+ return resolve(connection);
144
+ }
145
+ );
146
+ });
147
+ }
148
+
149
+ async connect(host, stream) {
150
+ this.debug('Connecting to "%s"', host);
151
+ const connection = new Client();
152
+ return new Promise(async (resolve, reject) => {
153
+ const options = {
154
+ host,
155
+ port: this.options.endPort,
156
+ username: this.options.username,
157
+ password: this.options.password,
158
+ privateKey: this.options.privateKey,
159
+ };
160
+ if (this.options.agentForward) {
161
+ options['agentForward'] = true;
162
+
163
+ // see https://github.com/mscdex/ssh2#client for agents on Windows
164
+ // guaranteed to give the ssh agent sock if the agent is running (posix)
165
+ let agentDefault = process.env['SSH_AUTH_SOCK'];
166
+ if (this.isWindows) {
167
+ // null or undefined
168
+ if (agentDefault == null) {
169
+ agentDefault = 'pageant';
170
+ }
171
+ }
172
+
173
+ const agentSock = this.options.agentSocket ? this.options.agentSocket : agentDefault;
174
+ if (agentSock == null) {
175
+ throw new Error('SSH Agent Socket is not provided, or is not set in the SSH_AUTH_SOCK env variable');
176
+ }
177
+ options['agent'] = agentSock;
178
+ }
179
+ if (stream) {
180
+ options['sock'] = stream;
181
+ }
182
+ // PPK private keys can be encrypted, but won't contain the word 'encrypted'
183
+ // in fact they always contain a `encryption` header, so we can't do a simple check
184
+ options['passphrase'] = this.options.passphrase;
185
+ const looksEncrypted = this.options.privateKey
186
+ ? this.options.privateKey.toString().toLowerCase().includes('encrypted')
187
+ : false;
188
+ if (looksEncrypted && !options['passphrase'] && !this.options.noReadline) {
189
+ // options['passphrase'] = await this.getPassphrase();
190
+ }
191
+ connection.on('ready', () => {
192
+ this.connections.push(connection);
193
+ return resolve(connection);
194
+ });
195
+
196
+ connection.on('error', error => {
197
+ reject(error);
198
+ });
199
+ try {
200
+ connection.connect(options);
201
+ } catch (error) {
202
+ reject(error);
203
+ }
204
+ });
205
+ }
206
+
207
+ // private async getPassphrase() {
208
+ // return new Promise(resolve => {
209
+ // const rl = readline.createInterface({
210
+ // input: process.stdin,
211
+ // output: process.stdout,
212
+ // });
213
+ // rl.question('Please type in the passphrase for your private key: ', answer => {
214
+ // return resolve(answer);
215
+ // });
216
+ // });
217
+ // }
218
+
219
+ async forward(options) {
220
+ const connection = await this.establish();
221
+ return new Promise((resolve, reject) => {
222
+ this.server = net
223
+ .createServer(socket => {
224
+ this.debug(
225
+ 'Forwarding connection from "localhost:%d" to "%s:%d"',
226
+ options.fromPort,
227
+ options.toHost,
228
+ options.toPort
229
+ );
230
+ connection.forwardOut(
231
+ 'localhost',
232
+ options.fromPort,
233
+ options.toHost || 'localhost',
234
+ options.toPort,
235
+ (error, stream) => {
236
+ if (error) {
237
+ return reject(error);
238
+ }
239
+ socket.pipe(stream);
240
+ stream.pipe(socket);
241
+ }
242
+ );
243
+ })
244
+ .listen(options.fromPort, 'localhost', () => {
245
+ return resolve();
246
+ });
247
+ });
248
+ }
249
+ }
250
+
251
+ module.exports = { SSHConnection };
@@ -1,8 +1,5 @@
1
- const { SSHConnection } = require('node-ssh-forward');
2
- const portfinder = require('portfinder');
3
1
  const fs = require('fs-extra');
4
2
  const { decryptConnection } = require('./crypting');
5
- const { getSshTunnel } = require('./sshTunnel');
6
3
  const { getSshTunnelProxy } = require('./sshTunnelProxy');
7
4
  const platformInfo = require('../utility/platformInfo');
8
5
  const connections = require('../controllers/connections');