调整stun逻辑

This commit is contained in:
lbl8603
2024-04-24 23:01:40 +08:00
parent 4f52d58d6e
commit 7c799589df
+7 -4
View File
@@ -38,7 +38,7 @@ pub fn stun_test_nat(stun_servers: Vec<String>) -> io::Result<(NatType, Vec<Ipv4
}
pub fn stun_test_nat0(stun_servers: Vec<String>) -> io::Result<(NatType, Vec<Ipv4Addr>, u16)> {
let udp = UdpSocket::bind("0.0.0.0:0")?;
udp.set_read_timeout(Some(Duration::from_millis(300)))?;
udp.set_read_timeout(Some(Duration::from_millis(500)))?;
let mut nat_type = NatType::Cone;
let mut port_range = 0;
let mut hash_set = HashSet::new();
@@ -76,7 +76,9 @@ fn test_nat(
let mut port_range = 0;
let mut hash_set = HashSet::new();
let mut nat_type = NatType::Cone;
let (mapped_addr1, changed_addr1) = test_nat_(&udp, true, true)?;
// 随便搞个当id
let tid = stun_server.as_ptr() as u128;
let (mapped_addr1, changed_addr1) = test_nat_(&udp, true, true, tid)?;
match mapped_addr1.ip() {
IpAddr::V4(ip) => {
hash_set.insert(ip);
@@ -84,7 +86,7 @@ fn test_nat(
IpAddr::V6(_) => {}
}
if udp.connect(changed_addr1).is_ok() {
match test_nat_(&udp, false, false) {
match test_nat_(&udp, false, false, tid + 1) {
Ok((mapped_addr2, _)) => {
match mapped_addr2.ip() {
IpAddr::V4(ip) => {
@@ -120,12 +122,13 @@ fn test_nat_(
udp: &UdpSocket,
change_ip: bool,
change_port: bool,
tid: u128,
) -> io::Result<(SocketAddr, SocketAddr)> {
for _ in 0..2 {
let mut buf = [0u8; 28];
let mut msg = stun_format::MsgBuilder::from(buf.as_mut_slice());
msg.typ(stun_format::MsgType::BindingRequest).unwrap();
msg.tid(1).unwrap();
msg.tid(tid).unwrap();
msg.add_attr(Attr::ChangeRequest {
change_ip,
change_port,