|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
package com.fr.design.mainframe.socketio; |
|
|
|
|
|
|
|
|
|
import com.fr.config.RemoteConfigEvent; |
|
|
|
|
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
|
|
|
|
import com.fr.design.DesignerEnvManager; |
|
|
|
|
import com.fr.design.EnvChangeEntrance; |
|
|
|
|
import com.fr.design.dialog.FineJOptionPane; |
|
|
|
@ -19,34 +18,24 @@ import com.fr.design.ui.util.UIUtil;
|
|
|
|
|
import com.fr.design.utils.BrowseUtils; |
|
|
|
|
import com.fr.event.EventDispatcher; |
|
|
|
|
import com.fr.general.CloudCenter; |
|
|
|
|
import com.fr.general.ComparatorUtils; |
|
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
|
import com.fr.report.RemoteDesignConstants; |
|
|
|
|
import com.fr.serialization.SerializerHelper; |
|
|
|
|
import com.fr.stable.ArrayUtils; |
|
|
|
|
import com.fr.stable.StableUtils; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
import com.fr.third.apache.log4j.spi.LoggingEvent; |
|
|
|
|
import com.fr.third.org.apache.http.client.config.RequestConfig; |
|
|
|
|
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
|
import com.fr.third.org.apache.http.client.methods.HttpGet; |
|
|
|
|
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
|
|
|
|
import com.fr.third.org.apache.http.conn.ssl.TrustSelfSignedStrategy; |
|
|
|
|
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
|
import com.fr.third.org.apache.http.impl.client.HttpClients; |
|
|
|
|
import com.fr.third.org.apache.http.ssl.SSLContexts; |
|
|
|
|
import com.fr.web.WebSocketConfig; |
|
|
|
|
import com.fr.web.socketio.WebSocketProtocol; |
|
|
|
|
import com.fr.workspace.WorkContext; |
|
|
|
|
import com.fr.workspace.Workspace; |
|
|
|
|
import com.fr.workspace.base.WorkspaceConstants; |
|
|
|
|
import com.fr.workspace.connect.WorkspaceConnection; |
|
|
|
|
import com.fr.workspace.connect.WorkspaceConnectionInfo; |
|
|
|
|
import io.socket.client.IO; |
|
|
|
|
import io.socket.client.Socket; |
|
|
|
|
import io.socket.emitter.Emitter; |
|
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
|
import javax.swing.BorderFactory; |
|
|
|
|
import javax.swing.JOptionPane; |
|
|
|
|
import javax.swing.JPanel; |
|
|
|
@ -55,13 +44,8 @@ import java.awt.BorderLayout;
|
|
|
|
|
import java.awt.Color; |
|
|
|
|
import java.awt.Cursor; |
|
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.net.URL; |
|
|
|
|
import java.security.KeyStore; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Timer; |
|
|
|
|
|
|
|
|
|
public class DesignerSocketIO { |
|
|
|
@ -80,8 +64,8 @@ public class DesignerSocketIO {
|
|
|
|
|
private static Timer disConnectHintTimer = null; |
|
|
|
|
private static long disConnectHintTimerDelay = 3000; |
|
|
|
|
private static final int TIMEOUT = 5000; |
|
|
|
|
//维护一个当前工作环境的uri列表
|
|
|
|
|
private static String[] uri; |
|
|
|
|
//维护一个当前工作环境的websocket连接信息列表
|
|
|
|
|
private static ConnectionInfo[] connectionInfos; |
|
|
|
|
//维护一个关于uri列表的计数器
|
|
|
|
|
private static int count; |
|
|
|
|
// 当前webSocket选择的协议
|
|
|
|
@ -104,7 +88,7 @@ public class DesignerSocketIO {
|
|
|
|
|
} |
|
|
|
|
//每当更换工作环境,更新uri列表,同时更新计数器count
|
|
|
|
|
try { |
|
|
|
|
uri = getSocketUri(); |
|
|
|
|
connectionInfos = ConnectionInfoFactory.createConnectionInfo(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
@ -116,8 +100,9 @@ public class DesignerSocketIO {
|
|
|
|
|
private static void createSocket() { |
|
|
|
|
//根据uri和计数器建立连接,并注册监听
|
|
|
|
|
try { |
|
|
|
|
if (count < uri.length) { |
|
|
|
|
socket = IO.socket(new URI(uri[count]), createOptions()); |
|
|
|
|
if (count < connectionInfos.length) { |
|
|
|
|
ConnectionInfo connectionInfo = connectionInfos[count]; |
|
|
|
|
socket = IO.socket(new URI(connectionInfo.getUri()), connectionInfo.getSocketConfig().createOptions()); |
|
|
|
|
socket.on(WorkspaceConstants.WS_LOGRECORD, printLog); |
|
|
|
|
socket.on(WorkspaceConstants.CONFIG_MODIFY, modifyConfig); |
|
|
|
|
socket.on(Socket.EVENT_CONNECT_ERROR, failRetry); |
|
|
|
@ -135,37 +120,7 @@ public class DesignerSocketIO {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static IO.Options createOptions() { |
|
|
|
|
IO.Options options = new IO.Options(); |
|
|
|
|
try { |
|
|
|
|
if (ComparatorUtils.equals(currentProtocol, HTTPS)) { |
|
|
|
|
options.sslContext = getSSLContext(); |
|
|
|
|
options.hostnameVerifier = NoopHostnameVerifier.INSTANCE; |
|
|
|
|
options.secure = true; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
return options; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static SSLContext getSSLContext() throws Exception { |
|
|
|
|
WorkspaceConnectionInfo connection = getConnectionInfo(); |
|
|
|
|
String certPath = connection.getCertPath(); |
|
|
|
|
String certSecretKey = connection.getCertSecretKey(); |
|
|
|
|
if (StringUtils.isBlank(certPath) || StringUtils.isBlank(certSecretKey)) { |
|
|
|
|
return SSLContexts.createDefault(); |
|
|
|
|
} |
|
|
|
|
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
|
|
|
try (FileInputStream keystore = new FileInputStream(new File(certPath))) { |
|
|
|
|
trustStore.load(keystore, certSecretKey.toCharArray()); |
|
|
|
|
} |
|
|
|
|
return SSLContexts.custom() |
|
|
|
|
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) |
|
|
|
|
.build(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static WorkspaceConnectionInfo getConnectionInfo() { |
|
|
|
|
public static WorkspaceConnectionInfo getConnectionInfo() { |
|
|
|
|
if (DesignerWorkspaceInfoContext.getWorkspaceInfo() == null) { |
|
|
|
|
String currentName = DesignerEnvManager.getEnvManager().getCurEnvName(); |
|
|
|
|
DesignerWorkspaceInfo info = DesignerEnvManager.getEnvManager().getWorkspaceInfo(currentName); |
|
|
|
@ -175,34 +130,6 @@ public class DesignerSocketIO {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static String[] getSocketUri() throws IOException { |
|
|
|
|
Workspace current = WorkContext.getCurrent(); |
|
|
|
|
URL url = new URL(current.getPath()); |
|
|
|
|
Integer[] ports = WebSocketConfig.getInstance().getAvailablePorts(); |
|
|
|
|
WorkspaceConnection connection = current.getConnection(); |
|
|
|
|
// 服务器配置https webSocket可能是wss也可能是ws webSocket的协议可以单独配置
|
|
|
|
|
WebSocketProtocol webSocketProtocol = WebSocketConfig.getInstance().getProtocol(); |
|
|
|
|
currentProtocol = webSocketProtocol == WebSocketProtocol.PLAIN ? HTTP : HTTPS; |
|
|
|
|
if (WebSocketConfig.getInstance().isUsingProxy()) { |
|
|
|
|
// 如果配置了代理服务器跟随服务器协议
|
|
|
|
|
currentProtocol = url.getProtocol(); |
|
|
|
|
} |
|
|
|
|
String[] result = new String[ports.length]; |
|
|
|
|
for (int i = 0; i < ports.length; i++) { |
|
|
|
|
result[i] = String.format("%s://%s:%s%s?%s=%s&%s=%s", |
|
|
|
|
currentProtocol, |
|
|
|
|
url.getHost(), |
|
|
|
|
ports[i], |
|
|
|
|
WorkspaceConstants.WS_NAMESPACE, |
|
|
|
|
DecisionServiceConstants.WEB_SOCKET_TOKEN_NAME, |
|
|
|
|
connection.getToken(), |
|
|
|
|
RemoteDesignConstants.USER_LOCK_ID, |
|
|
|
|
connection.getId()); |
|
|
|
|
} |
|
|
|
|
FineLoggerFactory.getLogger().info("Available ports: {}, current Protocol: {}", Arrays.toString(ports), currentProtocol); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//失败重试监听器:1、关闭失败的socket 2、计数器加1 3、调用创建socket方法
|
|
|
|
|
private static final Emitter.Listener failRetry = new Emitter.Listener() { |
|
|
|
|
@Override |
|
|
|
|