Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
5b03e99404
@ -133,6 +133,16 @@ message LocalAddr {
|
|||||||
string version = 5;
|
string version = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message PeerDiscovery {
|
||||||
|
string cmd = 1;
|
||||||
|
string mac = 2;
|
||||||
|
string id = 3;
|
||||||
|
string username = 4;
|
||||||
|
string hostname = 5;
|
||||||
|
string platform = 6;
|
||||||
|
string misc = 7;
|
||||||
|
}
|
||||||
|
|
||||||
message RendezvousMessage {
|
message RendezvousMessage {
|
||||||
oneof union {
|
oneof union {
|
||||||
RegisterPeer register_peer = 6;
|
RegisterPeer register_peer = 6;
|
||||||
@ -151,5 +161,6 @@ message RendezvousMessage {
|
|||||||
RelayResponse relay_response = 19;
|
RelayResponse relay_response = 19;
|
||||||
TestNatRequest test_nat_request = 20;
|
TestNatRequest test_nat_request = 20;
|
||||||
TestNatResponse test_nat_response = 21;
|
TestNatResponse test_nat_response = 21;
|
||||||
|
PeerDiscovery peer_discovery = 22;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ pub use sodiumoxide;
|
|||||||
pub use tokio_socks;
|
pub use tokio_socks;
|
||||||
pub use tokio_socks::IntoTargetAddr;
|
pub use tokio_socks::IntoTargetAddr;
|
||||||
pub use tokio_socks::TargetAddr;
|
pub use tokio_socks::TargetAddr;
|
||||||
|
pub use mac_address;
|
||||||
|
|
||||||
#[cfg(feature = "quic")]
|
#[cfg(feature = "quic")]
|
||||||
pub type Stream = quic::Connection;
|
pub type Stream = quic::Connection;
|
||||||
|
@ -3,8 +3,8 @@ use anyhow::anyhow;
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
use protobuf::Message;
|
use protobuf::Message;
|
||||||
use socket2::{Domain, Socket, Type};
|
use socket2::{Domain, Protocol, Socket, Type};
|
||||||
use std::net::SocketAddr;
|
use std::net::{SocketAddr, SocketAddrV4};
|
||||||
use tokio::net::{ToSocketAddrs, UdpSocket};
|
use tokio::net::{ToSocketAddrs, UdpSocket};
|
||||||
use tokio_socks::{udp::Socks5UdpFramed, IntoTargetAddr, TargetAddr, ToProxyAddrs};
|
use tokio_socks::{udp::Socks5UdpFramed, IntoTargetAddr, TargetAddr, ToProxyAddrs};
|
||||||
use tokio_util::{codec::BytesCodec, udp::UdpFramed};
|
use tokio_util::{codec::BytesCodec, udp::UdpFramed};
|
||||||
@ -142,3 +142,18 @@ impl FramedSocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const DEFAULT_MULTICAST: &str = "239.255.42.98";
|
||||||
|
|
||||||
|
pub fn bind_multicast(addr: &SocketAddrV4, multi_addr: &SocketAddrV4) -> ResultType<FramedSocket> {
|
||||||
|
assert!(multi_addr.ip().is_multicast(), "Must be multcast address");
|
||||||
|
let socket = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?;
|
||||||
|
socket.set_reuse_address(true)?;
|
||||||
|
socket.bind(&socket2::SockAddr::from(*addr))?;
|
||||||
|
socket.set_multicast_loop_v4(true)?;
|
||||||
|
socket.join_multicast_v4(multi_addr.ip(), addr.ip())?;
|
||||||
|
Ok(FramedSocket::Direct(UdpFramed::new(
|
||||||
|
UdpSocket::from_std(socket.into_udp_socket())?,
|
||||||
|
BytesCodec::new(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
@ -12,11 +12,11 @@ use hbb_common::{
|
|||||||
self, select,
|
self, select,
|
||||||
time::{interval, Duration},
|
time::{interval, Duration},
|
||||||
},
|
},
|
||||||
udp::FramedSocket,
|
udp::{self, FramedSocket},
|
||||||
AddrMangle, IntoTargetAddr, ResultType, TargetAddr,
|
AddrMangle, IntoTargetAddr, ResultType, TargetAddr,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
net::SocketAddr,
|
net::{SocketAddr, SocketAddrV4},
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc, Mutex,
|
Arc, Mutex,
|
||||||
@ -59,6 +59,9 @@ impl RendezvousMediator {
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
allow_err!(direct_server(server_cloned).await);
|
allow_err!(direct_server(server_cloned).await);
|
||||||
});
|
});
|
||||||
|
tokio::spawn(async move {
|
||||||
|
allow_err!(lan_discovery().await);
|
||||||
|
});
|
||||||
loop {
|
loop {
|
||||||
Config::reset_online();
|
Config::reset_online();
|
||||||
if Config::get_option("stop-service").is_empty() {
|
if Config::get_option("stop-service").is_empty() {
|
||||||
@ -501,3 +504,48 @@ async fn direct_server(server: ServerPtr) -> ResultType<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_multicast_socket() -> ResultType<(FramedSocket, SocketAddr)> {
|
||||||
|
let port = (RENDEZVOUS_PORT + 3) as u16;
|
||||||
|
let maddr = SocketAddrV4::new([239, 255, 42, 98].into(), port);
|
||||||
|
Ok((
|
||||||
|
udp::bind_multicast(&SocketAddrV4::new([0, 0, 0, 0].into(), port), &maddr)?,
|
||||||
|
SocketAddr::V4(maddr),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn lan_discovery() -> ResultType<()> {
|
||||||
|
let (mut socket, maddr) = create_multicast_socket()?;
|
||||||
|
loop {
|
||||||
|
select! {
|
||||||
|
Some(Ok((bytes, _))) = socket.next() => {
|
||||||
|
if let Ok(msg_in) = Message::parse_from_bytes(&bytes) {
|
||||||
|
match msg_in.union {
|
||||||
|
Some(rendezvous_message::Union::peer_discovery(p)) => {
|
||||||
|
if p.cmd == "ping" {
|
||||||
|
let mut msg_out = Message::new();
|
||||||
|
let mac = if let Ok(Some(mac)) = mac_address::get_mac_address() {
|
||||||
|
mac.to_string()
|
||||||
|
} else {
|
||||||
|
"".to_owned()
|
||||||
|
};
|
||||||
|
let peer = PeerDiscovery {
|
||||||
|
cmd: "pong".to_owned(),
|
||||||
|
mac,
|
||||||
|
id: Config::get_id(),
|
||||||
|
hostname: whoami::hostname(),
|
||||||
|
username: crate::platform::get_active_username(),
|
||||||
|
platform: whoami::platform().to_string(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
msg_out.set_peer_discovery(peer);
|
||||||
|
socket.send(&msg_out, maddr).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user