Skip to content

Commit

Permalink
fix compile
Browse files Browse the repository at this point in the history
  • Loading branch information
hoping committed Dec 27, 2023
1 parent 01ac3f4 commit 2bfc1f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
40 changes: 30 additions & 10 deletions src/aproxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,17 @@ async fn wait_until_stop(_running: Arc<AtomicBool>, _ip: IpAddr) {}
#[cfg(not(target_os = "windows"))]
async fn wait_until_stop(running: Arc<AtomicBool>, ip: IpAddr) {
let timeout = OPTIONS.proxy_args().ipset_timeout;
if timeout == 0 || OPTIONS.proxy_args().skip_dns_ip == Some(ip) {
return;
{
let proxy_data = OPTIONS
.proxy_args()
.proxy_data
.as_ref()
.unwrap()
.lock()
.await;
if timeout == 0 || proxy_data.skip_dns == Some(ip) {
return;
}
}
let mut tick = tokio::time::interval(std::time::Duration::from_secs(1));
let mut counter = 0;
Expand All @@ -155,14 +164,17 @@ async fn wait_until_stop(running: Arc<AtomicBool>, ip: IpAddr) {
if counter % timeout != 1 {
continue;
}
let mut session = OPTIONS
let mut proxy_data = OPTIONS
.proxy_args()
.session
.proxy_data
.as_ref()
.unwrap()
.lock()
.unwrap();
match session.add(ip, Some(timeout as u32 + 5)) {
.await;
match proxy_data
.no_bypass_session
.add(ip, Some(timeout as u32 + 5))
{
Ok(ret) => {
if !ret {
log::error!("add ip:{} to ipset failed", ip);
Expand All @@ -187,11 +199,19 @@ pub async fn init_tls_conn(
.collect();
#[cfg(target_os = "linux")]
{
let mut proxy_data = OPTIONS.proxy_args().proxy_data.as_ref().unwrap().lock();
let mut proxy_data = OPTIONS
.proxy_args()
.proxy_data
.as_ref()
.unwrap()
.lock()
.await;
for ip in &ips {
if (!proxy_data.ips.contains(ip)) {
proxy_data.ips.push(ip.clone());
proxy_data.bypass_session.add(ip);
if !proxy_data.server_ips.contains(&ip.ip()) {
proxy_data.server_ips.push(ip.ip());
if let Err(err) = proxy_data.bypass_session.add(ip.ip(), None) {
log::error!("add ip:{} to session failed:{}", ip, err);
}
}
}
}
Expand Down
20 changes: 5 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ use std::{
use clap::Parser;
use sha2::{Digest, Sha224};

#[cfg(target_os = "linux")]
use ipset::{
types::{EnvOption, HashIp},
Session,
};

use crate::{
types::TrojanError,
utils::{get_system_dns, resolve},
Expand Down Expand Up @@ -214,14 +208,10 @@ pub struct ProxyArgs {
#[clap(short = 'd', long, default_value = "8.8.8.8")]
pub skip_dns: String,

/// skip_dns in IpAddr
#[clap(skip)]
pub skip_dns_ip: Option<IpAddr>,

/// session used for no bypass ipset
#[clap(skip)]
#[cfg(target_os = "linux")]
pub proxy_data: Option<std::sync::Mutex<ProxyData>>,
pub proxy_data: Option<tokio::sync::Mutex<crate::types::ProxyData>>,
}

#[derive(Parser)]
Expand Down Expand Up @@ -374,13 +364,13 @@ impl Opts {
Mode::Proxy(ref mut args) | Mode::Aproxy(ref mut args) => {
#[cfg(target_os = "linux")]
{
let proxy_data = crate::types::ProxyData::new(
args.no_bypass_ipset.as_str(),
args.bypass_ipset.as_str(),
let mut proxy_data = crate::types::ProxyData::new(
args.no_bypass_ipset.clone(),
args.bypass_ipset.clone(),
);
if args.ipset_timeout > 0 {
let ip: Option<IpAddr> = args.skip_dns.as_str().parse().ok();
args.skip_dns_ip = ip;
proxy_data.skip_dns = ip;
}
args.proxy_data = Some(tokio::sync::Mutex::new(proxy_data));
}
Expand Down
7 changes: 4 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::net::IpAddr;

use derive_more::From;
use log::SetLoggerError;

#[cfg(target_os = "linux")]
use ipset::{
types::{EnvOption, HashIp},
Session,
};
use log::SetLoggerError;

#[allow(dead_code)]
#[derive(From, Debug)]
Expand Down Expand Up @@ -55,19 +54,21 @@ pub type Result<T> = std::result::Result<T, TrojanError>;
#[cfg(target_os = "linux")]
pub struct ProxyData {
pub server_ips: Vec<IpAddr>,
pub skip_dns: Option<IpAddr>,
pub bypass_session: Session<HashIp>,
pub no_bypass_session: Session<HashIp>,
}

#[cfg(target_os = "linux")]
impl ProxyData {
pub fn new(no_bypass: &str, bypass: &str) -> Self {
pub fn new(no_bypass: String, bypass: String) -> Self {
let no_bypass_session = Session::new(no_bypass);
no_bypass_session.set_option(EnvOption::Exist);
let bypass_session = Session::new(bypass);
bypass_session.set_option(EnvOption::Exist);
Self {
server_ips: vec![],
skip_dns: None,
bypass_session,
no_bypass_session,
}
Expand Down

0 comments on commit 2bfc1f3

Please sign in to comment.