Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,10 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
// **在起任何东西之前问**。等到 bind 失败时,网关已经在监听、客户端
// 可能已经连上来了,而这条错误当时只会进日志。
tw_control::socket_path_fits(&socket)?;
// 控制面的凭据,同样在起任何东西之前拿到:桌面端从环境变量交过来,
// 手工启动时生成一个写进配置目录。取不到就停在这儿 —— 一个装不上门的
// 控制面不该先起来再说。
let control_token = tw_control::token::Token::resolve(&dir)?;
let config_path = path.to_path_buf();
rt.block_on(async move {
let state = tw_gateway::AppState::new(cfg.clone())
Expand Down Expand Up @@ -836,7 +840,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
// 太长)只在日志里躺着。退出让那句话有机会走到人眼前。
let (control_died, control_dead) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
let r = tw_control::serve_unix(control, &sock).await;
let r = tw_control::serve_unix(control, &sock, control_token).await;
let msg = match r {
Err(e) => format!("{e}"),
// serve_unix 正常返回意味着 accept 循环结束了,同样是没了
Expand Down
2 changes: 2 additions & 0 deletions crates/tw-control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ tw-api = { workspace = true }
tw-types = { workspace = true }
# 列网卡用 getifaddrs —— 比引一个 crate 便宜
libc = { workspace = true }
# 生成控制面的凭据
rand = { workspace = true }
tw-config = { workspace = true }
tw-adopt = { workspace = true }
tw-scan = { workspace = true }
Expand Down
21 changes: 15 additions & 6 deletions crates/tw-control/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! 控制面:unix socket 上的 HTTP。
//!
//! **不占 TCP 端口**。理由不是省端口,是权限:一个
//! `0700` 的 socket 文件天然只有当前用户能连,不需要再发明一套 token。
//! 只有用户显式开启远程访问时才监听 TCP,那时才需要 token。
//! **不占 TCP 端口**。理由不是省端口,是权限:一个 `0700` 的 socket 文件
//! 天然只有当前用户能连。
//!
//! 但那是文件系统给的保证,**只在这个平台上成立** —— Windows 上没有对等物,
//! 控制面在那里只能落在回环 TCP 上。所以凭据这道门是另外装的一道,两个平台
//! 都走它,见 [`token`]。

use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -30,6 +33,7 @@ pub mod rotation;
pub mod routes;
pub mod scan;
pub mod security;
pub mod token;
pub mod zai;
pub use config::{ApplyError, ConfigManager, resolve_path, spawn_watcher};
pub use tw_observe::EventBus;
Expand Down Expand Up @@ -1468,7 +1472,11 @@ pub fn socket_path_fits(path: &Path) -> Result<(), ControlError> {
///
/// 陈旧的 socket 文件直接删掉重建 —— 它和 lock 文件不一样,没有「另一个
/// 实例可能还在用」的歧义:单实例锁已经在上一步挡住了。
pub async fn serve_unix(state: ControlState, path: &Path) -> Result<(), ControlError> {
pub async fn serve_unix(
state: ControlState,
path: &Path,
token: token::Token,
) -> Result<(), ControlError> {
socket_path_fits(path)?;
if path.exists() {
let _ = std::fs::remove_file(path);
Expand All @@ -1483,12 +1491,13 @@ pub async fn serve_unix(state: ControlState, path: &Path) -> Result<(), ControlE
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
// 0700:只有当前用户能连。这就是不需要 token 的原因。
// 0700:只有当前用户能连。凭据那道门在它之外,不是替代它 ——
// 两道都在,而只有这一道是平台给的。
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700));
}
tracing::info!(path = %path.display(), "the control plane is listening");

let app = router(state);
let app = token::guard(router(state), token);
loop {
let (stream, _) = match listener.accept().await {
Ok(x) => x,
Expand Down
Loading
Loading