-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shanshan
committed
Sep 27, 2021
1 parent
1de98d4
commit 6a1740c
Showing
7 changed files
with
104 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
const { createNewServer } = require('../../utils/createServer'); | ||
|
||
module.exports = app => { | ||
return class Controller extends app.Controller { | ||
async getShellCommand () { | ||
const { ctx, logger, app } = this; | ||
const command = ctx.args[0]; | ||
// const result = app.ssh.autoConnectExecCommand('cd /') | ||
ctx.socket.emit('res', { code: 1, content: 'Message received' }); | ||
logger.info(' ======= command ======= ', command) | ||
// logger.info(' ======= result ======= ', result) | ||
// async getShellCommand () { | ||
// const { ctx, logger, app } = this; | ||
// const command = ctx.args[0]; | ||
// ctx.socket.emit('res', { code: 1, content: 'Message received' }); | ||
// logger.info(' ======= command ======= ', command) | ||
// } | ||
|
||
async loginServer () { | ||
const { ctx } = this | ||
createNewServer({ | ||
host: '172.16.100.225', | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
username: 'root', | ||
password: 'abc123' | ||
}, ctx) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
module.exports = () => { | ||
return async (ctx, next) => { | ||
ctx.logger.info(' ======= socket connected ======= '); | ||
ctx.socket.emit('res', ' ======= socket connected ======= '); | ||
ctx.logger.info('*** SOCKET IO CONNECTION SUCCESS ***'); | ||
ctx.socket.emit('serverMsg', '\r\n*** SOCKET IO CONNECTION SUCCESS ***\r\n') | ||
await next(); | ||
ctx.logger.info(' ======== socket disconnection ======== '); | ||
ctx.logger.info('*** SOCKET IO DISCONNECTION ***'); | ||
ctx.socket.emit('serverMsg', '\r\n*** SOCKET IO DISCONNECTION ***\r\n') | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,47 @@ | ||
const SSHClient = require('ssh2').Client; | ||
const utf8 = require('utf8'); | ||
const SSHClient = require('ssh2').Client | ||
const utf8 = require('utf8') | ||
|
||
|
||
export const createNewServer = (machineConfig, socket) => { | ||
const ssh = new SSHClient(); | ||
const { host, username, password } = machineConfig; | ||
// 连接成功 | ||
ssh.on('ready', function () { | ||
const createNewServer = (machineConfig, ctx) => { | ||
const ssh = new SSHClient(); | ||
const { host, username, password } = machineConfig | ||
const { socket } = ctx | ||
|
||
socket.send('\r\n*** SSH CONNECTION SUCCESS ***\r\n'); | ||
// 连接成功 | ||
ssh.on('ready', () => { | ||
|
||
ssh.shell(function (err, stream) { | ||
// 出错 | ||
if (err) { | ||
return socket.send('\r\n*** SSH SHELL ERROR: ' + err.message + ' ***\r\n'); | ||
} | ||
socket.emit('serverMsg', '\r\n*** SSH CONNECTION SUCCESS ***\r\n') | ||
|
||
// 前端发送消息 | ||
socket.on('message', function (data) { | ||
stream.write(data); | ||
}); | ||
ssh.shell((err, stream) => { | ||
|
||
// 通过sh发送消息给前端 | ||
stream.on('data', function (d) { | ||
socket.send(utf8.decode(d.toString('binary'))); | ||
if (err) { | ||
return socket.send('\r\n*** SSH SHELL ERROR: ' + err.message + ' ***\r\n'); | ||
} | ||
|
||
// 关闭连接 | ||
}).on('close', function () { | ||
ssh.end(); | ||
}); | ||
socket.on('shellCommand', (command) => { | ||
stream.write(command) | ||
}) | ||
|
||
stream.on('data', (msg) => { | ||
socket.emit('serverMsg', utf8.decode(msg.toString('binary'))) | ||
|
||
}).on('close', () => { | ||
ssh.end(); | ||
}); | ||
}) | ||
|
||
}).on('close', () => { | ||
socket.emit('serverMsg', '\r\n*** SSH CONNECTION CLOSED ***\r\n') | ||
}).on('error', (err) => { | ||
socket.emit('serverMsg', '\r\n*** SSH CONNECTION ERROR: ' + err.message + ' ***\r\n') | ||
}).connect({ | ||
port: 22, | ||
host, | ||
username, | ||
password | ||
}) | ||
} | ||
|
||
// 关闭连接 | ||
}).on('close', function () { | ||
socket.send('\r\n*** SSH CONNECTION CLOSED ***\r\n'); | ||
|
||
// 连接错误 | ||
}).on('error', function (err) { | ||
socket.send('\r\n*** SSH CONNECTION ERROR: ' + err.message + ' ***\r\n'); | ||
|
||
// 连接 | ||
}).connect({ | ||
port: 22, | ||
host, | ||
username, | ||
password | ||
}); | ||
module.exports = { | ||
createNewServer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2113,7 +2113,7 @@ asn1.js@^5.2.0: | |
minimalistic-assert "^1.0.0" | ||
safer-buffer "^2.1.0" | ||
|
||
asn1@~0.2.0, asn1@~0.2.3: | ||
asn1@^0.2.4, asn1@~0.2.0, asn1@~0.2.3: | ||
version "0.2.4" | ||
resolved "http://registry.npm.dtstack.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136" | ||
integrity sha1-jSR136tVO7M+d7VOWeiAu4ziMTY= | ||
|
@@ -5380,6 +5380,13 @@ cosmiconfig@^7.0.0: | |
path-type "^4.0.0" | ||
yaml "^1.10.0" | ||
|
||
[email protected]: | ||
version "0.0.2" | ||
resolved "http://registry.npm.dtstack.com/cpu-features/-/cpu-features-0.0.2.tgz#9f636156f1155fd04bdbaa028bb3c2fbef3cea7a" | ||
integrity sha1-n2NhVvEVX9BL26oCi7PC++886no= | ||
dependencies: | ||
nan "^2.14.1" | ||
|
||
crc32-stream@^2.0.0: | ||
version "2.0.0" | ||
resolved "http://registry.npm.dtstack.com/crc32-stream/-/crc32-stream-2.0.0.tgz#e3cdd3b4df3168dd74e3de3fbbcb7b297fe908f4" | ||
|
@@ -12861,7 +12868,7 @@ named-placeholders@^1.1.2: | |
dependencies: | ||
lru-cache "^4.1.3" | ||
|
||
nan@^2.12.1, nan@^2.13.2, nan@^2.3.0: | ||
nan@^2.12.1, nan@^2.13.2, nan@^2.14.1, nan@^2.15.0, nan@^2.3.0: | ||
version "2.15.0" | ||
resolved "http://registry.npm.dtstack.com/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee" | ||
integrity sha1-PzSkc/8Y4VwbVia2KQO1rW5mX+4= | ||
|
@@ -18114,6 +18121,17 @@ ssh2@^0.8.2, ssh2@^0.8.9: | |
dependencies: | ||
ssh2-streams "~0.4.10" | ||
|
||
ssh2@^1.4.0: | ||
version "1.4.0" | ||
resolved "http://registry.npm.dtstack.com/ssh2/-/ssh2-1.4.0.tgz#e32e8343394364c922bad915a5a7fecd67d0f5c5" | ||
integrity sha1-4y6DQzlDZMkiutkVpaf+zWfQ9cU= | ||
dependencies: | ||
asn1 "^0.2.4" | ||
bcrypt-pbkdf "^1.0.2" | ||
optionalDependencies: | ||
cpu-features "0.0.2" | ||
nan "^2.15.0" | ||
|
||
sshpk@^1.7.0: | ||
version "1.16.1" | ||
resolved "http://registry.npm.dtstack.com/sshpk/-/sshpk-1.16.1.tgz#fb661c0bef29b39db40769ee39fa70093d6f6877" | ||
|
@@ -19542,6 +19560,11 @@ user-home@^2.0.0: | |
dependencies: | ||
os-homedir "^1.0.0" | ||
|
||
utf8@^3.0.0: | ||
version "3.0.0" | ||
resolved "http://registry.npm.dtstack.com/utf8/-/utf8-3.0.0.tgz#f052eed1364d696e769ef058b183df88c87f69d1" | ||
integrity sha1-8FLu0TZNaW52nvBYsYPfiMh/adE= | ||
|
||
util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: | ||
version "1.0.2" | ||
resolved "http://registry.npm.dtstack.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" | ||
|
这种敏感信息,要移除,另外这种 server 配置最好,提取到配置文件中去