fix get relay server for ipv6
This commit is contained in:
		
							parent
							
								
									55962f2fc9
								
							
						
					
					
						commit
						72162c9a31
					
				@ -478,7 +478,7 @@ pub fn username() -> String {
 | 
			
		||||
pub fn check_port<T: std::string::ToString>(host: T, port: i32) -> String {
 | 
			
		||||
    let host = host.to_string();
 | 
			
		||||
    if is_ipv6_str(&host) {
 | 
			
		||||
        if host.contains("[") {
 | 
			
		||||
        if host.starts_with("[") {
 | 
			
		||||
            return host;
 | 
			
		||||
        }
 | 
			
		||||
        return format!("[{}]:{}", host, port);
 | 
			
		||||
@ -489,6 +489,31 @@ pub fn check_port<T: std::string::ToString>(host: T, port: i32) -> String {
 | 
			
		||||
    return host;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[inline]
 | 
			
		||||
pub fn increase_port<T: std::string::ToString>(host: T, offset: i32) -> String {
 | 
			
		||||
    let host = host.to_string();
 | 
			
		||||
    if is_ipv6_str(&host) {
 | 
			
		||||
        if host.starts_with("[") {
 | 
			
		||||
            let tmp: Vec<&str> = host.split("]:").collect();
 | 
			
		||||
            if tmp.len() == 2 {
 | 
			
		||||
                let port: i32 = tmp[1].parse().unwrap_or(0);
 | 
			
		||||
                if port > 0 {
 | 
			
		||||
                    return format!("{}]:{}", tmp[0], port + offset);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } else if host.contains(":") {
 | 
			
		||||
        let tmp: Vec<&str> = host.split(":").collect();
 | 
			
		||||
        if tmp.len() == 2 {
 | 
			
		||||
            let port: i32 = tmp[1].parse().unwrap_or(0);
 | 
			
		||||
            if port > 0 {
 | 
			
		||||
                return format!("{}:{}", tmp[0], port + offset);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return host;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub const POSTFIX_SERVICE: &'static str = "_service";
 | 
			
		||||
 | 
			
		||||
#[inline]
 | 
			
		||||
@ -721,8 +746,17 @@ mod test_common {
 | 
			
		||||
    fn test_check_port() {
 | 
			
		||||
        assert_eq!(check_port("[1:2]:12", 32), "[1:2]:12");
 | 
			
		||||
        assert_eq!(check_port("1:2", 32), "[1:2]:32");
 | 
			
		||||
        assert_eq!(check_port("z1:2", 32), "z1:2");
 | 
			
		||||
        assert_eq!(check_port("1.1.1.1", 32), "1.1.1.1:32");
 | 
			
		||||
        assert_eq!(check_port("1.1.1.1:32", 32), "1.1.1.1:32");
 | 
			
		||||
        assert_eq!(check_port("test.com:32", 0), "test.com:32");
 | 
			
		||||
        assert_eq!(increase_port("[1:2]:12", 1), "[1:2]:13");
 | 
			
		||||
        assert_eq!(increase_port("1.2.2.4:12", 1), "1.2.2.4:13");
 | 
			
		||||
        assert_eq!(increase_port("1.2.2.4", 1), "1.2.2.4");
 | 
			
		||||
        assert_eq!(increase_port("test.com", 1), "test.com");
 | 
			
		||||
        assert_eq!(increase_port("test.com:13", 4), "test.com:17");
 | 
			
		||||
        assert_eq!(increase_port("1:13", 4), "1:13");
 | 
			
		||||
        assert_eq!(increase_port("22:1:13", 4), "22:1:13");
 | 
			
		||||
        assert_eq!(increase_port("z1:2", 1), "z1:3");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -475,15 +475,7 @@ impl RendezvousMediator {
 | 
			
		||||
            relay_server = provided_by_rendzvous_server;
 | 
			
		||||
        }
 | 
			
		||||
        if relay_server.is_empty() {
 | 
			
		||||
            if self.host.contains(":") {
 | 
			
		||||
                let tmp: Vec<&str> = self.host.split(":").collect();
 | 
			
		||||
                if tmp.len() == 2 {
 | 
			
		||||
                    let port: u16 = tmp[1].parse().unwrap_or(0);
 | 
			
		||||
                    relay_server = format!("{}:{}", tmp[0], port + 1);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                relay_server = self.host.clone();
 | 
			
		||||
            }
 | 
			
		||||
            relay_server = crate::increase_port(&self.host, 1);
 | 
			
		||||
        }
 | 
			
		||||
        relay_server
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user