1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| mod getinputban; mod get_local_ip;
use std::{fs, io}; use std::io::Write; use actix_web::dev::ServiceRequest; use actix_web::{App,web, HttpServer, Responder}; use actix_web_httpauth::extractors::basic::BasicAuth; use actix_web_httpauth::middleware::HttpAuthentication; use actix_files::NamedFile; use serde::{Serialize,Deserialize}; use crate::getinputban::{get_console_ban_handler, ConsoleBan}; use crate::get_local_ip::get_local_ip;
async fn download(data: web::Data<Config>) -> impl Responder { NamedFile::open(&data.file_path) }
#[derive(Serialize, Deserialize)] struct Config { port: u16, user_id: String, password: String, file_path: String, }
async fn validator( req: ServiceRequest, credentials: BasicAuth, ) -> Result<ServiceRequest, (actix_web::error::Error, ServiceRequest)> { let user_id = credentials.user_id(); let app_data = req.app_data::<web::Data<Config>>().expect("No Config Data"); let ban_data = req.app_data::<web::Data<ConsoleBan>>().expect("No Ban Data"); let mut req_banned = false; { let conn_info = req.connection_info(); let ip = conn_info.peer_addr().unwrap_or("Unknown Address"); print!( "\nUser ID: {} / Password: {} / IP: {}\n>>", user_id, credentials.password().unwrap_or("No Password"), ip ); io::stdout().flush().expect("[*] Flush Failed"); if let Ok(list) = ban_data.list.lock() { for banned in list.iter() { println!("{}",banned.trim()); if banned.trim() == ip { req_banned = true; } } } } if req_banned{ return Err(( actix_web::error::ErrorUnauthorized("黑名单用户,禁止访问"), req, )) } if let Some(pwd) = credentials.password() && pwd == app_data.password { if user_id == app_data.user_id { return Ok(req); } } Err(( actix_web::error::ErrorUnauthorized("未授权用户,禁止访问"), req, )) }
#[actix_web::main] async fn main() -> io::Result<()> { println!("Running"); let local_ip = get_local_ip().expect("No Network"); let config_data: Config = serde_json::from_str(&fs::read_to_string(r".\config.json").expect("No Config"))?;
let config_data = web::Data::new(config_data);
let ban_data = web::Data::new(ConsoleBan::new());
println!(r" _____ ______ ___ ________ _______ ________ ________ ________ ________ _________ ________ _______ ________ ___ ___ _______ ________ |\ _ \ _ \|\ \|\ ___ \|\ ___ \ |\ ____\|\ __ \|\ __ \|\ _____\\___ ___\ |\ ____\|\ ___ \ |\ __ \|\ \ / /|\ ___ \ |\ __ \ \ \ \\\__\ \ \ \ \ \ \\ \ \ \ __/|\ \ \___|\ \ \|\ \ \ \|\ \ \ \__/\|___ \ \_| \ \ \___|\ \ __/|\ \ \|\ \ \ \ / / | \ __/|\ \ \|\ \ \ \ \\|__| \ \ \ \ \ \\ \ \ \ \_|/_\ \ \ \ \ _ _\ \ __ \ \ __\ \ \ \ \ \_____ \ \ \_|/_\ \ _ _\ \ \/ / / \ \ \_|/_\ \ _ _\ \ \ \ \ \ \ \ \ \ \\ \ \ \ \_|\ \ \ \____\ \ \\ \\ \ \ \ \ \ \_| \ \ \ \|____|\ \ \ \_|\ \ \ \\ \\ \ / / \ \ \_|\ \ \ \\ \| \ \__\ \ \__\ \__\ \__\\ \__\ \_______\ \_______\ \__\\ _\\ \__\ \__\ \__\ \ \__\ ____\_\ \ \_______\ \__\\ _\\ \__/ / \ \_______\ \__\\ _\ \|__| \|__|\|__|\|__| \|__|\|_______|\|_______|\|__|\|__|\|__|\|__|\|__| \|__| |\_________\|_______|\|__|\|__|\|__|/ \|_______|\|__|\|__| \|_________|"); println!("Path: {}",config_data.file_path); println!("User Name: {} / Password: {}",config_data.user_id,config_data.password); println!("Start Listening from 0.0.0.0:{}",config_data.port); println!("通过 {}:{} 访问",local_ip.to_string(),config_data.port);
tokio::spawn(get_console_ban_handler(ban_data.clone()));
HttpServer::new({ let value = config_data.clone(); let banned_data = ban_data.clone(); move || { App::new() .wrap(HttpAuthentication::basic(validator)) .app_data(value.clone()) .app_data(banned_data.clone()) .route("/download", web::get().to(download)) .service(web::redirect("/","/download")) } }) .bind(("0.0.0.0", config_data.port))? .run() .await }
|