summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-09-21 11:38:16 +0200
committermetamuffin <metamuffin@disroot.org>2024-09-21 11:38:16 +0200
commit526bde82158fc58f59a8147566ef0ef01845ac49 (patch)
treef7e7ca5dbaa55173e3179334a3e3ff8007ccf02f
parentcb9d58b50c76f8c4a4068830ecb93e9aed0cf008 (diff)
downloadhurrycurry-526bde82158fc58f59a8147566ef0ef01845ac49.tar
hurrycurry-526bde82158fc58f59a8147566ef0ef01845ac49.tar.bz2
hurrycurry-526bde82158fc58f59a8147566ef0ef01845ac49.tar.zst
register ip4 and ip6
-rw-r--r--server/src/register.rs66
1 files changed, 54 insertions, 12 deletions
diff --git a/server/src/register.rs b/server/src/register.rs
index ef940fbf..2aaec770 100644
--- a/server/src/register.rs
+++ b/server/src/register.rs
@@ -1,10 +1,15 @@
use crate::server::Server;
-use anyhow::{bail, Result};
+use anyhow::{bail, Context, Result};
use hurrycurry_protocol::{registry::Submission, VERSION};
use log::{info, warn};
use rand::random;
use reqwest::{header::USER_AGENT, Client, Url};
-use std::{str::FromStr, sync::Arc, time::Duration};
+use std::{
+ net::{IpAddr, Ipv4Addr, Ipv6Addr},
+ str::FromStr,
+ sync::Arc,
+ time::Duration,
+};
use tokio::{sync::RwLock, time::interval};
const REGISTRY_URI: &'static str = "https://hurrycurry-registry.metamuffin.org";
@@ -14,7 +19,9 @@ pub struct Register {
port: u16,
register_uri: Option<String>,
state: Arc<RwLock<Server>>,
- client: Client,
+ inet_client: Client,
+ ip4_client: Client,
+ ip6_client: Client,
secret: u128,
players: usize,
}
@@ -33,7 +40,15 @@ impl Register {
port,
secret: random(),
state,
- client: Client::new(),
+ inet_client: Client::new(),
+ ip4_client: Client::builder()
+ .local_address(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
+ .build()
+ .unwrap(),
+ ip6_client: Client::builder()
+ .local_address(IpAddr::V6(Ipv6Addr::UNSPECIFIED))
+ .build()
+ .unwrap(),
}
}
pub async fn register_loop(mut self) {
@@ -46,10 +61,40 @@ impl Register {
}
}
}
+
+ pub async fn register(&self) -> Result<()> {
+ if let Some(uri) = &self.register_uri {
+ self.register_with("uri", &self.inet_client, uri.to_owned())
+ .await?;
+ } else {
+ let x = tokio::join!(
+ async {
+ self.register_with(
+ "ip4",
+ &self.ip4_client,
+ format!("ws://0.0.0.0:{}", self.port),
+ )
+ .await
+ .context("ipv4")
+ },
+ async {
+ self.register_with(
+ "ip6",
+ &self.ip6_client,
+ format!("ws://0.0.0.0:{}", self.port),
+ )
+ .await
+ .context("ipv6")
+ }
+ );
+ x.0?;
+ x.1?;
+ }
+ Ok(())
+ }
// TODO ip v6
- pub async fn register(&mut self) -> Result<()> {
- let res = self
- .client
+ pub async fn register_with(&self, label: &str, client: &Client, uri: String) -> Result<()> {
+ let res = client
.post(Url::from_str(&format!("{REGISTRY_URI}/v1/register")).unwrap())
.header(
USER_AGENT,
@@ -58,10 +103,7 @@ impl Register {
.json(&Submission {
last_game: 0,
name: self.name.clone(),
- uri: self
- .register_uri
- .clone()
- .unwrap_or_else(|| format!("ws://0.0.0.0:{}", self.port)),
+ uri,
players: self.players,
secret: self.secret,
version: VERSION,
@@ -71,7 +113,7 @@ impl Register {
let r = res.text().await?;
if r == "ok" {
- info!("register ok");
+ info!("register ok ({label})");
Ok(())
} else {
bail!("{r}");