Skip to content

Commit 2663ae6

Browse files
committed
fix(app-server): 修正本地监听告警文案
1 parent 7954d02 commit 2663ae6

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

  • src/cortex-app-server/src

src/cortex-app-server/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ pub async fn run_with_shutdown<F>(config: ServerConfig, shutdown: F) -> anyhow::
5757
where
5858
F: std::future::Future<Output = ()> + Send + 'static,
5959
{
60+
let addr: SocketAddr = config.listen_addr.parse()?;
61+
6062
// Warn if authentication is disabled
6163
if !config.auth.enabled {
6264
warn!("Server running without authentication!");
63-
warn!("Anyone on the network can access this server.");
65+
warn!("{}", unauthenticated_access_warning(addr));
6466
warn!("Use --auth to enable authentication.");
6567
}
6668

6769
let state = Arc::new(AppState::new(config.clone()).await?);
6870
let state_for_cleanup = Arc::clone(&state);
6971
let app = create_router_with_state(state);
7072

71-
let addr: SocketAddr = config.listen_addr.parse()?;
7273
info!("Starting Cortex server on {}", addr);
7374

7475
// Start mDNS publisher if enabled
@@ -121,6 +122,14 @@ where
121122
Ok(())
122123
}
123124

125+
fn unauthenticated_access_warning(addr: SocketAddr) -> &'static str {
126+
if addr.ip().is_loopback() {
127+
"Only local processes can access this server."
128+
} else {
129+
"Anyone on the network can access this server."
130+
}
131+
}
132+
124133
/// Create the application router.
125134
pub fn create_router(state: AppState) -> Router {
126135
create_router_with_state(Arc::new(state))
@@ -143,3 +152,27 @@ pub fn create_router_with_state(state: Arc<AppState>) -> Router {
143152
.layer(CorsLayer::permissive())
144153
.with_state(state)
145154
}
155+
156+
#[cfg(test)]
157+
mod tests {
158+
use super::unauthenticated_access_warning;
159+
use std::net::SocketAddr;
160+
161+
#[test]
162+
fn loopback_bind_uses_local_only_warning() {
163+
let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
164+
assert_eq!(
165+
unauthenticated_access_warning(addr),
166+
"Only local processes can access this server."
167+
);
168+
}
169+
170+
#[test]
171+
fn non_loopback_bind_uses_network_warning() {
172+
let addr: SocketAddr = "0.0.0.0:3000".parse().unwrap();
173+
assert_eq!(
174+
unauthenticated_access_warning(addr),
175+
"Anyone on the network can access this server."
176+
);
177+
}
178+
}

0 commit comments

Comments
 (0)