Skip to content

Commit

Permalink
feat: add ssh2 to deal with command
Browse files Browse the repository at this point in the history
  • Loading branch information
shanshan committed Sep 27, 2021
1 parent 1de98d4 commit 6a1740c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 61 deletions.
22 changes: 15 additions & 7 deletions app/io/controller/home.js
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.

Copy link
@wewoor

wewoor Sep 28, 2021

Contributor

这种敏感信息,要移除,另外这种 server 配置最好,提取到配置文件中去

This comment has been minimized.

Copy link
@ting0130

ting0130 Sep 29, 2021

Contributor

好的,我先把敏感信息去掉,服务器配置是后面直接去调接口取不同的服务器的配置,不是写死的配置

username: 'root',
password: 'abc123'
}, ctx)
}
}
}
7 changes: 4 additions & 3 deletions app/io/middleware/connection.js
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')
};
};
3 changes: 2 additions & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ module.exports = app => {
app.post('/api/tags/update-tag', app.controller.tagManagement.editTag);
app.post('/api/tags/delete-tag', app.controller.tagManagement.deleteTag);

io.of('/').route('getShellCommand', io.controller.home.getShellCommand)
// io.of('/').route('getShellCommand', io.controller.home.getShellCommand)
io.of('/').route('loginServer', io.controller.home.loginServer)
};
76 changes: 37 additions & 39 deletions app/utils/createServer.js
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
}
28 changes: 19 additions & 9 deletions app/web/pages/webTerminal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import 'xterm/css/xterm.css'
const WebTerminal: React.FC = () => {
const [terminal, setTerminal] = useState(null)
const [socket, setSocket] = useState(Socket)
const prefix = 'admin $ '
// const prefix = ''
// const prefix = 'admin $ '
const prefix = ''

let inputText = ''
let currentIndex = 0
Expand All @@ -38,7 +38,7 @@ const WebTerminal: React.FC = () => {
currentIndex = inputTextList.length
}
// socket 通信
socket.emit('chat', inputText)
socket.emit('shellCommand', inputText + '\r')
terminal.prompt()
}

Expand Down Expand Up @@ -145,13 +145,15 @@ const WebTerminal: React.FC = () => {

const initSocket = () => {
socket.on('connect', () => {
console.log(' ======= 与服务端连接成功 ======= ')
})
socket.on('res', (res) => {
console.log(' ======= 服务端的消息 ======= ', res)
console.log('*** SOCKET IO SERVER CONNECTION SUCCESS ***')
})

// 发送消息
socket.emit('getShellCommand', { title: 'name', content: 'zhuting' })
// socket.send('*** CLIENT SEND MESSAGE ***')
// socket.emit('getShellCommand', { command: 'cd /' })

// 登录服务器
socket.emit('loginServer')
}

useEffect(() => {
Expand All @@ -160,7 +162,15 @@ const WebTerminal: React.FC = () => {
}, [])

useEffect(() => {
if (terminal) { onKeyAction() }
if (terminal) {
onKeyAction()

socket.on('serverMsg', (res: string) => {
console.log('*** SERVER MESSAGE ***', res)
if (res) terminal.write(res)
})

}
}, [terminal])

return (
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
"redux-thunk": "^2.3.0",
"socket.io": "^4.1.0",
"socket.io-client": "1.7.0",
"ssh2": "^1.4.0",
"typescript": "^4.2.3",
"utf8": "^3.0.0",
"xterm": "^4.12.0",
"xterm-addon-attach": "^0.6.0",
"xterm-addon-fit": "^0.5.0"
Expand Down
27 changes: 25 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6a1740c

Please sign in to comment.