From f5d21272b7599292b0fa770ee907c04ec0ce5277 Mon Sep 17 00:00:00 2001 From: hades Date: Fri, 20 Aug 2021 10:04:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?REPORT-55048=20https=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=97=B6=EF=BC=8C=E8=AE=BE=E8=AE=A1=E5=99=A8?= =?UTF-8?q?=E4=B8=8A=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=8E=A8=E9=80=81=E8=BF=87=E6=9D=A5=E7=9A=84eventhrea?= =?UTF-8?q?d=E7=BA=BF=E7=A8=8B=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/socketio/DesignerSocketIO.java | 124 +++++++++++++++--- 1 file changed, 103 insertions(+), 21 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java index 157ce6f98..c1e994bdc 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java @@ -5,29 +5,39 @@ import com.fr.decision.webservice.utils.DecisionServiceConstants; import com.fr.design.DesignerEnvManager; import com.fr.design.EnvChangeEntrance; import com.fr.design.dialog.FineJOptionPane; +import com.fr.design.env.DesignerWorkspaceInfo; import com.fr.design.i18n.Toolkit; import com.fr.design.mainframe.DesignerContext; import com.fr.design.mainframe.loghandler.DesignerLogger; import com.fr.design.ui.util.UIUtil; -import com.fr.event.Event; import com.fr.event.EventDispatcher; -import com.fr.event.Listener; +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.StringUtils; import com.fr.third.apache.log4j.spi.LoggingEvent; +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.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.WorkspaceEvent; import com.fr.workspace.base.WorkspaceConstants; import com.fr.workspace.connect.WorkspaceConnection; +import com.fr.workspace.connect.WorkspaceConnectionInfo; import com.fr.workspace.server.socket.SocketInfoOperator; import io.socket.client.IO; import io.socket.client.Socket; import io.socket.emitter.Emitter; +import java.io.File; +import java.io.FileInputStream; +import java.security.KeyStore; import java.util.Arrays; +import javax.net.ssl.SSLContext; import javax.swing.*; import java.io.IOException; import java.net.URI; @@ -37,26 +47,14 @@ import java.util.TimerTask; public class DesignerSocketIO { - - static { - EventDispatcher.listen(WorkspaceEvent.LostConnect, new Listener() { - @Override - public void on(Event event, Workspace param) { - // 远程设计websocket不支持wss 所以断开无法提醒 - // 使用远程设计的心跳断开来提醒断开 - if (DesignerEnvManager.getEnvManager().isHttps()) { - showConnectionLostDialog(); - } - } - }); - } - enum Status { Connected, Disconnected, Disconnecting } + private static final String HTTPS = "https"; + private static final String HTTP = "http"; private static Socket socket = null; private static Status status = Status.Disconnected; private static Timer disConnectHintTimer = null; @@ -65,6 +63,8 @@ public class DesignerSocketIO { private static String[] uri; //维护一个关于uri列表的计数器 private static int count; + // 当前webSocket选择的协议 + private static String currentProtocol; public static void close() { @@ -95,7 +95,7 @@ public class DesignerSocketIO { //根据uri和计数器建立连接,并注册监听 try { if (count < uri.length) { - socket = IO.socket(new URI(uri[count])); + socket = IO.socket(new URI(uri[count]), createOptions()); socket.on(WorkspaceConstants.WS_LOGRECORD, printLog); socket.on(WorkspaceConstants.CONFIG_MODIFY, modifyConfig); socket.on(Socket.EVENT_CONNECT_ERROR, failRetry); @@ -112,15 +112,54 @@ 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 { + String currentName = DesignerEnvManager.getEnvManager().getCurEnvName(); + DesignerWorkspaceInfo info = DesignerEnvManager.getEnvManager().getWorkspaceInfo(currentName); + WorkspaceConnectionInfo connection = info.getConnection(); + 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 String[] getSocketUri() throws IOException { Workspace current = WorkContext.getCurrent(); URL url = new URL(current.getPath()); Integer[] ports = current.get(SocketInfoOperator.class).getPort(); 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", - url.getProtocol(), + currentProtocol, url.getHost(), ports[i], WorkspaceConstants.WS_NAMESPACE, @@ -129,6 +168,7 @@ public class DesignerSocketIO { RemoteDesignConstants.USER_LOCK_ID, connection.getId()); } + FineLoggerFactory.getLogger().error("Available ports: {}, current Protocol: {}", Arrays.toString(ports), currentProtocol); return result; } @@ -136,7 +176,7 @@ public class DesignerSocketIO { private static final Emitter.Listener failRetry = new Emitter.Listener() { @Override public void call(Object... args) { - FineLoggerFactory.getLogger().warn("failed args: {}", Arrays.toString(args)); + printLog(args, PrintEventLogImpl.WARN, "failed args: {}"); status = Status.Disconnecting; socket.close(); count++; @@ -183,7 +223,7 @@ public class DesignerSocketIO { * todo 远程心跳断开不一定 socket 断开 和远程紧密相关的业务都绑定在心跳上,切换成心跳断开之后进行提醒, * socket 只用推日志和通知配置变更 */ - FineLoggerFactory.getLogger().error("disConnected args: {}", Arrays.toString(objects)); + printLog(objects, PrintEventLogImpl.ERROR, "disConnected args: {}"); if (status != Status.Disconnecting) { showConnectionLostDialog(); } @@ -226,4 +266,46 @@ public class DesignerSocketIO { } }; + private static void printLog(Object[] objects, PrintEventLog printEventLog, String prefix) { + for (Object object : objects) { + if (object instanceof Throwable) { + Throwable throwable = (Throwable) object; + printEventLog.printThrowable(throwable.getMessage(), throwable); + } else { + printEventLog.print(prefix, object); + } + } + } + + interface PrintEventLog { + void printThrowable(String s, Throwable throwable); + void print(String s, Object ...object); + } + + enum PrintEventLogImpl implements PrintEventLog { + ERROR { + @Override + public void printThrowable(String s, Throwable throwable) { + FineLoggerFactory.getLogger().error(s, throwable); + } + + @Override + public void print(String s, Object... object) { + FineLoggerFactory.getLogger().error(s, object); + } + }, + + WARN { + @Override + public void printThrowable(String s, Throwable throwable) { + FineLoggerFactory.getLogger().warn(s, throwable); + } + + @Override + public void print(String s, Object... object) { + FineLoggerFactory.getLogger().warn(s, object); + } + }; + } + } From 23bab19229ad582e5739fbd05eaf445d8dd3a480 Mon Sep 17 00:00:00 2001 From: hades Date: Tue, 24 Aug 2021 18:43:38 +0800 Subject: [PATCH 2/4] =?UTF-8?q?REPORT-55048=20https=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=97=B6=EF=BC=8C=E8=AE=BE=E8=AE=A1=E5=99=A8?= =?UTF-8?q?=E4=B8=8A=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E6=8E=A8=E9=80=81=E8=BF=87=E6=9D=A5=E7=9A=84eventhrea?= =?UTF-8?q?d=E7=BA=BF=E7=A8=8B=E6=97=A5=E5=BF=97=20fix=20npe=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fr/design/EnvChangeEntrance.java | 2 ++ .../env/DesignerWorkspaceInfoContext.java | 20 +++++++++++++++++++ .../mainframe/socketio/DesignerSocketIO.java | 15 +++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/env/DesignerWorkspaceInfoContext.java diff --git a/designer-base/src/main/java/com/fr/design/EnvChangeEntrance.java b/designer-base/src/main/java/com/fr/design/EnvChangeEntrance.java index 6881c8445..cd9c835f3 100644 --- a/designer-base/src/main/java/com/fr/design/EnvChangeEntrance.java +++ b/designer-base/src/main/java/com/fr/design/EnvChangeEntrance.java @@ -8,6 +8,7 @@ import com.fr.design.dialog.DialogActionAdapter; import com.fr.design.dialog.FineJOptionPane; import com.fr.design.env.DesignerWorkspaceGenerator; import com.fr.design.env.DesignerWorkspaceInfo; +import com.fr.design.env.DesignerWorkspaceInfoContext; import com.fr.design.env.DesignerWorkspaceType; import com.fr.design.env.RemoteDesignerWorkspaceInfo; import com.fr.design.env.RemoteWorkspace; @@ -105,6 +106,7 @@ public class EnvChangeEntrance { private boolean switch2Env(final String envName, PopTipStrategy strategy) { DesignerEnvManager envManager = DesignerEnvManager.getEnvManager(); DesignerWorkspaceInfo selectedEnv = envManager.getWorkspaceInfo(envName); + DesignerWorkspaceInfoContext.setWorkspaceInfo(selectedEnv); WorkspaceConnectionInfo connectionInfo = selectedEnv.getConnection(); try { diff --git a/designer-base/src/main/java/com/fr/design/env/DesignerWorkspaceInfoContext.java b/designer-base/src/main/java/com/fr/design/env/DesignerWorkspaceInfoContext.java new file mode 100644 index 000000000..ba8a5b09f --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/env/DesignerWorkspaceInfoContext.java @@ -0,0 +1,20 @@ +package com.fr.design.env; + +/** + * + * @author hades + * @version 10.0 + * Created by hades on 2021/8/24 + */ +public class DesignerWorkspaceInfoContext { + + private static DesignerWorkspaceInfo workspaceInfo; + + public static DesignerWorkspaceInfo getWorkspaceInfo() { + return workspaceInfo; + } + + public static void setWorkspaceInfo(DesignerWorkspaceInfo workspaceInfo) { + DesignerWorkspaceInfoContext.workspaceInfo = workspaceInfo; + } +} diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java index c1e994bdc..5f3e57b26 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java @@ -6,6 +6,7 @@ import com.fr.design.DesignerEnvManager; import com.fr.design.EnvChangeEntrance; import com.fr.design.dialog.FineJOptionPane; import com.fr.design.env.DesignerWorkspaceInfo; +import com.fr.design.env.DesignerWorkspaceInfoContext; import com.fr.design.i18n.Toolkit; import com.fr.design.mainframe.DesignerContext; import com.fr.design.mainframe.loghandler.DesignerLogger; @@ -127,9 +128,7 @@ public class DesignerSocketIO { } private static SSLContext getSSLContext() throws Exception { - String currentName = DesignerEnvManager.getEnvManager().getCurEnvName(); - DesignerWorkspaceInfo info = DesignerEnvManager.getEnvManager().getWorkspaceInfo(currentName); - WorkspaceConnectionInfo connection = info.getConnection(); + WorkspaceConnectionInfo connection = getConnectionInfo(); String certPath = connection.getCertPath(); String certSecretKey = connection.getCertSecretKey(); if (StringUtils.isBlank(certPath) || StringUtils.isBlank(certSecretKey)) { @@ -144,6 +143,16 @@ public class DesignerSocketIO { .build(); } + private static WorkspaceConnectionInfo getConnectionInfo() { + if (DesignerWorkspaceInfoContext.getWorkspaceInfo() == null) { + String currentName = DesignerEnvManager.getEnvManager().getCurEnvName(); + DesignerWorkspaceInfo info = DesignerEnvManager.getEnvManager().getWorkspaceInfo(currentName); + return info.getConnection(); + } else { + return DesignerWorkspaceInfoContext.getWorkspaceInfo().getConnection(); + } + } + private static String[] getSocketUri() throws IOException { Workspace current = WorkContext.getCurrent(); URL url = new URL(current.getPath()); From 0c7550a04b73379be262b05708c35f392cfe16bd Mon Sep 17 00:00:00 2001 From: hades Date: Tue, 24 Aug 2021 18:45:19 +0800 Subject: [PATCH 3/4] =?UTF-8?q?REPORT-57490=20websocket=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E6=97=B6=E5=BC=B9=E7=AA=97=E4=BD=8D=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fr/design/mainframe/socketio/DesignerSocketIO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java index 5f3e57b26..648d7cdde 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java @@ -248,7 +248,7 @@ public class DesignerSocketIO { private static void showConnectionLostDialog() { try { - UIUtil.invokeAndWaitIfNeeded(new Runnable() { + UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { FineJOptionPane.showMessageDialog( From 9992d53bf2b5f226ed5a6f6087affd04edb9c25f Mon Sep 17 00:00:00 2001 From: hades Date: Tue, 24 Aug 2021 19:48:29 +0800 Subject: [PATCH 4/4] =?UTF-8?q?REPORT-55048=20fix=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=8B=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/socketio/DesignerSocketIO.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java index 648d7cdde..782c7f35d 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java @@ -161,10 +161,6 @@ public class DesignerSocketIO { // 服务器配置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", @@ -279,7 +275,7 @@ public class DesignerSocketIO { for (Object object : objects) { if (object instanceof Throwable) { Throwable throwable = (Throwable) object; - printEventLog.printThrowable(throwable.getMessage(), throwable); + printEventLog.printThrowable(throwable); } else { printEventLog.print(prefix, object); } @@ -287,15 +283,15 @@ public class DesignerSocketIO { } interface PrintEventLog { - void printThrowable(String s, Throwable throwable); + void printThrowable(Throwable throwable); void print(String s, Object ...object); } enum PrintEventLogImpl implements PrintEventLog { ERROR { @Override - public void printThrowable(String s, Throwable throwable) { - FineLoggerFactory.getLogger().error(s, throwable); + public void printThrowable(Throwable throwable) { + FineLoggerFactory.getLogger().error(throwable.getMessage(), throwable); } @Override @@ -306,8 +302,8 @@ public class DesignerSocketIO { WARN { @Override - public void printThrowable(String s, Throwable throwable) { - FineLoggerFactory.getLogger().warn(s, throwable); + public void printThrowable(Throwable throwable) { + FineLoggerFactory.getLogger().warn(throwable.getMessage(), throwable); } @Override