forked from qishibo/AnotherRedisDesktopManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisClient.js
More file actions
221 lines (176 loc) 路 6.12 KB
/
Copy pathredisClient.js
File metadata and controls
221 lines (176 loc) 路 6.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
import Redis from 'ioredis';
import tunnelssh from 'tunnel-ssh';
import vue from '@/main.js';
const fs = require('fs');
export default {
createConnection(host, port, auth, config, promise = true) {
const options = this.getRedisOptions(host, port, auth, config);
// standalone redis
if (!config.cluster) {
var client = new Redis(port, host, options);
}
// cluster redis
else {
const clusterOptions = this.getClusterOptions(options, config.natMap ? config.natMap : {});
var client = new Redis.Cluster([{port, host}], clusterOptions)
}
if (promise) {
return new Promise((resolve, reject) => {
resolve(client);
});
}
return client;
},
createSSHConnection(sshOptions, host, port, auth, config) {
const sshConfig = {
username: sshOptions.username,
password: sshOptions.password,
host: sshOptions.host,
port: sshOptions.port,
readyTimeout: 20000,
dstHost: host,
dstPort: port,
localHost: '127.0.0.1',
localPort: null, // set null to use available port in local machine
privateKey: sshOptions.privatekey ?
fs.readFileSync(sshOptions.privatekey) : undefined,
passphrase: sshOptions.passphrase ? sshOptions.passphrase : undefined,
keepaliveInterval: 10000,
};
const sshConfigRaw = JSON.parse(JSON.stringify(sshConfig));
const sshPromise = new Promise((resolve, reject) => {
var server = tunnelssh(sshConfig, (error, server) => {
// ssh error only on this, not the 'error' argument...
server.on('error', error => {
vue.$message.error(error.message + ' SSH config right?');
vue.$bus.$emit('closeConnection');
// return reject(error);
});
if (error) {
return reject(error);
}
const listenAddress = server.address();
// ssh standalone redis
if (!config.cluster) {
let client = this.createConnection(listenAddress.address, listenAddress.port, auth, config, false);
return resolve(client);
}
// ssh cluster mode
const configRaw = JSON.parse(JSON.stringify(config));
configRaw.cluster = false;
// forerunner as a single client
let client = this.createConnection(listenAddress.address, listenAddress.port, auth, configRaw, false);
client.on('ready', () => {
// get all cluster nodes info
client.call('cluster', 'nodes', (error, reply) => {
if (error) {
return reject(error);
}
let nodes = this.getClusterNodes(reply);
// create ssh tunnel for each node
this.createClusterSSHTunnels(sshConfigRaw, nodes).then((tunnels) => {
configRaw.cluster = true;
configRaw.natMap = this.initNatMap(tunnels);
// select first line of tunnels to connect
const clusterClient = this.createConnection(tunnels[0].localHost, tunnels[0].localPort, auth, configRaw, false);
resolve(clusterClient);
});
})
});
});
});
return sshPromise;
},
getRedisOptions(host, port, auth, config) {
return {
connectTimeout: 20000,
retryStrategy: (times) => {return this.retryStragety(times, {host, port})},
enableReadyCheck: false,
connectionName: config.connectionName ? config.connectionName : null,
password: auth,
tls: config.sslOptions ? this.getTLSOptions(config.sslOptions) : undefined,
};
},
getClusterOptions(redisOptions, natMap = {}) {
return {
connectionName: redisOptions.connectionName,
enableReadyCheck: false,
slotsRefreshTimeout: 20000,
redisOptions: redisOptions,
natMap: natMap,
};
},
getClusterNodes(nodes, type = 'master') {
let result = [];
nodes = nodes.split("\n");
for (let node of nodes) {
if (!node) {
continue;
}
node = node.trim().split(' ');
if (node[2].includes(type)) {
let dsn = node[1].split('@')[0];
let lastIndex = dsn.lastIndexOf(':');
let host = dsn.substr(0, lastIndex);
let port = dsn.substr(lastIndex + 1);
result.push({host: host, port: port})
}
}
return result;
},
createClusterSSHTunnels(sshConfig, nodes) {
let sshTunnelStack = [];
for (let node of nodes) {
// tunnelssh will change 'config' param, so just copy it
let sshConfigCopy = JSON.parse(JSON.stringify(sshConfig));
// revocery the buffer after json.parse
sshConfigCopy.privateKey && (sshConfigCopy.privateKey = Buffer.from(sshConfigCopy.privateKey));
sshConfigCopy.dstHost = node.host;
sshConfigCopy.dstPort = node.port;
let promise = new Promise((resolve, reject) => {
tunnelssh(sshConfigCopy, (error, server) => {
if (error) {
return reject(error);
}
let addr = server.address();
let line = {
localHost: addr.address, localPort: addr.port,
dstHost: node.host, dstPort: node.port
};
resolve(line);
});
});
sshTunnelStack.push(promise);
}
return Promise.all(sshTunnelStack);
},
initNatMap(tunnels) {
let natMap = {};
for (let line of tunnels) {
natMap[`${line.dstHost}:${line.dstPort}`] = {host: line.localHost, port: line.localPort};
}
return natMap;
},
getTLSOptions(options) {
return {
ca: options.ca ? fs.readFileSync(options.ca) : '',
key: options.key ? fs.readFileSync(options.key) : '',
cert: options.cert ? fs.readFileSync(options.cert) : '',
checkServerIdentity: (servername, cert) => {
// skip certificate hostname validation
return undefined;
},
rejectUnauthorized: false,
}
},
retryStragety(times, connection) {
const maxRetryTimes = 3;
if (times >= maxRetryTimes) {
vue.$message.error("Too Many Attempts To Reconnect. Please Check The Server Status!");
vue.$bus.$emit('closeConnection');
return false;
}
// reconnect after
return Math.min(times * 200, 1000);
},
};