Skip to content

Commit 6fba4c4

Browse files
committed
feat(shadowsocks-service): local-tun removed dependency of tun on not supported platforms
Making non-supported platforms to be able to build with the default "full" feature set.
1 parent e8cb099 commit 6fba4c4

File tree

7 files changed

+204
-36
lines changed

7 files changed

+204
-36
lines changed

crates/shadowsocks-service/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ flate2 = { version = "1.0", optional = true }
178178
brotli = { version = "8.0", optional = true }
179179
zstd = { version = "0.13", optional = true }
180180

181-
tun = { version = "0.7", optional = true, features = ["async"] }
182181
etherparse = { version = "0.18", optional = true }
183182
smoltcp = { version = "0.12", optional = true, default-features = false, features = [
184183
"std",
@@ -206,6 +205,9 @@ nix = { version = "0.29", features = ["ioctl"] }
206205
[target.'cfg(windows)'.dependencies]
207206
windows-sys = { version = "0.59", features = ["Win32_Networking_WinSock"] }
208207

208+
[target.'cfg(any(target_os = "ios", target_os = "macos", target_os = "linux", target_os = "android", target_os = "windows", target_os = "freebsd"))'.dependencies]
209+
tun = { version = "0.7", optional = true, features = ["async"] }
210+
209211
[dev-dependencies]
210212
byteorder = "1.5"
211213
env_logger = "0.11"

crates/shadowsocks-service/src/local/redir/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cfg_if! {
1212
}
1313

1414
#[cfg(unix)]
15+
#[allow(dead_code)]
1516
pub fn set_ipv6_only<S>(socket: &S, ipv6_only: bool) -> io::Result<()>
1617
where
1718
S: std::os::unix::io::AsRawFd,

crates/shadowsocks-service/src/local/redir/tcprelay/sys/unix/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ cfg_if! {
1010
target_os = "openbsd"))] {
1111
mod bsd;
1212
pub use self::bsd::*;
13+
} else {
14+
mod not_supported;
15+
pub use self::not_supported::*;
1316
}
1417
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::{io, net::SocketAddr};
2+
3+
use shadowsocks::net::AcceptOpts;
4+
use tokio::net::{TcpListener, TcpStream};
5+
6+
use crate::{
7+
config::RedirType,
8+
local::redir::redir_ext::{TcpListenerRedirExt, TcpStreamRedirExt},
9+
};
10+
11+
impl TcpListenerRedirExt for TcpListener {
12+
async fn bind_redir(_ty: RedirType, _addr: SocketAddr, _accept_opts: AcceptOpts) -> io::Result<TcpListener> {
13+
unimplemented!("TCP transparent proxy is not supported on this platform")
14+
}
15+
}
16+
17+
impl TcpStreamRedirExt for TcpStream {
18+
fn destination_addr(&self, _ty: RedirType) -> io::Result<SocketAddr> {
19+
unimplemented!("TCP transparent proxy is not supported on this platform")
20+
}
21+
}

crates/shadowsocks-service/src/local/redir/udprelay/sys/unix/not_supported.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{io, net::SocketAddr};
1+
use std::{
2+
io,
3+
net::SocketAddr,
4+
task::{Context, Poll},
5+
};
26

