diff --git a/Cargo.toml b/Cargo.toml index 1be9d97..639f55c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ log = "0.4" simplelog = "0.12" base64 = "0.21" dotenvy = "0.15" +chrono = { version = "0.4", default-features = false, features = ["clock"] } diff --git a/src/handler.rs b/src/handler.rs index 6153800..d40fe9a 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -3,6 +3,9 @@ use tokio_rustls::TlsAcceptor; use log::{info, error, warn}; use crate::auth::verify_basic_auth; use std::env; +use std::fs::OpenOptions; +use std::io::Write; +use chrono::Local; // 获取系统代理设置 fn get_system_proxy() -> Option<(String, u16)> { @@ -43,6 +46,21 @@ fn parse_proxy_url(url: &str) -> Option<(String, u16)> { None } +fn record_connection(ip: std::net::IpAddr, target: &str) { + let now = Local::now().format("%Y-%m-%d %H:%M:%S"); + match OpenOptions::new().create(true).append(true).open("connections.txt") { + Ok(mut file) => { + let line = format!("{} {} {}\n", now, ip, target); + if let Err(e) = file.write_all(line.as_bytes()) { + error!("\u5199\u5165\u8fde\u63a5\u8bb0\u5f55\u5931\u8d25: {}", e); + } + } + Err(e) => { + error!("\u65e0\u6cd5\u6253\u5f00\u8fde\u63a5\u8bb0\u5f55\u6587\u4ef6: {}", e); + } + } +} + // 通过代理连接目标 async fn connect_through_proxy(proxy_host: &str, proxy_port: u16, target: &str) -> Result> { info!("通过代理 {}:{} 连接目标: {}", proxy_host, proxy_port, target); @@ -204,6 +222,8 @@ Proxy-Authenticate: Basic realm=\"EasyProxy\"\r\n\r\n", .await; return; } + + record_connection(peer.ip(), target); // 连接目标服务器(通过系统代理或直接连接) info!("开始连接目标服务器: {}", target);