This repository was archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Implement /dns and /resolve #353
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
520b9dd
feat: implement /resolve
ljedrz 1c9ac1a
feat: implement /dns
ljedrz 8aee359
fix: enable domain names not starting with /ipns/ to be resolved with…
ljedrz ce88339
feat: enable recursive /dns
ljedrz a29f3d3
refactor: reuse an existing resolve function
ljedrz 9d76f13
temp: preserve the DNS error values
ljedrz bef69b1
fix: remove the panics from dag resolution methods
ljedrz 6825c4e
fix: use a HashSet to avoid loops when resolving recursively
ljedrz 3174225
fix: make DNS resolution work in Windows
ljedrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,90 @@ | ||
use crate::v0::support::{with_ipfs, StringError, StringSerialized}; | ||
use ipfs::{Ipfs, IpfsPath, IpfsTypes}; | ||
use serde::{Deserialize, Serialize}; | ||
use warp::{query, Filter, Rejection, Reply}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct ResolveQuery { | ||
// the name to resolve | ||
arg: StringSerialized<IpfsPath>, | ||
#[serde(rename = "dht-record-count")] | ||
dht_record_count: Option<usize>, | ||
#[serde(rename = "dht-timeout")] | ||
dht_timeout: Option<String>, | ||
} | ||
|
||
pub fn resolve<T: IpfsTypes>( | ||
ipfs: &Ipfs<T>, | ||
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { | ||
with_ipfs(ipfs) | ||
.and(query::<ResolveQuery>()) | ||
.and_then(resolve_query) | ||
} | ||
|
||
async fn resolve_query<T: IpfsTypes>( | ||
ipfs: Ipfs<T>, | ||
query: ResolveQuery, | ||
) -> Result<impl Reply, Rejection> { | ||
let ResolveQuery { arg, .. } = query; | ||
let name = arg.into_inner(); | ||
let path = ipfs | ||
.resolve_ipns(&name, false) | ||
.await | ||
.map_err(StringError::from)? | ||
.to_string(); | ||
|
||
let response = ResolveResponse { path }; | ||
|
||
Ok(warp::reply::json(&response)) | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct ResolveResponse { | ||
path: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct DnsQuery { | ||
// the name to resolve | ||
arg: String, | ||
recursive: Option<bool>, | ||
} | ||
|
||
pub fn dns<T: IpfsTypes>( | ||
ipfs: &Ipfs<T>, | ||
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { | ||
with_ipfs(ipfs).and(query::<DnsQuery>()).and_then(dns_query) | ||
} | ||
|
||
async fn dns_query<T: IpfsTypes>(ipfs: Ipfs<T>, query: DnsQuery) -> Result<impl Reply, Rejection> { | ||
let DnsQuery { arg, recursive } = query; | ||
// attempt to parse the argument prepended with "/ipns/" if it fails to parse like a compliant | ||
// IpfsPath and there is no leading slash | ||
let path = if !arg.starts_with('/') { | ||
if let Ok(parsed) = arg.parse() { | ||
Ok(parsed) | ||
} else { | ||
format!("/ipns/{}", arg).parse() | ||
} | ||
} else { | ||
arg.parse() | ||
} | ||
.map_err(StringError::from)?; | ||
|
||
let path = ipfs | ||
.resolve_ipns(&path, recursive.unwrap_or(false)) | ||
.await | ||
.map_err(StringError::from)? | ||
.to_string(); | ||
|
||
let response = DnsResponse { path }; | ||
|
||
Ok(warp::reply::json(&response)) | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct DnsResponse { | ||
path: String, | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this one gave me a bit of a headache - the compiler was pointing to a very long issue with the HTTP warp call, but this was actually the root cause of the problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wonder if the future should be just written open as
async fn
to be simpler in every way, but haven't tried this myself ... for a while at least. Though, there's nothing obvious causing this, perhaps the path inside domain-resolve is just so long.