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
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