|
@@ -20,24 +20,27 @@ class PageCahe extends Model { |
|
@@ -20,24 +20,27 @@ class PageCahe extends Model { |
20
|
async removeCache(keys, storeTableName) {
|
20
|
async removeCache(keys, storeTableName) {
|
21
|
let self = this;
|
21
|
let self = this;
|
22
|
let servers = await self.findAll();
|
22
|
let servers = await self.findAll();
|
23
|
- for (var i = 0; i < servers.length; i++) {
|
23
|
+ for (let i = 0; i < servers.length; i++) {
|
24
|
try {
|
24
|
try {
|
25
|
- await self._removeAllNginxCache(servers[i], keys, storeTableName);
|
25
|
+ await self._removeCache(servers[i], keys, storeTableName);
|
26
|
} catch (err) {
|
26
|
} catch (err) {
|
27
|
console.log(err)
|
27
|
console.log(err)
|
28
|
}
|
28
|
}
|
29
|
}
|
29
|
}
|
|
|
30
|
+ console.log('over')
|
30
|
}
|
31
|
}
|
31
|
async removeAllCache(storeTableName) {
|
32
|
async removeAllCache(storeTableName) {
|
32
|
let self = this;
|
33
|
let self = this;
|
33
|
let servers = await self.findAll();
|
34
|
let servers = await self.findAll();
|
34
|
- for (var i = 0; i < servers.length; i++) {
|
35
|
+ for (let i = 0; i < servers.length; i++) {
|
|
|
36
|
+ let server = servers[i];
|
35
|
try {
|
37
|
try {
|
36
|
- let server = servers[i];
|
38
|
+ //验证两个变量是否为空避免特殊bug造成为空后拼接的shell执行危险操作
|
37
|
if (server.cachepath && storeTableName) {
|
39
|
if (server.cachepath && storeTableName) {
|
38
|
- await self._connStart(async (conn) => {
|
|
|
39
|
- let scirpt = `rm -rf ${server.cachepath}/${storeTableName}`;
|
|
|
40
|
- await self._evalScript(conn, scirpt);
|
40
|
+ //连接ssh后执行操作
|
|
|
41
|
+ return await self._connStart(async (conn) => {
|
|
|
42
|
+ let script = `rm -rf ${server.cachepath}/${storeTableName}`;
|
|
|
43
|
+ return await self._evalScript(conn, script);
|
41
|
}, server);
|
44
|
}, server);
|
42
|
}
|
45
|
}
|
43
|
} catch (err) {
|
46
|
} catch (err) {
|
|
@@ -45,24 +48,26 @@ class PageCahe extends Model { |
|
@@ -45,24 +48,26 @@ class PageCahe extends Model { |
45
|
}
|
48
|
}
|
46
|
}
|
49
|
}
|
47
|
}
|
50
|
}
|
48
|
- async _removeAllNginxCache(server, keys, storeTableName) {
|
51
|
+ async _removeCache(server, keys, storeTableName) {
|
49
|
let self = this;
|
52
|
let self = this;
|
50
|
let limit = 2;
|
53
|
let limit = 2;
|
51
|
let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0);
|
54
|
let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0);
|
52
|
let blockIndex = 0;
|
55
|
let blockIndex = 0;
|
|
|
56
|
+ //连接ssh后执行操作
|
53
|
await self._connStart(async (conn) => {
|
57
|
await self._connStart(async (conn) => {
|
54
|
for (var i = blockIndex; i < block; i++) {
|
58
|
for (var i = blockIndex; i < block; i++) {
|
55
|
blockIndex = i;
|
59
|
blockIndex = i;
|
56
|
let items = keys.slice(i * limit, i * limit + 2);
|
60
|
let items = keys.slice(i * limit, i * limit + 2);
|
57
|
- let scirpt = self._joinRemoveScript(server, items, storeTableName);
|
|
|
58
|
- self._evalScript(conn, scirpt);
|
61
|
+ let script = self._joinRemoveScript(server, items, storeTableName);
|
|
|
62
|
+ await self._evalScript(conn, script);
|
59
|
}
|
63
|
}
|
60
|
}, server)
|
64
|
}, server)
|
61
|
}
|
65
|
}
|
62
|
- _checkDangerScirpt(scirpt) {
|
|
|
63
|
- if (scirpt.test(/rm -rf \/($| )/)) {
|
66
|
+ _checkDangerScript(script) {
|
|
|
67
|
+ if (/rm -rf \/($| )/.test(script)) {
|
64
|
return false;
|
68
|
return false;
|
65
|
}
|
69
|
}
|
|
|
70
|
+ return true;
|
66
|
}
|
71
|
}
|
67
|
_connStart(callback, server) {
|
72
|
_connStart(callback, server) {
|
68
|
//ssh掉线重连
|
73
|
//ssh掉线重连
|
|
@@ -70,6 +75,7 @@ class PageCahe extends Model { |
|
@@ -70,6 +75,7 @@ class PageCahe extends Model { |
70
|
let connStart = (err) => {
|
75
|
let connStart = (err) => {
|
71
|
let conn = new ssh.Client();
|
76
|
let conn = new ssh.Client();
|
72
|
conn.on('ready', async() => {
|
77
|
conn.on('ready', async() => {
|
|
|
78
|
+ console.log(`connect ${server.host}`);
|
73
|
await callback(conn)
|
79
|
await callback(conn)
|
74
|
resolve();
|
80
|
resolve();
|
75
|
}).on('error', () => {
|
81
|
}).on('error', () => {
|
|
@@ -90,19 +96,23 @@ class PageCahe extends Model { |
|
@@ -90,19 +96,23 @@ class PageCahe extends Model { |
90
|
connStart(0, 0);
|
96
|
connStart(0, 0);
|
91
|
});
|
97
|
});
|
92
|
}
|
98
|
}
|
93
|
- _evalScript(conn, scirpt) {
|
99
|
+ _evalScript(conn, script) {
|
94
|
let self = this;
|
100
|
let self = this;
|
95
|
- return new Promise((resolve) => {
|
|
|
96
|
- console.log(scirpt)
|
|
|
97
|
- resolve();
|
|
|
98
|
- // let result = conn.exec(scirpt, (err, stream) => {
|
|
|
99
|
- // if (!err) {
|
|
|
100
|
- // resolve();
|
|
|
101
|
- // }
|
|
|
102
|
- // });
|
|
|
103
|
- })
|
101
|
+ return new Promise((resolve, reject) => {
|
|
|
102
|
+ //检查shell脚本的危险操作
|
|
|
103
|
+ if (self._checkDangerScript(script)) {
|
|
|
104
|
+ console.log('_evalScript start');
|
|
|
105
|
+ resolve();
|
|
|
106
|
+ // let result = conn.exec(script, (err, stream) => {
|
|
|
107
|
+ // if (!err) {
|
|
|
108
|
+ // resolve();
|
|
|
109
|
+ // }
|
|
|
110
|
+ // });
|
|
|
111
|
+ } else {
|
|
|
112
|
+ reject('检测到危险操作');
|
|
|
113
|
+ }
|
104
|
|
114
|
|
105
|
-
|
115
|
+ })
|
106
|
}
|
116
|
}
|
107
|
_joinRemoveScript(server, keys, storeTableName) {
|
117
|
_joinRemoveScript(server, keys, storeTableName) {
|
108
|
let script = 'rm';
|
118
|
let script = 'rm';
|