37
use crate::{
48
config::RedirType,

crates/shadowsocks-service/src/local/tun/fake_tun.rs

+168-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Fake `tun` for those platforms that doesn't support `tun`
22
3+
#![allow(dead_code)]
4+
35
use std::{
46
io::{self, Read, Write},
57
net::IpAddr,
@@ -9,7 +11,142 @@ use std::{
911
};
1012

1113
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
12-
use tun::{AbstractDevice, Configuration, Error as TunError};
14+
15+
/// TUN interface OSI layer of operation.
16+
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
17+
pub enum Layer {
18+
L2,
19+
#[default]
20+
L3,
21+
}
22+
23+
/// Configuration builder for a TUN interface.
24+
#[derive(Clone, Default, Debug)]
25+
pub struct Configuration;
26+
27+
impl Configuration {
28+
/// Set the tun name.
29+
///
30+
/// [Note: on macOS, the tun name must be the form `utunx` where `x` is a number, such as `utun3`. -- end note]
31+
pub fn tun_name<S: AsRef<str>>(&mut self, _tun_name: S) -> &mut Self {
32+
self
33+
}
34+
35+
/// Set the address.
36+
pub fn address(&mut self, _value: IpAddr) -> &mut Self {
37+
self
38+
}
39+
40+
/// Set the destination address.
41+
pub fn destination(&mut self, _value: IpAddr) -> &mut Self {
42+
self
43+
}
44+
45+
/// Set the broadcast address.
46+
pub fn broadcast(&mut self, _value: IpAddr) -> &mut Self {
47+
self
48+
}
49+
50+
/// Set the netmask.
51+
pub fn netmask(&mut self, _value: IpAddr) -> &mut Self {
52+
self
53+
}
54+
55+
/// Set the MTU.
56+
pub fn mtu(&mut self, _value: u16) -> &mut Self {
57+
self
58+
}
59+
60+
/// Set the interface to be enabled once created.
61+
pub fn up(&mut self) -> &mut Self {
62+
self
63+
}
64+
65+
/// Set the interface to be disabled once created.
66+
pub fn down(&mut self) -> &mut Self {
67+
self
68+
}
69+
70+
/// Set the OSI layer of operation.
71+
pub fn layer(&mut self, _value: Layer) -> &mut Self {
72+
self
73+
}
74+
75+
/// Set the raw fd.
76+
#[cfg(unix)]
77+
pub fn raw_fd(&mut self, _fd: ::std::os::fd::RawFd) -> &mut Self {
78+
self
79+
}
80+
}
81+
82+
/// tun Error type
83+
#[derive(thiserror::Error, Debug)]
84+
pub enum Error {
85+
#[error("not implementated")]
86+
NotImplemented,
87+
88+
#[error(transparent)]
89+
Io(#[from] std::io::Error),
90+
}
91+
92+
pub type Result<T, E = Error> = ::std::result::Result<T, E>;
93+
94+
/// A TUN abstract device interface.
95+
pub trait AbstractDevice: Read + Write {
96+
/// Reconfigure the device.
97+
fn configure(&mut self, _config: &Configuration) -> Result<()> {
98+
Ok(())
99+
}
100+
101+
/// Get the device index.
102+
fn tun_index(&self) -> Result<i32>;
103+
104+
/// Get the device tun name.
105+
fn tun_name(&self) -> Result<String>;
106+
107+
/// Set the device tun name.
108+
fn set_tun_name(&mut self, tun_name: &str) -> Result<()>;
109+
110+
/// Turn on or off the interface.
111+
fn enabled(&mut self, value: bool) -> Result<()>;
112+
113+
/// Get the address.
114+
fn address(&self) -> Result<IpAddr>;
115+
116+
/// Set the address.
117+
fn set_address(&mut self, value: IpAddr) -> Result<()>;
118+
119+
/// Get the destination address.
120+
fn destination(&self) -> Result<IpAddr>;
121+
122+
/// Set the destination address.
123+
fn set_destination(&mut self, value: IpAddr) -> Result<()>;
124+
125+
/// Get the broadcast address.
126+
fn broadcast(&self) -> Result<IpAddr>;
127+
128+
/// Set the broadcast address.
129+
fn set_broadcast(&mut self, value: IpAddr) -> Result<()>;
130+
131+
/// Get the netmask.
132+
fn netmask(&self) -> Result<IpAddr>;
133+
134+
/// Set the netmask.
135+
fn set_netmask(&mut self, value: IpAddr) -> Result<()>;
136+
137+
/// Get the MTU.
138+
fn mtu(&self) -> Result<u16>;
139+
140+
/// Set the MTU.
141+
///
142+
/// [Note: This setting has no effect on the Windows platform due to the mtu of wintun is always 65535. --end note]
143+
fn set_mtu(&mut self, value: u16) -> Result<()>;
144+
145+
/// Return whether the underlying tun device on the platform has packet information
146+
///
147+
/// [Note: This value is not used to specify whether the packets delivered from/to tun have packet information. -- end note]
148+
fn packet_information(&self) -> bool;
149+
}
13150

14151
pub struct FakeQueue;
15152

@@ -32,60 +169,60 @@ impl Write for FakeQueue {
32169
pub struct FakeDevice;
33170

34171
impl AbstractDevice for FakeDevice {
35-
fn tun_name(&self) -> tun::Result<String> {
36-
Err(TunError::NotImplemented)
172+
fn tun_name(&self) -> Result<String> {
173+
Err(Error::NotImplemented)
37174
}
38175

39-
fn tun_index(&self) -> tun::Result<i32> {
40-
Err(TunError::NotImplemented)
176+
fn tun_index(&self) -> Result<i32> {
177+
Err(Error::NotImplemented)
41178
}
42179

43-
fn set_tun_name(&mut self, _: &str) -> tun::Result<()> {
44-
Err(TunError::NotImplemented)
180+
fn set_tun_name(&mut self, _: &str) -> Result<()> {
181+
Err(Error::NotImplemented)
45182
}
46183

47-
fn enabled(&mut self, _: bool) -> tun::Result<()> {
48-
Err(TunError::NotImplemented)
184+
fn enabled(&mut self, _: bool) -> Result<()> {
185+
Err(Error::NotImplemented)
49186
}
50187

51-
fn address(&self) -> tun::Result<IpAddr> {
52-
Err(TunError::NotImplemented)
188+
fn address(&self) -> Result<IpAddr> {
189+
Err(Error::NotImplemented)
53190
}
54191

55-
fn set_address(&mut self, _: IpAddr) -> tun::Result<()> {
56-
Err(TunError::NotImplemented)
192+
fn set_address(&mut self, _: IpAddr) -> Result<()> {
193+
Err(Error::NotImplemented)
57194
}
58195

59-
fn destination(&self) -> tun::Result<IpAddr> {
60-
Err(TunError::NotImplemented)
196+
fn destination(&self) -> Result<IpAddr> {
197+
Err(Error::NotImplemented)
61198
}
62199

63-
fn set_destination(&mut self, _: IpAddr) -> tun::Result<()> {
64-
Err(TunError::NotImplemented)
200+
fn set_destination(&mut self, _: IpAddr) -> Result<()> {
201+
Err(Error::NotImplemented)
65202
}
66203

67-
fn broadcast(&self) -> tun::Result<IpAddr> {
68-
Err(TunError::NotImplemented)
204+
fn broadcast(&self) -> Result<IpAddr> {
205+
Err(Error::NotImplemented)
69206
}
70207

71-
fn set_broadcast(&mut self, _: IpAddr) -> tun::Result<()> {
72-
Err(TunError::NotImplemented)
208+
fn set_broadcast(&mut self, _: IpAddr) -> Result<()> {
209+
Err(Error::NotImplemented)
73210
}
74211

75-
fn netmask(&self) -> tun::Result<IpAddr> {
76-
Err(TunError::NotImplemented)
212+
fn netmask(&self) -> Result<IpAddr> {
213+
Err(Error::NotImplemented)
77214
}
78215

79-
fn set_netmask(&mut self, _: IpAddr) -> tun::Result<()> {
80-
Err(TunError::NotImplemented)
216+
fn set_netmask(&mut self, _: IpAddr) -> Result<()> {
217+
Err(Error::NotImplemented)
81218
}
82219

83-
fn mtu(&self) -> tun::Result<u16> {
84-
Err(TunError::NotImplemented)
220+
fn mtu(&self) -> Result<u16> {
221+
Err(Error::NotImplemented)
85222
}
86223

87-
fn set_mtu(&mut self, _: u16) -> tun::Result<()> {
88-
Err(TunError::NotImplemented)
224+
fn set_mtu(&mut self, _: u16) -> Result<()> {
225+
Err(Error::NotImplemented)
89226
}
90227

91228
fn packet_information(&self) -> bool {
@@ -158,6 +295,6 @@ impl AsyncWrite for AsyncDevice {
158295
}
159296

160297
/// Create a TUN device with the given name.
161-
pub fn create_as_async(_: &Configuration) -> Result<AsyncDevice, TunError> {
162-
Err(TunError::NotImplemented)
298+
pub fn create_as_async(_: &Configuration) -> Result<AsyncDevice, Error> {
299+
Err(Error::NotImplemented)
163300
}

crates/shadowsocks-service/src/local/tun/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ cfg_if! {
3333
create_as_async, AsyncDevice, Configuration as TunConfiguration, AbstractDevice, Error as TunError, Layer,
3434
};
3535
} else {
36-
use tun::{AbstractDevice, Configuration as TunConfiguration, Error as TunError, Layer};
37-
3836
mod fake_tun;
39-
use self::fake_tun::{create_as_async, AsyncDevice};
37+
use self::fake_tun::{
38+
AbstractDevice, AsyncDevice, Configuration as TunConfiguration, Error as TunError, Layer, create_as_async,
39+
};
4040
}
4141
}
4242

0 commit comments

Comments
 (0)