Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support fwmark in server side to split outbound tunnel #1466

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@

use std::{
borrow::Cow,
convert::{From, Infallible},

Check warning on line 46 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (ubuntu-latest)

the item `From` is imported redundantly

warning: the item `From` is imported redundantly --> crates/shadowsocks-service/src/config.rs:46:15 | 46 | convert::{From, Infallible}, | ^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:129:13 | = note: the item `From` is already defined here | = note: `#[warn(unused_imports)]` on by default

Check warning on line 46 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (macos-latest)

the item `From` is imported redundantly

warning: the item `From` is imported redundantly --> crates/shadowsocks-service/src/config.rs:46:15 | 46 | convert::{From, Infallible}, | ^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:129:13 | = note: the item `From` is already defined here | = note: `#[warn(unused_imports)]` on by default
default::Default,
env,
fmt::{self, Debug, Display, Formatter},
fs::OpenOptions,
io::Read,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
option::Option,

Check warning on line 53 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (ubuntu-latest)

the item `Option` is imported redundantly

warning: the item `Option` is imported redundantly --> crates/shadowsocks-service/src/config.rs:53:5 | 53 | option::Option, | ^^^^^^^^^^^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:129:13 | = note: the item `Option` is already defined here

Check warning on line 53 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (macos-latest)

the item `Option` is imported redundantly

warning: the item `Option` is imported redundantly --> crates/shadowsocks-service/src/config.rs:53:5 | 53 | option::Option, | ^^^^^^^^^^^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:129:13 | = note: the item `Option` is already defined here
path::{Path, PathBuf},
str::FromStr,
string::ToString,

Check warning on line 56 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (ubuntu-latest)

the item `ToString` is imported redundantly

warning: the item `ToString` is imported redundantly --> crates/shadowsocks-service/src/config.rs:56:5 | 56 | string::ToString, | ^^^^^^^^^^^^^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:125:13 | = note: the item `ToString` is already defined here

Check warning on line 56 in crates/shadowsocks-service/src/config.rs

View workflow job for this annotation

GitHub Actions / clippy-check (macos-latest)

the item `ToString` is imported redundantly

warning: the item `ToString` is imported redundantly --> crates/shadowsocks-service/src/config.rs:56:5 | 56 | string::ToString, | ^^^^^^^^^^^^^^^^ --> /rustc/3c85e56249b0b1942339a6a989a971bf6f1c9e0f/library/std/src/prelude/mod.rs:125:13 | = note: the item `ToString` is already defined here
time::Duration,
};

Expand Down Expand Up @@ -365,6 +365,10 @@

#[serde(skip_serializing_if = "Option::is_none")]
acl: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: Option<u32>,
}

/// Server config type
Expand Down Expand Up @@ -1119,12 +1123,20 @@
pub config: ServerConfig,
/// Server's private ACL, set to `None` will use the global `AccessControl`
pub acl: Option<AccessControl>,
/// Server's outbound fwmark to support split tunnel
#[cfg(any(target_os = "linux", target_os = "android"))]
pub outbound_fwmark: Option<u32>,
}

impl ServerInstanceConfig {
/// Create with `ServerConfig`
pub fn with_server_config(config: ServerConfig) -> ServerInstanceConfig {
ServerInstanceConfig { config, acl: None }
ServerInstanceConfig {
config,
acl: None,
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: None,
}
}
}

Expand Down Expand Up @@ -1762,6 +1774,8 @@
let server_instance = ServerInstanceConfig {
config: nsvr,
acl: None,
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: config.outbound_fwmark,
};

nconfig.server.push(server_instance);
Expand Down Expand Up @@ -1928,6 +1942,8 @@
let mut server_instance = ServerInstanceConfig {
config: nsvr,
acl: None,
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: config.outbound_fwmark,
};

if let Some(acl_path) = svr.acl {
Expand All @@ -1945,6 +1961,11 @@
server_instance.acl = Some(acl);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(outbound_fwmark) = svr.outbound_fwmark {
server_instance.outbound_fwmark = Some(outbound_fwmark);
}

nconfig.server.push(server_instance);
}
}
Expand Down Expand Up @@ -2699,6 +2720,8 @@
.acl
.as_ref()
.and_then(|a| a.file_path().to_str().map(ToOwned::to_owned)),
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: inst.outbound_fwmark.clone(),
});
}

Expand Down
2 changes: 2 additions & 0 deletions crates/shadowsocks-service/src/manager/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ impl Manager {
let server_instance = ServerInstanceConfig {
config: svr_cfg.clone(),
acl: None, // Set with --acl command line argument
#[cfg(any(target_os = "linux", target_os = "android"))]
outbound_fwmark: None,
};

let mut config = Config::new(ConfigType::Server);
Expand Down
5 changes: 5 additions & 0 deletions crates/shadowsocks-service/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub async fn run(config: Config) -> io::Result<()> {
server_builder.set_dns_resolver(r.clone());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(fwmark) = inst.outbound_fwmark {
connect_opts.fwmark = Some(fwmark);
}

server_builder.set_connect_opts(connect_opts.clone());
server_builder.set_accept_opts(accept_opts.clone());

Expand Down
Loading