Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Better support of Gateway Subdomain spec #546

Merged
merged 4 commits into from
Dec 15, 2022

Conversation

ppodolsky
Copy link
Contributor

@ppodolsky ppodolsky commented Nov 23, 2022

Added basic subdomain support by uplifting parameters extraction from Host header required for handlers.
DnsLink part of spec is not supported in this PR.

Related to n0-computer/beetle#78

Spec:

  • HEAD, GET methods
  • DNSLink support
  • Redirection from Path methods to Subdomain methods (behaviour is configured by option)
  • X-Forwarded-Host support
  • X-Forwarded-Proto support

Current PR won't cover URI router, I believe it is better to implement in a separate PR.

@ppodolsky ppodolsky changed the title [WIP] [feat] Support subdomains [feat] Support subdomains Nov 23, 2022
@ppodolsky ppodolsky changed the title [feat] Support subdomains [feat] Support parsing Host header for aligning with Gateway Subdomain spec Nov 23, 2022
@b5 b5 added the c-gateway label Nov 23, 2022
@b5 b5 requested a review from Arqu November 23, 2022 14:27
@ppodolsky ppodolsky changed the title [feat] Support parsing Host header for aligning with Gateway Subdomain spec [feat] Better support of Gateway Subdomain spec Nov 23, 2022
@ppodolsky ppodolsky changed the title [feat] Better support of Gateway Subdomain spec feat: Better support of Gateway Subdomain spec Nov 24, 2022
@ppodolsky
Copy link
Contributor Author

Can't figure out if we should do anything here with the note from spec:

Note: Base36 must be used to ensure CIDv1 with ED25519 fits in a single DNS label (63 characters).

Decode and then encode IPNS name? Or what?

let res = do_request(
"GET",
&format!("localhost:{}", test_setup.gateway_addr.port()),
"/ipns/k51qzi5uqu5dlvj2baxnqndepeb86cbk3ng7n3i46uzyxzyqj2xjonzllnv0v8",
Copy link

@lidel lidel Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: needs a separate test for legacy notation that was not a valid CID (e.g., 12D3KooWJHxkQKX8C5KAyqEPhn2ssT2in4TExyG9SXxi519tycL9)
See explainer in multiformats/cid-utils-website#48

This is important feature of subdomain gateway: when a request is made to example.com/ipns/id, subdomain set up on example.com will return HTTP 301 redirect to proper subdomain.
Before redirect is made, id is converted to cidv1-libp2p-key-base36.

Copy link
Contributor Author

@ppodolsky ppodolsky Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

I had to add nasty from_peer_id function for dealing with discrepancy between multihash:0.16 and multihash:0.17. This func may be safely deleted after updating to multihash:0.17

cc @dignifiedquire Would you mind to release multiaddr 0.17 for bringing new multihash crate to rust-libp2p?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vmx what't the status here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dignifiedquire I'm not maintaining rust-multiaddr, that's a question for @mxinden :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kicked off release process here. Please approve. multiformats/rust-multiaddr#66

let res = do_request(
"GET",
&format!("localhost:{}", test_setup.gateway_addr.port()),
"/ipns/ipfs-subdomain.ipfs-domain.eth",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change to en.wikipedia-on-ipfs.org? this is our usual test case for handling - (i suggest avoiding using .eth in tests unrelated to ENS interop).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Btw, while I was implementing code around - processing with regular expressions I had faced that it is hard or even impossible to properly process cases like aaaa-b-aaaa.com (a single symbol between 2 dashes) because captured groups were overlapping there (a-b and b-a with b belonging to both groups) and it is forbidden in Regex crate.

I mean, it may be useful to consider in E2E and other tests this case too cause it belongs to a separate equivalence class.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw Kubo also started with regex, realized it comes with footguns, and replaced with a plain function :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it doesn't bring extra crate. You are right, dunno why I've decided regex would be better. It might be useful for complicated cases or expected future changes in format, but replacing dashes are definitely not this case.

iroh-gateway/src/core.rs Show resolved Hide resolved
Comment on lines 4 to 6
pub static SUBDOMAIN_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(?P<cid_or_domain>[A-Za-z\d\-.]+)\.(?P<scheme>ip(?:ns|fs))\.(?P<hostname>[^.]+)$")
.unwrap()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure there are no regressions in the future, add tests for edge cases where hostname is the same as scheme, e.g.:

bafy.ipfs.ipfs.com
bafy.ipns.ipfs.com
bafy.ipns.ipns.com
bafy.ipns.ipfs.com

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -47,6 +47,25 @@ pub struct Block {
links: Vec<Cid>,
}

// ToDo: Replace to PeerId::from_str after updating multihash crate to 0.17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have this handled before we merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a moment to fix it, but we should decide who will release multiaddr 0.17 and bring it through rust-libp2p and possibly other deps

Copy link
Collaborator

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In regards to the gateway changes, this looks great. Regex is a bit scary looking.
Only thing I think we should do before the merge is resolve the multihash dependency.

@ppodolsky
Copy link
Contributor Author

In regards to the gateway changes, this looks great. Regex is a bit scary looking. Only thing I think we should do before the merge is resolve the multihash dependency.

No need to scare, I replace regex with function :)

@ppodolsky
Copy link
Contributor Author

Almost done, but should not merge until I set proper version of libp2p in Cargo.toml

@dignifiedquire
Copy link
Contributor

I would love to merge this sooner rather than later, but I am not sure how long another libp2p release is out

@ppodolsky
Copy link
Contributor Author

We may create an issue for cleaning things out after libp2p release and merge this PR as is. If it is ok, I'll prepare proper commits tomorrow

@dignifiedquire
Copy link
Contributor

We may create an issue for cleaning things out after libp2p release and merge this PR as is. If it is ok, I'll prepare proper commits tomorrow

Yeah I think this makes the most sense, appreciated

@ppodolsky ppodolsky force-pushed the feature/subdomain-support branch 3 times, most recently from c9f6b0e to db13ab9 Compare December 14, 2022 10:03
@ppodolsky
Copy link
Contributor Author

ppodolsky commented Dec 14, 2022

We may create an issue for cleaning things out after libp2p release and merge this PR as is. If it is ok, I'll prepare proper commits tomorrow

Yeah I think this makes the most sense, appreciated

Done. I've created reminding issue with "What-to-do" description
n0-computer/beetle#47

@ppodolsky ppodolsky force-pushed the feature/subdomain-support branch 3 times, most recently from 5053dfe to 1dd0e23 Compare December 14, 2022 12:54
Cargo.toml Outdated
@@ -45,3 +45,6 @@ incremental = false

[workspace.metadata.release]
consolidate-commits = true

[workspace.dependencies]
libp2p = { version = "0.50" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this from this PR here please, see discussion on #595

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ppodolsky ppodolsky force-pushed the feature/subdomain-support branch 3 times, most recently from 6596d4e to 1e07829 Compare December 15, 2022 07:05
Copy link
Collaborator

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some very minor comments.
Need to get CI to pass, otherwise I'm happy to merge.

@@ -48,6 +52,9 @@ pub struct Config {
/// set of user provided headers to attach to all responses
#[serde(with = "http_serde::header_map")]
pub headers: HeaderMap,
/// Redirects to subdomains for path requests
#[serde(default = "set_true")]
pub redirect_to_subdomain: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should default this to false. This requires additional config on the DNS side and making sure certificates are valid for anyone hosting it. Since there's the burden of configuration anyways, we should have this off by default and those interested can then just enable it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -1,6 +1,8 @@
use axum::http::{header::HeaderName, HeaderValue};

// Headers
pub static HEADER_X_FORWARDED_HOST: HeaderName = HeaderName::from_static("x-forwarded-host");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overlaps with #588, we should get both of these in. Think same tests are affected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this one firstly and then I'll rebase and refine left one

pub format: ResponseFormat,
pub cid: CidOrDomain,
pub resolved_path: iroh_resolver::resolver::Path,
pub query_file_name: String,
pub download: bool,
pub query_params: GetParams,
pub is_subdomain_mode: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, can we make this subdomain_mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -46,27 +53,56 @@ use crate::{
templates::{icon_class_name, ICONS_STYLESHEET, STYLESHEET},
};

enum RequestPreprocessingResult {
ResponseImmediately(GatewayResponse),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to RespondImmediately is a little bit more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ppodolsky
Copy link
Contributor Author

Argh, something happened during rebase, let me check what is wrong with the test

@ppodolsky
Copy link
Contributor Author

Fixed tests

Copy link
Collaborator

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Arqu Arqu merged commit dfe3134 into n0-computer:main Dec 15, 2022
ppodolsky added a commit to izihawa/iroh that referenced this pull request Dec 28, 2022
* [feat] Support subdomains

* pr: fixes

* pr: fix test

* fix: remove mut in iroh/src/run.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants