Fix auth
This commit is contained in:
+1
-1
@@ -163,7 +163,7 @@ pub async fn read_loop(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerMethod::JoinVoice { channel_id } => {
|
ServerMethod::JoinVoice { channel_id } => {
|
||||||
let pin = rand::random();
|
let pin = rand::random::<u64>() % (1 << 53);
|
||||||
|
|
||||||
server
|
server
|
||||||
.voice_pins
|
.voice_pins
|
||||||
|
|||||||
+34
-25
@@ -16,44 +16,50 @@ impl Server {
|
|||||||
|
|
||||||
let mut buf = [0u8; 4096];
|
let mut buf = [0u8; 4096];
|
||||||
|
|
||||||
println!("UDP server started");
|
eprintln!("[vc] UDP server listening on port {}", self.config.port);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (len, addr) = self.get_voice_socket()?.recv_from(&mut buf).await?;
|
let (len, addr) = self.get_voice_socket()?.recv_from(&mut buf).await?;
|
||||||
|
|
||||||
|
eprintln!("[vc] UDP packet received: {len} bytes from {addr}");
|
||||||
|
|
||||||
if len < 8 {
|
if len < 8 {
|
||||||
continue; // too short to even contain a pincode, drop silently
|
eprintln!("[vc] dropping packet too short for pin");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let pin_bytes: [u8; 8] = buf[..8].try_into().unwrap();
|
let pin_bytes: [u8; 8] = buf[..8].try_into().unwrap();
|
||||||
let pin = u64::from_be_bytes(pin_bytes);
|
let pin = u64::from_be_bytes(pin_bytes);
|
||||||
|
|
||||||
// First packet carries the pin for authentication; subsequent packets
|
let (sender_pubkey, channel_id, payload) = {
|
||||||
// are raw audio identified by UDP address alone.
|
let mut pins = self.voice_pins.lock().await;
|
||||||
let (sender_pubkey, channel_id, payload) = {
|
|
||||||
let mut pins = self.voice_pins.lock().await;
|
|
||||||
|
|
||||||
if let Some((pubkey, channel_id)) = pins.remove(&pin) {
|
if let Some((pubkey, channel_id)) = pins.remove(&pin) {
|
||||||
// First packet: pin consumed, strip 8-byte prefix
|
eprintln!("[vc] pin {pin} authenticated for channel {channel_id}");
|
||||||
(pubkey, channel_id, &buf[8..len])
|
(pubkey, channel_id, &buf[8..len])
|
||||||
} else {
|
} else {
|
||||||
drop(pins);
|
drop(pins);
|
||||||
|
|
||||||
// Not a first-time pin — match by address
|
match self.find_voice_sender(&addr).await {
|
||||||
match self.find_voice_sender(&addr).await {
|
Some((pubkey, channel_id)) => {
|
||||||
Some((pubkey, channel_id)) => (pubkey, channel_id, &buf[..]),
|
eprintln!("[vc] known sender in channel {channel_id}");
|
||||||
None => continue,
|
(pubkey, channel_id, &buf[..])
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
eprintln!("[vc] unknown pin {pin} and unknown addr {addr}, dropping");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let clients = self.clients.lock().await;
|
let clients = self.clients.lock().await;
|
||||||
let Some(user) = clients.get(&sender_pubkey).cloned() else {
|
let Some(user) = clients.get(&sender_pubkey).cloned() else {
|
||||||
continue; // pin referenced a user that's since disconnected
|
eprintln!("[vc] sender pubkey not found in clients, dropping");
|
||||||
|
continue;
|
||||||
};
|
};
|
||||||
drop(clients);
|
drop(clients);
|
||||||
|
|
||||||
// Record/refresh this user's known voice address + channel.
|
|
||||||
*user.voice.lock().await = Some((addr, channel_id.clone()));
|
*user.voice.lock().await = Some((addr, channel_id.clone()));
|
||||||
|
|
||||||
self.relay_voice(&sender_pubkey, &channel_id, payload).await;
|
self.relay_voice(&sender_pubkey, &channel_id, payload).await;
|
||||||
@@ -80,11 +86,8 @@ impl Server {
|
|||||||
async fn relay_voice(&self, _sender: &VerifyingKey, channel_id: &str, payload: &[u8]) {
|
async fn relay_voice(&self, _sender: &VerifyingKey, channel_id: &str, payload: &[u8]) {
|
||||||
let clients = self.clients.lock().await;
|
let clients = self.clients.lock().await;
|
||||||
|
|
||||||
|
let mut sent = 0;
|
||||||
for (_pubkey, user) in clients.iter() {
|
for (_pubkey, user) in clients.iter() {
|
||||||
// if pubkey == sender {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
let Some((addr, channel)) = &*user.voice.lock().await else {
|
let Some((addr, channel)) = &*user.voice.lock().await else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -94,7 +97,13 @@ impl Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _ = self.udp_send_to(addr, payload).await;
|
let _ = self.udp_send_to(addr, payload).await;
|
||||||
|
sent += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"[vc] relayed {} bytes to {sent} users in {channel_id}",
|
||||||
|
payload.len()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_voice_socket(&self) -> anyhow::Result<&UdpSocket> {
|
pub fn get_voice_socket(&self) -> anyhow::Result<&UdpSocket> {
|
||||||
|
|||||||
Reference in New Issue
Block a user