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

Introduce Local Port Routing Builder #1392

Merged
merged 2 commits into from
Dec 5, 2019

Conversation

hosamaly
Copy link
Contributor

@hosamaly hosamaly commented Oct 9, 2019

Subsystem
ktor-server

Motivation
I'm building an application that has some functions that should be exposed on a separate port (e.g. metrics). The existing (experimental) HostRouteSelector (introduced in #835) is geared towards multi-tenant/VirtualHost-style configurations, allowing a client to send a Host header to get information that perhaps shouldn't be exposed to them.

Solution
I suggest a simple RouteSelector based on the local: RequestConnectionPoint port, whose documentation says (emphasis mine):

Contains http request and connection details such as a host name used to connect, port, scheme and so on. No proxy headers could affect it.

Sadly, some implementations don't abide by this rule. Particularly noteworthy is the TestApplicationRequest, which extracts the port from headers. I think that it should be changed to enable setting the port in the request builder, but this could be left for another PR.

@hosamaly hosamaly changed the title Introduce Local Port Route Builder Introduce Local Port Routing Builder Oct 9, 2019
@spand
Copy link
Contributor

spand commented Oct 10, 2019

I dont think its correct to check that the port is outside an ephemeral port range. I believe the range is configurable and different on many systems.

Also a separate pr maybe but I feel an ip version of the selector ought to be there also.

@hosamaly
Copy link
Contributor Author

hosamaly commented Oct 10, 2019

Thanks for the feedback, @spand. I'm happy to remove the check for ephemeral ports; perhaps it was too specific to my design decisions. I'll change this now.

As for the IP version, I agree that it's better left to a separate PR.

@hosamaly
Copy link
Contributor Author

Hi, @spand. How does this PR look now from your perspective?

*/
@KtorExperimentalAPI
fun Route.localPort(port: Int, build: Route.() -> Unit): Route {
require(port in 1..49151) { "Port $port must be a positive number within the range of non-ephemeral ports" }
require(port in 1..65535) { "Port $port must be a positive number between 1 and 65,535" }
Copy link
Contributor

Choose a reason for hiding this comment

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

Entering bike shedding territory here but afaik 0 is also valid. A quick look in InetSocketAddress has:

private static int checkPort(int port) {
        if (port < 0 || port > 0xFFFF)
            throw new IllegalArgumentException("port out of range:" + port);
        return port;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to Wikipedia, 0 is reserved. It also adds that:

in programming APIs (not in communication between hosts), [it] requests a system-allocated (dynamic) port

That wouldn't be useful as a filter.

@spand
Copy link
Contributor

spand commented Oct 23, 2019

I put in a comment but looks good to me.. though I am just a nobody in this project :)

@cy6erGn0m cy6erGn0m merged commit 85306df into ktorio:1.2.x Dec 5, 2019
@hosamaly hosamaly deleted the local-port-selector branch December 5, 2019 12:22
cy6erGn0m pushed a commit that referenced this pull request Dec 27, 2019
* Introduce Local Port Route Builder

See discussion at:
#1392 (comment)
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.

3 participants