mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
rename list to rules, change api to free list control namespace
This commit is contained in:
parent
949a8a3b2c
commit
c821fa7492
5 changed files with 43 additions and 41 deletions
54
src/main.rs
54
src/main.rs
|
|
@ -1,5 +1,5 @@
|
|||
mod list;
|
||||
mod opt;
|
||||
mod rules;
|
||||
mod stats;
|
||||
|
||||
use anyhow::Context;
|
||||
|
|
@ -7,10 +7,10 @@ use fast_socks5::{
|
|||
ReplyError, Result, Socks5Command, SocksError,
|
||||
server::{DnsResolveHelper as _, Socks5ServerProtocol, run_tcp_proxy, run_udp_proxy},
|
||||
};
|
||||
use list::List;
|
||||
use log::*;
|
||||
use opt::{AuthMode, Opt};
|
||||
use rocket::{State, http::Status, serde::json::Json};
|
||||
use rules::Rules;
|
||||
use stats::{Snap, Total};
|
||||
use std::{future::Future, sync::Arc, time::Instant};
|
||||
use structopt::StructOpt;
|
||||
|
|
@ -29,11 +29,11 @@ async fn api_totals(totals: &State<Arc<Total>>, startup_time: &State<Instant>) -
|
|||
#[rocket::get("/api/allow/<rule>")]
|
||||
async fn api_allow(
|
||||
rule: &str,
|
||||
list: &State<Arc<List>>,
|
||||
rules: &State<Arc<Rules>>,
|
||||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<bool>, Status> {
|
||||
let result = list.allow(rule).await;
|
||||
totals.set_entries(list.rules().await);
|
||||
let result = rules.allow(rule).await;
|
||||
totals.set_entries(rules.rules().await);
|
||||
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Allow request handle error for `{rule}`: `{e}`");
|
||||
|
|
@ -44,11 +44,11 @@ async fn api_allow(
|
|||
#[rocket::get("/api/block/<rule>")]
|
||||
async fn api_block(
|
||||
rule: &str,
|
||||
list: &State<Arc<List>>,
|
||||
rules: &State<Arc<Rules>>,
|
||||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<bool>, Status> {
|
||||
let result = list.block(rule).await;
|
||||
totals.set_entries(list.rules().await);
|
||||
let result = rules.block(rule).await;
|
||||
totals.set_entries(rules.rules().await);
|
||||
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Block request handle error for `{rule}`: `{e}`");
|
||||
|
|
@ -56,16 +56,16 @@ async fn api_block(
|
|||
})?))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/list")]
|
||||
async fn api_list(list: &State<Arc<List>>) -> Result<Json<Vec<String>>, Status> {
|
||||
let result = list.list().await;
|
||||
info!("Get list rules (total: {})", result.len());
|
||||
#[rocket::get("/api/rules")]
|
||||
async fn api_rules(rules: &State<Arc<Rules>>) -> Result<Json<Vec<String>>, Status> {
|
||||
let result = rules.list().await;
|
||||
info!("Get rules (total: {})", result.len());
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/cache/clean")]
|
||||
async fn api_cache_clean(list: &State<Arc<List>>) -> Result<Json<Option<Vec<String>>>, Status> {
|
||||
let result = list.cache_clean().await;
|
||||
async fn api_cache_clean(rules: &State<Arc<Rules>>) -> Result<Json<Option<Vec<String>>>, Status> {
|
||||
let result = rules.cache_clean().await;
|
||||
info!("Clean cache file: {result:?}");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Could not handle cache clean request: `{e}`");
|
||||
|
|
@ -79,19 +79,19 @@ async fn rocket() -> _ {
|
|||
|
||||
let opt: &'static Opt = Box::leak(Box::new(Opt::from_args()));
|
||||
|
||||
let list = Arc::new(
|
||||
List::from_opt(&opt.allow_list, opt.cache.clone())
|
||||
let rules = Arc::new(
|
||||
Rules::from_opt(&opt.allow_list, opt.cache.clone())
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let totals = Arc::new(Total::with_rules(list.rules().await));
|
||||
let totals = Arc::new(Total::with_rules(rules.rules().await));
|
||||
|
||||
tokio::spawn({
|
||||
let socks_list = list.clone();
|
||||
let socks_rules = rules.clone();
|
||||
let socks_totals = totals.clone();
|
||||
async move {
|
||||
if let Err(err) = spawn_socks_server(opt, socks_list, socks_totals).await {
|
||||
if let Err(err) = spawn_socks_server(opt, socks_rules, socks_totals).await {
|
||||
error!("SOCKS server failed: `{err}`");
|
||||
}
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ async fn rocket() -> _ {
|
|||
address: opt.api_addr.ip(),
|
||||
..rocket::Config::release_default()
|
||||
})
|
||||
.manage(list)
|
||||
.manage(rules)
|
||||
.manage(totals)
|
||||
.manage(Instant::now())
|
||||
.mount(
|
||||
|
|
@ -113,13 +113,17 @@ async fn rocket() -> _ {
|
|||
api_totals,
|
||||
api_allow,
|
||||
api_block,
|
||||
api_list,
|
||||
api_rules,
|
||||
api_cache_clean
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
async fn spawn_socks_server(opt: &'static Opt, list: Arc<List>, totals: Arc<Total>) -> Result<()> {
|
||||
async fn spawn_socks_server(
|
||||
opt: &'static Opt,
|
||||
rules: Arc<Rules>,
|
||||
totals: Arc<Total>,
|
||||
) -> Result<()> {
|
||||
if opt.allow_udp && opt.public_addr.is_none() {
|
||||
return Err(SocksError::ArgumentInputError(
|
||||
"Can't allow UDP if public-addr is not set",
|
||||
|
|
@ -138,7 +142,7 @@ async fn spawn_socks_server(opt: &'static Opt, list: Arc<List>, totals: Arc<Tota
|
|||
loop {
|
||||
match listener.accept().await {
|
||||
Ok((socket, _client_addr)) => {
|
||||
spawn_and_log_error(serve_socks5(opt, socket, list.clone(), totals.clone()));
|
||||
spawn_and_log_error(serve_socks5(opt, socket, rules.clone(), totals.clone()));
|
||||
}
|
||||
Err(err) => error!("accept error = {:?}", err),
|
||||
}
|
||||
|
|
@ -148,7 +152,7 @@ async fn spawn_socks_server(opt: &'static Opt, list: Arc<List>, totals: Arc<Tota
|
|||
async fn serve_socks5(
|
||||
opt: &Opt,
|
||||
socket: tokio::net::TcpStream,
|
||||
list: Arc<List>,
|
||||
rules: Arc<Rules>,
|
||||
totals: Arc<Total>,
|
||||
) -> Result<(), SocksError> {
|
||||
totals.increase_request();
|
||||
|
|
@ -170,7 +174,7 @@ async fn serve_socks5(
|
|||
|
||||
let (host, _) = request.2.clone().into_string_and_port();
|
||||
|
||||
if !list.any(&host).await {
|
||||
if !rules.any(&host).await {
|
||||
totals.increase_blocked();
|
||||
info!("Blocked connection attempt to: {host}");
|
||||
request
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue