Commit eb9d27ae9e75a6093bc906a44d01623bb2693a3e

Authored by YunaiV
1 parent a77e1780

feat(iot):【协议改造】SSL 配置,统一使用 SslConfig 替代,更统一

Showing 13 changed files with 109 additions and 107 deletions
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java
@@ -35,7 +35,7 @@ public class IotGatewayProperties { @@ -35,7 +35,7 @@ public class IotGatewayProperties {
35 /** 35 /**
36 * 协议实例列表 36 * 协议实例列表
37 */ 37 */
38 - private List<ProtocolInstanceProperties> protocols; 38 + private List<ProtocolProperties> protocols;
39 39
40 @Data 40 @Data
41 public static class RpcProperties { 41 public static class RpcProperties {
@@ -78,7 +78,7 @@ public class IotGatewayProperties { @@ -78,7 +78,7 @@ public class IotGatewayProperties {
78 * 协议实例配置 78 * 协议实例配置
79 */ 79 */
80 @Data 80 @Data
81 - public static class ProtocolInstanceProperties { 81 + public static class ProtocolProperties {
82 82
83 /** 83 /**
84 * 协议实例 ID,如 "http-alink"、"tcp-binary" 84 * 协议实例 ID,如 "http-alink"、"tcp-binary"
@@ -117,6 +117,14 @@ public class IotGatewayProperties { @@ -117,6 +117,14 @@ public class IotGatewayProperties {
117 */ 117 */
118 private String serialize; 118 private String serialize;
119 119
  120 + // ========== SSL 配置 ==========
  121 +
  122 + /**
  123 + * SSL 配置(可选,配置文件中不配置则为 null)
  124 + */
  125 + @Valid
  126 + private SslConfig ssl;
  127 +
120 // ========== 各协议配置 ========== 128 // ========== 各协议配置 ==========
121 129
122 /** 130 /**
@@ -160,4 +168,52 @@ public class IotGatewayProperties { @@ -160,4 +168,52 @@ public class IotGatewayProperties {
160 168
161 } 169 }
162 170
  171 + /**
  172 + * SSL 配置
  173 + */
  174 + @Data
  175 + public static class SslConfig {
  176 +
  177 + /**
  178 + * 是否启用 SSL
  179 + */
  180 + @NotNull(message = "是否启用 SSL 不能为空")
  181 + private Boolean ssl = false;
  182 +
  183 + /**
  184 + * SSL 证书路径
  185 + */
  186 + @NotEmpty(message = "SSL 证书路径不能为空")
  187 + private String sslCertPath;
  188 +
  189 + /**
  190 + * SSL 私钥路径
  191 + */
  192 + @NotEmpty(message = "SSL 私钥路径不能为空")
  193 + private String sslKeyPath;
  194 +
  195 + /**
  196 + * 密钥库(KeyStore)路径
  197 + * <p>
  198 + * 包含客户端自己的证书和私钥,用于向服务端证明身份(双向认证)
  199 + */
  200 + private String keyStorePath;
  201 + /**
  202 + * 密钥库密码
  203 + */
  204 + private String keyStorePassword;
  205 +
  206 + /**
  207 + * 信任库(TrustStore)路径
  208 + * <p>
  209 + * 包含服务端信任的 CA 证书,用于验证服务端的身份
  210 + */
  211 + private String trustStorePath;
  212 + /**
  213 + * 信任库密码
  214 + */
  215 + private String trustStorePassword;
  216 +
  217 + }
  218 +
163 } 219 }
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/IotProtocolManager.java
@@ -45,13 +45,13 @@ public class IotProtocolManager implements SmartLifecycle { @@ -45,13 +45,13 @@ public class IotProtocolManager implements SmartLifecycle {
45 if (running) { 45 if (running) {
46 return; 46 return;
47 } 47 }
48 - List<IotGatewayProperties.ProtocolInstanceProperties> protocolConfigs = gatewayProperties.getProtocols(); 48 + List<IotGatewayProperties.ProtocolProperties> protocolConfigs = gatewayProperties.getProtocols();
49 if (CollUtil.isEmpty(protocolConfigs)) { 49 if (CollUtil.isEmpty(protocolConfigs)) {
50 log.info("[start][没有配置协议实例,跳过启动]"); 50 log.info("[start][没有配置协议实例,跳过启动]");
51 return; 51 return;
52 } 52 }
53 53
54 - for (IotGatewayProperties.ProtocolInstanceProperties config : protocolConfigs) { 54 + for (IotGatewayProperties.ProtocolProperties config : protocolConfigs) {
55 if (BooleanUtil.isFalse(config.getEnabled())) { 55 if (BooleanUtil.isFalse(config.getEnabled())) {
56 log.info("[start][协议实例 {} 未启用,跳过]", config.getId()); 56 log.info("[start][协议实例 {} 未启用,跳过]", config.getId());
57 continue; 57 continue;
@@ -91,7 +91,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -91,7 +91,7 @@ public class IotProtocolManager implements SmartLifecycle {
91 * @return 协议实例 91 * @return 协议实例
92 */ 92 */
93 @SuppressWarnings({"EnhancedSwitchMigration"}) 93 @SuppressWarnings({"EnhancedSwitchMigration"})
94 - private IotProtocol createProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 94 + private IotProtocol createProtocol(IotGatewayProperties.ProtocolProperties config) {
95 IotProtocolTypeEnum protocolType = IotProtocolTypeEnum.of(config.getProtocol()); 95 IotProtocolTypeEnum protocolType = IotProtocolTypeEnum.of(config.getProtocol());
96 if (protocolType == null) { 96 if (protocolType == null) {
97 log.error("[createProtocol][协议实例 {} 的协议类型 {} 不存在]", config.getId(), config.getProtocol()); 97 log.error("[createProtocol][协议实例 {} 的协议类型 {} 不存在]", config.getId(), config.getProtocol());
@@ -124,7 +124,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -124,7 +124,7 @@ public class IotProtocolManager implements SmartLifecycle {
124 * @param config 协议实例配置 124 * @param config 协议实例配置
125 * @return HTTP 协议实例 125 * @return HTTP 协议实例
126 */ 126 */
127 - private IotHttpProtocol createHttpProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 127 + private IotHttpProtocol createHttpProtocol(IotGatewayProperties.ProtocolProperties config) {
128 return new IotHttpProtocol(config); 128 return new IotHttpProtocol(config);
129 } 129 }
130 130
@@ -134,7 +134,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -134,7 +134,7 @@ public class IotProtocolManager implements SmartLifecycle {
134 * @param config 协议实例配置 134 * @param config 协议实例配置
135 * @return TCP 协议实例 135 * @return TCP 协议实例
136 */ 136 */
137 - private IotTcpProtocol createTcpProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 137 + private IotTcpProtocol createTcpProtocol(IotGatewayProperties.ProtocolProperties config) {
138 return new IotTcpProtocol(config); 138 return new IotTcpProtocol(config);
139 } 139 }
140 140
@@ -144,7 +144,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -144,7 +144,7 @@ public class IotProtocolManager implements SmartLifecycle {
144 * @param config 协议实例配置 144 * @param config 协议实例配置
145 * @return UDP 协议实例 145 * @return UDP 协议实例
146 */ 146 */
147 - private IotUdpProtocol createUdpProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 147 + private IotUdpProtocol createUdpProtocol(IotGatewayProperties.ProtocolProperties config) {
148 return new IotUdpProtocol(config); 148 return new IotUdpProtocol(config);
149 } 149 }
150 150
@@ -154,7 +154,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -154,7 +154,7 @@ public class IotProtocolManager implements SmartLifecycle {
154 * @param config 协议实例配置 154 * @param config 协议实例配置
155 * @return CoAP 协议实例 155 * @return CoAP 协议实例
156 */ 156 */
157 - private IotCoapProtocol createCoapProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 157 + private IotCoapProtocol createCoapProtocol(IotGatewayProperties.ProtocolProperties config) {
158 return new IotCoapProtocol(config); 158 return new IotCoapProtocol(config);
159 } 159 }
160 160
@@ -164,7 +164,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -164,7 +164,7 @@ public class IotProtocolManager implements SmartLifecycle {
164 * @param config 协议实例配置 164 * @param config 协议实例配置
165 * @return WebSocket 协议实例 165 * @return WebSocket 协议实例
166 */ 166 */
167 - private IotWebSocketProtocol createWebSocketProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 167 + private IotWebSocketProtocol createWebSocketProtocol(IotGatewayProperties.ProtocolProperties config) {
168 return new IotWebSocketProtocol(config); 168 return new IotWebSocketProtocol(config);
169 } 169 }
170 170
@@ -174,7 +174,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -174,7 +174,7 @@ public class IotProtocolManager implements SmartLifecycle {
174 * @param config 协议实例配置 174 * @param config 协议实例配置
175 * @return MQTT 协议实例 175 * @return MQTT 协议实例
176 */ 176 */
177 - private IotMqttProtocol createMqttProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 177 + private IotMqttProtocol createMqttProtocol(IotGatewayProperties.ProtocolProperties config) {
178 return new IotMqttProtocol(config); 178 return new IotMqttProtocol(config);
179 } 179 }
180 180
@@ -184,7 +184,7 @@ public class IotProtocolManager implements SmartLifecycle { @@ -184,7 +184,7 @@ public class IotProtocolManager implements SmartLifecycle {
184 * @param config 协议实例配置 184 * @param config 协议实例配置
185 * @return EMQX 协议实例 185 * @return EMQX 协议实例
186 */ 186 */
187 - private IotEmqxProtocol createEmqxProtocol(IotGatewayProperties.ProtocolInstanceProperties config) { 187 + private IotEmqxProtocol createEmqxProtocol(IotGatewayProperties.ProtocolProperties config) {
188 return new IotEmqxProtocol(config); 188 return new IotEmqxProtocol(config);
189 } 189 }
190 190
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/IotCoapProtocol.java
@@ -4,7 +4,7 @@ import cn.hutool.extra.spring.SpringUtil; @@ -4,7 +4,7 @@ import cn.hutool.extra.spring.SpringUtil;
4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; 4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
5 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 5 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
6 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 6 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
7 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 7 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
8 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 8 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
9 import cn.iocoder.yudao.module.iot.gateway.protocol.coap.handler.downstream.IotCoapDownstreamSubscriber; 9 import cn.iocoder.yudao.module.iot.gateway.protocol.coap.handler.downstream.IotCoapDownstreamSubscriber;
10 import cn.iocoder.yudao.module.iot.gateway.protocol.coap.handler.upstream.IotCoapAuthHandler; 10 import cn.iocoder.yudao.module.iot.gateway.protocol.coap.handler.upstream.IotCoapAuthHandler;
@@ -43,7 +43,7 @@ public class IotCoapProtocol implements IotProtocol { @@ -43,7 +43,7 @@ public class IotCoapProtocol implements IotProtocol {
43 /** 43 /**
44 * 协议配置 44 * 协议配置
45 */ 45 */
46 - private final ProtocolInstanceProperties properties; 46 + private final ProtocolProperties properties;
47 /** 47 /**
48 * 服务器 ID(用于消息追踪,全局唯一) 48 * 服务器 ID(用于消息追踪,全局唯一)
49 */ 49 */
@@ -66,7 +66,7 @@ public class IotCoapProtocol implements IotProtocol { @@ -66,7 +66,7 @@ public class IotCoapProtocol implements IotProtocol {
66 */ 66 */
67 private final IotCoapDownstreamSubscriber downstreamSubscriber; 67 private final IotCoapDownstreamSubscriber downstreamSubscriber;
68 68
69 - public IotCoapProtocol(ProtocolInstanceProperties properties) { 69 + public IotCoapProtocol(ProtocolProperties properties) {
70 IotCoapConfig coapConfig = properties.getCoap(); 70 IotCoapConfig coapConfig = properties.getCoap();
71 Assert.notNull(coapConfig, "CoAP 协议配置(coap)不能为空"); 71 Assert.notNull(coapConfig, "CoAP 协议配置(coap)不能为空");
72 this.properties = properties; 72 this.properties = properties;
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxProtocol.java
@@ -7,7 +7,7 @@ import cn.hutool.extra.spring.SpringUtil; @@ -7,7 +7,7 @@ import cn.hutool.extra.spring.SpringUtil;
7 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; 7 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
8 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 8 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
9 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 9 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
10 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 10 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
11 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 11 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
12 import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.handler.downstream.IotEmqxDownstreamSubscriber; 12 import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.handler.downstream.IotEmqxDownstreamSubscriber;
13 import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.handler.upstream.IotEmqxAuthEventHandler; 13 import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.handler.upstream.IotEmqxAuthEventHandler;
@@ -47,7 +47,7 @@ public class IotEmqxProtocol implements IotProtocol { @@ -47,7 +47,7 @@ public class IotEmqxProtocol implements IotProtocol {
47 /** 47 /**
48 * 协议配置 48 * 协议配置
49 */ 49 */
50 - private final ProtocolInstanceProperties properties; 50 + private final ProtocolProperties properties;
51 /** 51 /**
52 * EMQX 配置 52 * EMQX 配置
53 */ 53 */
@@ -92,7 +92,7 @@ public class IotEmqxProtocol implements IotProtocol { @@ -92,7 +92,7 @@ public class IotEmqxProtocol implements IotProtocol {
92 */ 92 */
93 private final IotEmqxDownstreamSubscriber downstreamSubscriber; 93 private final IotEmqxDownstreamSubscriber downstreamSubscriber;
94 94
95 - public IotEmqxProtocol(ProtocolInstanceProperties properties) { 95 + public IotEmqxProtocol(ProtocolProperties properties) {
96 Assert.notNull(properties, "协议实例配置不能为空"); 96 Assert.notNull(properties, "协议实例配置不能为空");
97 Assert.notNull(properties.getEmqx(), "EMQX 协议配置(emqx)不能为空"); 97 Assert.notNull(properties.getEmqx(), "EMQX 协议配置(emqx)不能为空");
98 this.properties = properties; 98 this.properties = properties;
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpConfig.java
@@ -10,19 +10,4 @@ import lombok.Data; @@ -10,19 +10,4 @@ import lombok.Data;
10 @Data 10 @Data
11 public class IotHttpConfig { 11 public class IotHttpConfig {
12 12
13 - /**  
14 - * 是否启用 SSL  
15 - */  
16 - private Boolean sslEnabled = false;  
17 -  
18 - /**  
19 - * SSL 证书路径  
20 - */  
21 - private String sslCertPath;  
22 -  
23 - /**  
24 - * SSL 私钥路径  
25 - */  
26 - private String sslKeyPath;  
27 -  
28 } 13 }
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpProtocol.java
@@ -4,7 +4,8 @@ import cn.hutool.extra.spring.SpringUtil; @@ -4,7 +4,8 @@ import cn.hutool.extra.spring.SpringUtil;
4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; 4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
5 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 5 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
6 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 6 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
7 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 7 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties;
  8 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
8 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 9 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
9 import cn.iocoder.yudao.module.iot.gateway.protocol.http.handler.downstream.IotHttpDownstreamSubscriber; 10 import cn.iocoder.yudao.module.iot.gateway.protocol.http.handler.downstream.IotHttpDownstreamSubscriber;
10 import cn.iocoder.yudao.module.iot.gateway.protocol.http.handler.upstream.IotHttpAuthHandler; 11 import cn.iocoder.yudao.module.iot.gateway.protocol.http.handler.upstream.IotHttpAuthHandler;
@@ -33,7 +34,7 @@ public class IotHttpProtocol implements IotProtocol { @@ -33,7 +34,7 @@ public class IotHttpProtocol implements IotProtocol {
33 /** 34 /**
34 * 协议配置 35 * 协议配置
35 */ 36 */
36 - private final ProtocolInstanceProperties properties; 37 + private final ProtocolProperties properties;
37 /** 38 /**
38 * 服务器 ID(用于消息追踪,全局唯一) 39 * 服务器 ID(用于消息追踪,全局唯一)
39 */ 40 */
@@ -60,7 +61,7 @@ public class IotHttpProtocol implements IotProtocol { @@ -60,7 +61,7 @@ public class IotHttpProtocol implements IotProtocol {
60 */ 61 */
61 private IotHttpDownstreamSubscriber downstreamSubscriber; 62 private IotHttpDownstreamSubscriber downstreamSubscriber;
62 63
63 - public IotHttpProtocol(ProtocolInstanceProperties properties) { 64 + public IotHttpProtocol(ProtocolProperties properties) {
64 this.properties = properties; 65 this.properties = properties;
65 this.serverId = IotDeviceMessageUtils.generateServerId(properties.getPort()); 66 this.serverId = IotDeviceMessageUtils.generateServerId(properties.getPort());
66 67
@@ -104,12 +105,12 @@ public class IotHttpProtocol implements IotProtocol { @@ -104,12 +105,12 @@ public class IotHttpProtocol implements IotProtocol {
104 router.post(IotHttpUpstreamHandler.PATH).handler(upstreamHandler); 105 router.post(IotHttpUpstreamHandler.PATH).handler(upstreamHandler);
105 106
106 // 1.4 启动 HTTP 服务器 107 // 1.4 启动 HTTP 服务器
107 - IotHttpConfig httpConfig = properties.getHttp();  
108 HttpServerOptions options = new HttpServerOptions().setPort(properties.getPort()); 108 HttpServerOptions options = new HttpServerOptions().setPort(properties.getPort());
109 - if (httpConfig != null && Boolean.TRUE.equals(httpConfig.getSslEnabled())) { 109 + IotGatewayProperties.SslConfig sslConfig = properties.getSsl();
  110 + if (sslConfig != null && Boolean.TRUE.equals(sslConfig.getSsl())) {
110 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions() 111 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions()
111 - .setKeyPath(httpConfig.getSslKeyPath())  
112 - .setCertPath(httpConfig.getSslCertPath()); 112 + .setKeyPath(sslConfig.getSslKeyPath())
  113 + .setCertPath(sslConfig.getSslCertPath());
113 options = options.setSsl(true).setKeyCertOptions(pemKeyCertOptions); 114 options = options.setSsl(true).setKeyCertOptions(pemKeyCertOptions);
114 } 115 }
115 try { 116 try {
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/IotMqttConfig.java
@@ -26,20 +26,4 @@ public class IotMqttConfig { @@ -26,20 +26,4 @@ public class IotMqttConfig {
26 @Min(value = 1, message = "连接超时时间不能小于 1 秒") 26 @Min(value = 1, message = "连接超时时间不能小于 1 秒")
27 private Integer connectTimeoutSeconds = 60; 27 private Integer connectTimeoutSeconds = 60;
28 28
29 - /**  
30 - * 是否启用 SSL  
31 - */  
32 - @NotNull(message = "是否启用 SSL 不能为空")  
33 - private Boolean sslEnabled = false;  
34 -  
35 - /**  
36 - * SSL 证书路径  
37 - */  
38 - private String sslCertPath;  
39 -  
40 - /**  
41 - * SSL 私钥路径  
42 - */  
43 - private String sslKeyPath;  
44 -  
45 } 29 }
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/IotMqttProtocol.java
@@ -8,7 +8,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; @@ -8,7 +8,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
8 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 8 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
9 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 9 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
10 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 10 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
11 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 11 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties;
  12 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
12 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 13 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
13 import cn.iocoder.yudao.module.iot.gateway.protocol.mqtt.handler.downstream.IotMqttDownstreamHandler; 14 import cn.iocoder.yudao.module.iot.gateway.protocol.mqtt.handler.downstream.IotMqttDownstreamHandler;
14 import cn.iocoder.yudao.module.iot.gateway.protocol.mqtt.handler.downstream.IotMqttDownstreamSubscriber; 15 import cn.iocoder.yudao.module.iot.gateway.protocol.mqtt.handler.downstream.IotMqttDownstreamSubscriber;
@@ -51,7 +52,7 @@ public class IotMqttProtocol implements IotProtocol { @@ -51,7 +52,7 @@ public class IotMqttProtocol implements IotProtocol {
51 /** 52 /**
52 * 协议配置 53 * 协议配置
53 */ 54 */
54 - private final ProtocolInstanceProperties properties; 55 + private final ProtocolProperties properties;
55 /** 56 /**
56 * 服务器 ID(用于消息追踪,全局唯一) 57 * 服务器 ID(用于消息追踪,全局唯一)
57 */ 58 */
@@ -88,7 +89,7 @@ public class IotMqttProtocol implements IotProtocol { @@ -88,7 +89,7 @@ public class IotMqttProtocol implements IotProtocol {
88 private final IotMqttRegisterHandler registerHandler; 89 private final IotMqttRegisterHandler registerHandler;
89 private final IotMqttUpstreamHandler upstreamHandler; 90 private final IotMqttUpstreamHandler upstreamHandler;
90 91
91 - public IotMqttProtocol(ProtocolInstanceProperties properties) { 92 + public IotMqttProtocol(ProtocolProperties properties) {
92 IotMqttConfig mqttConfig = properties.getMqtt(); 93 IotMqttConfig mqttConfig = properties.getMqtt();
93 Assert.notNull(mqttConfig, "MQTT 协议配置(mqtt)不能为空"); 94 Assert.notNull(mqttConfig, "MQTT 协议配置(mqtt)不能为空");
94 this.properties = properties; 95 this.properties = properties;
@@ -136,10 +137,11 @@ public class IotMqttProtocol implements IotProtocol { @@ -136,10 +137,11 @@ public class IotMqttProtocol implements IotProtocol {
136 .setPort(properties.getPort()) 137 .setPort(properties.getPort())
137 .setMaxMessageSize(mqttConfig.getMaxMessageSize()) 138 .setMaxMessageSize(mqttConfig.getMaxMessageSize())
138 .setTimeoutOnConnect(mqttConfig.getConnectTimeoutSeconds()); 139 .setTimeoutOnConnect(mqttConfig.getConnectTimeoutSeconds());
139 - if (Boolean.TRUE.equals(mqttConfig.getSslEnabled())) { 140 + IotGatewayProperties.SslConfig sslConfig = properties.getSsl();
  141 + if (sslConfig != null && Boolean.TRUE.equals(sslConfig.getSsl())) {
140 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions() 142 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions()
141 - .setKeyPath(mqttConfig.getSslKeyPath())  
142 - .setCertPath(mqttConfig.getSslCertPath()); 143 + .setKeyPath(sslConfig.getSslKeyPath())
  144 + .setCertPath(sslConfig.getSslCertPath());
143 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions); 145 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions);
144 } 146 }
145 147
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotTcpConfig.java
@@ -28,20 +28,6 @@ public class IotTcpConfig { @@ -28,20 +28,6 @@ public class IotTcpConfig {
28 private Long keepAliveTimeoutMs = 30000L; 28 private Long keepAliveTimeoutMs = 30000L;
29 29
30 /** 30 /**
31 - * 是否启用 SSL  
32 - */  
33 - @NotNull(message = "是否启用 SSL 不能为空")  
34 - private Boolean sslEnabled = false;  
35 - /**  
36 - * SSL 证书路径  
37 - */  
38 - private String sslCertPath;  
39 - /**  
40 - * SSL 私钥路径  
41 - */  
42 - private String sslKeyPath;  
43 -  
44 - /**  
45 * 拆包配置 31 * 拆包配置
46 */ 32 */
47 @Valid 33 @Valid
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotTcpProtocol.java
@@ -5,7 +5,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; @@ -5,7 +5,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
5 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum; 5 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum;
6 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 6 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
7 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 7 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
8 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 8 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties;
  9 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
9 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 10 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
10 import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.codec.IotTcpFrameCodec; 11 import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.codec.IotTcpFrameCodec;
11 import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.codec.IotTcpFrameCodecFactory; 12 import cn.iocoder.yudao.module.iot.gateway.protocol.tcp.codec.IotTcpFrameCodecFactory;
@@ -36,7 +37,7 @@ public class IotTcpProtocol implements IotProtocol { @@ -36,7 +37,7 @@ public class IotTcpProtocol implements IotProtocol {
36 /** 37 /**
37 * 协议配置 38 * 协议配置
38 */ 39 */
39 - private final ProtocolInstanceProperties properties; 40 + private final ProtocolProperties properties;
40 /** 41 /**
41 * 服务器 ID(用于消息追踪,全局唯一) 42 * 服务器 ID(用于消息追踪,全局唯一)
42 */ 43 */
@@ -76,7 +77,7 @@ public class IotTcpProtocol implements IotProtocol { @@ -76,7 +77,7 @@ public class IotTcpProtocol implements IotProtocol {
76 */ 77 */
77 private final IotTcpFrameCodec frameCodec; 78 private final IotTcpFrameCodec frameCodec;
78 79
79 - public IotTcpProtocol(ProtocolInstanceProperties properties) { 80 + public IotTcpProtocol(ProtocolProperties properties) {
80 IotTcpConfig tcpConfig = properties.getTcp(); 81 IotTcpConfig tcpConfig = properties.getTcp();
81 Assert.notNull(tcpConfig, "TCP 协议配置(tcp)不能为空"); 82 Assert.notNull(tcpConfig, "TCP 协议配置(tcp)不能为空");
82 Assert.notNull(tcpConfig.getCodec(), "TCP 拆包配置(tcp.codec)不能为空"); 83 Assert.notNull(tcpConfig.getCodec(), "TCP 拆包配置(tcp.codec)不能为空");
@@ -128,10 +129,11 @@ public class IotTcpProtocol implements IotProtocol { @@ -128,10 +129,11 @@ public class IotTcpProtocol implements IotProtocol {
128 .setTcpNoDelay(true) 129 .setTcpNoDelay(true)
129 .setReuseAddress(true) 130 .setReuseAddress(true)
130 .setIdleTimeout((int) (tcpConfig.getKeepAliveTimeoutMs() / 1000)); // 设置空闲超时 131 .setIdleTimeout((int) (tcpConfig.getKeepAliveTimeoutMs() / 1000)); // 设置空闲超时
131 - if (Boolean.TRUE.equals(tcpConfig.getSslEnabled())) { 132 + IotGatewayProperties.SslConfig sslConfig = properties.getSsl();
  133 + if (sslConfig != null && Boolean.TRUE.equals(sslConfig.getSsl())) {
132 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions() 134 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions()
133 - .setKeyPath(tcpConfig.getSslKeyPath())  
134 - .setCertPath(tcpConfig.getSslCertPath()); 135 + .setKeyPath(sslConfig.getSslKeyPath())
  136 + .setCertPath(sslConfig.getSslCertPath());
135 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions); 137 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions);
136 } 138 }
137 139
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/udp/IotUdpProtocol.java
@@ -4,7 +4,7 @@ import cn.hutool.extra.spring.SpringUtil; @@ -4,7 +4,7 @@ import cn.hutool.extra.spring.SpringUtil;
4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; 4 import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
5 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum; 5 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum;
6 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 6 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
7 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 7 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
8 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 8 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
9 import cn.iocoder.yudao.module.iot.gateway.protocol.udp.handler.downstream.IotUdpDownstreamHandler; 9 import cn.iocoder.yudao.module.iot.gateway.protocol.udp.handler.downstream.IotUdpDownstreamHandler;
10 import cn.iocoder.yudao.module.iot.gateway.protocol.udp.handler.downstream.IotUdpDownstreamSubscriber; 10 import cn.iocoder.yudao.module.iot.gateway.protocol.udp.handler.downstream.IotUdpDownstreamSubscriber;
@@ -33,7 +33,7 @@ public class IotUdpProtocol implements IotProtocol { @@ -33,7 +33,7 @@ public class IotUdpProtocol implements IotProtocol {
33 /** 33 /**
34 * 协议配置 34 * 协议配置
35 */ 35 */
36 - private final ProtocolInstanceProperties properties; 36 + private final ProtocolProperties properties;
37 /** 37 /**
38 * 服务器 ID(用于消息追踪,全局唯一) 38 * 服务器 ID(用于消息追踪,全局唯一)
39 */ 39 */
@@ -70,7 +70,7 @@ public class IotUdpProtocol implements IotProtocol { @@ -70,7 +70,7 @@ public class IotUdpProtocol implements IotProtocol {
70 */ 70 */
71 private final IotMessageSerializer serializer; 71 private final IotMessageSerializer serializer;
72 72
73 - public IotUdpProtocol(ProtocolInstanceProperties properties) { 73 + public IotUdpProtocol(ProtocolProperties properties) {
74 IotUdpConfig udpConfig = properties.getUdp(); 74 IotUdpConfig udpConfig = properties.getUdp();
75 Assert.notNull(udpConfig, "UDP 协议配置(udp)不能为空"); 75 Assert.notNull(udpConfig, "UDP 协议配置(udp)不能为空");
76 this.properties = properties; 76 this.properties = properties;
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/IotWebSocketConfig.java
@@ -35,20 +35,4 @@ public class IotWebSocketConfig { @@ -35,20 +35,4 @@ public class IotWebSocketConfig {
35 @NotNull(message = "空闲超时时间不能为空") 35 @NotNull(message = "空闲超时时间不能为空")
36 private Integer idleTimeoutSeconds = 60; 36 private Integer idleTimeoutSeconds = 60;
37 37
38 - /**  
39 - * 是否启用 SSL(wss://)  
40 - */  
41 - @NotNull(message = "是否启用 SSL 不能为空")  
42 - private Boolean sslEnabled = false;  
43 -  
44 - /**  
45 - * SSL 证书路径  
46 - */  
47 - private String sslCertPath;  
48 -  
49 - /**  
50 - * SSL 私钥路径  
51 - */  
52 - private String sslKeyPath;  
53 -  
54 } 38 }
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/IotWebSocketProtocol.java
@@ -6,7 +6,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum; @@ -6,7 +6,8 @@ import cn.iocoder.yudao.module.iot.core.enums.IotProtocolTypeEnum;
6 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum; 6 import cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum;
7 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; 7 import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
8 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 8 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
9 -import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolInstanceProperties; 9 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties;
  10 +import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties.ProtocolProperties;
10 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol; 11 import cn.iocoder.yudao.module.iot.gateway.protocol.IotProtocol;
11 import cn.iocoder.yudao.module.iot.gateway.protocol.websocket.handler.downstream.IotWebSocketDownstreamSubscriber; 12 import cn.iocoder.yudao.module.iot.gateway.protocol.websocket.handler.downstream.IotWebSocketDownstreamSubscriber;
12 import cn.iocoder.yudao.module.iot.gateway.protocol.websocket.handler.downstream.IotWebSocketDownstreamHandler; 13 import cn.iocoder.yudao.module.iot.gateway.protocol.websocket.handler.downstream.IotWebSocketDownstreamHandler;
@@ -35,7 +36,7 @@ public class IotWebSocketProtocol implements IotProtocol { @@ -35,7 +36,7 @@ public class IotWebSocketProtocol implements IotProtocol {
35 /** 36 /**
36 * 协议配置 37 * 协议配置
37 */ 38 */
38 - private final ProtocolInstanceProperties properties; 39 + private final ProtocolProperties properties;
39 /** 40 /**
40 * 服务器 ID(用于消息追踪,全局唯一) 41 * 服务器 ID(用于消息追踪,全局唯一)
41 */ 42 */
@@ -71,7 +72,7 @@ public class IotWebSocketProtocol implements IotProtocol { @@ -71,7 +72,7 @@ public class IotWebSocketProtocol implements IotProtocol {
71 */ 72 */
72 private final IotMessageSerializer serializer; 73 private final IotMessageSerializer serializer;
73 74
74 - public IotWebSocketProtocol(ProtocolInstanceProperties properties) { 75 + public IotWebSocketProtocol(ProtocolProperties properties) {
75 Assert.notNull(properties, "协议实例配置不能为空"); 76 Assert.notNull(properties, "协议实例配置不能为空");
76 Assert.notNull(properties.getWebsocket(), "WebSocket 协议配置(websocket)不能为空"); 77 Assert.notNull(properties.getWebsocket(), "WebSocket 协议配置(websocket)不能为空");
77 this.properties = properties; 78 this.properties = properties;
@@ -120,10 +121,11 @@ public class IotWebSocketProtocol implements IotProtocol { @@ -120,10 +121,11 @@ public class IotWebSocketProtocol implements IotProtocol {
120 .setIdleTimeout(wsConfig.getIdleTimeoutSeconds()) 121 .setIdleTimeout(wsConfig.getIdleTimeoutSeconds())
121 .setMaxWebSocketFrameSize(wsConfig.getMaxFrameSize()) 122 .setMaxWebSocketFrameSize(wsConfig.getMaxFrameSize())
122 .setMaxWebSocketMessageSize(wsConfig.getMaxMessageSize()); 123 .setMaxWebSocketMessageSize(wsConfig.getMaxMessageSize());
123 - if (Boolean.TRUE.equals(wsConfig.getSslEnabled())) { 124 + IotGatewayProperties.SslConfig sslConfig = properties.getSsl();
  125 + if (sslConfig != null && Boolean.TRUE.equals(sslConfig.getSsl())) {
124 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions() 126 PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions()
125 - .setKeyPath(wsConfig.getSslKeyPath())  
126 - .setCertPath(wsConfig.getSslCertPath()); 127 + .setKeyPath(sslConfig.getSslKeyPath())
  128 + .setCertPath(sslConfig.getSslCertPath());
127 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions); 129 options.setSsl(true).setKeyCertOptions(pemKeyCertOptions);
128 } 130 }
129 131