|
|
@@ -2,192 +2,83 @@
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
-use greetd_ipc::{AuthMessageType, ErrorType, Request, Response, codec::TokioCodec};
|
|
|
-use rpassword::prompt_password;
|
|
|
-use std::env;
|
|
|
-use std::io::{self, Write};
|
|
|
-use std::process;
|
|
|
-use std::time::Duration;
|
|
|
-use tokio::net::UnixStream;
|
|
|
-
|
|
|
-const MAX_RETRIES: u8 = 3;
|
|
|
-const RETRY_DELAY: u64 = 2;
|
|
|
-
|
|
|
-struct GreeterState {
|
|
|
- retry_count: u8,
|
|
|
- username: String,
|
|
|
- stream: Option<UnixStream>,
|
|
|
-}
|
|
|
+mod ipc;
|
|
|
+mod server;
|
|
|
|
|
|
-impl GreeterState {
|
|
|
- fn new() -> Self {
|
|
|
- Self {
|
|
|
- retry_count: 0,
|
|
|
- username: String::new(),
|
|
|
- stream: None,
|
|
|
- }
|
|
|
- }
|
|
|
+use crate::server::run_server;
|
|
|
+use clap::Parser;
|
|
|
+use std::process;
|
|
|
+use tokio::process::Command;
|
|
|
+use tracing::{error, info};
|
|
|
+
|
|
|
+/// 命令行参数
|
|
|
+#[derive(Parser, Debug)]
|
|
|
+#[command(author, version, about, long_about = None)]
|
|
|
+struct Args {
|
|
|
+ /// 服务器监听端口
|
|
|
+ #[arg(short, long, default_value_t = 12801)]
|
|
|
+ port: u16,
|
|
|
+
|
|
|
+ /// 用户会话启动命令
|
|
|
+ #[arg(short, long)]
|
|
|
+ session_command: Option<String>,
|
|
|
+
|
|
|
+ /// 启动子进程命令 (父进程退出时会自动终止子进程)
|
|
|
+ #[arg(short, long)]
|
|
|
+ launch_command: Option<String>,
|
|
|
}
|
|
|
|
|
|
#[tokio::main]
|
|
|
-async fn main() -> io::Result<()> {
|
|
|
- let mut state = GreeterState::new();
|
|
|
- state.username = prompt_input("用户名:")?;
|
|
|
-
|
|
|
- loop {
|
|
|
- match try_login_flow(&mut state).await {
|
|
|
- Ok(()) => break,
|
|
|
- Err(e) => {
|
|
|
- handle_error(&mut state, &e);
|
|
|
-
|
|
|
- if state.retry_count >= MAX_RETRIES {
|
|
|
- eprintln!("超过最大重试次数");
|
|
|
- process::exit(1);
|
|
|
- }
|
|
|
-
|
|
|
- // 根据错误类型决定重试策略
|
|
|
- if !should_retry_immediately(&e) {
|
|
|
- let delay =
|
|
|
- Duration::from_secs(RETRY_DELAY * 2u64.pow(state.retry_count as u32));
|
|
|
- eprintln!("等待 {:?} 后重试...", delay);
|
|
|
- tokio::time::sleep(delay).await;
|
|
|
- }
|
|
|
- }
|
|
|
+async fn main() {
|
|
|
+ // 初始化日志
|
|
|
+ tracing_subscriber::fmt::init();
|
|
|
+
|
|
|
+ let args = Args::parse();
|
|
|
+ info!("启动参数: {:?}", args);
|
|
|
+
|
|
|
+ // 设置会话命令环境变量
|
|
|
+ if let Some(cmd) = &args.session_command {
|
|
|
+ info!("设置会话命令: {}", cmd);
|
|
|
+ unsafe {
|
|
|
+ std::env::set_var("SESSION_COMMAND", cmd);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ info!("使用默认会话命令: $SHELL");
|
|
|
}
|
|
|
- Ok(())
|
|
|
-}
|
|
|
-
|
|
|
-async fn try_login_flow(state: &mut GreeterState) -> io::Result<()> {
|
|
|
- let stream = connect_to_greetd().await?;
|
|
|
- state.stream = Some(stream);
|
|
|
- state.retry_count = 0;
|
|
|
|
|
|
- let mut request = Request::CreateSession {
|
|
|
- username: state.username.clone(),
|
|
|
+ // 启动子进程
|
|
|
+ let child = if let Some(cmd) = &args.launch_command {
|
|
|
+ info!("启动子进程: {}", cmd);
|
|
|
+ Some(
|
|
|
+ Command::new("sh")
|
|
|
+ .arg("-c")
|
|
|
+ .arg(cmd)
|
|
|
+ .kill_on_drop(true)
|
|
|
+ .spawn()
|
|
|
+ .expect("启动子进程失败")
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ None
|
|
|
};
|
|
|
- let mut starting_session = false;
|
|
|
-
|
|
|
- loop {
|
|
|
- let stream = state.stream.as_mut().unwrap();
|
|
|
- request
|
|
|
- .write_to(stream)
|
|
|
- .await
|
|
|
- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
|
|
-
|
|
|
- match Response::read_from(stream)
|
|
|
- .await
|
|
|
- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
|
|
|
- {
|
|
|
- Response::AuthMessage {
|
|
|
- auth_message_type,
|
|
|
- auth_message,
|
|
|
- } => {
|
|
|
- let response = match auth_message_type {
|
|
|
- AuthMessageType::Visible => Some(prompt_input(&auth_message)?),
|
|
|
- AuthMessageType::Secret => {
|
|
|
- let password = prompt_password(&auth_message)
|
|
|
- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
|
|
- Some(password)
|
|
|
- }
|
|
|
- AuthMessageType::Info => {
|
|
|
- eprintln!("信息: {}", auth_message);
|
|
|
- None
|
|
|
- }
|
|
|
- AuthMessageType::Error => {
|
|
|
- eprintln!("错误: {}", auth_message);
|
|
|
- None
|
|
|
- }
|
|
|
- };
|
|
|
|
|
|
- request = Request::PostAuthMessageResponse { response };
|
|
|
- }
|
|
|
- Response::Success => {
|
|
|
- if starting_session {
|
|
|
- return Ok(());
|
|
|
- }
|
|
|
-
|
|
|
- starting_session = true;
|
|
|
- let cmd = env::var("SHELL")
|
|
|
- .map(|s| vec![s])
|
|
|
- .unwrap_or_else(|_| vec!["/bin/bash".to_string()]);
|
|
|
-
|
|
|
- // 安全的环境变量白名单
|
|
|
- let safe_env = vec!["LANG", "PATH", "DISPLAY", "XAUTHORITY", "WAYLAND_DISPLAY"];
|
|
|
- let env: Vec<String> = env::vars()
|
|
|
- .filter(|(k, _)| safe_env.contains(&k.as_str()))
|
|
|
- .map(|(k, v)| format!("{k}={v}"))
|
|
|
- .collect();
|
|
|
-
|
|
|
- request = Request::StartSession { cmd, env };
|
|
|
- }
|
|
|
- Response::Error {
|
|
|
- error_type,
|
|
|
- description,
|
|
|
- } => {
|
|
|
- match error_type {
|
|
|
- ErrorType::AuthError => {
|
|
|
- // 认证错误时取消会话
|
|
|
- Request::CancelSession
|
|
|
- .write_to(stream)
|
|
|
- .await
|
|
|
- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
|
|
- return Err(io::Error::new(io::ErrorKind::PermissionDenied, description));
|
|
|
- }
|
|
|
- ErrorType::Error => {
|
|
|
- Request::CancelSession
|
|
|
- .write_to(stream)
|
|
|
- .await
|
|
|
- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
|
|
- return Err(io::Error::new(io::ErrorKind::Other, description));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // 启动服务器
|
|
|
+ info!("启动服务器,监听端口: {}", args.port);
|
|
|
+ if let Err(e) = run_server(args.port).await {
|
|
|
+ error!("服务器启动失败: {}", e);
|
|
|
+ process::exit(1);
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-async fn connect_to_greetd() -> io::Result<UnixStream> {
|
|
|
- let sock_path = env::var("GREETD_SOCK")
|
|
|
- .map_err(|_| io::Error::new(io::ErrorKind::NotFound, "GREETD_SOCK环境变量未设置"))?;
|
|
|
-
|
|
|
- for retry in 0..MAX_RETRIES {
|
|
|
- match UnixStream::connect(&sock_path).await {
|
|
|
- Ok(stream) => return Ok(stream),
|
|
|
- Err(_e) if retry < MAX_RETRIES - 1 => {
|
|
|
- eprintln!("连接失败,重试中... ({}/{})", retry + 1, MAX_RETRIES);
|
|
|
- tokio::time::sleep(Duration::from_secs(RETRY_DELAY)).await;
|
|
|
+ // 等待子进程退出
|
|
|
+ if let Some(mut child) = child {
|
|
|
+ match child.wait().await {
|
|
|
+ Ok(status) => {
|
|
|
+ info!("子进程已退出,状态码: {}", status);
|
|
|
+ process::exit(0);
|
|
|
}
|
|
|
Err(e) => {
|
|
|
- return Err(io::Error::new(
|
|
|
- io::ErrorKind::ConnectionRefused,
|
|
|
- format!("连接greetd服务失败: {} (sock={})", e, sock_path),
|
|
|
- ));
|
|
|
+ error!("等待子进程时出错: {}", e);
|
|
|
+ process::exit(1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- unreachable!()
|
|
|
-}
|
|
|
-
|
|
|
-fn prompt_input(prompt: &str) -> io::Result<String> {
|
|
|
- print!("{}", prompt);
|
|
|
- io::stdout().flush()?;
|
|
|
- let mut input = String::new();
|
|
|
- io::stdin().read_line(&mut input)?;
|
|
|
- Ok(input.trim().to_string())
|
|
|
-}
|
|
|
-
|
|
|
-fn handle_error(state: &mut GreeterState, error: &io::Error) {
|
|
|
- eprintln!("错误: {}", error);
|
|
|
- state.retry_count += 1;
|
|
|
- state.stream = None;
|
|
|
-}
|
|
|
-
|
|
|
-// 错误分类处理
|
|
|
-fn should_retry_immediately(error: &io::Error) -> bool {
|
|
|
- match error.kind() {
|
|
|
- io::ErrorKind::PermissionDenied => true, // 认证错误
|
|
|
- io::ErrorKind::InvalidData => true, // 无效凭据
|
|
|
- _ => false, // 连接或其他错误
|
|
|
- }
|
|
|
}
|