mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-01-18 07:43:01 +08:00
nat64
This commit is contained in:
parent
75f57cf0fc
commit
08b8f40397
@ -511,13 +511,12 @@ impl Config {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_any_listen_addr() -> SocketAddr {
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_any_listen_addr_v6() -> SocketAddr {
|
||||
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
|
||||
pub fn get_any_listen_addr(is_ipv4: bool) -> SocketAddr {
|
||||
if is_ipv4 {
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
|
||||
} else {
|
||||
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rendezvous_server() -> String {
|
||||
|
@ -130,7 +130,7 @@ impl AddrMangle {
|
||||
pub fn decode(bytes: &[u8]) -> SocketAddr {
|
||||
if bytes.len() > 16 {
|
||||
if bytes.len() != 18 {
|
||||
return Config::get_any_listen_addr_v6();
|
||||
return Config::get_any_listen_addr(false);
|
||||
}
|
||||
#[allow(invalid_value)]
|
||||
let mut tmp: [u8; 2] = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
|
||||
@ -300,3 +300,10 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_ipv4_str(id: &str) -> bool {
|
||||
regex::Regex::new(r"^\d+\.\d+\.\d+\.\d+(:\d+)?$")
|
||||
.unwrap()
|
||||
.is_match(id)
|
||||
}
|
@ -85,10 +85,21 @@ impl<'a> IntoTargetAddr2<'a> for &str {
|
||||
|
||||
pub async fn connect_tcp<'t, T: IntoTargetAddr2<'t> + std::fmt::Debug>(
|
||||
target: T,
|
||||
local: SocketAddr,
|
||||
ms_timeout: u64,
|
||||
) -> ResultType<FramedStream> {
|
||||
let target_addr = target.into_target_addr2()?;
|
||||
let local = Config::get_any_listen_addr(is_ipv4(&target_addr));
|
||||
connect_tcp_local(target_addr, local, ms_timeout)
|
||||
.await
|
||||
.context(format!("Invalid target addr: {:?}", target))
|
||||
}
|
||||
|
||||
pub async fn connect_tcp_local<'t, T: IntoTargetAddr<'t> + std::fmt::Debug>(
|
||||
target: T,
|
||||
local: SocketAddr,
|
||||
ms_timeout: u64,
|
||||
) -> ResultType<FramedStream> {
|
||||
let target_addr = target.into_target_addr()?;
|
||||
if let Some(conf) = Config::get_socks() {
|
||||
return FramedStream::connect(
|
||||
conf.proxy.as_str(),
|
||||
@ -100,13 +111,43 @@ pub async fn connect_tcp<'t, T: IntoTargetAddr2<'t> + std::fmt::Debug>(
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let addr = ToSocketAddrs::to_socket_addrs(&target_addr)?
|
||||
let mut addr = ToSocketAddrs::to_socket_addrs(&target_addr)?
|
||||
.next()
|
||||
.context(format!("Invalid target addr: {:?}", target))?;
|
||||
.context(format!("Invalid target addr: {:?}", target_addr))?;
|
||||
if local.is_ipv6() && addr.is_ipv4() {
|
||||
addr = query_nip_io(&addr)?;
|
||||
}
|
||||
Ok(FramedStream::new(addr, local, ms_timeout).await?)
|
||||
}
|
||||
|
||||
pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
#[inline]
|
||||
pub fn is_ipv4(target: &TargetAddr<'_>) -> bool {
|
||||
match target {
|
||||
TargetAddr::Ip(addr) => addr.is_ipv4(),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn query_nip_io(addr: &SocketAddr) -> ResultType<SocketAddr> {
|
||||
to_socket_addr(format!("{}.nip.io:{}", addr.ip(), addr.port()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ipv4_to_ipv6(addr: String, ipv4: bool) -> String {
|
||||
if !ipv4 && crate::is_ipv4_str(&addr) {
|
||||
if let Some(ip) = addr.split(":").next() {
|
||||
return addr.replace(ip, &format!("{}.nip.io", ip));
|
||||
}
|
||||
}
|
||||
addr
|
||||
}
|
||||
|
||||
pub async fn new_udp_for(target: &TargetAddr<'_>, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
new_udp(Config::get_any_listen_addr(is_ipv4(target)), ms_timeout).await
|
||||
}
|
||||
|
||||
async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<FramedSocket> {
|
||||
match Config::get_socks() {
|
||||
None => Ok(FramedSocket::new(to_socket_addr(&local)?).await?),
|
||||
Some(conf) => {
|
||||
@ -123,19 +164,49 @@ pub async fn new_udp<T: ToSocketAddrs>(local: T, ms_timeout: u64) -> ResultType<
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rebind_udp<T: ToSocketAddrs>(local: T) -> ResultType<Option<FramedSocket>> {
|
||||
pub async fn rebind_udp_for(target: &TargetAddr<'_>) -> ResultType<Option<FramedSocket>> {
|
||||
match Config::get_network_type() {
|
||||
NetworkType::Direct => Ok(Some(FramedSocket::new(to_socket_addr(local)?).await?)),
|
||||
NetworkType::Direct => Ok(Some(
|
||||
FramedSocket::new(Config::get_any_listen_addr(is_ipv4(target))).await?,
|
||||
)),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_to_socket_addr() {
|
||||
assert_eq!(to_socket_addr("127.0.0.1:8080").unwrap(), "127.0.0.1:8080".parse().unwrap());
|
||||
assert_eq!(
|
||||
to_socket_addr("127.0.0.1:8080").unwrap(),
|
||||
"127.0.0.1:8080".parse().unwrap()
|
||||
);
|
||||
assert!(to_socket_addr("[ff::]:0").unwrap().is_ipv6());
|
||||
assert!(to_socket_addr("xx").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nat64() {
|
||||
assert_eq!(ipv4_to_ipv6("1.1.1.1".to_owned(), true), "1.1.1.1");
|
||||
assert_eq!(ipv4_to_ipv6("1.1.1.1".to_owned(), false), "1.1.1.1.nip.io");
|
||||
assert_eq!(
|
||||
ipv4_to_ipv6("1.1.1.1:8080".to_owned(), false),
|
||||
"1.1.1.1.nip.io:8080"
|
||||
);
|
||||
assert_eq!(
|
||||
ipv4_to_ipv6("rustdesk.com".to_owned(), false),
|
||||
"rustdesk.com"
|
||||
);
|
||||
if to_socket_addr("rustdesk.com:80").unwrap().is_ipv6() {
|
||||
assert!(query_nip_io(&"1.1.1.1:80".parse().unwrap())
|
||||
.unwrap()
|
||||
.is_ipv6());
|
||||
return;
|
||||
}
|
||||
assert_eq!(
|
||||
query_nip_io(&"1.1.1.1:80".parse().unwrap()).unwrap(),
|
||||
"1.1.1.1:80".parse().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -167,12 +167,10 @@ impl Client {
|
||||
interface: impl Interface,
|
||||
) -> ResultType<(Stream, bool)> {
|
||||
// to-do: remember the port for each peer, so that we can retry easier
|
||||
let any_addr = Config::get_any_listen_addr();
|
||||
if crate::is_ip(peer) {
|
||||
if hbb_common::is_ipv4_str(peer) {
|
||||
return Ok((
|
||||
socket_client::connect_tcp(
|
||||
crate::check_port(peer, RELAY_PORT + 1),
|
||||
any_addr,
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await?,
|
||||
@ -180,13 +178,12 @@ impl Client {
|
||||
));
|
||||
}
|
||||
let (mut rendezvous_server, servers, contained) = crate::get_rendezvous_server(1_000).await;
|
||||
let mut socket =
|
||||
socket_client::connect_tcp(&*rendezvous_server, any_addr, RENDEZVOUS_TIMEOUT).await;
|
||||
let mut socket = socket_client::connect_tcp(&*rendezvous_server, RENDEZVOUS_TIMEOUT).await;
|
||||
debug_assert!(!servers.contains(&rendezvous_server));
|
||||
if socket.is_err() && !servers.is_empty() {
|
||||
log::info!("try the other servers: {:?}", servers);
|
||||
for server in servers {
|
||||
socket = socket_client::connect_tcp(&*server, any_addr, RENDEZVOUS_TIMEOUT).await;
|
||||
socket = socket_client::connect_tcp(&*server, RENDEZVOUS_TIMEOUT).await;
|
||||
if socket.is_ok() {
|
||||
rendezvous_server = server;
|
||||
break;
|
||||
@ -203,7 +200,7 @@ impl Client {
|
||||
let mut relay_server = "".to_owned();
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
let mut peer_addr = any_addr;
|
||||
let mut peer_addr = Config::get_any_listen_addr(true);
|
||||
let mut peer_nat_type = NatType::UNKNOWN_NAT;
|
||||
let my_nat_type = crate::get_nat_type(100).await;
|
||||
let mut is_local = false;
|
||||
@ -264,9 +261,15 @@ impl Client {
|
||||
rr.relay_server
|
||||
);
|
||||
signed_id_pk = rr.pk().into();
|
||||
let mut conn =
|
||||
Self::create_relay(peer, rr.uuid, rr.relay_server, key, conn_type)
|
||||
.await?;
|
||||
let mut conn = Self::create_relay(
|
||||
peer,
|
||||
rr.uuid,
|
||||
rr.relay_server,
|
||||
key,
|
||||
conn_type,
|
||||
my_addr.is_ipv4(),
|
||||
)
|
||||
.await?;
|
||||
Self::secure_connection(
|
||||
peer,
|
||||
signed_id_pk,
|
||||
@ -373,7 +376,7 @@ impl Client {
|
||||
log::info!("peer address: {}, timeout: {}", peer, connect_timeout);
|
||||
let start = std::time::Instant::now();
|
||||
// NOTICE: Socks5 is be used event in intranet. Which may be not a good way.
|
||||
let mut conn = socket_client::connect_tcp(peer, local_addr, connect_timeout).await;
|
||||
let mut conn = socket_client::connect_tcp_local(peer, local_addr, connect_timeout).await;
|
||||
let mut direct = !conn.is_err();
|
||||
if interface.is_force_relay() || conn.is_err() {
|
||||
if !relay_server.is_empty() {
|
||||
@ -506,16 +509,16 @@ impl Client {
|
||||
token: &str,
|
||||
conn_type: ConnType,
|
||||
) -> ResultType<Stream> {
|
||||
let any_addr = Config::get_any_listen_addr();
|
||||
let mut succeed = false;
|
||||
let mut uuid = "".to_owned();
|
||||
let mut ipv4 = true;
|
||||
for i in 1..=3 {
|
||||
// use different socket due to current hbbs implement requiring different nat address for each attempt
|
||||
let mut socket =
|
||||
socket_client::connect_tcp(rendezvous_server, any_addr, RENDEZVOUS_TIMEOUT)
|
||||
.await
|
||||
.with_context(|| "Failed to connect to rendezvous server")?;
|
||||
let mut socket = socket_client::connect_tcp(rendezvous_server, RENDEZVOUS_TIMEOUT)
|
||||
.await
|
||||
.with_context(|| "Failed to connect to rendezvous server")?;
|
||||
|
||||
ipv4 = socket.local_addr().is_ipv4();
|
||||
let mut msg_out = RendezvousMessage::new();
|
||||
uuid = Uuid::new_v4().to_string();
|
||||
log::info!(
|
||||
@ -550,7 +553,7 @@ impl Client {
|
||||
if !succeed {
|
||||
bail!("Timeout");
|
||||
}
|
||||
Self::create_relay(peer, uuid, relay_server, key, conn_type).await
|
||||
Self::create_relay(peer, uuid, relay_server, key, conn_type, ipv4).await
|
||||
}
|
||||
|
||||
/// Create a relay connection to the server.
|
||||
@ -560,10 +563,10 @@ impl Client {
|
||||
relay_server: String,
|
||||
key: &str,
|
||||
conn_type: ConnType,
|
||||
ipv4: bool,
|
||||
) -> ResultType<Stream> {
|
||||
let mut conn = socket_client::connect_tcp(
|
||||
crate::check_port(relay_server, RELAY_PORT),
|
||||
Config::get_any_listen_addr(),
|
||||
socket_client::ipv4_to_ipv6(crate::check_port(relay_server, RELAY_PORT), ipv4),
|
||||
CONNECT_TIMEOUT,
|
||||
)
|
||||
.await
|
||||
|
@ -312,7 +312,6 @@ async fn test_nat_type_() -> ResultType<bool> {
|
||||
let mut port2 = 0;
|
||||
let server1 = socket_client::get_target_addr(&server1)?;
|
||||
let server2 = socket_client::get_target_addr(&server2)?;
|
||||
let mut addr = Config::get_any_listen_addr();
|
||||
for i in 0..2 {
|
||||
let mut socket = socket_client::connect_tcp(
|
||||
if i == 0 {
|
||||
@ -320,11 +319,15 @@ async fn test_nat_type_() -> ResultType<bool> {
|
||||
} else {
|
||||
server2.clone()
|
||||
},
|
||||
addr,
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await?;
|
||||
addr = socket.local_addr();
|
||||
if i == 0 {
|
||||
Config::set_option(
|
||||
"local-ip-addr".to_owned(),
|
||||
socket.local_addr().ip().to_string(),
|
||||
);
|
||||
}
|
||||
socket.send(&msg_out).await?;
|
||||
if let Some(Ok(bytes)) = socket.next_timeout(RENDEZVOUS_TIMEOUT).await {
|
||||
if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
|
||||
@ -347,7 +350,6 @@ async fn test_nat_type_() -> ResultType<bool> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Config::set_option("local-ip-addr".to_owned(), addr.ip().to_string());
|
||||
let ok = port1 > 0 && port2 > 0;
|
||||
if ok {
|
||||
let t = if port1 == port2 {
|
||||
@ -424,7 +426,6 @@ async fn test_rendezvous_server_() {
|
||||
let tm = std::time::Instant::now();
|
||||
if socket_client::connect_tcp(
|
||||
crate::check_port(&host, RENDEZVOUS_PORT),
|
||||
Config::get_any_listen_addr(),
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await
|
||||
@ -526,8 +527,7 @@ async fn check_software_update_() -> hbb_common::ResultType<()> {
|
||||
|
||||
let rendezvous_server =
|
||||
socket_client::get_target_addr(&format!("rs-sg.rustdesk.com:{}", config::RENDEZVOUS_PORT))?;
|
||||
let mut socket =
|
||||
socket_client::new_udp(Config::get_any_listen_addr(), RENDEZVOUS_TIMEOUT).await?;
|
||||
let mut socket = socket_client::new_udp_for(&rendezvous_server, RENDEZVOUS_TIMEOUT).await?;
|
||||
|
||||
let mut msg_out = RendezvousMessage::new();
|
||||
msg_out.set_software_update(SoftwareUpdate {
|
||||
@ -567,12 +567,6 @@ pub fn get_full_name() -> String {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn is_ip(id: &str) -> bool {
|
||||
hbb_common::regex::Regex::new(r"^\d+\.\d+\.\d+\.\d+(:\d+)?$")
|
||||
.unwrap()
|
||||
.is_match(id)
|
||||
}
|
||||
|
||||
pub fn is_setup(name: &str) -> bool {
|
||||
name.to_lowercase().ends_with("install.exe")
|
||||
}
|
||||
|
@ -111,15 +111,13 @@ impl RendezvousMediator {
|
||||
})
|
||||
.unwrap_or(host.to_owned());
|
||||
let mut rz = Self {
|
||||
addr: Config::get_any_listen_addr().into_target_addr()?,
|
||||
addr: socket_client::get_target_addr(&crate::check_port(&host, RENDEZVOUS_PORT))?,
|
||||
host: host.clone(),
|
||||
host_prefix,
|
||||
last_id_pk_registry: "".to_owned(),
|
||||
};
|
||||
|
||||
rz.addr = socket_client::get_target_addr(&crate::check_port(&host, RENDEZVOUS_PORT))?;
|
||||
let any_addr = Config::get_any_listen_addr();
|
||||
let mut socket = socket_client::new_udp(any_addr, RENDEZVOUS_TIMEOUT).await?;
|
||||
let mut socket = socket_client::new_udp_for(&rz.addr, RENDEZVOUS_TIMEOUT).await?;
|
||||
|
||||
const TIMER_OUT: Duration = Duration::from_secs(1);
|
||||
let mut timer = interval(TIMER_OUT);
|
||||
@ -253,7 +251,7 @@ impl RendezvousMediator {
|
||||
rz.addr = socket_client::get_target_addr(&crate::check_port(&host, RENDEZVOUS_PORT))?;
|
||||
// in some case of network reconnect (dial IP network),
|
||||
// old UDP socket not work any more after network recover
|
||||
if let Some(s) = socket_client::rebind_udp(any_addr).await? {
|
||||
if let Some(s) = socket_client::rebind_udp_for(&rz.addr).await? {
|
||||
socket = s;
|
||||
}
|
||||
last_dns_check = Instant::now();
|
||||
@ -293,19 +291,15 @@ impl RendezvousMediator {
|
||||
) -> ResultType<()> {
|
||||
let peer_addr = AddrMangle::decode(&socket_addr);
|
||||
log::info!(
|
||||
"create_relay requested from from {:?}, relay_server: {}, uuid: {}, secure: {}",
|
||||
"create_relay requested from {:?}, relay_server: {}, uuid: {}, secure: {}",
|
||||
peer_addr,
|
||||
relay_server,
|
||||
uuid,
|
||||
secure,
|
||||
);
|
||||
|
||||
let mut socket = socket_client::connect_tcp(
|
||||
self.addr.to_owned(),
|
||||
Config::get_any_listen_addr(),
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await?;
|
||||
let mut socket =
|
||||
socket_client::connect_tcp(self.addr.to_owned(), RENDEZVOUS_TIMEOUT).await?;
|
||||
|
||||
let mut msg_out = Message::new();
|
||||
let mut rr = RelayResponse {
|
||||
@ -320,24 +314,35 @@ impl RendezvousMediator {
|
||||
}
|
||||
msg_out.set_relay_response(rr);
|
||||
socket.send(&msg_out).await?;
|
||||
crate::create_relay_connection(server, relay_server, uuid, peer_addr, secure).await;
|
||||
let v4 = socket_client::is_ipv4(&self.addr);
|
||||
crate::create_relay_connection(server, relay_server, uuid, peer_addr, secure, v4).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_intranet(&self, fla: FetchLocalAddr, server: ServerPtr) -> ResultType<()> {
|
||||
let relay_server = self.get_relay_server(fla.relay_server);
|
||||
if !socket_client::is_ipv4(&self.addr) {
|
||||
// nat64, go relay directly, because current hbbs will crash if demangle ipv6 address
|
||||
let uuid = Uuid::new_v4().to_string();
|
||||
return self
|
||||
.create_relay(
|
||||
fla.socket_addr.into(),
|
||||
relay_server,
|
||||
uuid,
|
||||
server,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let peer_addr = AddrMangle::decode(&fla.socket_addr);
|
||||
log::debug!("Handle intranet from {:?}", peer_addr);
|
||||
let mut socket = socket_client::connect_tcp(
|
||||
self.addr.to_owned(),
|
||||
Config::get_any_listen_addr(),
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await?;
|
||||
let mut socket =
|
||||
socket_client::connect_tcp(self.addr.to_owned(), RENDEZVOUS_TIMEOUT).await?;
|
||||
let local_addr = socket.local_addr();
|
||||
let local_addr: SocketAddr =
|
||||
format!("{}:{}", local_addr.ip(), local_addr.port()).parse()?;
|
||||
let mut msg_out = Message::new();
|
||||
let relay_server = self.get_relay_server(fla.relay_server);
|
||||
msg_out.set_local_addr(LocalAddr {
|
||||
id: Config::get_id(),
|
||||
socket_addr: AddrMangle::encode(peer_addr).into(),
|
||||
@ -372,16 +377,12 @@ impl RendezvousMediator {
|
||||
let peer_addr = AddrMangle::decode(&ph.socket_addr);
|
||||
log::debug!("Punch hole to {:?}", peer_addr);
|
||||
let mut socket = {
|
||||
let socket = socket_client::connect_tcp(
|
||||
self.addr.to_owned(),
|
||||
Config::get_any_listen_addr(),
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await?;
|
||||
let socket =
|
||||
socket_client::connect_tcp(self.addr.to_owned(), RENDEZVOUS_TIMEOUT).await?;
|
||||
let local_addr = socket.local_addr();
|
||||
// key important here for punch hole to tell my gateway incoming peer is safe.
|
||||
// it can not be async here, because local_addr can not be reused, we must close the connection before use it again.
|
||||
allow_err!(socket_client::connect_tcp(peer_addr, local_addr, 30).await);
|
||||
allow_err!(socket_client::connect_tcp_local(peer_addr, local_addr, 30).await);
|
||||
socket
|
||||
};
|
||||
let mut msg_out = Message::new();
|
||||
@ -534,7 +535,9 @@ async fn direct_server(server: ServerPtr) {
|
||||
if let Ok(Ok((stream, addr))) = hbb_common::timeout(1000, l.accept()).await {
|
||||
stream.set_nodelay(true).ok();
|
||||
log::info!("direct access from {}", addr);
|
||||
let local_addr = stream.local_addr().unwrap_or(Config::get_any_listen_addr());
|
||||
let local_addr = stream
|
||||
.local_addr()
|
||||
.unwrap_or(Config::get_any_listen_addr(true));
|
||||
let server = server.clone();
|
||||
tokio::spawn(async move {
|
||||
allow_err!(
|
||||
@ -653,12 +656,7 @@ async fn create_online_stream() -> ResultType<FramedStream> {
|
||||
}
|
||||
let online_server = format!("{}:{}", tmp[0], port - 1);
|
||||
let server_addr = socket_client::get_target_addr(&online_server)?;
|
||||
socket_client::connect_tcp(
|
||||
server_addr,
|
||||
Config::get_any_listen_addr(),
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await
|
||||
socket_client::connect_tcp(server_addr, RENDEZVOUS_TIMEOUT).await
|
||||
}
|
||||
|
||||
async fn query_online_states_(
|
||||
|
@ -215,9 +215,10 @@ pub async fn create_relay_connection(
|
||||
uuid: String,
|
||||
peer_addr: SocketAddr,
|
||||
secure: bool,
|
||||
ipv4: bool,
|
||||
) {
|
||||
if let Err(err) =
|
||||
create_relay_connection_(server, relay_server, uuid.clone(), peer_addr, secure).await
|
||||
create_relay_connection_(server, relay_server, uuid.clone(), peer_addr, secure, ipv4).await
|
||||
{
|
||||
log::error!(
|
||||
"Failed to create relay connection for {} with uuid {}: {}",
|
||||
@ -234,10 +235,10 @@ async fn create_relay_connection_(
|
||||
uuid: String,
|
||||
peer_addr: SocketAddr,
|
||||
secure: bool,
|
||||
ipv4: bool,
|
||||
) -> ResultType<()> {
|
||||
let mut stream = socket_client::connect_tcp(
|
||||
crate::check_port(relay_server, RELAY_PORT),
|
||||
Config::get_any_listen_addr(),
|
||||
socket_client::ipv4_to_ipv6(crate::check_port(relay_server, RELAY_PORT), ipv4),
|
||||
CONNECT_TIMEOUT,
|
||||
)
|
||||
.await?;
|
||||
|
@ -1148,7 +1148,7 @@ impl Connection {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if !crate::is_ip(&lr.username) && lr.username != Config::get_id() {
|
||||
if !hbb_common::is_ipv4_str(&lr.username) && lr.username != Config::get_id() {
|
||||
self.send_login_error("Offline").await;
|
||||
} else if password::approve_mode() == ApproveMode::Click
|
||||
|| password::approve_mode() == ApproveMode::Both && !password::has_valid_password()
|
||||
|
115
src/ui.rs
115
src/ui.rs
@ -9,13 +9,9 @@ use sciter::Value;
|
||||
|
||||
use hbb_common::{
|
||||
allow_err,
|
||||
config::{self, Config, PeerConfig, RENDEZVOUS_PORT, RENDEZVOUS_TIMEOUT},
|
||||
futures::future::join_all,
|
||||
config::{self, PeerConfig},
|
||||
log,
|
||||
protobuf::Message as _,
|
||||
rendezvous_proto::*,
|
||||
tcp::FramedStream,
|
||||
tokio,
|
||||
};
|
||||
|
||||
use crate::common::get_app_name;
|
||||
@ -31,8 +27,6 @@ pub mod remote;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub mod win_privacy;
|
||||
|
||||
type Message = RendezvousMessage;
|
||||
|
||||
pub type Children = Arc<Mutex<(bool, HashMap<(String, String), Child>)>>;
|
||||
#[allow(dead_code)]
|
||||
type Status = (i32, bool, i64, String);
|
||||
@ -124,15 +118,11 @@ pub fn start(args: &mut [String]) {
|
||||
let args: Vec<String> = iter.map(|x| x.clone()).collect();
|
||||
frame.set_title(&id);
|
||||
frame.register_behavior("native-remote", move || {
|
||||
let handler = remote::SciterSession::new(
|
||||
cmd.clone(),
|
||||
id.clone(),
|
||||
pass.clone(),
|
||||
args.clone(),
|
||||
);
|
||||
let handler =
|
||||
remote::SciterSession::new(cmd.clone(), id.clone(), pass.clone(), args.clone());
|
||||
#[cfg(not(feature = "flutter"))]
|
||||
crate::keyboard::set_cur_session(handler.inner());
|
||||
|
||||
|
||||
Box::new(handler)
|
||||
});
|
||||
page = "remote.html";
|
||||
@ -514,7 +504,7 @@ impl UI {
|
||||
|
||||
fn change_id(&self, id: String) {
|
||||
let old_id = self.get_id();
|
||||
change_id(id, old_id);
|
||||
change_id_shared(id, old_id);
|
||||
}
|
||||
|
||||
fn post_request(&self, url: String, body: String, header: String) {
|
||||
@ -694,101 +684,6 @@ fn get_sound_inputs() -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
const INVALID_FORMAT: &'static str = "Invalid format";
|
||||
const UNKNOWN_ERROR: &'static str = "Unknown error";
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn change_id(id: String, old_id: String) -> &'static str {
|
||||
if !hbb_common::is_valid_custom_id(&id) {
|
||||
return INVALID_FORMAT;
|
||||
}
|
||||
let uuid = machine_uid::get().unwrap_or("".to_owned());
|
||||
if uuid.is_empty() {
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
let rendezvous_servers = crate::ipc::get_rendezvous_servers(1_000).await;
|
||||
let mut futs = Vec::new();
|
||||
let err: Arc<Mutex<&str>> = Default::default();
|
||||
for rendezvous_server in rendezvous_servers {
|
||||
let err = err.clone();
|
||||
let id = id.to_owned();
|
||||
let uuid = uuid.clone();
|
||||
let old_id = old_id.clone();
|
||||
futs.push(tokio::spawn(async move {
|
||||
let tmp = check_id(rendezvous_server, old_id, id, uuid).await;
|
||||
if !tmp.is_empty() {
|
||||
*err.lock().unwrap() = tmp;
|
||||
}
|
||||
}));
|
||||
}
|
||||
join_all(futs).await;
|
||||
let err = *err.lock().unwrap();
|
||||
if err.is_empty() {
|
||||
crate::ipc::set_config_async("id", id.to_owned()).await.ok();
|
||||
}
|
||||
err
|
||||
}
|
||||
|
||||
async fn check_id(
|
||||
rendezvous_server: String,
|
||||
old_id: String,
|
||||
id: String,
|
||||
uuid: String,
|
||||
) -> &'static str {
|
||||
let any_addr = Config::get_any_listen_addr();
|
||||
if let Ok(mut socket) = FramedStream::new(
|
||||
crate::check_port(rendezvous_server, RENDEZVOUS_PORT),
|
||||
any_addr,
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await
|
||||
{
|
||||
let mut msg_out = Message::new();
|
||||
msg_out.set_register_pk(RegisterPk {
|
||||
old_id,
|
||||
id,
|
||||
uuid: uuid.into(),
|
||||
..Default::default()
|
||||
});
|
||||
let mut ok = false;
|
||||
if socket.send(&msg_out).await.is_ok() {
|
||||
if let Some(Ok(bytes)) = socket.next_timeout(3_000).await {
|
||||
if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
|
||||
match msg_in.union {
|
||||
Some(rendezvous_message::Union::RegisterPkResponse(rpr)) => {
|
||||
match rpr.result.enum_value_or_default() {
|
||||
register_pk_response::Result::OK => {
|
||||
ok = true;
|
||||
}
|
||||
register_pk_response::Result::ID_EXISTS => {
|
||||
return "Not available";
|
||||
}
|
||||
register_pk_response::Result::TOO_FREQUENT => {
|
||||
return "Too frequent";
|
||||
}
|
||||
register_pk_response::Result::NOT_SUPPORT => {
|
||||
return "server_not_support";
|
||||
}
|
||||
register_pk_response::Result::INVALID_ID_FORMAT => {
|
||||
return INVALID_FORMAT;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
} else {
|
||||
return "Failed to connect to rendezvous server";
|
||||
}
|
||||
""
|
||||
}
|
||||
|
||||
// sacrifice some memory
|
||||
pub fn value_crash_workaround(values: &[Value]) -> Arc<Vec<Value>> {
|
||||
let persist = Arc::new(values.to_vec());
|
||||
|
@ -14,20 +14,17 @@ use hbb_common::{
|
||||
tokio::{self, sync::mpsc, time},
|
||||
};
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
use hbb_common::{
|
||||
config::{RENDEZVOUS_PORT, RENDEZVOUS_TIMEOUT},
|
||||
futures::future::join_all,
|
||||
protobuf::Message as _,
|
||||
rendezvous_proto::*,
|
||||
tcp::FramedStream,
|
||||
};
|
||||
|
||||
#[cfg(feature = "flutter")]
|
||||
use crate::hbbs_http::account;
|
||||
use crate::{common::SOFTWARE_UPDATE_URL, ipc};
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
type Message = RendezvousMessage;
|
||||
|
||||
pub type Children = Arc<Mutex<(bool, HashMap<(String, String), Child>)>>;
|
||||
@ -752,7 +749,7 @@ pub fn change_id(id: String) {
|
||||
*ASYNC_JOB_STATUS.lock().unwrap() = " ".to_owned();
|
||||
let old_id = get_id();
|
||||
std::thread::spawn(move || {
|
||||
*ASYNC_JOB_STATUS.lock().unwrap() = change_id_(id, old_id).to_owned();
|
||||
*ASYNC_JOB_STATUS.lock().unwrap() = change_id_shared(id, old_id).to_owned();
|
||||
});
|
||||
}
|
||||
|
||||
@ -1009,14 +1006,11 @@ pub(crate) async fn send_to_cm(data: &ipc::Data) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
const INVALID_FORMAT: &'static str = "Invalid format";
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
const UNKNOWN_ERROR: &'static str = "Unknown error";
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn change_id_(id: String, old_id: String) -> &'static str {
|
||||
pub async fn change_id_shared(id: String, old_id: String) -> &'static str {
|
||||
if !hbb_common::is_valid_custom_id(&id) {
|
||||
return INVALID_FORMAT;
|
||||
}
|
||||
@ -1064,17 +1058,14 @@ async fn change_id_(id: String, old_id: String) -> &'static str {
|
||||
err
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||
async fn check_id(
|
||||
rendezvous_server: String,
|
||||
old_id: String,
|
||||
id: String,
|
||||
uuid: String,
|
||||
) -> &'static str {
|
||||
let any_addr = Config::get_any_listen_addr();
|
||||
if let Ok(mut socket) = FramedStream::new(
|
||||
if let Ok(mut socket) = hbb_common::socket_client::connect_tcp(
|
||||
crate::check_port(rendezvous_server, RENDEZVOUS_PORT),
|
||||
any_addr,
|
||||
RENDEZVOUS_TIMEOUT,
|
||||
)
|
||||
.await
|
||||
|
Loading…
Reference in New Issue
Block a user