-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check every matching endpoint for allowed IPs
- Loading branch information
Showing
5 changed files
with
117 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Endpoint } from "../agent/Config"; | ||
import { Context } from "../agent/Context"; | ||
import { tryParseURLPath } from "./tryParseURLPath"; | ||
|
||
export type LimitedContext = Pick<Context, "url" | "method" | "route">; | ||
|
||
export function matchEndpoints(context: LimitedContext, endpoints: Endpoint[]) { | ||
const matches: Endpoint[] = []; | ||
|
||
if (!context.method) { | ||
return matches; | ||
} | ||
|
||
const possible = endpoints.filter((endpoint) => { | ||
if (endpoint.method === "*") { | ||
return true; | ||
} | ||
|
||
return endpoint.method === context.method; | ||
}); | ||
|
||
const exact = possible.find((endpoint) => endpoint.route === context.route); | ||
if (exact) { | ||
matches.push(exact); | ||
} | ||
|
||
if (context.url) { | ||
// req.url is relative, so we need to prepend a host to make it absolute | ||
// We just match the pathname, we don't use the host for matching | ||
const path = tryParseURLPath(context.url); | ||
const wildcards = possible | ||
.filter((endpoint) => endpoint.route.includes("*")) | ||
.sort((a, b) => { | ||
// Sort endpoints based on the amount of * in the route | ||
return b.route.split("*").length - a.route.split("*").length; | ||
}); | ||
|
||
if (path) { | ||
for (const wildcard of wildcards) { | ||
const regex = new RegExp( | ||
`^${wildcard.route.replace(/\*/g, "(.*)")}\/?$`, | ||
"i" | ||
); | ||
|
||
if (regex.test(path)) { | ||
matches.push(wildcard); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return matches; | ||
} |
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