调整stun逻辑
This commit is contained in:
+7
-4
@@ -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)> {
|
pub fn stun_test_nat0(stun_servers: Vec<String>) -> io::Result<(NatType, Vec<Ipv4Addr>, u16)> {
|
||||||
let udp = UdpSocket::bind("0.0.0.0:0")?;
|
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 nat_type = NatType::Cone;
|
||||||
let mut port_range = 0;
|
let mut port_range = 0;
|
||||||
let mut hash_set = HashSet::new();
|
let mut hash_set = HashSet::new();
|
||||||
@@ -76,7 +76,9 @@ fn test_nat(
|
|||||||
let mut port_range = 0;
|
let mut port_range = 0;
|
||||||
let mut hash_set = HashSet::new();
|
let mut hash_set = HashSet::new();
|
||||||
let mut nat_type = NatType::Cone;
|
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() {
|
match mapped_addr1.ip() {
|
||||||
IpAddr::V4(ip) => {
|
IpAddr::V4(ip) => {
|
||||||
hash_set.insert(ip);
|
hash_set.insert(ip);
|
||||||
@@ -84,7 +86,7 @@ fn test_nat(
|
|||||||
IpAddr::V6(_) => {}
|
IpAddr::V6(_) => {}
|
||||||
}
|
}
|
||||||
if udp.connect(changed_addr1).is_ok() {
|
if udp.connect(changed_addr1).is_ok() {
|
||||||
match test_nat_(&udp, false, false) {
|
match test_nat_(&udp, false, false, tid + 1) {
|
||||||
Ok((mapped_addr2, _)) => {
|
Ok((mapped_addr2, _)) => {
|
||||||
match mapped_addr2.ip() {
|
match mapped_addr2.ip() {
|
||||||
IpAddr::V4(ip) => {
|
IpAddr::V4(ip) => {
|
||||||
@@ -120,12 +122,13 @@ fn test_nat_(
|
|||||||
udp: &UdpSocket,
|
udp: &UdpSocket,
|
||||||
change_ip: bool,
|
change_ip: bool,
|
||||||
change_port: bool,
|
change_port: bool,
|
||||||
|
tid: u128,
|
||||||
) -> io::Result<(SocketAddr, SocketAddr)> {
|
) -> io::Result<(SocketAddr, SocketAddr)> {
|
||||||
for _ in 0..2 {
|
for _ in 0..2 {
|
||||||
let mut buf = [0u8; 28];
|
let mut buf = [0u8; 28];
|
||||||
let mut msg = stun_format::MsgBuilder::from(buf.as_mut_slice());
|
let mut msg = stun_format::MsgBuilder::from(buf.as_mut_slice());
|
||||||
msg.typ(stun_format::MsgType::BindingRequest).unwrap();
|
msg.typ(stun_format::MsgType::BindingRequest).unwrap();
|
||||||
msg.tid(1).unwrap();
|
msg.tid(tid).unwrap();
|
||||||
msg.add_attr(Attr::ChangeRequest {
|
msg.add_attr(Attr::ChangeRequest {
|
||||||
change_ip,
|
change_ip,
|
||||||
change_port,
|
change_port,
|
||||||
|
|||||||
Reference in New Issue
Block a user