feat: kinda janky ip discovery impl

This commit is contained in:
kozabrada123 2023-11-12 12:54:32 +01:00
parent e4e4f2cc11
commit 8e6eb9926e
4 changed files with 465 additions and 297 deletions

641
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@ website = ["https://discord.com/invite/m3FpcapGDD"]
[features] [features]
default = ["client"] default = ["client"]
backend = ["poem", "sqlx"] backend = ["poem", "sqlx"]
client = [] client = ["discortp"]
[dependencies] [dependencies]
tokio = { version = "1.29.1", features = ["macros"] } tokio = { version = "1.29.1", features = ["macros"] }
@ -51,6 +51,7 @@ jsonwebtoken = "8.3.0"
log = "0.4.20" log = "0.4.20"
async-trait = "0.1.73" async-trait = "0.1.73"
chorus-macros = "0.2.0" chorus-macros = "0.2.0"
discortp = { version = "0.5.0", optional = true, features = ["rtp", "discord"] }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1.32.0", features = ["full"] } tokio = { version = "1.32.0", features = ["full"] }

View File

@ -13,9 +13,9 @@ use super::VoiceEncryptionMode;
/// See <https://discord-userdoccers.vercel.app/topics/voice-connections#ready-structure> /// See <https://discord-userdoccers.vercel.app/topics/voice-connections#ready-structure>
pub struct VoiceReady { pub struct VoiceReady {
/// See <https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpStreamStats/ssrc> /// See <https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpStreamStats/ssrc>
pub ssrc: i32, pub ssrc: u32,
pub ip: Ipv4Addr, pub ip: Ipv4Addr,
pub port: u32, pub port: u16,
/// The available encryption modes for the webrtc connection /// The available encryption modes for the webrtc connection
pub modes: Vec<VoiceEncryptionMode>, pub modes: Vec<VoiceEncryptionMode>,
#[serde(default)] #[serde(default)]

View File

@ -1 +1,115 @@
//! Defines voice raw udp socket handling //! Defines voice raw udp socket handling
use std::net::SocketAddr;
use log::{info, warn};
use tokio::net::UdpSocket;
use discortp::{discord::IpDiscoveryPacket, MutablePacket, Packet};
#[derive(Debug)]
pub struct UdpHandler {
url: SocketAddr,
socket: UdpSocket,
}
impl UdpHandler {
pub async fn new(url: SocketAddr, ssrc: u32) {
// Bind with a port number of 0, so the os assigns this listener a port
let udp_socket = UdpSocket::bind("0.0.0.0:0").await.unwrap();
udp_socket.connect(url).await.unwrap();
// First perform ip discovery
let ip_discovery = discortp::discord::IpDiscovery {
pkt_type: discortp::discord::IpDiscoveryType::Request,
ssrc,
length: 70,
address: Vec::new(),
port: 0,
payload: Vec::new(),
};
let mut buf: Vec<u8> = Vec::new();
let size = IpDiscoveryPacket::minimum_packet_size() + 64;
// wtf
for _i in 0..size {
buf.push(0);
}
let mut ip_discovery_packet =
discortp::discord::MutableIpDiscoveryPacket::new(&mut buf).expect("FUcking kill me");
ip_discovery_packet.populate(&ip_discovery);
let data = ip_discovery_packet.packet();
info!("VUDP: Sending Ip Discovery {:?}", &data);
udp_socket.send(&data).await.unwrap();
info!("VUDP: Sent packet discovery request");
// Handle the ip discovery response
let receieved_size = udp_socket.recv(&mut buf).await.unwrap();
info!(
"VUDP: Receiving messsage: {:?} - (expected {} vs real {})",
buf.clone(),
size,
receieved_size
);
let receieved_ip_discovery = discortp::discord::IpDiscoveryPacket::new(&buf).unwrap();
info!(
"VUDP: Received ip discovery!!! {:?}",
receieved_ip_discovery
);
let mut handler = UdpHandler {
url,
socket: udp_socket,
};
// Now we can continuously check for messages in a different task
tokio::spawn(async move {
handler.listen_task().await;
});
}
/// The main listen task;
///
/// Receives udp messages and parses them.
pub async fn listen_task(&mut self) {
loop {
let mut buf: Vec<u8> = Vec::new();
let size = IpDiscoveryPacket::minimum_packet_size() + 64;
// wtf
for _i in 0..size {
buf.push(0);
}
let msg = self.socket.recv(&mut buf).await;
if let Ok(size) = msg {
info!("VUDP: Receiving messsage: {:?} - {}", buf.clone(), size);
self.handle_message(&buf[0..size]).await;
continue;
}
if let Err(e) = msg {
warn!("VUDP: {:?}", e);
}
//warn!("VUDP: Voice UDP is broken, closing connection");
//break;
}
}
/// Handles a message buf
async fn handle_message(&self, buf: &[u8]) {
info!("VUDP: Received messsage {:?}", buf);
}
}