Announcing port on DHT
This commit is contained in:
parent
162afe3056
commit
6bb5d01c0f
3 changed files with 19 additions and 62 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use std::{
|
||||
cmp::Reverse,
|
||||
net::{SocketAddr, SocketAddrV4},
|
||||
net::SocketAddr,
|
||||
str::FromStr,
|
||||
sync::{
|
||||
atomic::{AtomicU16, Ordering},
|
||||
|
|
@ -109,7 +109,7 @@ impl RecursiveRequestCallbacks for RecursiveRequestCallbacksGetPeers {
|
|||
addr: SocketAddr,
|
||||
resp: &anyhow::Result<ResponseOrError>,
|
||||
) {
|
||||
let announce_addr = match req.dht.announce_addr {
|
||||
let announce_port = match req.dht.announce_port {
|
||||
Some(a) => a,
|
||||
None => return,
|
||||
};
|
||||
|
|
@ -132,7 +132,7 @@ impl RecursiveRequestCallbacks for RecursiveRequestCallbacksGetPeers {
|
|||
let (tid, message) = req.dht.create_request(Request::Announce {
|
||||
info_hash: req.info_hash,
|
||||
token: token.clone(),
|
||||
addr: announce_addr,
|
||||
port: announce_port,
|
||||
});
|
||||
|
||||
let _ = req.dht.worker_sender.send(WorkerSendRequest {
|
||||
|
|
@ -534,7 +534,7 @@ pub struct DhtState {
|
|||
worker_sender: UnboundedSender<WorkerSendRequest>,
|
||||
|
||||
pub(crate) peer_store: PeerStore,
|
||||
announce_addr: Option<SocketAddrV4>,
|
||||
announce_port: Option<u16>,
|
||||
}
|
||||
|
||||
impl DhtState {
|
||||
|
|
@ -544,7 +544,7 @@ impl DhtState {
|
|||
routing_table: Option<RoutingTable>,
|
||||
listen_addr: SocketAddr,
|
||||
peer_store: PeerStore,
|
||||
announce_addr: Option<SocketAddrV4>,
|
||||
announce_port: Option<u16>,
|
||||
) -> Self {
|
||||
let routing_table = routing_table.unwrap_or_else(|| RoutingTable::new(id, None));
|
||||
Self {
|
||||
|
|
@ -556,7 +556,7 @@ impl DhtState {
|
|||
listen_addr,
|
||||
rate_limiter: make_rate_limiter(),
|
||||
peer_store,
|
||||
announce_addr,
|
||||
announce_port,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -628,13 +628,13 @@ impl DhtState {
|
|||
Request::Announce {
|
||||
info_hash,
|
||||
token,
|
||||
addr,
|
||||
port,
|
||||
} => Message {
|
||||
kind: MessageKind::AnnouncePeer(AnnouncePeer {
|
||||
id: self.id,
|
||||
implied_port: 0,
|
||||
info_hash,
|
||||
port: addr.port(),
|
||||
port,
|
||||
token,
|
||||
}),
|
||||
transaction_id: ByteString::from(transaction_id_buf.as_ref()),
|
||||
|
|
@ -811,7 +811,7 @@ enum Request {
|
|||
Announce {
|
||||
info_hash: Id20,
|
||||
token: ByteString,
|
||||
addr: SocketAddrV4,
|
||||
port: u16,
|
||||
},
|
||||
Ping,
|
||||
}
|
||||
|
|
@ -1124,7 +1124,7 @@ pub struct DhtConfig {
|
|||
pub bootstrap_addrs: Option<Vec<String>>,
|
||||
pub routing_table: Option<RoutingTable>,
|
||||
pub listen_addr: Option<SocketAddr>,
|
||||
pub announce_addr: Option<SocketAddr>,
|
||||
pub announce_port: Option<u16>,
|
||||
pub peer_store: Option<PeerStore>,
|
||||
}
|
||||
|
||||
|
|
@ -1160,13 +1160,7 @@ impl DhtState {
|
|||
config.routing_table,
|
||||
listen_addr,
|
||||
config.peer_store.unwrap_or_else(|| PeerStore::new(peer_id)),
|
||||
config.announce_addr.and_then(|a| match a {
|
||||
SocketAddr::V4(v4) => Some(v4),
|
||||
SocketAddr::V6(_) => {
|
||||
warn!("libqrqbit-dht doesn't support announcing IPv6 addresses");
|
||||
None
|
||||
}
|
||||
}),
|
||||
config.announce_port,
|
||||
));
|
||||
|
||||
spawn(error_span!("dht"), {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ use crate::{Dht, DhtConfig, DhtState};
|
|||
pub struct PersistentDhtConfig {
|
||||
pub dump_interval: Option<Duration>,
|
||||
pub config_filename: Option<PathBuf>,
|
||||
pub announce_addr: Option<SocketAddr>,
|
||||
pub announce_port: Option<u16>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
|
@ -118,7 +118,7 @@ impl PersistentDht {
|
|||
routing_table,
|
||||
listen_addr,
|
||||
peer_store,
|
||||
announce_addr: config.announce_addr,
|
||||
announce_port: config.announce_port,
|
||||
..Default::default()
|
||||
};
|
||||
let dht = DhtState::with_config(dht_config).await?;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::{
|
|||
borrow::Cow,
|
||||
collections::{HashMap, HashSet},
|
||||
io::{BufReader, BufWriter, Read},
|
||||
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||
net::SocketAddr,
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
|
|
@ -357,34 +357,6 @@ async fn create_tcp_listener(
|
|||
bail!("no free TCP ports in range {port_range:?}");
|
||||
}
|
||||
|
||||
async fn get_public_announce_addr(port: u16) -> anyhow::Result<SocketAddr> {
|
||||
async fn get_ipify() -> anyhow::Result<Ipv4Addr> {
|
||||
#[derive(Deserialize)]
|
||||
struct Data {
|
||||
ip: Ipv4Addr,
|
||||
}
|
||||
let resp: Data = reqwest::get("https://api.ipify.org?format=json")
|
||||
.await
|
||||
.context("error getting public IP address")?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.await?;
|
||||
Ok(resp.ip)
|
||||
}
|
||||
|
||||
async fn get_public_ip() -> anyhow::Result<Ipv4Addr> {
|
||||
get_ipify().await
|
||||
}
|
||||
|
||||
let ip = get_public_ip()
|
||||
.await
|
||||
.context("error getting public IP address")?;
|
||||
|
||||
let addr = SocketAddr::V4(SocketAddrV4::new(ip, port));
|
||||
info!("using public IP address {addr} to publish on DHT");
|
||||
Ok(addr)
|
||||
}
|
||||
|
||||
pub(crate) struct CheckedIncomingConnection {
|
||||
pub addr: SocketAddr,
|
||||
pub stream: tokio::net::TcpStream,
|
||||
|
|
@ -406,7 +378,7 @@ impl Session {
|
|||
) -> anyhow::Result<Arc<Self>> {
|
||||
let peer_id = opts.peer_id.unwrap_or_else(generate_peer_id);
|
||||
|
||||
let (tcp_listener, port) = if let Some(port_range) = opts.listen_port_range {
|
||||
let (tcp_listener, tcp_listen_port) = if let Some(port_range) = opts.listen_port_range {
|
||||
let (l, p) = create_tcp_listener(port_range)
|
||||
.await
|
||||
.context("error listening on TCP")?;
|
||||
|
|
@ -419,24 +391,15 @@ impl Session {
|
|||
let dht = if opts.disable_dht {
|
||||
None
|
||||
} else {
|
||||
let announce_addr = if let Some(port) = port {
|
||||
Some(
|
||||
get_public_announce_addr(port)
|
||||
.await
|
||||
.context("error getting public announce address")?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let dht = if opts.disable_dht_persistence {
|
||||
DhtBuilder::with_config(DhtConfig {
|
||||
announce_addr,
|
||||
announce_port: tcp_listen_port,
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
} else {
|
||||
let mut pdht_config = opts.dht_config.take().unwrap_or_default();
|
||||
pdht_config.announce_addr = announce_addr;
|
||||
pdht_config.announce_port = tcp_listen_port;
|
||||
PersistentDht::create(Some(pdht_config)).await
|
||||
}
|
||||
.context("error initializing DHT")?;
|
||||
|
|
@ -468,12 +431,12 @@ impl Session {
|
|||
if let Some(tcp_listener) = tcp_listener {
|
||||
session.spawn(
|
||||
"tcp listener",
|
||||
error_span!("tcp_listen", port = port),
|
||||
error_span!("tcp_listen", port = tcp_listen_port),
|
||||
session.clone().task_tcp_listener(tcp_listener),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(listen_port) = port {
|
||||
if let Some(listen_port) = tcp_listen_port {
|
||||
if opts.enable_upnp_port_forwarding {
|
||||
session.spawn(
|
||||
"upnp_forward",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue