55 lines
1.5 KiB
Rust
55 lines
1.5 KiB
Rust
use std::{net::SocketAddr, sync::Arc};
|
|
|
|
use axum::{
|
|
Json,
|
|
extract::{ConnectInfo, State},
|
|
};
|
|
|
|
use crate::{Client, JoecalState, models::device::DeviceInfo};
|
|
|
|
impl Client {
|
|
pub async fn announce_http(&self, ip: Option<SocketAddr>) -> crate::error::Result<()> {
|
|
if let Some(ip) = ip {
|
|
let url = format!("http://{ip}/api/localsend/v2/register");
|
|
let client = reqwest::Client::new();
|
|
client.post(&url).json(&self.device).send().await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn announce_http_legacy(&self) -> crate::error::Result<()> {
|
|
// send the reqwest to all local ip addresses from 192.168.0.0 to
|
|
// 192.168.255.255
|
|
let mut address_list = Vec::new();
|
|
for j in 0..256 {
|
|
for k in 0..256 {
|
|
address_list.push(format!("192.168.{j:03}.{k}:53317"));
|
|
}
|
|
}
|
|
|
|
for ip in address_list {
|
|
let url = format!("http://{ip}/api/localsend/v2/register");
|
|
self.http_client
|
|
.post(&url)
|
|
.json(&self.device)
|
|
.send()
|
|
.await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub async fn register_device(
|
|
State(state): State<Arc<JoecalState>>,
|
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
|
Json(device): Json<DeviceInfo>,
|
|
) -> Json<DeviceInfo> {
|
|
let mut addr = addr;
|
|
addr.set_port(state.device.port);
|
|
state
|
|
.peers
|
|
.lock()
|
|
.await
|
|
.insert(device.fingerprint.clone(), (addr, device.clone()));
|
|
Json(device)
|
|
}
|