use more accurate regex for {domain}:{port}
This commit is contained in:
parent
12d446b217
commit
aa2cd37fb3
@ -322,10 +322,15 @@ pub fn is_ip_str(id: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_hostname_port_str(id: &str) -> bool {
|
pub fn is_domain_port_str(id: &str) -> bool {
|
||||||
regex::Regex::new(r"^[\-.0-9a-zA-Z]+:\d{1,5}$")
|
// modified regex for RFC1123 hostname. check https://stackoverflow.com/a/106223 for original version for hostname.
|
||||||
.unwrap()
|
// according to [TLD List](https://data.iana.org/TLD/tlds-alpha-by-domain.txt) version 2023011700,
|
||||||
.is_match(id)
|
// there is no digits in TLD, and length is 2~63.
|
||||||
|
regex::Regex::new(
|
||||||
|
r"(?i)^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z-]{0,61}[a-z]:\d{1,5}$",
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.is_match(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -350,17 +355,22 @@ mod test_lib {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hostname_port() {
|
fn test_hostname_port() {
|
||||||
assert_eq!(is_hostname_port_str("a:12"), true);
|
assert_eq!(is_domain_port_str("a:12"), false);
|
||||||
assert_eq!(is_hostname_port_str("a.b.c:12"), true);
|
assert_eq!(is_domain_port_str("a.b.c:12"), false);
|
||||||
assert_eq!(is_hostname_port_str("test.com:12"), true);
|
assert_eq!(is_domain_port_str("test.com:12"), true);
|
||||||
assert_eq!(is_hostname_port_str("1.2.3:12"), true);
|
assert_eq!(is_domain_port_str("test-UPPER.com:12"), true);
|
||||||
assert_eq!(is_hostname_port_str("a.b.c:123456"), false);
|
assert_eq!(is_domain_port_str("some-other.domain.com:12"), true);
|
||||||
// todo: should we also check for these edge case?
|
assert_eq!(is_domain_port_str("under_score:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str("a@bc:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str("1.1.1.1:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str("1.2.3:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str("1.2.3.45:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str("a.b.c:123456"), false);
|
||||||
|
assert_eq!(is_domain_port_str("---:12"), false);
|
||||||
|
assert_eq!(is_domain_port_str(".:12"), false);
|
||||||
|
// todo: should we also check for these edge cases?
|
||||||
// out-of-range port
|
// out-of-range port
|
||||||
assert_eq!(is_hostname_port_str("test.com:0"), true);
|
assert_eq!(is_domain_port_str("test.com:0"), true);
|
||||||
assert_eq!(is_hostname_port_str("test.com:98989"), true);
|
assert_eq!(is_domain_port_str("test.com:98989"), true);
|
||||||
// invalid hostname
|
|
||||||
assert_eq!(is_hostname_port_str("---:12"), true);
|
|
||||||
assert_eq!(is_hostname_port_str(".:12"), true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,10 @@ use cpal::{
|
|||||||
use magnum_opus::{Channels::*, Decoder as AudioDecoder};
|
use magnum_opus::{Channels::*, Decoder as AudioDecoder};
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
use std::{
|
use std::{
|
||||||
str::FromStr,
|
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
ops::{Deref, Not},
|
ops::{Deref, Not},
|
||||||
|
str::FromStr,
|
||||||
sync::{atomic::AtomicBool, mpsc, Arc, Mutex, RwLock},
|
sync::{atomic::AtomicBool, mpsc, Arc, Mutex, RwLock},
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -181,14 +181,10 @@ impl Client {
|
|||||||
true,
|
true,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
// Allow connect to {hostname}:{port}
|
// Allow connect to {domain}:{port}
|
||||||
if hbb_common::is_hostname_port_str(peer) {
|
if hbb_common::is_domain_port_str(peer) {
|
||||||
return Ok((
|
return Ok((
|
||||||
socket_client::connect_tcp(
|
socket_client::connect_tcp(peer, RENDEZVOUS_TIMEOUT).await?,
|
||||||
peer,
|
|
||||||
RENDEZVOUS_TIMEOUT,
|
|
||||||
)
|
|
||||||
.await?,
|
|
||||||
true,
|
true,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user