diff --git a/mica-mqtt-broker/pom.xml b/mica-mqtt-broker/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..e3b606c77e9689496b09eae7521c79e56278cfb8 --- /dev/null +++ b/mica-mqtt-broker/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + mica-mqtt-broker + ${artifactId} + https://www.dreamlu.net + + + net.dreamlu + mica-mqtt + ${revision} + + + + 1.8 + 2.5.4 + 2.5.4 + + + + + org.springframework.boot + spring-boot-starter-web + + + net.dreamlu + mica-lite + + + net.dreamlu + mica-redis + + + net.dreamlu + mica-logging + + + net.dreamlu + mica-swagger + + + net.dreamlu + mica-mqtt-spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-actuator + + + io.micrometer + micrometer-registry-prometheus + + + + + + + net.dreamlu + mica-bom + ${mica.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring.boot.version} + pom + import + + + + + + ${project.name} + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/MqttBrokerApplication.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/MqttBrokerApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..554d733b1aa601cb54009113b2b105f6aa2fb907 --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/MqttBrokerApplication.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * mica mqtt broker + * + * @author L.cm + */ +@SpringBootApplication +@EnableScheduling +@EnableCaching +public class MqttBrokerApplication { + + public static void main(String[] args) { + SpringApplication.run(MqttBrokerApplication.class, args); + } + +} diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttAuthHandler.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttAuthHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..e0e79e724a04db0ac755abdd68b679b15818190b --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttAuthHandler.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker.auth; + +import net.dreamlu.iot.mqtt.codec.MqttQoS; +import net.dreamlu.iot.mqtt.codec.MqttTopicSubscription; +import net.dreamlu.iot.mqtt.core.server.IMqttServerAuthHandler; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +/** + * mqtt tcp websocket 认证 + * + * @author L.cm + */ +@Configuration(proxyBeanMethods = false) +public class MqttAuthHandler implements IMqttServerAuthHandler { + + @Override + public boolean authenticate(String clientId, String userName, String password) { + // 客户端认证逻辑实现 + return true; + } + + @Override + public boolean isValidSubscribe(List topicSubscriptionList) { + // 校验客户端订阅的 topic,校验成功返回 true,失败返回 false + for (MqttTopicSubscription subscription : topicSubscriptionList) { + String topicName = subscription.topicName(); + MqttQoS mqttQoS = subscription.qualityOfService(); + } + return true; + } +} diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttHttpAuthFilter.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttHttpAuthFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..0afe2ae17bf2f87fc1c9f0f8ff4fed408b3a995e --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/auth/MqttHttpAuthFilter.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker.auth; + +import net.dreamlu.iot.mqtt.core.server.http.api.code.ResultCode; +import net.dreamlu.iot.mqtt.core.server.http.api.result.Result; +import net.dreamlu.iot.mqtt.core.server.http.handler.HttpFilter; +import net.dreamlu.iot.mqtt.core.server.http.handler.MqttHttpRoutes; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.annotation.Configuration; +import org.tio.http.common.HttpRequest; +import org.tio.http.common.HttpResponse; + +/** + * 自定义 mqtt http 接口认证 + * + * @author L.cm + */ +@Configuration(proxyBeanMethods = false) +public class MqttHttpAuthFilter implements HttpFilter, InitializingBean { + + @Override + public boolean filter(HttpRequest request) throws Exception { + // 自行实现逻辑 + return true; + } + + @Override + public HttpResponse response(HttpRequest request, HttpResponse response) { + // 认证不通过时的响应 + return Result.fail(response, ResultCode.E103); + } + + @Override + public void afterPropertiesSet() throws Exception { + MqttHttpRoutes.addFilter(this); + } +} diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/MqttBrokerConfiguration.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/MqttBrokerConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..1bffbfffa9a648a8e52c573866323a90d3106589 --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/MqttBrokerConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker.config; + +import net.dreamlu.iot.mqtt.broker.listener.MqttBrokerMessageListener; +import net.dreamlu.iot.mqtt.core.server.dispatcher.IMqttMessageDispatcher; +import net.dreamlu.iot.mqtt.core.server.support.DefaultMqttMessageDispatcher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * mica mqtt broker 配置 + * + * @author L.cm + */ +@Configuration(proxyBeanMethods = false) +public class MqttBrokerConfiguration { + + @Bean + public IMqttMessageDispatcher messageDispatcher() { + // TODO L.cm 此处采用 redis 实现广播 + return new DefaultMqttMessageDispatcher(); + } + + @Bean + public MqttBrokerMessageListener brokerMessageListener(IMqttMessageDispatcher dispatcher) { + return new MqttBrokerMessageListener(dispatcher); + } + +} diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/package-info.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..932f5b27f7db5b55347ca22dd7fe8c20b15045cd --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/config/package-info.java @@ -0,0 +1 @@ +package net.dreamlu.iot.mqtt.broker.config; diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerConnectListener.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerConnectListener.java new file mode 100644 index 0000000000000000000000000000000000000000..9bfb37b251eb874b603e6da3cc0deb3534fbdb98 --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerConnectListener.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker.listener; + +import net.dreamlu.iot.mqtt.core.server.event.IMqttConnectStatusListener; + +/** + * mqtt 连接监听 + * + * @author L.cm + */ +public class MqttBrokerConnectListener implements IMqttConnectStatusListener { + @Override + public void online(String clientId) { + + } + + @Override + public void offline(String clientId) { + + } +} diff --git a/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerMessageListener.java b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerMessageListener.java new file mode 100644 index 0000000000000000000000000000000000000000..e81f75243ee30c66a67d0e04baed6dff44314dcc --- /dev/null +++ b/mica-mqtt-broker/src/main/java/net/dreamlu/iot/mqtt/broker/listener/MqttBrokerMessageListener.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.iot.mqtt.broker.listener; + +import net.dreamlu.iot.mqtt.codec.MqttMessageType; +import net.dreamlu.iot.mqtt.codec.MqttQoS; +import net.dreamlu.iot.mqtt.core.server.dispatcher.IMqttMessageDispatcher; +import net.dreamlu.iot.mqtt.core.server.event.IMqttMessageListener; +import net.dreamlu.iot.mqtt.core.server.model.Message; + +import java.nio.ByteBuffer; +import java.util.Objects; + +/** + * 集群消息监听器 + * + * @author L.cm + */ +public class MqttBrokerMessageListener implements IMqttMessageListener { + private final IMqttMessageDispatcher dispatcher; + + public MqttBrokerMessageListener(IMqttMessageDispatcher dispatcher) { + this.dispatcher = Objects.requireNonNull(dispatcher, "MqttMessageDispatcher is null."); + } + + @Override + public void onMessage(String clientId, String topic, MqttQoS mqttQoS, ByteBuffer payload) { + Message message = new Message(); + message.setTopic(topic); + message.setQos(mqttQoS.value()); + if (payload != null) { + message.setPayload(payload.array()); + } + message.setMessageType(MqttMessageType.PUBLISH.value()); + message.setStoreTime(System.currentTimeMillis()); + dispatcher.send(message); + } +} diff --git a/mica-mqtt-broker/src/main/resources/application-dev.yml b/mica-mqtt-broker/src/main/resources/application-dev.yml new file mode 100644 index 0000000000000000000000000000000000000000..9317600d75b274b7fa8cca25367f7bbad55bf525 --- /dev/null +++ b/mica-mqtt-broker/src/main/resources/application-dev.yml @@ -0,0 +1,18 @@ +mqtt: + server: + enabled: true # 是否开启服务端,默认:true +# ip: 0.0.0.0 # 服务端 ip 默认为空,0.0.0.0 + port: 5883 # 端口,默认:1883 + name: Mica-Mqtt-Server # 名称,默认:Mica-Mqtt-Server + buffer-allocator: HEAP # 堆内存和堆外内存,默认:堆内存 + heartbeat-timeout: 120000 # 心跳超时,单位毫秒,默认: 1000 * 120 + read-buffer-size: 8092 # 接收数据的 buffer size,默认:8092 + max-bytes-in-message: 8092 # 消息解析最大 bytes 长度,默认:8092 + debug: true # 如果开启 prometheus 指标收集建议关闭 + web-port: 8083 # http、websocket 端口,默认:8083 + websocket-enable: true # 是否开启 websocket,默认: true + http-enable: false # 是否开启 http api,默认: false + http-basic-auth: + enable: false # 是否开启 http basic auth,默认: false + username: "mica" # http basic auth 用户名 + password: "mica" # http basic auth 密码 diff --git a/mica-mqtt-broker/src/main/resources/application.yml b/mica-mqtt-broker/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..615e67cc42481cf6e829c8145d65b152d5ca779b --- /dev/null +++ b/mica-mqtt-broker/src/main/resources/application.yml @@ -0,0 +1,29 @@ +server: + port: 30001 +spring: + application: + name: mica-mqtt-broker + profiles: + active: dev +# actuator management +management: + info: + defaults: + enabled: true + metrics: + tags: + application: ${spring.application.name} + endpoint: + health: + show-details: ALWAYS + prometheus: + enabled: true + endpoints: + web: + exposure: + include: '*' +logging: + level: + root: info + server: info # t-io 服务端默认日志 + org.tio: info # t-io 服务端默认日志 diff --git a/mica-mqtt-broker/src/main/resources/banner.txt b/mica-mqtt-broker/src/main/resources/banner.txt new file mode 100644 index 0000000000000000000000000000000000000000..2eb7254cb74b7ea97e3e18feb9f7cdeb7a4654d5 --- /dev/null +++ b/mica-mqtt-broker/src/main/resources/banner.txt @@ -0,0 +1,12 @@ + +${AnsiColor.BRIGHT_BLUE}## ## #### ###### ### ${AnsiColor.RED} ## ## ####### ######## ######## +${AnsiColor.BRIGHT_BLUE}### ### ## ## ## ## ## ${AnsiColor.RED} ### ### ## ## ## ## +${AnsiColor.BRIGHT_BLUE}#### #### ## ## ## ## ${AnsiColor.RED} #### #### ## ## ## ## +${AnsiColor.BRIGHT_BLUE}## ### ## ## ## ## ##${AnsiColor.RED} ## ### ## ## ## ## ## +${AnsiColor.BRIGHT_BLUE}## ## ## ## #########${AnsiColor.RED} ## ## ## ## ## ## ## +${AnsiColor.BRIGHT_BLUE}## ## ## ## ## ## ##${AnsiColor.RED} ## ## ## ## ## ## +${AnsiColor.BRIGHT_BLUE}## ## #### ###### ## ##${AnsiColor.RED} ## ## ##### ## ## ## + + https://www.dreamlu.net + +${AnsiColor.BRIGHT_BLUE}:: ${spring.application.name} :: Running Spring Boot ${spring-boot.version} 🏃🏃🏃 ${AnsiColor.DEFAULT} diff --git a/pom.xml b/pom.xml index 8a78cd26bdf476a07de9efe25fbe33084f03dcaf..cd1fe9f18757e216f9d0f1d4a18e14ff63845436 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ mica-mqtt-spring-boot-starter mica-mqtt-example mica-mqtt-spring-boot-example + mica-mqtt-broker