mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
DNS Resolver Implementation (#4)
* [dns-resolver] Added trust-dns-resolver and restored cargo.lock * [dns_resolver] Implemented feature
This commit is contained in:
parent
6159a7c385
commit
e8cbe7b9f5
6 changed files with 796 additions and 19 deletions
|
|
@ -22,11 +22,13 @@ pub enum GDError {
|
|||
/// Unknown cast while translating a value to an enum.
|
||||
UnknownEnumCast,
|
||||
/// The server queried is not from the queried game.
|
||||
BadGame(String)
|
||||
BadGame(String),
|
||||
/// Problems occurred while dns resolving.
|
||||
DnsResolve(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for GDError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
GDError::PacketOverflow(details) => write!(f, "Packet overflow: {details}"),
|
||||
GDError::PacketUnderflow(details) => write!(f, "Packet underflow: {details}"),
|
||||
|
|
@ -36,6 +38,7 @@ impl fmt::Display for GDError {
|
|||
GDError::Decompress(details) => write!(f, "Couldn't decompress data: {details}"),
|
||||
GDError::UnknownEnumCast => write!(f, "Unknown enum cast encountered."),
|
||||
GDError::BadGame(details) => write!(f, "Queried another game that the supposed one: {details}"),
|
||||
GDError::DnsResolve(details) => write!(f, "DNS Resolve: {details}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -308,11 +308,11 @@ impl SplitPacket {
|
|||
}
|
||||
|
||||
impl ValveProtocol {
|
||||
fn new(address: &str, port: u16) -> Self {
|
||||
Self {
|
||||
fn new(address: &str, port: u16) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
socket: UdpSocket::bind("0.0.0.0:0").unwrap(),
|
||||
complete_address: complete_address(address, port)
|
||||
}
|
||||
complete_address: complete_address(address, port)?
|
||||
})
|
||||
}
|
||||
|
||||
fn send(&self, data: &[u8]) -> GDResult<()> {
|
||||
|
|
@ -324,8 +324,8 @@ impl ValveProtocol {
|
|||
let mut buf: Vec<u8> = vec![0; buffer_size];
|
||||
let (amt, _) = self.socket.recv_from(&mut buf.as_mut_slice()).map_err(|e| GDError::PacketReceive(e.to_string()))?;
|
||||
|
||||
if amt < 9 {
|
||||
return Err(GDError::PacketUnderflow("Any Valve Protocol response can't be under 9 bytes long.".to_string()));
|
||||
if amt < 6 {
|
||||
return Err(GDError::PacketUnderflow("Any Valve Protocol response can't be under 6 bytes long.".to_string()));
|
||||
}
|
||||
|
||||
Ok(buf[..amt].to_vec())
|
||||
|
|
@ -489,7 +489,7 @@ impl ValveProtocol {
|
|||
}
|
||||
|
||||
pub(crate) fn query(app: App, address: &str, port: u16, gather: GatheringSettings) -> Result<Response, GDError> {
|
||||
let client = ValveProtocol::new(address, port);
|
||||
let client = ValveProtocol::new(address, port)?;
|
||||
|
||||
let info = client.get_server_info(&app)?;
|
||||
|
||||
|
|
|
|||
22
src/utils.rs
22
src/utils.rs
|
|
@ -1,8 +1,19 @@
|
|||
use std::ops::Add;
|
||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||
use trust_dns_resolver::Resolver;
|
||||
use crate::{GDResult, GDError};
|
||||
|
||||
pub fn complete_address(address: &str, port: u16) -> String {
|
||||
String::from(address.to_owned() + ":").add(&*port.to_string())
|
||||
fn resolve_dns(address: &str) -> GDResult<String> {
|
||||
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default())
|
||||
.map_err(|e| GDError::DnsResolve(e.to_string()))?;
|
||||
|
||||
let response = resolver.lookup_ip(address)
|
||||
.map_err(|e| GDError::DnsResolve(e.to_string()))?;
|
||||
|
||||
Ok(response.iter().next().ok_or(GDError::DnsResolve("Couldn't resolve the DNS address.".to_string()))?.to_string())
|
||||
}
|
||||
|
||||
pub fn complete_address(address: &str, port: u16) -> GDResult<String> {
|
||||
Ok(resolve_dns(address)? + ":" + &*port.to_string())
|
||||
}
|
||||
|
||||
pub mod buffer {
|
||||
|
|
@ -73,9 +84,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn complete_address_test() {
|
||||
let address = "192.168.0.1";
|
||||
let port = 27015;
|
||||
assert_eq!(complete_address(address, port), "192.168.0.1:27015");
|
||||
assert_eq!(complete_address("192.168.0.1", 27015).unwrap(), "192.168.0.1:27015");
|
||||
assert!(complete_address("not_existent_address", 9999).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue