-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
286 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import mitmproxy_rs | ||
import asyncio | ||
import socket | ||
|
||
async def main(): | ||
builder = mitmproxy_rs.DnsResolverBuilder() | ||
builder.use_hosts_file(False) | ||
builder.use_nameserver(["8.8.8.8"]) | ||
resolver = builder.build() | ||
|
||
async def lookup(host: str): | ||
try: | ||
r = await resolver.lookup_ip(host) | ||
except socket.gaierror as e: | ||
print(f"{host=} {e=}") | ||
else: | ||
print(f"{host=} {r=}") | ||
|
||
await lookup("example.com.") | ||
await lookup("nxdomain.mitmproxy.org.") | ||
await lookup("no-a-records.mitmproxy.org.") | ||
|
||
print(f"{mitmproxy_rs.get_system_dns_servers()=}") | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,78 @@ | ||
use pyo3::types::PyAny; | ||
use pyo3::prelude::*; | ||
use mitmproxy::dns::{ | ||
NameServerConfig, Protocol, ResolveErrorKind, ResolverConfig, ResponseCode, DNS_SERVERS, | ||
}; | ||
use pyo3::exceptions::socket::gaierror; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyAny; | ||
use std::{net::IpAddr, net::SocketAddr, sync::Arc}; | ||
|
||
use mitmproxy::dns::{LookupIpStrategy, ResolveErrorKind, ResponseCode, DNS_SERVERS}; | ||
#[pyclass] | ||
pub struct DnsResolverBuilder(mitmproxy::dns::DnsResolverBuilder); | ||
|
||
#[pymethods] | ||
impl DnsResolverBuilder { | ||
#[new] | ||
fn new() -> Self { | ||
Self(mitmproxy::dns::DnsResolverBuilder::default()) | ||
} | ||
|
||
#[pyclass] | ||
#[derive(Copy, Clone)] | ||
pub enum AddressFamily { | ||
Ipv6Only, | ||
Ipv4Only, | ||
DualStack, | ||
} | ||
fn use_hosts_file(&mut self, value: bool) { | ||
self.0.use_hosts_file(value); | ||
} | ||
|
||
impl From<AddressFamily> for LookupIpStrategy { | ||
fn from(value: AddressFamily) -> Self { | ||
match value { | ||
AddressFamily::DualStack => LookupIpStrategy::Ipv4AndIpv6, | ||
AddressFamily::Ipv4Only => LookupIpStrategy::Ipv4Only, | ||
AddressFamily::Ipv6Only => LookupIpStrategy::Ipv6Only, | ||
fn use_name_servers(&mut self, value: Vec<IpAddr>) { | ||
let mut conf = ResolverConfig::new(); | ||
for ip in value.into_iter() { | ||
let addr = SocketAddr::from((ip, 53)); | ||
conf.add_name_server(NameServerConfig::new(addr, Protocol::Udp)); | ||
conf.add_name_server(NameServerConfig::new(addr, Protocol::Tcp)); | ||
} | ||
self.0.use_config(conf); | ||
} | ||
|
||
fn build(&self) -> PyResult<DnsResolver> { | ||
let inner = self.0.build().map_err(|e| { | ||
pyo3::exceptions::PyRuntimeError::new_err(format!( | ||
"failed to build dns resolver: {}", | ||
e | ||
)) | ||
})?; | ||
Ok(DnsResolver(Arc::new(inner))) | ||
} | ||
} | ||
|
||
#[pyclass] | ||
pub struct DnsResolver(Arc<mitmproxy::dns::DnsResolver>); | ||
|
||
#[pyfunction] | ||
#[pyo3(signature = (host, family, use_hosts_file=true))] | ||
pub fn getaddrinfo(py: Python<'_>, host: String, family: AddressFamily, use_hosts_file: bool) -> PyResult<Bound<PyAny>> { | ||
pyo3_asyncio_0_21::tokio::future_into_py(py, async move { | ||
match mitmproxy::dns::getaddrinfo(host, family.into(), use_hosts_file).await { | ||
Ok(resp) => { | ||
Ok(resp.into_iter().map(|ip| ip.to_string()).collect::<Vec<String>>()) | ||
}, | ||
Err(e) => match *e.kind() { | ||
ResolveErrorKind::NoRecordsFound { response_code: ResponseCode::NXDomain, .. } => { | ||
Err(gaierror::new_err("NXDOMAIN")) | ||
} | ||
ResolveErrorKind::NoRecordsFound { response_code: ResponseCode::NoError, .. } => { | ||
Err(gaierror::new_err("NOERROR")) | ||
} | ||
#[pymethods] | ||
impl DnsResolver { | ||
pub fn lookup_ip<'py>(&self, py: Python<'py>, host: String) -> PyResult<Bound<'py, PyAny>> { | ||
let resolver = self.0.clone(); | ||
pyo3_asyncio_0_21::tokio::future_into_py(py, async move { | ||
match resolver.lookup_ip(host).await { | ||
Ok(resp) => Ok(resp | ||
.into_iter() | ||
.map(|ip| ip.to_string()) | ||
.collect::<Vec<String>>()), | ||
Err(e) => match *e.kind() { | ||
ResolveErrorKind::NoRecordsFound { | ||
response_code: ResponseCode::NXDomain, | ||
.. | ||
} => Err(gaierror::new_err("NXDOMAIN")), | ||
ResolveErrorKind::NoRecordsFound { | ||
response_code: ResponseCode::NoError, | ||
.. | ||
} => Err(gaierror::new_err("NOERROR")), | ||
_ => Err(gaierror::new_err(e.to_string())), | ||
} | ||
|
||
} | ||
}) | ||
}, | ||
} | ||
}) | ||
} | ||
} | ||
|
||
#[pyfunction] | ||
pub fn get_system_dns_servers() -> PyResult<Vec<String>> { | ||
DNS_SERVERS | ||
.clone() | ||
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{}", e))) | ||
DNS_SERVERS.clone().map_err(|e| { | ||
pyo3::exceptions::PyRuntimeError::new_err(format!("failed to get dns servers: {}", e)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.