Replace net2 with socket2

This commit is contained in:
Max Lv
2020-03-01 14:05:45 +08:00
committed by Y. T. CHUNG
parent c963673642
commit dd888ebba2
4 changed files with 12 additions and 20 deletions
+1 -2
View File
@@ -50,7 +50,7 @@ aes-ctr = ["openssl"]
camellia-cfb = ["openssl"]
single-threaded = []
trust-dns = ["trust-dns-resolver"]
android = ['sendfd', 'net2']
android = ['sendfd']
openssl-vendored = ["openssl/vendored"]
[dependencies]
@@ -76,7 +76,6 @@ serde_urlencoded = "0.6"
serde = { version = "1.0", features = ["derive"] }
url = "2.1"
byte_string = "1.0"
net2 = { version = "0.2", optional = true }
sendfd = { version = "0.3", optional = true }
libsodium-sys = { version = "0.2", optional = true }
miscreant = { version = "0.5", optional = true }
+9 -8
View File
@@ -5,10 +5,9 @@ use std::{
os::unix::io::{AsRawFd, RawFd},
};
use tokio::net::TcpStream;
use socket2::{Domain, Protocol, Socket, Type};
use net2::*;
use tokio::net::UdpSocket;
use tokio::net::{TcpStream, UdpSocket};
#[cfg(all(feature = "sendfd", target_os = "android"))]
use sendfd::*;
@@ -77,20 +76,22 @@ fn protect(_protect_path: &Option<String>, _fd: RawFd) -> io::Result<()> {
/// create a new TCP stream
#[inline(always)]
pub async fn tcp_stream_connect(saddr: &SocketAddr, context: &Context) -> io::Result<TcpStream> {
let builder = match saddr {
SocketAddr::V4(_) => TcpBuilder::new_v4()?,
SocketAddr::V6(_) => TcpBuilder::new_v6()?,
let domain = match *saddr {
SocketAddr::V4(..) => Domain::ipv4(),
SocketAddr::V6(..) => Domain::ipv6(),
};
let socket = Socket::new(domain, Type::stream(), Some(Protocol::tcp()))?;
// Any traffic to localhost should be protected
// This is a workaround for VPNService
if cfg!(target_os = "android") {
if saddr.ip() != IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)) {
protect(&context.config().protect_path, builder.as_raw_fd())?;
protect(&context.config().protect_path, socket.as_raw_fd())?;
}
}
let stream = builder.to_tcp_stream()?;
let stream = socket.into_tcp_stream();
TcpStream::connect_std(stream, &saddr).await
}
+1 -7
View File
@@ -1,10 +1,4 @@
use std::{
io,
mem,
net::{SocketAddr},
os::windows::io::AsRawSocket,
ptr,
};
use std::{io, mem, net::SocketAddr, os::windows::io::AsRawSocket, ptr};
use tokio::net::{TcpStream, UdpSocket};
use winapi::{
+1 -3
View File
@@ -12,9 +12,7 @@ use std::{
use bytes::BytesMut;
use log::{debug, error, trace};
use tokio::{
io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadHalf, WriteHalf},
};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadHalf, WriteHalf};
use crate::{
config::{ConfigType, ServerAddr, ServerConfig},