tab,关闭连接

This commit is contained in:
bwcx_jzy 2019-08-09 22:28:59 +08:00
parent aaa4271c40
commit 1dc6b82739
5 changed files with 101 additions and 15 deletions

View File

@ -26,6 +26,8 @@ import java.nio.charset.Charset;
import java.util.concurrent.ConcurrentHashMap;
/**
* ssh 处理
*
* @author bwcx_jzy
* @date 2019/8/9
*/
@ -77,6 +79,10 @@ public class SshHandler extends BaseHandler {
if (this.checkCommand(handlerItem, data)) {
return;
}
if ("\t".equals(data)) {
this.sendCommand(handlerItem, data);
return;
}
if (StrUtil.CR.equals(data)) {
if (handlerItem.dataToDst.length() > 0) {
data = StrUtil.CRLF;
@ -88,13 +94,17 @@ public class SshHandler extends BaseHandler {
return;
}
}
handlerItem.outputStream.write(data.getBytes());
handlerItem.outputStream.flush();
this.sendCommand(handlerItem, data);
if (!StrUtil.CRLF.equals(data) && !StrUtil.CR.equals(data)) {
sendBinary(handlerItem.session, data);
}
}
private void sendCommand(HandlerItem handlerItem, String data) throws IOException {
handlerItem.outputStream.write(data.getBytes());
handlerItem.outputStream.flush();
}
private boolean checkCommand(HandlerItem handlerItem, String data) throws Exception {
UserModel userInfo = (UserModel) handlerItem.session.getAttributes().get("userInfo");
if (!userInfo.isDemoUser()) {
@ -116,7 +126,7 @@ public class SshHandler extends BaseHandler {
}
private static class HandlerItem {
private class HandlerItem implements Runnable {
private WebSocketSession session;
private StringBuilder dataToDst = new StringBuilder();
private InputStream inputStream;
@ -141,8 +151,13 @@ public class SshHandler extends BaseHandler {
void startRead() throws JSchException {
this.channel.connect();
ThreadUtil.execute(() -> {
final String[] preMsg = {""};
ThreadUtil.execute(this);
}
@Override
public void run() {
final String[] preMsg = {""};
try {
IoUtil.readLines(inputStream, charset, (LineHandler) msg -> {
msg = StrUtil.CRLF + msg;
if (preMsg[0].equals(msg)) {
@ -160,7 +175,13 @@ public class SshHandler extends BaseHandler {
sendBinary(session, msg);
dataToDst.setLength(0);
});
});
} catch (Exception e) {
if (!this.openSession.isConnected()) {
return;
}
DefaultSystemLog.ERROR().error("读取错误", e);
SshHandler.this.destroy(this.session);
}
}
}
@ -185,7 +206,7 @@ public class SshHandler extends BaseHandler {
try {
session.sendMessage(byteBuffer);
} catch (IOException e) {
DefaultSystemLog.ERROR().error("发送消息失败", e);
DefaultSystemLog.ERROR().error("发送消息失败:" + msg, e);
}
}
}

View File

@ -15,7 +15,7 @@
<script type="text/html" id="bar_monitor">
<a href="javascript:;" th:if="${session.user.isSystemUser()}" class="layui-btn layui-btn-sm layui-btn-normal"
lay-event="update">编辑</a>
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="terminal">终端</a>
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-warm" lay-event="terminal">终端</a>
</script>
<script type="text/javascript">
var col = [

View File

@ -12,7 +12,7 @@
</style>
</head>
<body>
<body onbeforeunload="goodbye()">
<div id="terminal"></div>
</body>
<script type="text/javascript">
@ -21,6 +21,14 @@
var sock;
function loadSuccess() {
top.layer.load({
icon: 16,
shade: 0.2,
time: 1000 * 30,
end: function () {
top.location.reload();
}
});
var url = getSocketHost() + "/ssh?userId=[[${session.user.getUserMd5Key()}]]&sshId=[[${item?.id}]]&nodeId=system&type=ssh";
var terminal = document.getElementById("#terminal");
sock = new WebSocket(url);
@ -35,6 +43,7 @@
//
sock.onopen = function () {
term.open(terminal, true);
top.layer.closeAll();
};
sock.onmessage = function (msg) {
@ -57,7 +66,6 @@
console.log(e);
};
sock.onclose = function (e) {
console.log(e);
term.destroy();
term = null;
sock = null;
@ -76,11 +84,28 @@
height: term._core.charMeasure.height
};
var cols = parseInt(window.innerWidth / style.width, 10) - 1;
var rows = parseInt(window.innerHeight / style.height, 10);
var cols = parseInt((window.innerWidth) / style.width, 10);
var rows = parseInt((window.innerHeight * 0.95) / style.height, 10);
var geometry = {cols: cols, rows: rows};
term.resize(geometry.cols, geometry.rows);
sock.send(JSON.stringify({'resize': geometry}));
}
window.onbeforeunload = function (e) {
goodbye();
};
window.onunload = function () {
goodbye();
};
function goodbye() {
if (sock) {
sock.close();
sock = null;
term = null;
}
}
</script>
</html>

View File

@ -0,0 +1,30 @@
import cn.hutool.extra.ssh.ChannelType;
import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.util.Vector;
import java.util.function.Consumer;
/**
* @author bwcx_jzy
* @date 2019/8/9
*/
public class TestSftp {
public static void main(String[] args) throws SftpException {
Session session = JschUtil.createSession("39.105.190.109", 23, "root", "keepbx&BX123");
ChannelSftp channel = (ChannelSftp) JschUtil.openChannel(session, ChannelType.SFTP);
Vector<ChannelSftp.LsEntry> vector = channel.ls("/jpom/");
String pwd = channel.pwd();
System.out.println(pwd);
// System.out.println(vector);
vector.forEach(new Consumer<ChannelSftp.LsEntry>() {
@Override
public void accept(ChannelSftp.LsEntry lsEntry) {
System.out.println(lsEntry.getFilename());
}
});
}
}

View File

@ -1,9 +1,9 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
import java.nio.charset.Charset;
import java.util.SortedMap;
import java.util.function.Consumer;
import java.util.regex.Pattern;
/**
@ -28,6 +28,16 @@ public class TestString {
System.out.println(Integer.MAX_VALUE);
System.out.println(Charset.availableCharsets());
SortedMap<String, Charset> x = Charset.availableCharsets();
x.values().forEach(new Consumer<Charset>() {
@Override
public void accept(Charset charset) {
String name = charset.name();
if ("utf-8".equalsIgnoreCase(name)) {
System.out.println(charset);
}
}
});
System.out.println(x);
}
}