Compare commits
39 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
23e23fc39c | |
|
|
4f79c0350a | |
|
|
9c9524f143 | |
|
|
05509e1b22 | |
|
|
ab8d1e7884 | |
|
|
946e95ca01 | |
|
|
c2746343db | |
|
|
58ce89db32 | |
|
|
11f9a4def3 | |
|
|
c0f3c001eb | |
|
|
5515110795 | |
|
|
6c42fc8eb7 | |
|
|
96d548066d | |
|
|
25d0872950 | |
|
|
1f4ce10d09 | |
|
|
b493994e14 | |
|
|
6c927541ab | |
|
|
36e9eaadc1 | |
|
|
6c14e0c6f4 | |
|
|
dafab616af | |
|
|
33a851b7bc | |
|
|
665ad2d6a8 | |
|
|
a54f970dc7 | |
|
|
7c224d362b | |
|
|
9bb185e3eb | |
|
|
dbc1c99f85 | |
|
|
1eb45e6581 | |
|
|
def875a80d | |
|
|
4d6d43279c | |
|
|
7450bb849c | |
|
|
4046f6de2f | |
|
|
bbd047dcc9 | |
|
|
b5a31830c0 | |
|
|
86dc186b87 | |
|
|
053e232e56 | |
|
|
0574e858af | |
|
|
778b8f7ee3 | |
|
|
31c646e348 | |
|
|
4fc74e3375 |
45
pom.xml
45
pom.xml
|
|
@ -100,6 +100,51 @@
|
|||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!-- Main Guacamole library -->
|
||||
<dependency>
|
||||
<groupId>org.apache.guacamole</groupId>
|
||||
<artifactId>guacamole-common</artifactId>
|
||||
<version>1.4.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.48</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.5.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package net.educoder.bridge;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@MapperScan("net.educoder.bridge.dao")
|
||||
@SpringBootApplication()
|
||||
public class WebsshApplication {
|
||||
|
||||
|
|
@ -10,4 +13,6 @@ public class WebsshApplication {
|
|||
SpringApplication.run(WebsshApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
package net.educoder.bridge.common.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* windows实例信息
|
||||
*/
|
||||
@Data
|
||||
public class WindowsInfo {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String uniqId;
|
||||
|
||||
private String instanceId;
|
||||
|
||||
private String userID;
|
||||
|
||||
private String templateName;
|
||||
|
||||
private String port;
|
||||
|
||||
private String vncPort;
|
||||
|
||||
private String forwardTableId;
|
||||
|
||||
private String forwardEntryId;
|
||||
|
||||
private String vncForwardEntryId;
|
||||
|
||||
private LocalDateTime autoReleaseTime;
|
||||
|
||||
private LocalDateTime createTime;
|
||||
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
private Integer status;
|
||||
|
||||
public WindowsInfo() {
|
||||
}
|
||||
|
||||
public WindowsInfo(String uniqId, String instanceId, String userID, String templateName, String port, String vncPort,
|
||||
LocalDateTime autoReleaseTime, String forwardTableId, String forwardEntryId, String vncForwardEntryId) {
|
||||
this.uniqId = uniqId;
|
||||
this.instanceId = instanceId;
|
||||
this.userID = userID;
|
||||
this.templateName = templateName;
|
||||
this.port = port;
|
||||
this.vncPort = vncPort;
|
||||
this.autoReleaseTime = autoReleaseTime;
|
||||
this.forwardTableId = forwardTableId;
|
||||
this.forwardEntryId = forwardEntryId;
|
||||
this.vncForwardEntryId = vncForwardEntryId;
|
||||
this.createTime = LocalDateTime.now();
|
||||
this.updateTime = LocalDateTime.now();
|
||||
this.status = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package net.educoder.bridge.dao;
|
||||
|
||||
|
||||
import net.educoder.bridge.common.model.WindowsInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface WindowsInfoMapper {
|
||||
|
||||
int deleteByUniqId(String uniqId);
|
||||
|
||||
int insertSelective(WindowsInfo record);
|
||||
|
||||
WindowsInfo selectByUniqId(String uniqId);
|
||||
|
||||
List<WindowsInfo> selectByTpiId(String tpiId);
|
||||
|
||||
List<WindowsInfo> selectByUserId(String userID);
|
||||
|
||||
int updateByUniqIdSelective(WindowsInfo windowsInfo);
|
||||
|
||||
List<String> selectUniqIdByAutoReleaseTime(WindowsInfo windowsInfo);
|
||||
|
||||
List<WindowsInfo> selectNotForwardEntryHost();
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
package net.educoder.bridge.webssh.config;
|
||||
|
||||
import net.educoder.bridge.webssh.handler.GuacamoleWebSocketTunnelHandler;
|
||||
import net.educoder.bridge.webssh.handler.RunOnlyHandler;
|
||||
import net.educoder.bridge.webssh.handler.EducoderGuacamoleWebSocketTunnelHandler;
|
||||
import net.educoder.bridge.webssh.handler.WebsshHandler;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
|
|
@ -19,12 +21,17 @@ public class WebSocketConfig implements WebSocketConfigurer {
|
|||
@Resource
|
||||
RunOnlyHandler runOnlyHandler;
|
||||
|
||||
@Resource
|
||||
EducoderGuacamoleWebSocketTunnelHandler EducoderGuacamoleWebSocketTunnelHandler;
|
||||
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(websshHandler, "/ws").setAllowedOrigins("*");
|
||||
registry.addHandler(runOnlyHandler, "/log").setAllowedOrigins("*");
|
||||
registry.addHandler(EducoderGuacamoleWebSocketTunnelHandler,"/tunnel").setAllowedOrigins("*");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
package net.educoder.bridge.webssh.handler;
|
||||
|
||||
import net.educoder.bridge.common.model.WindowsInfo;
|
||||
import net.educoder.bridge.dao.WindowsInfoMapper;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.apache.guacamole.GuacamoleException;
|
||||
import org.apache.guacamole.net.GuacamoleSocket;
|
||||
import org.apache.guacamole.net.GuacamoleTunnel;
|
||||
import org.apache.guacamole.net.InetGuacamoleSocket;
|
||||
import org.apache.guacamole.net.SimpleGuacamoleTunnel;
|
||||
import org.apache.guacamole.protocol.ConfiguredGuacamoleSocket;
|
||||
import org.apache.guacamole.protocol.GuacamoleConfiguration;
|
||||
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;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
/**
|
||||
* rdp协议连接
|
||||
* @author 悟空
|
||||
*/
|
||||
@Component
|
||||
public class EducoderGuacamoleWebSocketTunnelHandler extends GuacamoleWebSocketTunnelHandler {
|
||||
|
||||
|
||||
@Resource
|
||||
private WindowsInfoMapper windowsInfoMapper;
|
||||
|
||||
|
||||
private static final String DEFAULT_POSITION = "1";
|
||||
|
||||
|
||||
@Value("${guacamole.server.ip}")
|
||||
private String guacamoleServer;
|
||||
@Value("${guacamole.server.port}")
|
||||
private int guacamoleServerPort;
|
||||
|
||||
/**
|
||||
* Logger for this class.
|
||||
*/
|
||||
private final Logger logger = LoggerFactory.getLogger(EducoderGuacamoleWebSocketTunnelHandler.class);
|
||||
|
||||
|
||||
/**
|
||||
* 通过tpiId及position生成uniqId,默认postion为1
|
||||
*/
|
||||
public String generateUniqId(String tpiId) {
|
||||
return generateUniqId(tpiId, DEFAULT_POSITION);
|
||||
}
|
||||
|
||||
public String generateUniqId(String tpiId, String position) {
|
||||
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();
|
||||
try {
|
||||
properties.load(new ByteArrayInputStream(session.getUri().getQuery().replace('&','\n').getBytes()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String tpiID = properties.getProperty("tpiID");
|
||||
String envId = properties.getProperty("envId");
|
||||
String tpiType = properties.getProperty("tpiType");
|
||||
//生成唯一键
|
||||
String uniqId = generateUniqId(envId, tpiID, tpiType);
|
||||
WindowsInfo windowsInfo = getWindowsInfo(uniqId);
|
||||
|
||||
|
||||
// Create our configuration
|
||||
GuacamoleConfiguration config = new GuacamoleConfiguration();
|
||||
config.setProtocol("rdp");
|
||||
config.setParameter("hostname", "39.105.62.120");
|
||||
config.setParameter("port", windowsInfo.getVncPort());
|
||||
config.setParameter("username", "Administrator");
|
||||
config.setParameter("password", "Edu_123123");
|
||||
config.setParameter("security", "nla");
|
||||
config.setParameter("ignore-cert", "true");
|
||||
config.setParameter("width",properties.getProperty("width"));
|
||||
config.setParameter("height",properties.getProperty("height"));
|
||||
|
||||
// Connect to guacd - everything is hard-coded here.
|
||||
GuacamoleSocket socket = null;
|
||||
try{
|
||||
socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(guacamoleServer, guacamoleServerPort),
|
||||
config);
|
||||
}catch (Exception e){
|
||||
logger.error("createTunnel is Exception uniqueId:{}", uniqId, e);
|
||||
}
|
||||
|
||||
// Return a new tunnel which uses the connected socket
|
||||
return new SimpleGuacamoleTunnel(socket);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public WindowsInfo getWindowsInfo(String uniqId) {
|
||||
return windowsInfoMapper.selectByUniqId(uniqId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSubProtocols() {
|
||||
return Collections.singletonList("guacamole");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,200 @@
|
|||
package net.educoder.bridge.webssh.handler;
|
||||
|
||||
import org.apache.guacamole.GuacamoleClientException;
|
||||
import org.apache.guacamole.GuacamoleConnectionClosedException;
|
||||
import org.apache.guacamole.GuacamoleException;
|
||||
import org.apache.guacamole.io.GuacamoleReader;
|
||||
import org.apache.guacamole.io.GuacamoleWriter;
|
||||
import org.apache.guacamole.net.GuacamoleTunnel;
|
||||
import org.apache.guacamole.protocol.GuacamoleStatus;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* The default, minimum buffer size for instructions.
|
||||
*/
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
/**
|
||||
* Logger for this class.
|
||||
*/
|
||||
private final Logger logger = LoggerFactory.getLogger(GuacamoleWebSocketTunnelHandler.class);
|
||||
|
||||
/**
|
||||
* The underlying GuacamoleTunnel. WebSocket reads/writes will be handled as
|
||||
* reads/writes to this tunnel.
|
||||
*/
|
||||
private GuacamoleTunnel tunnel;
|
||||
|
||||
/**
|
||||
* Returns a new tunnel for the given session. How this tunnel is created or
|
||||
* retrieved is implementation-dependent.
|
||||
*
|
||||
* @param session
|
||||
* The session associated with the active WebSocket connection.
|
||||
* @return A connected tunnel, or null if no such tunnel exists.
|
||||
* @throws GuacamoleException
|
||||
* If an error occurs while retrieving the tunnel, or if access
|
||||
* to the tunnel is denied.
|
||||
*/
|
||||
abstract GuacamoleTunnel createTunnel(WebSocketSession session) throws GuacamoleException;
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
logger.debug("afterConnectionEstablished");
|
||||
try {
|
||||
|
||||
// Get tunnel
|
||||
tunnel = createTunnel(session);
|
||||
if (tunnel == null) {
|
||||
String message = Integer.toString(GuacamoleStatus.RESOURCE_NOT_FOUND.getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session,
|
||||
new CloseStatus(GuacamoleStatus.RESOURCE_NOT_FOUND.getWebSocketCode(), message));
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (GuacamoleException e) {
|
||||
logger.error("Creation of WebSocket tunnel to guacd failed: {}", e.getMessage());
|
||||
logger.debug("Error connecting WebSocket tunnel.", e);
|
||||
String message = Integer.toString(e.getStatus().getGuacamoleStatusCode());
|
||||
afterConnectionClosed(session, new CloseStatus(e.getStatus().getWebSocketCode(), message));
|
||||
return;
|
||||
}
|
||||
Thread readThread = new Thread() {
|
||||
|
||||
AtomicBoolean flag = new AtomicBoolean(true);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
process(session, flag);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
readThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
||||
logger.debug("handleMessage");
|
||||
// Ignore inbound messages if there is no associated tunnel
|
||||
if (tunnel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||
|
||||
try {
|
||||
// Write received message
|
||||
writer.write(message.getPayload().toString().toCharArray());
|
||||
} catch (GuacamoleConnectionClosedException e) {
|
||||
logger.debug("Connection to guacd closed.", e);
|
||||
} catch (GuacamoleException e) {
|
||||
logger.debug("WebSocket tunnel write failed.", e);
|
||||
}
|
||||
|
||||
tunnel.releaseWriter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
logger.debug("handleTransportError");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the given status on the given WebSocket connection and closes the
|
||||
* connection.
|
||||
*
|
||||
* @param session
|
||||
* The outbound WebSocket connection to close.
|
||||
* @param closeStatus
|
||||
* The status to send.
|
||||
*/
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
||||
logger.debug("afterConnectionClosed");
|
||||
session.close(closeStatus);
|
||||
try {
|
||||
if (tunnel != null) {
|
||||
tunnel.close();
|
||||
}
|
||||
} catch (GuacamoleException e) {
|
||||
logger.debug("Unable to close WebSocket tunnel.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsPartialMessages() {
|
||||
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,6 +50,12 @@ 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,86 +1,21 @@
|
|||
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 gameid;
|
||||
private String myshixun_id;
|
||||
|
||||
private int rows;
|
||||
private int columns;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
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;
|
||||
public String getTpiID() {
|
||||
return myshixun_id;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,22 +66,19 @@ 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","gameid":"1080","rows":"30"}}
|
||||
// {"tp":"init","data":{"host":"106.75.96.108","port":"41080","username":"root","secret":"123123","myshixun_id":"1080","rows":"30"}}
|
||||
ConnectInfo connectInfo = object.getObject("data", ConnectInfo.class);
|
||||
if (webscoketObj != null) {
|
||||
WebscoketObj finalWebscoketObj = webscoketObj;
|
||||
|
|
@ -98,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);
|
||||
|
|
@ -114,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.getGameid(), 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);
|
||||
|
|
@ -127,15 +117,7 @@ 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 {
|
||||
|
|
@ -146,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);
|
||||
|
|
@ -154,7 +136,7 @@ public class JchService {
|
|||
// 启动线程
|
||||
java.util.Properties config = new java.util.Properties();
|
||||
config.put("StrictHostKeyChecking", "no");
|
||||
session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), connectInfo.getPort());
|
||||
session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), Integer.parseInt(connectInfo.getPort()));
|
||||
|
||||
session.setConfig(config);
|
||||
session.setPassword(connectInfo.getSecret());
|
||||
|
|
@ -208,8 +190,7 @@ public class JchService {
|
|||
|
||||
webscoketObj.setConnectInfo(connectInfo);
|
||||
|
||||
websshService.active("" + connectInfo.getGameid());
|
||||
logger.info("websocket {} open,连接: tpiId:{},host:{},端口:{}", webSocketSession.getId(), connectInfo.getGameid(),
|
||||
logger.info("websocket {} open,连接: tpiId:{},host:{},端口:{}", webSocketSession.getId(), connectInfo.getTpiID(),
|
||||
connectInfo.getHost(), connectInfo.getPort());
|
||||
|
||||
// 循环读取
|
||||
|
|
@ -220,7 +201,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.getGameid(), connectInfo.getHost(), connectInfo.getPort());
|
||||
connectInfo.getTpiID(), connectInfo.getHost(), connectInfo.getPort());
|
||||
if (webSocketSession.isOpen()) {
|
||||
logger.error(str);
|
||||
} else {
|
||||
|
|
@ -232,7 +213,7 @@ public class JchService {
|
|||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("连接关闭: websocketId:" + webscoketObj.getSession().getId() + ", tpiId:" + connectInfo.getGameid()
|
||||
logger.error("连接关闭: websocketId:" + webscoketObj.getSession().getId() + ", tpiId:" + connectInfo.getTpiID()
|
||||
+ ", host:" + connectInfo.getHost() + ",端口: " + connectInfo.getPort(), e);
|
||||
} finally {
|
||||
if (session != null) {
|
||||
|
|
@ -294,7 +275,7 @@ public class JchService {
|
|||
}
|
||||
this._close(webscoketObj);
|
||||
}
|
||||
|
||||
|
||||
public void closeByWebsocket(WebSocketSession session) {
|
||||
WebscoketObj webscoketObj = findBySession(session);
|
||||
sessionQueue.remove(webscoketObj);
|
||||
|
|
@ -303,20 +284,20 @@ public class JchService {
|
|||
return;
|
||||
} else {
|
||||
ConnectInfo info = webscoketObj.getConnectInfo();
|
||||
logger.info("websocket {}连接中断 tpiId {} ", session.getId(), info.getGameid());
|
||||
logger.info("websocket {}连接中断 tpiId {} ", session.getId(), info.getTpiID());
|
||||
this._close(webscoketObj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过gameId来匹配
|
||||
*
|
||||
*
|
||||
* @param gameId
|
||||
*/
|
||||
public int findExistConnectByGameId(String gameId) {
|
||||
|
||||
long count = sessionQueue.stream().filter(webscoketObj -> webscoketObj.getConnectInfo() != null
|
||||
&& gameId.equals(webscoketObj.getConnectInfo().getGameid())).count();
|
||||
&& gameId.equals(webscoketObj.getConnectInfo().getTpiID())).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);
|
||||
|
|
|
|||
|
|
@ -21,4 +21,34 @@ git.backUpDirs=/data/repositories/2018,/data/repositories/2018-2
|
|||
git.repoDir=/data/repositories
|
||||
|
||||
spring.redis.host=127.0.0.1
|
||||
spring.redis.port=6379
|
||||
spring.redis.port=6379
|
||||
|
||||
|
||||
#guacamole
|
||||
guacamole.server.ip=121.40.224.66
|
||||
guacamole.server.port=54482
|
||||
|
||||
|
||||
# mysql
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://rm-bp13v5020p7828r5rso.mysql.rds.aliyuncs.com:3306/testbridge?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
|
||||
spring.datasource.username=testeducoder
|
||||
spring.datasource.password=TEST@123
|
||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||
spring.datasource.initialSize=200
|
||||
spring.datasource.maxActive=400
|
||||
spring.datasource.minIdle=200
|
||||
spring.datasource.validationQuery=select 1
|
||||
spring.datasource.testOnBorrow=false
|
||||
spring.datasource.testOnReturn=false
|
||||
spring.datasource.testWhileIdle=true
|
||||
#mybatis.config-location=classpath:mybatis-config.xml
|
||||
mybatis.type-aliases-package=net.educoder.bridge.**.model
|
||||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||||
#mapper
|
||||
mapper.mappers=tk.mybatis.springboot.util.MyMapper
|
||||
mapper.not-empty=false
|
||||
mapper.identity=MYSQL
|
||||
|
||||
#showSql
|
||||
#logging.level.com.example.demo.dao=debug
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="net.educoder.bridge.dao.WindowsInfoMapper" >
|
||||
<resultMap id="BaseResultMap" type="net.educoder.bridge.common.model.WindowsInfo" >
|
||||
<id column="id" property="id" jdbcType="BIGINT" />
|
||||
<result column="uniq_id" property="uniqId" jdbcType="VARCHAR" />
|
||||
<result column="instance_id" property="instanceId" jdbcType="VARCHAR" />
|
||||
<result column="user_id" property="userID" jdbcType="VARCHAR" />
|
||||
<result column="port" property="port" jdbcType="VARCHAR" />
|
||||
<result column="vnc_port" property="vncPort" jdbcType="VARCHAR" />
|
||||
<result column="template_name" property="templateName" jdbcType="VARCHAR" />
|
||||
<result column="auto_release_time" property="autoReleaseTime" jdbcType="TIMESTAMP" />
|
||||
<result column="forward_table_id" property="forwardTableId" jdbcType="VARCHAR" />
|
||||
<result column="forward_entry_id" property="forwardEntryId" jdbcType="VARCHAR" />
|
||||
<result column="vnc_forward_entry_id" property="vncForwardEntryId" jdbcType="VARCHAR" />
|
||||
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
|
||||
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
|
||||
<result column="status" property="status" jdbcType="INTEGER" />
|
||||
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List" >
|
||||
id, uniq_id, instance_id, user_id, port, vnc_port, template_name, auto_release_time, forward_table_id, forward_entry_id, vnc_forward_entry_id, status
|
||||
</sql>
|
||||
|
||||
<select id="selectByUniqId" resultMap="BaseResultMap" parameterType="java.lang.String" >
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from windows_info
|
||||
where uniq_id = #{uniqId,jdbcType=VARCHAR} and status = 0 limit 1
|
||||
</select>
|
||||
|
||||
<select id="selectByUserId" resultMap="BaseResultMap" parameterType="java.lang.String" >
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from windows_info
|
||||
where user_id = #{userID,jdbcType=VARCHAR}
|
||||
and status = 0
|
||||
</select>
|
||||
|
||||
<select id="selectByTpiId" resultMap="BaseResultMap" parameterType="java.lang.String" >
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from windows_info
|
||||
where uniq_id like concat( #{tpiId,jdbcType=VARCHAR} , '%') and status = 0
|
||||
</select>
|
||||
|
||||
<select id="selectUniqIdByAutoReleaseTime" resultType="java.lang.String" parameterType="net.educoder.bridge.common.model.WindowsInfo">
|
||||
SELECT uniq_id FROM windows_info
|
||||
where status = 0
|
||||
<if test="autoReleaseTime != null">
|
||||
and auto_release_time < #{autoReleaseTime,jdbcType=TIMESTAMP}
|
||||
</if>
|
||||
</select>
|
||||
<select id="selectNotForwardEntryHost" resultMap="BaseResultMap" >
|
||||
SELECT
|
||||
<include refid="Base_Column_List" />
|
||||
FROM windows_info
|
||||
WHERE (forward_entry_id IS NULL OR (vnc_port IS NOT NULL AND vnc_forward_entry_id IS NULL)) and status = 0
|
||||
</select>
|
||||
|
||||
<delete id="deleteByUniqId" parameterType="java.lang.String" >
|
||||
update windows_info set status = -1
|
||||
where uniq_id = #{uniqId,jdbcType=VARCHAR}
|
||||
</delete>
|
||||
|
||||
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="id"
|
||||
parameterType="net.educoder.bridge.common.model.WindowsInfo" >
|
||||
insert into windows_info
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
id,
|
||||
</if>
|
||||
<if test="uniqId != null" >
|
||||
uniq_id,
|
||||
</if>
|
||||
<if test="instanceId != null" >
|
||||
instance_id,
|
||||
</if>
|
||||
<if test="userID != null" >
|
||||
user_id,
|
||||
</if>
|
||||
<if test="port != null" >
|
||||
port,
|
||||
</if>
|
||||
<if test="vncPort != null" >
|
||||
vnc_port,
|
||||
</if>
|
||||
<if test="templateName != null" >
|
||||
template_name,
|
||||
</if>
|
||||
<if test="autoReleaseTime != null" >
|
||||
auto_release_time,
|
||||
</if>
|
||||
<if test="forwardTableId != null" >
|
||||
forward_table_id,
|
||||
</if>
|
||||
<if test="forwardEntryId != null" >
|
||||
forward_entry_id,
|
||||
</if>
|
||||
<if test="vncForwardEntryId != null" >
|
||||
vnc_forward_entry_id,
|
||||
</if>
|
||||
<if test="createTime != null" >
|
||||
create_time,
|
||||
</if>
|
||||
<if test="updateTime != null" >
|
||||
update_time,
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
#{id,jdbcType=BIGINT},
|
||||
</if>
|
||||
<if test="uniqId != null" >
|
||||
#{uniqId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="instanceId != null" >
|
||||
#{instanceId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="userID != null" >
|
||||
#{userID,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="port != null" >
|
||||
#{port,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="vncPort != null" >
|
||||
#{vncPort,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="templateName != null" >
|
||||
#{templateName,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="autoReleaseTime != null" >
|
||||
#{autoReleaseTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
<if test="forwardTableId != null" >
|
||||
#{forwardTableId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="forwardEntryId != null" >
|
||||
#{forwardEntryId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="vncForwardEntryId != null" >
|
||||
#{vncForwardEntryId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="createTime != null" >
|
||||
#{createTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
<if test="updateTime != null" >
|
||||
#{updateTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<update id="updateByUniqIdSelective" parameterType="net.educoder.bridge.common.model.WindowsInfo" >
|
||||
update windows_info
|
||||
<set >
|
||||
<if test="instanceId != null" >
|
||||
instance_id = #{instanceId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="userID != null" >
|
||||
user_id = #{userID,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="port != null" >
|
||||
port = #{port,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="vncPort != null" >
|
||||
vnc_port = #{vncPort,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="templateName != null" >
|
||||
template_name = #{templateName,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="autoReleaseTime != null" >
|
||||
auto_release_time = #{autoReleaseTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
<if test="forwardTableId != null" >
|
||||
forward_table_id = #{forwardTableId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="forwardEntryId != null" >
|
||||
forward_entry_id = #{forwardEntryId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="vncForwardEntryId != null" >
|
||||
vnc_forward_entry_id = #{vncForwardEntryId,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="createTime != null" >
|
||||
create_time = #{createTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
<if test="updateTime != null" >
|
||||
update_time = #{updateTime,jdbcType=TIMESTAMP},
|
||||
</if>
|
||||
</set>
|
||||
where uniq_id = #{uniqId,jdbcType=VARCHAR}
|
||||
</update>
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue