mirror of
https://github.com/dndx/phantun.git
synced 2025-01-18 13:59:30 +08:00
fix(fake-tcp): when connect()
-ing, attempt to get ephemeral port 5 times to reduce the chance
of paniking Fixes #79
This commit is contained in:
parent
90b93370ce
commit
bad5108baf
@ -47,6 +47,7 @@ use log::{error, info, trace, warn};
|
|||||||
use packet::*;
|
use packet::*;
|
||||||
use pnet::packet::{tcp, Packet};
|
use pnet::packet::{tcp, Packet};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||||
@ -401,31 +402,47 @@ impl Stack {
|
|||||||
/// the connection attempt failed.
|
/// the connection attempt failed.
|
||||||
pub async fn connect(&mut self, addr: SocketAddr) -> Option<Socket> {
|
pub async fn connect(&mut self, addr: SocketAddr) -> Option<Socket> {
|
||||||
let mut rng = SmallRng::from_entropy();
|
let mut rng = SmallRng::from_entropy();
|
||||||
let local_port: u16 = rng.gen_range(1024..65535);
|
for _ in 0..5 {
|
||||||
let local_addr = SocketAddr::new(
|
let local_port: u16 = rng.gen_range(1024..=65535);
|
||||||
if addr.is_ipv4() {
|
let local_addr = SocketAddr::new(
|
||||||
IpAddr::V4(self.local_ip)
|
if addr.is_ipv4() {
|
||||||
} else {
|
IpAddr::V4(self.local_ip)
|
||||||
IpAddr::V6(self.local_ip6.expect("IPv6 local address undefined"))
|
} else {
|
||||||
},
|
IpAddr::V6(self.local_ip6.expect("IPv6 local address undefined"))
|
||||||
local_port,
|
},
|
||||||
);
|
local_port,
|
||||||
let tuple = AddrTuple::new(local_addr, addr);
|
);
|
||||||
let (mut sock, incoming) = Socket::new(
|
let tuple = AddrTuple::new(local_addr, addr);
|
||||||
self.shared.clone(),
|
let (mut sock, incoming) = Socket::new(
|
||||||
self.shared.tun.choose(&mut rng).unwrap().clone(),
|
self.shared.clone(),
|
||||||
local_addr,
|
self.shared.tun.choose(&mut rng).unwrap().clone(),
|
||||||
addr,
|
local_addr,
|
||||||
None,
|
addr,
|
||||||
State::Idle,
|
None,
|
||||||
);
|
State::Idle,
|
||||||
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut tuples = self.shared.tuples.write().unwrap();
|
let mut tuples = self.shared.tuples.write().unwrap();
|
||||||
assert!(tuples.insert(tuple, incoming.clone()).is_none());
|
match tuples.entry(tuple) {
|
||||||
|
Occupied(_) => {
|
||||||
|
// port conflict, try again
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Vacant(v) => {
|
||||||
|
v.insert(incoming.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock.connect().await.map(|_| sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
sock.connect().await.map(|_| sock)
|
error!(
|
||||||
|
"Fake TCP connection to {} failed, local port number not available after 5 attempts",
|
||||||
|
addr
|
||||||
|
);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reader_task(
|
async fn reader_task(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user