2021-02-03 21:53:24 +01:00
|
|
|
import fs from 'fs';
|
2020-05-14 15:21:57 +02:00
|
|
|
import { ipcMain } from 'electron';
|
2020-09-17 17:58:12 +02:00
|
|
|
import { ClientsFactory } from '../libs/ClientsFactory';
|
2020-05-14 15:21:57 +02:00
|
|
|
|
2020-08-19 16:25:42 +02:00
|
|
|
export default connections => {
|
|
|
|
ipcMain.handle('test-connection', async (event, conn) => {
|
2021-02-03 21:53:24 +01:00
|
|
|
const params = {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
2021-04-06 12:48:40 +02:00
|
|
|
password: conn.password,
|
2022-02-16 09:14:46 +01:00
|
|
|
application_name: 'Antares SQL',
|
|
|
|
readonly: conn.readonly
|
2021-02-03 21:53:24 +01:00
|
|
|
};
|
|
|
|
|
2021-03-17 16:51:26 +01:00
|
|
|
if (conn.database)
|
|
|
|
params.database = conn.database;
|
|
|
|
|
2022-02-16 09:14:46 +01:00
|
|
|
if (conn.databasePath)
|
|
|
|
params.databasePath = conn.databasePath;
|
|
|
|
|
2021-02-03 21:53:24 +01:00
|
|
|
if (conn.ssl) {
|
|
|
|
params.ssl = {
|
|
|
|
key: conn.key ? fs.readFileSync(conn.key) : null,
|
|
|
|
cert: conn.cert ? fs.readFileSync(conn.cert) : null,
|
|
|
|
ca: conn.ca ? fs.readFileSync(conn.ca) : null,
|
2022-04-10 10:49:27 +02:00
|
|
|
ciphers: conn.ciphers,
|
|
|
|
rejectUnauthorized: !conn.untrustedConnection
|
2021-02-03 21:53:24 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-05 09:30:52 +02:00
|
|
|
if (conn.ssh) {
|
|
|
|
params.ssh = {
|
|
|
|
host: conn.sshHost,
|
|
|
|
username: conn.sshUser,
|
|
|
|
password: conn.sshPass,
|
|
|
|
port: conn.sshPort ? conn.sshPort : 22,
|
2021-10-07 14:58:31 +02:00
|
|
|
privateKey: conn.sshKey ? fs.readFileSync(conn.sshKey) : null,
|
|
|
|
passphrase: conn.sshPassphrase
|
2021-07-05 09:30:52 +02:00
|
|
|
};
|
|
|
|
}
|
2020-06-05 21:00:15 +02:00
|
|
|
|
2020-05-14 15:21:57 +02:00
|
|
|
try {
|
2022-03-12 10:01:22 +01:00
|
|
|
const connection = await ClientsFactory.getClient({
|
2021-07-05 09:30:52 +02:00
|
|
|
client: conn.client,
|
|
|
|
params
|
|
|
|
});
|
2021-06-25 09:29:59 +02:00
|
|
|
await connection.connect();
|
2021-07-05 09:30:52 +02:00
|
|
|
|
2020-09-24 13:09:01 +02:00
|
|
|
await connection.select('1+1').run();
|
|
|
|
connection.destroy();
|
2020-05-14 15:21:57 +02:00
|
|
|
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
2022-02-16 09:14:46 +01:00
|
|
|
return { status: 'error', response: err.toString() };
|
2020-05-14 15:21:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-19 16:25:42 +02:00
|
|
|
ipcMain.handle('check-connection', async (event, uid) => {
|
2020-05-14 15:21:57 +02:00
|
|
|
return uid in connections;
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('connect', async (event, conn) => {
|
2021-02-03 21:53:24 +01:00
|
|
|
const params = {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
2021-04-06 12:48:40 +02:00
|
|
|
password: conn.password,
|
2022-02-16 09:14:46 +01:00
|
|
|
application_name: 'Antares SQL',
|
|
|
|
readonly: conn.readonly
|
2021-02-03 21:53:24 +01:00
|
|
|
};
|
|
|
|
|
2021-03-17 16:51:26 +01:00
|
|
|
if (conn.database)
|
|
|
|
params.database = conn.database;
|
|
|
|
|
2022-02-16 09:14:46 +01:00
|
|
|
if (conn.databasePath)
|
|
|
|
params.databasePath = conn.databasePath;
|
|
|
|
|
2021-07-09 10:26:16 +02:00
|
|
|
if (conn.schema)
|
|
|
|
params.schema = conn.schema;
|
|
|
|
|
2021-02-03 21:53:24 +01:00
|
|
|
if (conn.ssl) {
|
|
|
|
params.ssl = {
|
|
|
|
key: conn.key ? fs.readFileSync(conn.key) : null,
|
|
|
|
cert: conn.cert ? fs.readFileSync(conn.cert) : null,
|
|
|
|
ca: conn.ca ? fs.readFileSync(conn.ca) : null,
|
2022-04-10 10:49:27 +02:00
|
|
|
ciphers: conn.ciphers,
|
|
|
|
rejectUnauthorized: !conn.untrustedConnection
|
2021-02-03 21:53:24 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-05 09:30:52 +02:00
|
|
|
if (conn.ssh) {
|
|
|
|
params.ssh = {
|
|
|
|
host: conn.sshHost,
|
|
|
|
username: conn.sshUser,
|
|
|
|
password: conn.sshPass,
|
|
|
|
port: conn.sshPort ? conn.sshPort : 22,
|
2021-10-07 14:58:31 +02:00
|
|
|
privateKey: conn.sshKey ? fs.readFileSync(conn.sshKey) : null,
|
|
|
|
passphrase: conn.sshPassphrase
|
2021-07-05 09:30:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:34:56 +02:00
|
|
|
try {
|
2022-03-12 10:01:22 +01:00
|
|
|
const connection = ClientsFactory.getClient({
|
2021-03-16 18:42:03 +01:00
|
|
|
client: conn.client,
|
|
|
|
params,
|
2021-05-14 17:17:37 +02:00
|
|
|
poolSize: 5
|
2021-03-16 18:42:03 +01:00
|
|
|
});
|
|
|
|
|
2020-09-24 13:09:01 +02:00
|
|
|
await connection.connect();
|
2020-06-15 18:23:51 +02:00
|
|
|
|
2021-01-23 15:50:21 +01:00
|
|
|
const structure = await connection.getStructure(new Set());
|
2020-09-24 13:09:01 +02:00
|
|
|
|
|
|
|
connections[conn.uid] = connection;
|
2020-09-19 18:10:57 +02:00
|
|
|
|
2020-05-20 18:00:14 +02:00
|
|
|
return { status: 'success', response: structure };
|
2020-05-17 19:34:56 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
2020-05-14 15:21:57 +02:00
|
|
|
});
|
2020-05-18 18:06:32 +02:00
|
|
|
|
|
|
|
ipcMain.handle('disconnect', (event, uid) => {
|
|
|
|
connections[uid].destroy();
|
|
|
|
delete connections[uid];
|
|
|
|
});
|
2020-05-14 15:21:57 +02:00
|
|
|
};
|