-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
594d8a9
commit 4eb076e
Showing
5 changed files
with
89 additions
and
1 deletion.
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
73 changes: 73 additions & 0 deletions
73
crates/erc20_payment_lib_common/src/dns_over_https_resolver.rs
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,73 @@ | ||
use reqwest; | ||
use serde; | ||
use std::env; | ||
use std::io::ErrorKind as IoErrorKind; | ||
|
||
#[derive(serde::Deserialize)] | ||
struct DnsOverHttpsResponse { | ||
#[serde(rename(deserialize = "Answer"))] | ||
pub answer: Vec<DnsOverHttpsAnswer>, | ||
} | ||
|
||
#[derive(serde::Deserialize)] | ||
struct DnsOverHttpsAnswer { | ||
pub data: String, | ||
} | ||
|
||
fn strip_quotes(data: &String) -> String { | ||
let v1 = match data.strip_prefix('"') { | ||
None => data.as_str(), | ||
Some(x) => x, | ||
}; | ||
String::from(match v1.strip_suffix('"') { | ||
None => v1, | ||
Some(x) => x, | ||
}) | ||
} | ||
|
||
pub enum DnsOverHttpsServer { | ||
Google, | ||
Cloudflare, | ||
} | ||
|
||
impl DnsOverHttpsServer { | ||
pub fn get_dns_url(self: &DnsOverHttpsServer) -> &str { | ||
match self { | ||
DnsOverHttpsServer::Google => "https://dns.google/resolve", | ||
DnsOverHttpsServer::Cloudflare => "https://cloudflare-dns.com/dns-query", | ||
} | ||
} | ||
} | ||
|
||
pub async fn resolve_dns_record_https( | ||
record: &str, | ||
record_type: &str, | ||
dns_server: DnsOverHttpsServer, | ||
) -> std::io::Result<Vec<String>> { | ||
let result = reqwest::Client::new() | ||
.get(dns_server.get_dns_url()) | ||
.query(&[("name", record), ("type", record_type)]) | ||
.header(reqwest::header::ACCEPT, "application/dns-json") | ||
.send() | ||
.await | ||
.map_err(|_| std::io::Error::new(IoErrorKind::Other, "Couldn't fetch DNS record."))? | ||
.json::<DnsOverHttpsResponse>() | ||
.await | ||
.map_err(|_| std::io::Error::new(IoErrorKind::Other, "Couldn't fetch DNS record."))? | ||
.answer | ||
.iter() | ||
.map(|a| strip_quotes(&a.data)) | ||
.collect(); | ||
Ok(result) | ||
} | ||
|
||
pub async fn resolve_txt_record_to_string_array_https( | ||
record: &str, | ||
dns_server: DnsOverHttpsServer, | ||
) -> std::io::Result<Vec<String>> { | ||
resolve_dns_record_https(record, "TXT", dns_server).await | ||
} | ||
|
||
pub fn should_use_dns_over_https() -> bool { | ||
matches!(env::var("YA_USE_HTTPS_DNS_RESOLVER"), Ok(value) if value == "1") | ||
} |
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,4 +1,5 @@ | ||
mod db; | ||
pub mod dns_over_https_resolver; | ||
pub mod error; | ||
mod events; | ||
mod metrics; | ||
|
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