Compare commits
No commits in common. "master" and "dev_local_v6" have entirely different histories.
master
...
dev_local_
|
|
@ -14,7 +14,6 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
|
@ -63,9 +62,6 @@ public class EducoderGuacamoleWebSocketTunnelHandler extends GuacamoleWebSocketT
|
|||
return tpiId + "-" + NumberUtils.toInt(position, 1);
|
||||
}
|
||||
|
||||
public String generateUniqId(String envId, String tpiId, String tpiType) {
|
||||
return tpiId + "-" + envId + "-" + tpiType;
|
||||
}
|
||||
@Override
|
||||
GuacamoleTunnel createTunnel(WebSocketSession session) throws GuacamoleException {
|
||||
Properties properties = new Properties();
|
||||
|
|
@ -75,10 +71,8 @@ public class EducoderGuacamoleWebSocketTunnelHandler extends GuacamoleWebSocketT
|
|||
e.printStackTrace();
|
||||
}
|
||||
String tpiID = properties.getProperty("tpiID");
|
||||
String envId = properties.getProperty("envId");
|
||||
String tpiType = properties.getProperty("tpiType");
|
||||
//生成唯一键
|
||||
String uniqId = generateUniqId(envId, tpiID, tpiType);
|
||||
String uniqId = generateUniqId(tpiID);
|
||||
WindowsInfo windowsInfo = getWindowsInfo(uniqId);
|
||||
|
||||
|
||||
|
|
@ -100,7 +94,7 @@ public class EducoderGuacamoleWebSocketTunnelHandler extends GuacamoleWebSocketT
|
|||
socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(guacamoleServer, guacamoleServerPort),
|
||||
config);
|
||||
}catch (Exception e){
|
||||
logger.error("createTunnel is Exception uniqueId:{}", uniqId, e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Return a new tunnel which uses the connected socket
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.socket.*;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public abstract class GuacamoleWebSocketTunnelHandler implements WebSocketHandler, SubProtocolCapable {
|
||||
|
||||
/**
|
||||
|
|
@ -67,11 +65,62 @@ public abstract class GuacamoleWebSocketTunnelHandler implements WebSocketHandle
|
|||
}
|
||||
Thread readThread = new Thread() {
|
||||
|
||||
AtomicBoolean flag = new AtomicBoolean(true);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
process(session, flag);
|
||||
|
||||
StringBuilder buffer = new StringBuilder(BUFFER_SIZE);
|
||||
GuacamoleReader reader = tunnel.acquireReader();
|
||||
char[] readMessage;
|
||||
|
||||
try {
|
||||
|
||||
try {
|
||||
|
||||
// Attempt to read
|
||||
while ((readMessage = reader.read()) != null) {
|
||||
|
||||
// Buffer message
|
||||
buffer.append(readMessage);
|
||||
|
||||
// Flush if we expect to wait or buffer is getting
|
||||
// full
|
||||
if (!reader.available() || buffer.length() >= BUFFER_SIZE) {
|
||||
session.sendMessage(new TextMessage(buffer));
|
||||
buffer.setLength(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// No more data
|
||||
String message = Integer.toString(GuacamoleStatus.SUCCESS.getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session,
|
||||
new CloseStatus(GuacamoleStatus.SUCCESS.getWebSocketCode(), message));
|
||||
}
|
||||
|
||||
// Catch any thrown guacamole exception and attempt
|
||||
// to pass within the WebSocket connection, logging
|
||||
// each error appropriately.
|
||||
catch (GuacamoleClientException e) {
|
||||
logger.info("WebSocket connection terminated: {}", e.getMessage());
|
||||
logger.debug("WebSocket connection terminated due to client error.", e);
|
||||
String message = Integer.toString(e.getStatus().getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session, new CloseStatus(e.getStatus().getWebSocketCode(), message));
|
||||
} catch (GuacamoleConnectionClosedException e) {
|
||||
logger.debug("Connection to guacd closed.", e);
|
||||
String message = Integer.toString(GuacamoleStatus.SUCCESS.getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session,
|
||||
new CloseStatus(GuacamoleStatus.SUCCESS.getWebSocketCode(), message));
|
||||
} catch (GuacamoleException e) {
|
||||
logger.error("Connection to guacd terminated abnormally: {}", e.getMessage());
|
||||
logger.debug("Internal error during connection to guacd.", e);
|
||||
String message = Integer.toString(e.getStatus().getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session, new CloseStatus(e.getStatus().getWebSocketCode(), message));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.debug("I/O error prevents further reads.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
|
@ -133,68 +182,4 @@ public abstract class GuacamoleWebSocketTunnelHandler implements WebSocketHandle
|
|||
return false;
|
||||
}
|
||||
|
||||
private void process(WebSocketSession session, AtomicBoolean flag) {
|
||||
|
||||
if (flag.get()) {
|
||||
StringBuilder buffer = new StringBuilder(BUFFER_SIZE);
|
||||
GuacamoleReader reader = tunnel.acquireReader();
|
||||
char[] readMessage;
|
||||
|
||||
try {
|
||||
|
||||
try {
|
||||
|
||||
// Attempt to read
|
||||
while ((readMessage = reader.read()) != null) {
|
||||
|
||||
// Buffer message
|
||||
buffer.append(readMessage);
|
||||
|
||||
// Flush if we expect to wait or buffer is getting
|
||||
// full
|
||||
if (!reader.available() || buffer.length() >= BUFFER_SIZE) {
|
||||
session.sendMessage(new TextMessage(buffer));
|
||||
buffer.setLength(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// No more data
|
||||
String message = Integer.toString(GuacamoleStatus.SUCCESS.getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session,
|
||||
new CloseStatus(GuacamoleStatus.SUCCESS.getWebSocketCode(), message));
|
||||
}
|
||||
|
||||
// Catch any thrown guacamole exception and attempt
|
||||
// to pass within the WebSocket connection, logging
|
||||
// each error appropriately.
|
||||
catch (GuacamoleClientException e) {
|
||||
logger.info("WebSocket connection terminated: {}", e.getMessage());
|
||||
String message = Integer.toString(e.getStatus().getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session, new CloseStatus(e.getStatus().getWebSocketCode(), message));
|
||||
} catch (GuacamoleConnectionClosedException e) {
|
||||
logger.error("Connection to guacd closed.", e);
|
||||
if(flag.getAndSet(false)) {
|
||||
process(session, flag);
|
||||
}else {
|
||||
String message = Integer.toString(GuacamoleStatus.SUCCESS.getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session,
|
||||
new CloseStatus(GuacamoleStatus.SUCCESS.getWebSocketCode(), message));
|
||||
}
|
||||
} catch (GuacamoleException e) {
|
||||
logger.error("Connection to guacd terminated abnormally ", e);
|
||||
if(flag.getAndSet(false)) {
|
||||
process(session, flag);
|
||||
}else{
|
||||
String message = Integer.toString(e.getStatus().getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session, new CloseStatus(e.getStatus().getWebSocketCode(), message));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.debug("I/O error prevents further reads.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -50,12 +50,6 @@ public class RunOnlyHandler extends TextWebSocketHandler {
|
|||
JSONObject msg = JSON.parseObject(payload);
|
||||
if (LOG_TYPE.equals(msg.getString(TYPE))) {
|
||||
String runOnlyResultKey = RUN_ONLY_RESULT_KEY_PREFIX + msg.getString(DATA);
|
||||
// webssh先收到请求,需要等待一会
|
||||
int i = 0;
|
||||
while (!redisHelper.hasKey(runOnlyResultKey) && i < 5 * 10) {
|
||||
ThreadUtil.sleepSilently(100);
|
||||
i++;
|
||||
}
|
||||
String runOnlyResult = redisHelper.get(runOnlyResultKey);
|
||||
while (CASE_OUTPUT_SEPARATOR.equals(runOnlyResult) && redisHelper.hasKey(runOnlyResultKey)) {
|
||||
ThreadUtil.sleepSilently(100);
|
||||
|
|
|
|||
|
|
@ -1,21 +1,86 @@
|
|||
package net.educoder.bridge.webssh.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ConnectInfo {
|
||||
private String host;
|
||||
private String port;
|
||||
private String username;
|
||||
private String secret;
|
||||
private String myshixun_id;
|
||||
private String gameid;
|
||||
|
||||
private int rows;
|
||||
private int columns;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public String getTpiID() {
|
||||
return myshixun_id;
|
||||
public int getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(int rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
public int getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(int columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return Integer.parseInt(port);
|
||||
}
|
||||
|
||||
public void setPort(String port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
public String getGameid() {
|
||||
return gameid;
|
||||
}
|
||||
|
||||
public void setGameid(String gameid) {
|
||||
this.gameid = gameid;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,11 @@ import net.educoder.bridge.common.utils.Base64Util;
|
|||
|
||||
@Service
|
||||
public class JchService {
|
||||
@Autowired
|
||||
private WebsshService websshService;
|
||||
|
||||
@Autowired
|
||||
private WebsshOverTimeService overTimeService;
|
||||
|
||||
private static List<WebscoketObj> sessionQueue = new CopyOnWriteArrayList<>();
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
|
@ -66,19 +70,22 @@ public class JchService {
|
|||
|
||||
/**
|
||||
* 处理客户端发过来的数据
|
||||
*
|
||||
*
|
||||
* @param buffer
|
||||
*/
|
||||
public void recv(String buffer, WebSocketSession session) {
|
||||
logger.debug("webssh收到数据:{}", buffer);
|
||||
WebscoketObj webscoketObj = findBySession(session);
|
||||
boolean overtime = Boolean.FALSE;
|
||||
|
||||
try {
|
||||
// logger.debug("recv函数,进程:{},sessionID:{},信息:{}",
|
||||
// Thread.currentThread().getId(), session.getId(), buffer);
|
||||
JSONObject object = JSONObject.parseObject(buffer);
|
||||
String tp = object.getString("tp");
|
||||
if ("init".equals(tp)) {
|
||||
// 初始化连接
|
||||
// {"tp":"init","data":{"host":"106.75.96.108","port":"41080","username":"root","secret":"123123","myshixun_id":"1080","rows":"30"}}
|
||||
// {"tp":"init","data":{"host":"106.75.96.108","port":"41080","username":"root","secret":"123123","gameid":"1080","rows":"30"}}
|
||||
ConnectInfo connectInfo = object.getObject("data", ConnectInfo.class);
|
||||
if (webscoketObj != null) {
|
||||
WebscoketObj finalWebscoketObj = webscoketObj;
|
||||
|
|
@ -91,6 +98,7 @@ public class JchService {
|
|||
});
|
||||
}
|
||||
} else if ("client".equals(tp)) {
|
||||
overtime = Boolean.TRUE;
|
||||
String data = object.getString("data");
|
||||
if (webscoketObj != null) {
|
||||
transTossh(webscoketObj.getOutputStream(), data);
|
||||
|
|
@ -106,10 +114,12 @@ public class JchService {
|
|||
connectInfo.getWidth(), connectInfo.getHeight());
|
||||
}
|
||||
} else if ("overtime".equals(tp)) {
|
||||
overtime = Boolean.TRUE;
|
||||
ConnectInfo connectInfo = webscoketObj.getConnectInfo();
|
||||
logger.info("前端主动延长pod存活时间, 不需要通过此接口进行延长了:{}, tpiID:{},host:{},端口:{}", session.getId(),
|
||||
connectInfo.getTpiID(), connectInfo.getHost(), connectInfo.getPort());
|
||||
|
||||
if (webscoketObj.getConnectInfo() != null) {
|
||||
logger.info("前端主动延长pod存活时间, websocket:{}, tpiID:{},host:{},端口:{}", session.getId(),
|
||||
connectInfo.getGameid(), connectInfo.getHost(), connectInfo.getPort());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("转发 websocket {}命令到ssh出错: ", session.getId(), e);
|
||||
|
|
@ -117,7 +127,15 @@ public class JchService {
|
|||
closeByWebsocket(session);
|
||||
}
|
||||
|
||||
|
||||
// 延长pod存活时间
|
||||
try {
|
||||
if (webscoketObj.getConnectInfo() != null && overtime) {
|
||||
String gameId = webscoketObj.getConnectInfo().getGameid();
|
||||
overTimeService.websshOverTime(gameId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("pod 接收消息出错", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void transTossh(OutputStream outputStream, String data) throws IOException {
|
||||
|
|
@ -128,7 +146,7 @@ public class JchService {
|
|||
}
|
||||
|
||||
private void connectTossh(WebscoketObj webscoketObj, ConnectInfo connectInfo, WebSocketSession webSocketSession) {
|
||||
Session session = null;
|
||||
Session session = null;
|
||||
try {
|
||||
JSch jsch = new JSch();
|
||||
JSch.setLogger(jschLogger);
|
||||
|
|
@ -136,7 +154,7 @@ public class JchService {
|
|||
// 启动线程
|
||||
java.util.Properties config = new java.util.Properties();
|
||||
config.put("StrictHostKeyChecking", "no");
|
||||
session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), Integer.parseInt(connectInfo.getPort()));
|
||||
session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), connectInfo.getPort());
|
||||
|
||||
session.setConfig(config);
|
||||
session.setPassword(connectInfo.getSecret());
|
||||
|
|
@ -190,7 +208,8 @@ public class JchService {
|
|||
|
||||
webscoketObj.setConnectInfo(connectInfo);
|
||||
|
||||
logger.info("websocket {} open,连接: tpiId:{},host:{},端口:{}", webSocketSession.getId(), connectInfo.getTpiID(),
|
||||
websshService.active("" + connectInfo.getGameid());
|
||||
logger.info("websocket {} open,连接: tpiId:{},host:{},端口:{}", webSocketSession.getId(), connectInfo.getGameid(),
|
||||
connectInfo.getHost(), connectInfo.getPort());
|
||||
|
||||
// 循环读取
|
||||
|
|
@ -201,7 +220,7 @@ public class JchService {
|
|||
String str = webSocketSession.isOpen() ? "is not" : "is";
|
||||
str = webscoketObj.getSession().getId() + " " + str;
|
||||
str = String.format("websocket %s closed.ssh读取终止,连接: tpiId:%s,host:%s,端口:%s", str,
|
||||
connectInfo.getTpiID(), connectInfo.getHost(), connectInfo.getPort());
|
||||
connectInfo.getGameid(), connectInfo.getHost(), connectInfo.getPort());
|
||||
if (webSocketSession.isOpen()) {
|
||||
logger.error(str);
|
||||
} else {
|
||||
|
|
@ -213,7 +232,7 @@ public class JchService {
|
|||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("连接关闭: websocketId:" + webscoketObj.getSession().getId() + ", tpiId:" + connectInfo.getTpiID()
|
||||
logger.error("连接关闭: websocketId:" + webscoketObj.getSession().getId() + ", tpiId:" + connectInfo.getGameid()
|
||||
+ ", host:" + connectInfo.getHost() + ",端口: " + connectInfo.getPort(), e);
|
||||
} finally {
|
||||
if (session != null) {
|
||||
|
|
@ -275,7 +294,7 @@ public class JchService {
|
|||
}
|
||||
this._close(webscoketObj);
|
||||
}
|
||||
|
||||
|
||||
public void closeByWebsocket(WebSocketSession session) {
|
||||
WebscoketObj webscoketObj = findBySession(session);
|
||||
sessionQueue.remove(webscoketObj);
|
||||
|
|
@ -284,20 +303,20 @@ public class JchService {
|
|||
return;
|
||||
} else {
|
||||
ConnectInfo info = webscoketObj.getConnectInfo();
|
||||
logger.info("websocket {}连接中断 tpiId {} ", session.getId(), info.getTpiID());
|
||||
logger.info("websocket {}连接中断 tpiId {} ", session.getId(), info.getGameid());
|
||||
this._close(webscoketObj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过gameId来匹配
|
||||
*
|
||||
*
|
||||
* @param gameId
|
||||
*/
|
||||
public int findExistConnectByGameId(String gameId) {
|
||||
|
||||
long count = sessionQueue.stream().filter(webscoketObj -> webscoketObj.getConnectInfo() != null
|
||||
&& gameId.equals(webscoketObj.getConnectInfo().getTpiID())).count();
|
||||
&& gameId.equals(webscoketObj.getConnectInfo().getGameid())).count();
|
||||
logger.debug("当前gameID对应的websocket数目:{}", count);
|
||||
return (int) count;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ import javax.annotation.PostConstruct;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
//@Service
|
||||
@Service
|
||||
public class WebsshOverTimeService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// @Autowired
|
||||
@Autowired
|
||||
private WebsshService websshService;
|
||||
|
||||
private static ConcurrentHashMap<String, String> recvGameIdMap = new ConcurrentHashMap<>();
|
||||
|
|
@ -45,7 +45,7 @@ public class WebsshOverTimeService {
|
|||
}
|
||||
if (recvGameIds.length() > 0) {
|
||||
recvGameIds.setLength(recvGameIds.length() - 1);
|
||||
// websshService.overTime(recvGameIds.toString());
|
||||
websshService.overTime(recvGameIds.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("发送接收到websocket消息 tpi失败", e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue