Compare commits

...

9 Commits

Author SHA1 Message Date
youys 23e23fc39c fix 2023-06-09 19:39:28 +08:00
youys 4f79c0350a fix 2023-06-09 18:50:20 +08:00
youys 9c9524f143 add error log 2023-06-09 18:38:30 +08:00
youys 05509e1b22 add error log 2023-06-09 16:19:58 +08:00
youys ab8d1e7884 fix 2023-06-08 18:58:15 +08:00
youys 946e95ca01 windows云主机唯一id,tpiID和envId更换位置 2023-06-08 18:54:20 +08:00
youys c2746343db 去掉延长时间接口调用 2023-06-07 18:23:19 +08:00
youys 58ce89db32 阿里云唯一id生成添加tpiType 2023-05-19 21:16:55 +08:00
youys 11f9a4def3 增加envId 2023-05-18 20:12:39 +08:00
4 changed files with 89 additions and 85 deletions

View File

@ -14,6 +14,7 @@ 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;
@ -62,6 +63,9 @@ 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();
@ -71,8 +75,10 @@ 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(tpiID);
String uniqId = generateUniqId(envId, tpiID, tpiType);
WindowsInfo windowsInfo = getWindowsInfo(uniqId);
@ -94,7 +100,7 @@ public class EducoderGuacamoleWebSocketTunnelHandler extends GuacamoleWebSocketT
socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(guacamoleServer, guacamoleServerPort),
config);
}catch (Exception e){
e.printStackTrace();
logger.error("createTunnel is Exception uniqueId:{}", uniqId, e);
}
// Return a new tunnel which uses the connected socket

View File

@ -11,6 +11,8 @@ 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 {
/**
@ -65,62 +67,11 @@ public abstract class GuacamoleWebSocketTunnelHandler implements WebSocketHandle
}
Thread readThread = new Thread() {
AtomicBoolean flag = new AtomicBoolean(true);
@Override
public void run() {
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);
}
process(session, flag);
}
};
@ -182,4 +133,68 @@ 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);
}
}
}
}

View File

@ -29,11 +29,7 @@ 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());
@ -70,13 +66,12 @@ 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 {
JSONObject object = JSONObject.parseObject(buffer);
@ -96,7 +91,6 @@ public class JchService {
});
}
} else if ("client".equals(tp)) {
overtime = Boolean.TRUE;
String data = object.getString("data");
if (webscoketObj != null) {
transTossh(webscoketObj.getOutputStream(), data);
@ -112,12 +106,10 @@ public class JchService {
connectInfo.getWidth(), connectInfo.getHeight());
}
} else if ("overtime".equals(tp)) {
overtime = Boolean.TRUE;
ConnectInfo connectInfo = webscoketObj.getConnectInfo();
if (webscoketObj.getConnectInfo() != null) {
logger.info("前端主动延长pod存活时间, websocket:{}, tpiID:{},host:{},端口:{}", session.getId(),
connectInfo.getTpiID(), connectInfo.getHost(), connectInfo.getPort());
}
logger.info("前端主动延长pod存活时间, 不需要通过此接口进行延长了:{}, tpiID:{},host:{},端口:{}", session.getId(),
connectInfo.getTpiID(), connectInfo.getHost(), connectInfo.getPort());
}
} catch (Exception e) {
logger.error("转发 websocket {}命令到ssh出错: ", session.getId(), e);
@ -125,15 +117,7 @@ public class JchService {
closeByWebsocket(session);
}
// 延长pod存活时间
try {
if (webscoketObj.getConnectInfo() != null && overtime) {
String gameId = webscoketObj.getConnectInfo().getTpiID();
overTimeService.websshOverTime(gameId);
}
} catch (Exception e) {
logger.error("pod 接收消息出错", e);
}
}
private void transTossh(OutputStream outputStream, String data) throws IOException {
@ -144,7 +128,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);
@ -206,7 +190,6 @@ public class JchService {
webscoketObj.setConnectInfo(connectInfo);
websshService.active("" + connectInfo.getTpiID());
logger.info("websocket {} open连接: tpiId:{},host:{},端口:{}", webSocketSession.getId(), connectInfo.getTpiID(),
connectInfo.getHost(), connectInfo.getPort());
@ -292,7 +275,7 @@ public class JchService {
}
this._close(webscoketObj);
}
public void closeByWebsocket(WebSocketSession session) {
WebscoketObj webscoketObj = findBySession(session);
sessionQueue.remove(webscoketObj);
@ -308,7 +291,7 @@ public class JchService {
/**
* 通过gameId来匹配
*
*
* @param gameId
*/
public int findExistConnectByGameId(String gameId) {

View File

@ -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);