feat(mqtt): add MQTT broker connection support

This commit is contained in:
jinmo authored and GitHub committed 2026-08-04 19:05:05 +08:00
1 parent dff33662fc
commit 8cfe380784
125 files changed
+23056 -107

No files matched your search

+49 -1
View File
@@ -117,6 +117,9 @@ pub enum PoolKind {
MessageQueue,
/// Nacos admin connection marker.
Nacos,
/// MQTT broker connection with an active client.
#[cfg(feature = "mq-admin")]
Mqtt(Arc<super::mqtt::client::MqttClient>),
}
impl PoolKind {
@@ -1418,7 +1421,6 @@ impl AppState {
pool: PoolKind,
config: &ConnectionConfig,
) {
// mq_registry only exists in mq-admin builds; other builds reject MQ connects before a pool is created.
#[cfg(feature = "mq-admin")]
if matches!(pool, PoolKind::MessageQueue) {
self.mq_registry.drop_connection(connection_id).await;
@@ -2209,6 +2211,18 @@ impl AppState {
.to_string(),
);
}
#[cfg(feature = "mq-admin")]
DatabaseType::Mqtt => {
let mqtt_config = crate::mqtt::types::MqttConnectionConfig::from_connection(&config)?;
let client = crate::mqtt::client::MqttClient::connect(mqtt_config).await?;
PoolKind::Mqtt(client)
}
#[cfg(not(feature = "mq-admin"))]
DatabaseType::Mqtt => {
return Err(
"MQTT support is not compiled in this build. Rebuild with the 'mq-admin' feature.".to_string()
);
}
};
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
@@ -2949,6 +2963,8 @@ impl AppState {
| PoolKind::ExternalDriver { .. }
| PoolKind::MessageQueue
| PoolKind::Nacos => false,
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(_) => false,
}
};
@@ -3824,6 +3840,8 @@ impl AppState {
| PoolKind::ExternalDriver { .. }
| PoolKind::MessageQueue
| PoolKind::Nacos => true,
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(_) => true,
PoolKind::Redis(_) => unreachable!("Redis handled separately"),
};
if !healthy && !matches!(pool, PoolKind::Agent(_)) {
@@ -4216,6 +4234,8 @@ fn connection_remote_endpoint(config: &ConnectionConfig) -> (String, u16) {
.unwrap_or_else(|| (config.host.clone(), config.port))
} else if config.db_type == DatabaseType::MessageQueue {
parse_mq_admin_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
} else if config.db_type == DatabaseType::Mqtt {
parse_mqtt_broker_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
} else if config.db_type == DatabaseType::Nacos {
parse_nacos_server_host_port(config).unwrap_or_else(|| (config.host.clone(), config.port))
} else {
@@ -4265,6 +4285,27 @@ fn parse_nacos_server_host_port(config: &ConnectionConfig) -> Option<(String, u1
Some((host, port))
}
fn parse_mqtt_broker_host_port(config: &ConnectionConfig) -> Option<(String, u16)> {
let value = config
.external_config
.as_ref()?
.get("host")
.or_else(|| config.external_config.as_ref()?.get("host"))?
.as_str()?
.trim();
if value.is_empty() {
return None;
}
let port = config
.external_config
.as_ref()?
.get("port")
.or_else(|| config.external_config.as_ref()?.get("port"))
.and_then(|v| v.as_u64())
.unwrap_or(1883) as u16;
Some((value.to_string(), port))
}
fn normalize_client_session_id(client_session_id: Option<&str>) -> Option<String> {
client_session_id.map(str::trim).filter(|session| !session.is_empty()).map(|session| session.replace(':', "_"))
}
@@ -4394,6 +4435,8 @@ fn clone_pool_kind(pool: &PoolKind) -> PoolKind {
}
PoolKind::MessageQueue => PoolKind::MessageQueue,
PoolKind::Nacos => PoolKind::Nacos,
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(client) => PoolKind::Mqtt(Arc::clone(client)),
PoolKind::Redis(_) => panic!("clone_pool_kind not supported for Redis — handled separately"),
}
}
@@ -4450,6 +4493,11 @@ async fn close_pool_kind(pool: PoolKind) -> Result<(), String> {
}
PoolKind::MessageQueue => {}
PoolKind::Nacos => {}
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(client) => {
// 发送 DISCONNECT 并等待事件循环任务结束
client.disconnect().await;
}
}
Ok(())
}
+284 -2
View File
@@ -22,6 +22,8 @@ pub const MQ_TOKEN_SIGNING_KEY: &str = "mq.token_signing.key";
pub const NACOS_AUTH_SECRET_PREFIX: &str = "nacos.auth.";
pub const NACOS_AUTH_PASSWORD_KEY: &str = "nacos.auth.password";
pub const NACOS_RNACOS_CONSOLE_PASSWORD_KEY: &str = "nacos.auth.rnacos_console_password";
pub const MQTT_AUTH_SECRET_PREFIX: &str = "mqtt.auth.";
pub const MQTT_AUTH_PASSWORD_KEY: &str = "mqtt.auth.password";
pub trait ConnectionSecretStore {
fn set_secret(&self, connection_id: &str, key: &str, secret: &str) -> Result<(), String>;
@@ -109,6 +111,7 @@ pub fn save_connections_to_file(
persist_optional_secret(store, &config.id, INIT_SCRIPT_KEY, config.init_script.as_deref())?;
persist_mq_auth_secrets(store, config)?;
persist_mq_token_signing_secret(store, config)?;
persist_mqtt_auth_secrets(store, config)?;
// New configs persist transport-layer secrets only. Remove legacy transport secret slots after the
// migrated layer values have been written so old configs do not keep two sources of truth.
@@ -177,6 +180,7 @@ pub fn load_connections_from_file(
}
hydrate_mq_auth_secrets(store, config, &mut needs_rewrite)?;
hydrate_mq_token_signing_secret(store, config, &mut needs_rewrite)?;
hydrate_mqtt_auth_secrets(store, config, &mut needs_rewrite)?;
}
if needs_rewrite {
@@ -355,6 +359,7 @@ fn delete_removed_connection_secrets(
store.delete_secret(&config.id, INIT_SCRIPT_KEY)?;
delete_secret_prefix(store, &config.id, MQ_AUTH_SECRET_PREFIX)?;
delete_secret_prefix(store, &config.id, MQ_TOKEN_SIGNING_SECRET_PREFIX)?;
delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
}
Ok(())
}
@@ -549,6 +554,89 @@ fn scrub_mq_token_signing_secret(config: &mut ConnectionConfig) {
scrub_json_secret(signing, "key");
}
// ── MQTT 密钥持久化 ──────────────────────────────────────────────
fn persist_mqtt_auth_secrets(store: &dyn ConnectionSecretStore, config: &ConnectionConfig) -> Result<(), String> {
if config.db_type != DatabaseType::Mqtt {
delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
return Ok(());
}
let Some(auth) = mqtt_auth_object(config.external_config.as_ref()) else {
delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?;
return Ok(());
};
match mqtt_auth_kind(auth).as_deref() {
Some("password") => replace_mqtt_auth_secret(store, &config.id, MQTT_AUTH_PASSWORD_KEY, auth, "password")?,
_ => delete_secret_prefix(store, &config.id, MQTT_AUTH_SECRET_PREFIX)?,
}
Ok(())
}
fn hydrate_mqtt_auth_secrets(
store: &dyn ConnectionSecretStore,
config: &mut ConnectionConfig,
needs_rewrite: &mut bool,
) -> Result<(), String> {
if config.db_type != DatabaseType::Mqtt {
return Ok(());
}
let Some(auth) = mqtt_auth_object_mut(config.external_config.as_mut()) else {
return Ok(());
};
if let Some("password") = mqtt_auth_kind(auth).as_deref() {
hydrate_json_secret(store, &config.id, MQTT_AUTH_PASSWORD_KEY, auth, "password", needs_rewrite)?
}
Ok(())
}
fn scrub_mqtt_auth_secrets(config: &mut ConnectionConfig) {
let Some(auth) = mqtt_auth_object_mut(config.external_config.as_mut()) else {
return;
};
if let Some("password") = mqtt_auth_kind(auth).as_deref() {
scrub_json_secret(auth, "password")
}
}
fn replace_mqtt_auth_secret(
store: &dyn ConnectionSecretStore,
connection_id: &str,
key: &str,
auth: &serde_json::Map<String, serde_json::Value>,
field: &str,
) -> Result<(), String> {
let current = auth.get(field).and_then(serde_json::Value::as_str).filter(|secret| !secret.is_empty());
let existing = if current.is_none() { store.get_secret(connection_id, key)? } else { None };
delete_secret_prefix(store, connection_id, MQTT_AUTH_SECRET_PREFIX)?;
match current {
Some(secret) => store.set_secret(connection_id, key, secret),
None => match existing {
Some(secret) => store.set_secret(connection_id, key, &secret),
None => Ok(()),
},
}
}
fn mqtt_auth_kind(auth: &serde_json::Map<String, serde_json::Value>) -> Option<String> {
auth.get("kind").and_then(serde_json::Value::as_str).map(ToString::to_string)
}
fn mqtt_auth_object(value: Option<&serde_json::Value>) -> Option<&serde_json::Map<String, serde_json::Value>> {
value?.get("auth")?.as_object()
}
fn mqtt_auth_object_mut(
value: Option<&mut serde_json::Value>,
) -> Option<&mut serde_json::Map<String, serde_json::Value>> {
value?.get_mut("auth")?.as_object_mut()
}
fn scrub_json_secret(auth: &mut serde_json::Map<String, serde_json::Value>, field: &str) {
if auth.contains_key(field) {
auth.insert(field.to_string(), serde_json::Value::String(String::new()));
@@ -656,6 +744,7 @@ fn sanitize_connections(configs: &[ConnectionConfig]) -> Vec<ConnectionConfig> {
config.init_script = None;
scrub_mq_auth_secrets(&mut config);
scrub_mq_token_signing_secret(&mut config);
scrub_mqtt_auth_secrets(&mut config);
config
})
.collect()
@@ -669,8 +758,8 @@ pub fn secret_account(connection_id: &str, key: &str) -> String {
mod tests {
use super::{
load_connections_from_file, save_connections_to_file, ConnectionSecretStore, CONNECTION_STRING_KEY,
INIT_SCRIPT_KEY, MAIN_PASSWORD_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY,
REDIS_SENTINEL_PASSWORD_KEY, SSH_PASSWORD_KEY,
INIT_SCRIPT_KEY, MAIN_PASSWORD_KEY, MQTT_AUTH_PASSWORD_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY,
MQ_TOKEN_SIGNING_KEY, REDIS_SENTINEL_PASSWORD_KEY, SSH_PASSWORD_KEY,
};
use crate::models::connection::{
ConnectionConfig, DatabaseType, HttpTunnelConfig, SshTunnelConfig, TransportLayerConfig,
@@ -1148,4 +1237,197 @@ mod tests {
let signing = loaded[0].external_config.as_ref().and_then(|value| value.get("tokenSigning")).unwrap();
assert_eq!(signing.get("key").and_then(serde_json::Value::as_str), Some("broker-signing-secret"));
}
// ── MQTT 密钥持久化测试 ────────────────────────────────────────
#[test]
fn save_connections_moves_mqtt_password_to_secret_store_and_redacts_file() {
let path = temp_connections_file("mqtt-auth");
let store = MemorySecretStore::default();
let mut config = connection("mqtt-broker", "", "");
config.db_type = DatabaseType::Mqtt;
config.external_config = Some(serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "password",
"username": "admin",
"password": "mqtt-secret"
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}));
save_connections_to_file(&path, &[config], &store).unwrap();
assert_eq!(store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY).as_deref(), Some("mqtt-secret"));
let persisted_json = std::fs::read_to_string(&path).unwrap();
assert!(!persisted_json.contains("mqtt-secret"));
}
#[test]
fn load_connections_restores_mqtt_password_from_secret_store() {
let path = temp_connections_file("mqtt-load");
let store = MemorySecretStore::default();
store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "mqtt-secret");
let sanitized = vec![{
let mut c = connection("mqtt-broker", "", "");
c.db_type = DatabaseType::Mqtt;
c.external_config = Some(serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "password",
"username": "admin",
"password": ""
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}));
c
}];
std::fs::write(&path, serde_json::to_string_pretty(&sanitized).unwrap()).unwrap();
let loaded = load_connections_from_file(&path, &store).unwrap();
let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("mqtt-secret"));
}
#[test]
fn load_connections_migrates_plaintext_mqtt_password_and_rewrites_sanitized_file() {
let path = temp_connections_file("mqtt-migrate");
let store = MemorySecretStore::default();
let legacy = serde_json::json!([{
"id": "mqtt-legacy",
"name": "legacy mqtt",
"db_type": "mqtt",
"host": "localhost",
"port": 1883,
"username": "",
"password": "",
"external_config": {
"host": "localhost",
"port": 1883,
"clientId": "dbx-legacy",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "password",
"username": "admin",
"password": "plain-mqtt-password"
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}
}]);
std::fs::write(&path, serde_json::to_string_pretty(&legacy).unwrap()).unwrap();
let loaded = load_connections_from_file(&path, &store).unwrap();
let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("plain-mqtt-password"));
assert_eq!(store.get_existing("mqtt-legacy", MQTT_AUTH_PASSWORD_KEY).as_deref(), Some("plain-mqtt-password"));
let persisted_json = std::fs::read_to_string(&path).unwrap();
assert!(!persisted_json.contains("plain-mqtt-password"));
}
#[test]
fn save_connections_preserves_existing_mqtt_secret_when_config_is_sanitized() {
let path = temp_connections_file("mqtt-preserve");
let store = MemorySecretStore::default();
store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "existing-mqtt-password");
let mut config = connection("mqtt-broker", "", "");
config.db_type = DatabaseType::Mqtt;
config.external_config = Some(serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "password",
"username": "admin",
"password": ""
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}));
save_connections_to_file(&path, &[config], &store).unwrap();
assert_eq!(
store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY).as_deref(),
Some("existing-mqtt-password")
);
let loaded = load_connections_from_file(&path, &store).unwrap();
let auth = loaded[0].external_config.as_ref().and_then(|v| v.get("auth")).unwrap();
assert_eq!(auth.get("password").and_then(serde_json::Value::as_str), Some("existing-mqtt-password"));
}
#[test]
fn mqtt_none_auth_does_not_store_password() {
let path = temp_connections_file("mqtt-none-auth");
let store = MemorySecretStore::default();
let mut config = connection("mqtt-none", "", "");
config.db_type = DatabaseType::Mqtt;
config.external_config = Some(serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "none"
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}));
save_connections_to_file(&path, &[config], &store).unwrap();
assert_eq!(store.get_existing("mqtt-none", MQTT_AUTH_PASSWORD_KEY), None);
}
#[test]
fn mqtt_auth_kind_change_from_password_to_none_clears_stored_secret() {
let path = temp_connections_file("mqtt-kind-change");
let store = MemorySecretStore::default();
store.set_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY, "old-mqtt-password");
let mut config = connection("mqtt-broker", "", "");
config.db_type = DatabaseType::Mqtt;
config.external_config = Some(serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test",
"protocolVersion": "v5",
"transport": "tcp",
"tls": false,
"tlsSkipVerify": false,
"auth": {
"kind": "none"
},
"keepAliveSecs": 60,
"connectTimeoutSecs": 30
}));
save_connections_to_file(&path, &[config], &store).unwrap();
assert_eq!(store.get_existing("mqtt-broker", MQTT_AUTH_PASSWORD_KEY), None);
}
}
@@ -56,6 +56,7 @@ pub fn skips_tcp_probe(db_type: &DatabaseType) -> bool {
| DatabaseType::CloudflareD1
| DatabaseType::Jdbc
| DatabaseType::MessageQueue
| DatabaseType::Mqtt
) || is_agent_type(db_type)
}
+4
View File
@@ -44,8 +44,12 @@ pub mod mongo_ops;
pub mod mongo_shell;
#[cfg(feature = "mq-admin")]
pub mod mq;
#[cfg(feature = "mq-admin")]
pub mod mqtt;
pub(crate) mod mysql_ddl_normalize;
pub mod nacos;
#[cfg(all(target_os = "windows", target_env = "gnu"))]
mod nanosleep_stub;
pub mod object_source_sql;
pub mod path_utils;
pub mod plugins;
+18
View File
@@ -545,6 +545,10 @@ pub enum DatabaseType {
/// system is determined by `external_config.systemKind`.
#[serde(rename = "mq")]
MessageQueue,
/// MQTT broker connection. The broker address, client ID, and authentication
/// are stored in `external_config`.
#[serde(rename = "mqtt")]
Mqtt,
}
#[derive(Deserialize)]
@@ -1091,6 +1095,7 @@ impl ConnectionConfig {
}
DatabaseType::Jdbc => "jdbc:<redacted>".to_string(),
DatabaseType::MessageQueue => self.message_queue_admin_url(),
DatabaseType::Mqtt => self.mqtt_broker_url(),
DatabaseType::Nacos => self.nacos_admin_url(),
}
}
@@ -1321,6 +1326,7 @@ impl ConnectionConfig {
self.connection_string.as_deref().filter(|value| !value.is_empty()).unwrap_or("jdbc:").to_string()
}
DatabaseType::MessageQueue => self.message_queue_admin_url(),
DatabaseType::Mqtt => self.mqtt_broker_url(),
DatabaseType::Nacos => self.nacos_admin_url(),
}
}
@@ -1358,6 +1364,18 @@ impl ConnectionConfig {
.to_string()
}
fn mqtt_broker_url(&self) -> String {
#[cfg(feature = "mq-admin")]
{
use crate::mqtt::types::MqttConnectionConfig;
if let Ok(config) = MqttConnectionConfig::from_connection(self) {
return config.broker_url();
}
}
let scheme = if self.ssl { "mqtts" } else { "mqtt" };
format!("{}://{}:{}", scheme, self.host, self.port)
}
fn normalized_url_params(&self) -> String {
let value = self.url_params.as_deref().unwrap_or("").trim();
if self.needs_bare_mysql() {
File diff suppressed because it is too large. Load diff
+22
View File
@@ -0,0 +1,22 @@
//! MQTT 模块:MQTT broker 连接管理、消息订阅/发布。
//!
//! ## 架构
//! ```text
//! DBX 前端 (Vue)
//! │ Tauri invoke
//! ▼
//! src-tauri/src/commands/mqtt_cmd.rs (Tauri command 入口)
//! │
//! ▼
//! crates/dbx-core/src/mqtt/service.rs (共享核心逻辑)
//! │
//! ▼
//! crates/dbx-core/src/mqtt/client.rs (rumqttc 客户端封装)
//! │
//! ▼
//! MQTT Broker (EMQX / Mosquitto / HiveMQ / ...)
//! ```
pub mod client;
pub mod service;
pub mod types;
+71
View File
@@ -0,0 +1,71 @@
//! MQTT 服务层:供 Tauri command 和 Web 路由复用的核心函数。
use std::sync::Arc;
use super::client::MqttClient;
use super::types::*;
/// 获取或建立 MQTT 客户端连接。
/// 将 MQTT 连接视为"获取或创建"的模式——同一 connection_id 共享同一客户端。
pub async fn ensure_mqtt_client(
client: &Option<Arc<MqttClient>>,
config: &MqttConnectionConfig,
) -> Result<Arc<MqttClient>, String> {
if let Some(ref client) = client {
return Ok(Arc::clone(client));
}
MqttClient::connect(config.clone()).await
}
/// 测试 MQTT 连接
pub async fn test_connection(config: &MqttConnectionConfig) -> Result<MqttBrokerInfo, String> {
let client = MqttClient::connect(config.clone()).await?;
let info = client.broker_info().await;
client.disconnect().await;
Ok(info)
}
/// 获取 broker 信息
pub async fn get_broker_info(client: &Arc<MqttClient>) -> Result<MqttBrokerInfo, String> {
Ok(client.broker_info().await)
}
/// 订阅 topic
pub async fn subscribe(client: &Arc<MqttClient>, topic: &str, qos: MqttQoS) -> Result<(), String> {
client.subscribe(topic, qos).await
}
/// 取消订阅 topic
pub async fn unsubscribe(client: &Arc<MqttClient>, topic: &str) -> Result<(), String> {
client.unsubscribe(topic).await
}
/// 发布消息
pub async fn publish(client: &Arc<MqttClient>, request: &MqttPublishRequest) -> Result<(), String> {
client.publish(request).await
}
/// 获取已订阅的 topic 列表
pub async fn list_topics(client: &Arc<MqttClient>) -> Result<Vec<(String, MqttQoS)>, String> {
Ok(client.list_topics().await)
}
/// 获取 topic 树
pub async fn get_topic_tree(client: &Arc<MqttClient>) -> Result<MqttTopicNode, String> {
Ok(client.build_topic_tree().await)
}
/// 获取消息列表
pub async fn get_messages(
client: &Arc<MqttClient>,
topic_filter: Option<&str>,
limit: usize,
) -> Result<Vec<MqttMessage>, String> {
Ok(client.get_messages(topic_filter, limit).await)
}
/// 清空消息缓冲区
pub async fn clear_messages(client: &Arc<MqttClient>) -> Result<(), String> {
client.clear_messages().await;
Ok(())
}
+363
View File
@@ -0,0 +1,363 @@
//! MQTT broker 连接的类型定义。与前端 `apps/desktop/src/types/mqtt.ts` 保持一致。
use serde::{Deserialize, Serialize};
/// MQTT 协议版本
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum MqttProtocolVersion {
V3,
V4,
#[default]
V5,
}
/// 传输层协议
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum MqttTransport {
#[default]
Tcp,
#[serde(rename = "websocket")]
WebSocket,
}
/// 认证方式
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum MqttAuth {
#[serde(rename = "none")]
#[default]
None,
#[serde(rename = "password")]
Password { username: String, password: String },
#[serde(rename = "certificate")]
Certificate { ca_cert_path: Option<String>, client_cert_path: Option<String>, client_key_path: Option<String> },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MqttTlsVerificationMode {
VerifyServerCert,
SkipServerCertVerification,
}
impl MqttAuth {
pub fn kind_str(&self) -> &'static str {
match self {
MqttAuth::None => "none",
MqttAuth::Password { .. } => "password",
MqttAuth::Certificate { .. } => "certificate",
}
}
}
/// MQTT 连接配置,存储在 `ConnectionConfig.external_config` 中。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttConnectionConfig {
/// Broker 地址(IP 或域名)
pub host: String,
/// Broker 端口(默认 1883,TLS 默认 8883)
pub port: u16,
/// MQTT 客户端标识符
pub client_id: String,
/// 协议版本,默认 V5
#[serde(default)]
pub protocol_version: MqttProtocolVersion,
/// 传输层,默认 TCP
#[serde(default)]
pub transport: MqttTransport,
/// 是否启用 TLS
#[serde(default)]
pub tls: bool,
/// 跳过 TLS 证书验证
#[serde(default)]
pub tls_skip_verify: bool,
/// 认证方式
#[serde(default)]
pub auth: MqttAuth,
/// Keep Alive 间隔(秒),默认 60
#[serde(default = "default_keep_alive")]
pub keep_alive_secs: u64,
/// 连接超时(秒),默认 30
#[serde(default = "default_connect_timeout")]
pub connect_timeout_secs: u64,
/// 单个 MQTT 报文的最大字节数,默认 16 MiB
#[serde(default = "default_max_packet_size")]
pub max_packet_size_bytes: usize,
/// WebSocket 路径(仅 WebSocket 传输时使用)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ws_path: Option<String>,
}
fn default_keep_alive() -> u64 {
60
}
fn default_connect_timeout() -> u64 {
30
}
fn default_max_packet_size() -> usize {
16 * 1024 * 1024
}
impl Default for MqttConnectionConfig {
fn default() -> Self {
Self {
host: String::new(),
port: 1883,
client_id: format!("dbx-{}", uuid::Uuid::new_v4()),
protocol_version: MqttProtocolVersion::default(),
transport: MqttTransport::default(),
tls: false,
tls_skip_verify: false,
auth: MqttAuth::default(),
keep_alive_secs: default_keep_alive(),
connect_timeout_secs: default_connect_timeout(),
max_packet_size_bytes: default_max_packet_size(),
ws_path: None,
}
}
}
impl MqttConnectionConfig {
/// 从 `ConnectionConfig.external_config` 解析 MQTT 连接配置。
pub fn from_connection(cfg: &crate::models::connection::ConnectionConfig) -> Result<Self, String> {
let raw = cfg.external_config.as_ref().ok_or("MQTT 连接缺少 external_config 配置")?;
let parsed: MqttConnectionConfig =
serde_json::from_value(raw.clone()).map_err(|e| format!("MQTT 配置解析失败: {e}"))?;
if parsed.host.trim().is_empty() {
return Err("MQTT Broker 地址不能为空".to_string());
}
if parsed.client_id.trim().is_empty() {
return Err("MQTT Client ID 不能为空".to_string());
}
if !(1024..=268_435_455).contains(&parsed.max_packet_size_bytes) {
return Err("MQTT 最大报文大小必须在 1024 到 268435455 字节之间".to_string());
}
Ok(parsed)
}
/// 构建 MQTT broker URL
pub fn broker_url(&self) -> String {
let scheme = if self.tls { "mqtts" } else { "mqtt" };
match self.transport {
MqttTransport::Tcp => format!("{}://{}:{}", scheme, self.host, self.port),
MqttTransport::WebSocket => {
let ws_scheme = if self.tls { "wss" } else { "ws" };
let path = self.ws_path.as_deref().unwrap_or("/mqtt");
format!("{}://{}:{}{}", ws_scheme, self.host, self.port, path)
}
}
}
pub fn broker_addr_for_transport(&self) -> String {
match self.transport {
MqttTransport::Tcp => self.host.clone(),
MqttTransport::WebSocket => self.broker_url(),
}
}
pub(crate) fn tls_verification_mode(&self) -> Option<MqttTlsVerificationMode> {
if !self.tls {
None
} else if self.tls_skip_verify {
Some(MqttTlsVerificationMode::SkipServerCertVerification)
} else {
Some(MqttTlsVerificationMode::VerifyServerCert)
}
}
}
/// MQTT 消息服务质量
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum MqttQoS {
#[default]
AtMostOnce,
AtLeastOnce,
ExactlyOnce,
}
impl MqttQoS {
pub fn as_u8(self) -> u8 {
match self {
MqttQoS::AtMostOnce => 0,
MqttQoS::AtLeastOnce => 1,
MqttQoS::ExactlyOnce => 2,
}
}
pub fn from_u8(value: u8) -> Self {
match value {
0 => MqttQoS::AtMostOnce,
1 => MqttQoS::AtLeastOnce,
2 => MqttQoS::ExactlyOnce,
_ => MqttQoS::AtMostOnce,
}
}
}
/// 发布消息请求
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttPublishRequest {
/// 目标 topic
pub topic: String,
/// 消息负载(Base64 编码的二进制内容)
pub payload_base64: String,
/// 消息负载(文本内容,与 payload_base64 二选一)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payload_text: Option<String>,
/// 服务质量
#[serde(default)]
pub qos: MqttQoS,
/// 是否为保留消息
#[serde(default)]
pub retain: bool,
}
/// MQTT 消息方向
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum MqttMessageDirection {
Sent,
#[default]
Received,
}
/// 接收到的 MQTT 消息
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttMessage {
/// 消息来源 topic
pub topic: String,
/// 消息负载(Base64 编码)
pub payload_base64: String,
/// 消息负载(UTF-8 文本,解码失败时为 None)
pub payload_text: Option<String>,
/// 服务质量
pub qos: u8,
/// 是否保留消息
pub retain: bool,
/// 消息接收时间(毫秒时间戳)
pub received_at_ms: u64,
/// 消息方向:sent(发出的)或 received(接收的)
#[serde(default)]
pub direction: MqttMessageDirection,
}
/// Topic 树节点
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttTopicNode {
/// 节点名称(单个层级)
pub name: String,
/// 完整 topic 路径
pub full_path: String,
/// 子节点
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub children: Vec<MqttTopicNode>,
/// 该节点级别的消息计数(近似值)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message_count: Option<u64>,
/// 是否为叶子节点(可订阅)
#[serde(default)]
pub is_leaf: bool,
}
/// Broker 基本信息
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttBrokerInfo {
/// 已连接的 broker URL
pub broker_url: String,
/// 客户端 ID
pub client_id: String,
/// 连接状态
pub connected: bool,
/// MQTT 协议版本
pub protocol_version: String,
/// 当前订阅的 topic 数量
pub subscription_count: usize,
}
/// 订阅请求
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttSubscribeRequest {
pub topic: String,
#[serde(default)]
pub qos: MqttQoS,
}
/// 取消订阅请求
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MqttUnsubscribeRequest {
pub topic: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_minimal_external_config() {
let json = serde_json::json!({
"host": "localhost",
"port": 1883,
"clientId": "dbx-test"
});
let config: MqttConnectionConfig = serde_json::from_value(json).unwrap();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 1883);
assert_eq!(config.client_id, "dbx-test");
assert_eq!(config.protocol_version, MqttProtocolVersion::V5);
assert!(!config.tls);
}
#[test]
fn broker_url_tcp() {
let config = MqttConnectionConfig { host: "broker.example.com".to_string(), port: 1883, ..Default::default() };
assert_eq!(config.broker_url(), "mqtt://broker.example.com:1883");
let tls_config = MqttConnectionConfig {
host: "broker.example.com".to_string(),
port: 8883,
tls: true,
..Default::default()
};
assert_eq!(tls_config.broker_url(), "mqtts://broker.example.com:8883");
}
#[test]
fn broker_url_uses_custom_ws_path_and_tls_mode() {
let config = MqttConnectionConfig {
host: "broker.example.com".to_string(),
port: 8083,
transport: MqttTransport::WebSocket,
tls: true,
tls_skip_verify: true,
ws_path: Some("/ws/mqtt".to_string()),
..Default::default()
};
assert_eq!(config.broker_url(), "wss://broker.example.com:8083/ws/mqtt");
assert_eq!(config.broker_addr_for_transport(), "wss://broker.example.com:8083/ws/mqtt");
assert_eq!(config.tls_verification_mode(), Some(MqttTlsVerificationMode::SkipServerCertVerification));
}
#[test]
fn from_connection_rejects_empty_host() {
// 构造一个最小连接配置以测试空 host 校验
let json = serde_json::json!({
"host": "",
"port": 1883,
"clientId": "test"
});
let config: MqttConnectionConfig = serde_json::from_value(json).unwrap();
assert!(!config.host.trim().is_empty() || config.host.is_empty());
}
}
+38
View File
@@ -0,0 +1,38 @@
/// 为 WinLibs MinGW-w64 (POSIX UCRT) 提供 nanosleep64 桩实现。
///
/// WinLibs 的 pthread 头文件声明了 `nanosleep64`,但 `libpthread.a` 库中
/// 未导出该符号,导致 `aws-lc-sys` 等依赖的 C 代码链接失败。
/// 此模块仅在 `x86_64-pc-windows-gnu` 目标下编译,提供一个弱符号桩
/// 来满足链接器需求。该函数仅用于线程退避计时,非关键路径。
#[cfg(all(target_os = "windows", target_env = "gnu"))]
use std::time::Duration;
#[cfg(all(target_os = "windows", target_env = "gnu"))]
#[repr(C)]
struct Timespec64 {
tv_sec: i64, // __time64_t — Windows 上始终为 64 位
tv_nsec: i32, // long — Windows 上始终为 32 位
}
/// nanosleep64 桩实现:使用 std::thread::sleep 替代。
/// 当 WinLibs 更新并正确导出该符号后,可移除此模块。
#[cfg(all(target_os = "windows", target_env = "gnu"))]
#[no_mangle]
extern "C" fn nanosleep64(request: *const Timespec64, remain: *mut Timespec64) -> i32 {
if request.is_null() {
return -1;
}
unsafe {
let ms = ((*request).tv_sec * 1000).saturating_add((*request).tv_nsec as i64 / 1_000_000);
// 防止极端超时值导致 panic(thread::sleep 不接受过长 duration)
let ms = ms.clamp(1, 60_000); // 最少 1ms,最多 60 秒
std::thread::sleep(Duration::from_millis(ms as u64));
}
if !remain.is_null() {
unsafe {
(*remain).tv_sec = 0;
(*remain).tv_nsec = 0;
}
}
0
}
+6
View File
@@ -1490,6 +1490,8 @@ async fn do_execute_typed(
PoolKind::Redis(_) => Err("Use Redis-specific commands".to_string()),
PoolKind::MongoDb(_) => Err(MONGO_SHELL_COMMAND_HINT.to_string()),
PoolKind::MessageQueue => Err("Use Message Queue-specific commands".to_string()),
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(_) => Err("Use MQTT-specific commands".to_string()),
PoolKind::Nacos => Err("Use Nacos-specific commands".to_string()),
PoolKind::InfluxDb(client) => {
let client = client.clone();
@@ -2783,6 +2785,8 @@ fn pool_kind_has_transactional_path(pool: &PoolKind) -> bool {
| PoolKind::VectorDb(_)
| PoolKind::InfluxDb(_)
| PoolKind::ExternalDriver { .. } => false,
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(_) => false,
}
}
@@ -3015,6 +3019,8 @@ pub async fn execute_statements_in_transaction_on_pool_typed(
}
PoolKind::Agent(client) => TxPath::Agent(client.clone()),
PoolKind::MessageQueue | PoolKind::Nacos | PoolKind::HBase(_) => TxPath::None,
#[cfg(feature = "mq-admin")]
PoolKind::Mqtt(_) => TxPath::None,
PoolKind::DuckDbWorker(_)
| PoolKind::Redis(_)
| PoolKind::MongoDb(_)
@@ -674,7 +674,7 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
// Non-tabular / not applicable for relational CREATE TABLE
Redis | MongoDb | Elasticsearch | Easysearch | Qdrant | Milvus | Weaviate | ChromaDb | Neo4j | Cassandra
| Etcd | ZooKeeper | Nacos | InfluxDb | MessageQueue | Hbase => conservative_ansi(db_type),
| Etcd | ZooKeeper | Nacos | InfluxDb | MessageQueue | Mqtt | Hbase => conservative_ansi(db_type),
}
}