Skip to content

Commit 9411539

Browse files
committed
feat(local-tun): changed dependency from tun to tun2
tun (rust-tun) author was lost contact for a long time. Changed to another actively maintained fork: tun2. - fixes #1546, removed dependency of ioctl-sys
1 parent 0fc35d9 commit 9411539

File tree

12 files changed

+88
-109
lines changed

12 files changed

+88
-109
lines changed

Cargo.lock

Lines changed: 14 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/shadowsocks-service/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ local-tunnel = ["local"]
9393
# Enable socks4 protocol for sslocal
9494
local-socks4 = ["local"]
9595
# Enable Tun interface protocol for sslocal
96-
local-tun = ["local", "etherparse", "tun", "smoltcp"]
96+
local-tun = ["local", "etherparse", "tun2", "smoltcp"]
9797
# Enable Fake DNS
9898
local-fake-dns = ["local", "trust-dns", "sled", "bson"]
9999
# sslocal support online URL (SIP008 Online Configuration Delivery)
@@ -167,7 +167,7 @@ ipnet = "2.9"
167167
iprange = "0.6"
168168
regex = "1.4"
169169

170-
tun = { version = "0.6", optional = true, features = ["async"] }
170+
tun2 = { version = "1", optional = true, features = ["async"] }
171171
etherparse = { version = "0.15", optional = true }
172172
smoltcp = { version = "0.11", optional = true, default-features = false, features = [
173173
"std",

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
33
use std::{
44
io::{self, Read, Write},
5-
net::Ipv4Addr,
5+
net::IpAddr,
66
pin::Pin,
77
task::{Context, Poll},
88
};
99

1010
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
11-
use tun::{Configuration, Device, Error as TunError};
11+
use tun2::{AbstractDevice, Configuration, Error as TunError};
1212

1313
pub struct FakeQueue;
1414

@@ -30,63 +30,61 @@ impl Write for FakeQueue {
3030

3131
pub struct FakeDevice;
3232

33-
impl Device for FakeDevice {
34-
type Queue = FakeQueue;
35-
36-
fn name(&self) -> tun::Result<String> {
33+
impl AbstractDevice for FakeDevice {
34+
fn tun_name(&self) -> tun2::Result<String> {
3735
Err(TunError::NotImplemented)
3836
}
3937

40-
fn set_name(&mut self, _: &str) -> tun::Result<()> {
38+
fn set_tun_name(&mut self, _: &str) -> tun2::Result<()> {
4139
Err(TunError::NotImplemented)
4240
}
4341

44-
fn enabled(&mut self, _: bool) -> tun::Result<()> {
42+
fn enabled(&mut self, _: bool) -> tun2::Result<()> {
4543
Err(TunError::NotImplemented)
4644
}
4745

48-
fn address(&self) -> tun::Result<Ipv4Addr> {
46+
fn address(&self) -> tun2::Result<IpAddr> {
4947
Err(TunError::NotImplemented)
5048
}
5149

52-
fn set_address(&mut self, _: Ipv4Addr) -> tun::Result<()> {
50+
fn set_address(&mut self, _: IpAddr) -> tun2::Result<()> {
5351
Err(TunError::NotImplemented)
5452
}
5553

56-
fn destination(&self) -> tun::Result<Ipv4Addr> {
54+
fn destination(&self) -> tun2::Result<IpAddr> {
5755
Err(TunError::NotImplemented)
5856
}
5957

60-
fn set_destination(&mut self, _: Ipv4Addr) -> tun::Result<()> {
58+
fn set_destination(&mut self, _: IpAddr) -> tun2::Result<()> {
6159
Err(TunError::NotImplemented)
6260
}
6361

64-
fn broadcast(&self) -> tun::Result<Ipv4Addr> {
62+
fn broadcast(&self) -> tun2::Result<IpAddr> {
6563
Err(TunError::NotImplemented)
6664
}
6765

68-
fn set_broadcast(&mut self, _: Ipv4Addr) -> tun::Result<()> {
66+
fn set_broadcast(&mut self, _: IpAddr) -> tun2::Result<()> {
6967
Err(TunError::NotImplemented)
7068
}
7169

72-
fn netmask(&self) -> tun::Result<Ipv4Addr> {
70+
fn netmask(&self) -> tun2::Result<IpAddr> {
7371
Err(TunError::NotImplemented)
7472
}
7573

76-
fn set_netmask(&mut self, _: Ipv4Addr) -> tun::Result<()> {
74+
fn set_netmask(&mut self, _: IpAddr) -> tun2::Result<()> {
7775
Err(TunError::NotImplemented)
7876
}
7977

80-
fn mtu(&self) -> tun::Result<i32> {
78+
fn mtu(&self) -> tun2::Result<u16> {
8179
Err(TunError::NotImplemented)
8280
}
8381

84-
fn set_mtu(&mut self, _: i32) -> tun::Result<()> {
82+
fn set_mtu(&mut self, _: u16) -> tun2::Result<()> {
8583
Err(TunError::NotImplemented)
8684
}
8785

88-
fn queue(&mut self, _: usize) -> Option<&mut Self::Queue> {
89-
None
86+
fn packet_information(&self) -> bool {
87+
false
9088
}
9189
}
9290

@@ -108,12 +106,14 @@ impl Write for FakeDevice {
108106

109107
pub struct AsyncDevice(FakeDevice);
110108

111-
impl AsyncDevice {
112-
pub fn get_ref(&self) -> &FakeDevice {
109+
impl AsRef<FakeDevice> for AsyncDevice {
110+
fn as_ref(&self) -> &FakeDevice {
113111
&self.0
114112
}
113+
}
115114

116-
pub fn get_mut(&mut self) -> &mut FakeDevice {
115+
impl AsMut<FakeDevice> for AsyncDevice {
116+
fn as_mut(&mut self) -> &mut FakeDevice {
117117
&mut self.0
118118
}
119119
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
use std::os::unix::io::RawFd;
55
use std::{
66
io::{self, ErrorKind},
7-
net::{IpAddr, Ipv4Addr, SocketAddr},
7+
net::{IpAddr, SocketAddr},
88
sync::Arc,
99
time::Duration,
1010
};
1111

1212
use byte_string::ByteStr;
1313
use cfg_if::cfg_if;
14-
use ipnet::{IpNet, Ipv4Net};
14+
use ipnet::IpNet;
1515
use log::{debug, error, info, trace, warn};
1616
use shadowsocks::config::Mode;
1717
use smoltcp::wire::{IpProtocol, TcpPacket, UdpPacket};
@@ -23,11 +23,11 @@ cfg_if! {
2323
target_os = "linux",
2424
target_os = "android",
2525
target_os = "windows"))] {
26-
use tun::{
27-
create_as_async, AsyncDevice, Configuration as TunConfiguration, Device as TunDevice, Error as TunError, Layer,
26+
use tun2::{
27+
create_as_async, AsyncDevice, Configuration as TunConfiguration, AbstractDevice, Error as TunError, Layer,
2828
};
2929
} else {
30-
use tun::{Configuration as TunConfiguration, Device as TunDevice, Error as TunError, Layer};
30+
use tun2::{AbstractDevice, Configuration as TunConfiguration, Error as TunError, Layer};
3131

3232
mod fake_tun;
3333
use self::fake_tun::{create_as_async, AsyncDevice};
@@ -84,7 +84,7 @@ impl TunBuilder {
8484
}
8585

8686
pub fn name(&mut self, name: &str) {
87-
self.tun_config.name(name);
87+
self.tun_config.tun_name(name);
8888
}
8989

9090
#[cfg(unix)]
@@ -130,7 +130,7 @@ impl TunBuilder {
130130
let tcp = TcpTun::new(
131131
self.context,
132132
self.balancer,
133-
device.get_ref().mtu().unwrap_or(1500) as u32,
133+
device.as_ref().mtu().unwrap_or(1500) as u32,
134134
);
135135

136136
Ok(Tun {
@@ -157,37 +157,37 @@ pub struct Tun {
157157
impl Tun {
158158
/// Start serving
159159
pub async fn run(mut self) -> io::Result<()> {
160-
if let Ok(mtu) = self.device.get_ref().mtu() {
160+
if let Ok(mtu) = self.device.as_ref().mtu() {
161161
assert!(mtu > 0 && mtu as usize > IFF_PI_PREFIX_LEN);
162162
}
163163

164164
info!(
165165
"shadowsocks tun device {}, mode {}",
166166
self.device
167-
.get_ref()
168-
.name()
167+
.as_ref()
168+
.tun_name()
169169
.or_else(|r| Ok::<_, ()>(r.to_string()))
170170
.unwrap(),
171171
self.mode,
172172
);
173173

174-
let address = match self.device.get_ref().address() {
174+
let address = match self.device.as_ref().address() {
175175
Ok(a) => a,
176176
Err(err) => {
177177
error!("[TUN] failed to get device address, error: {}", err);
178178
return Err(io::Error::new(io::ErrorKind::Other, err));
179179
}
180180
};
181181

182-
let netmask = match self.device.get_ref().netmask() {
182+
let netmask = match self.device.as_ref().netmask() {
183183
Ok(n) => n,
184184
Err(err) => {
185185
error!("[TUN] failed to get device netmask, error: {}", err);
186186
return Err(io::Error::new(io::ErrorKind::Other, err));
187187
}
188188
};
189189

190-
let address_net = match Ipv4Net::with_netmask(address, netmask) {
190+
let address_net = match IpNet::with_netmask(address, netmask) {
191191
Ok(n) => n,
192192
Err(err) => {
193193
error!("[TUN] invalid address {}, netmask {}, error: {}", address, netmask, err);
@@ -203,7 +203,7 @@ impl Tun {
203203
);
204204

205205
// Set default route
206-
if let Err(err) = sys::set_route_configuration(self.device.get_mut()).await {
206+
if let Err(err) = sys::set_route_configuration(self.device.as_mut()).await {
207207
warn!("[TUN] tun device set route failed, error: {}", err);
208208
}
209209

@@ -266,7 +266,7 @@ impl Tun {
266266
}
267267
}
268268

269-
async fn handle_tun_frame(&mut self, device_broadcast_addr: &Ipv4Addr, frame: &[u8]) -> smoltcp::wire::Result<()> {
269+
async fn handle_tun_frame(&mut self, device_broadcast_addr: &IpAddr, frame: &[u8]) -> smoltcp::wire::Result<()> {
270270
let packet = match IpPacket::new_checked(frame)? {
271271
Some(packet) => packet,
272272
None => {

crates/shadowsocks-service/src/local/tun/sys/others.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use tokio::io::{AsyncWrite, AsyncWriteExt};
7-
use tun::Device;
7+
use tun2::Device;
88

99
/// Packet Information length in bytes
1010
pub const IFF_PI_PREFIX_LEN: usize = 0;
@@ -15,9 +15,9 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
1515
}
1616

1717
/// Set platform specific route configuration
18-
pub async fn set_route_configuration<Q>(_: &mut (dyn Device<Queue = Q> + Send)) -> io::Result<()>
18+
pub async fn set_route_configuration<D>(device: &mut D) -> io::Result<()>
1919
where
20-
Q: Read + Write,
20+
D: AbstractDevice,
2121
{
2222
Ok(())
2323
}

crates/shadowsocks-service/src/local/tun/sys/unix/android.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use tokio::io::{AsyncWrite, AsyncWriteExt};
7-
use tun::Device;
7+
use tun2::AbstractDevice;
88

99
/// Packet Information length in bytes
1010
///
@@ -19,9 +19,9 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
1919
}
2020

2121
/// Set platform specific route configuration
22-
pub async fn set_route_configuration<Q>(_: &mut (dyn Device<Queue = Q> + Send)) -> io::Result<()>
22+
pub async fn set_route_configuration<D>(device: &mut D) -> io::Result<()>
2323
where
24-
Q: Read + Write,
24+
D: AbstractDevice,
2525
{
2626
Ok(())
2727
}

0 commit comments

Comments
 (0)