Skip to content

Commit c24d902

Browse files
committed
fix: allow port parameter matching
1 parent 44bf2d1 commit c24d902

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/utils/__tests__/matchAndRewriteRoute.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ describe('matchAndRewriteURL', () => {
232232
expect(url4.toString()).toEqual('https://localhost/googleapis/foo/v1/test:url?key=abc123');
233233
});
234234

235+
it('Matches port parameters and remaps them properly', () => {
236+
const url = attemptRemap({
237+
url: new URL('https://foo.domain.com:4567/'),
238+
mappings: [{prefix: '/domain/{subdomain}/{port}', target: '{subdomain}.domain.com:{port}'}],
239+
});
240+
expect(url.toString()).toEqual('https://localhost/domain/foo/4567/');
241+
});
242+
243+
it('Does not remove port when url is not matched', () => {
244+
const url = attemptRemap({
245+
url: new URL('https://foo.domain.com:4567/'),
246+
mappings: [],
247+
});
248+
expect(url.toString()).toEqual('https://foo.domain.com:4567/');
249+
});
250+
235251
it('Applies the /.proxy/ mapping without any mappings', () => {
236252
const url = attemptRemap({
237253
url: new URL('https://1234567890.discordsays.com/api/token'),

src/utils/url.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function regexFromTarget(target: string): RegExp {
1515
return new RegExp(`${regexString}(/|$)`);
1616
}
1717

18+
function removePortParameterFromTarget(url: string): string {
19+
return url.replace(/:\{[^}]+\}/g, '');
20+
}
21+
1822
export interface MatchAndRewriteURLInputs {
1923
originalURL: URL;
2024
prefixHost: string;
@@ -32,15 +36,17 @@ export interface MatchAndRewriteURLInputs {
3236
* @returns null if URL doesn't match prefix, otherwise return rewritten URL
3337
*/
3438
export function matchAndRewriteURL({originalURL, prefix, prefixHost, target}: MatchAndRewriteURLInputs): URL | null {
35-
// coerce url with filler https protocol so we can retrieve host and pathname from target
36-
const targetURL = new URL(`https://${target}`);
39+
// Remove port parameter from target and coerce url with filler https protocol so we can retrieve host and pathname from target
40+
const targetURL = new URL(`https://${removePortParameterFromTarget(target)}`);
3741
// Depending on the environment, the URL constructor may turn `{` and `}` into `%7B` and `%7D`, respectively
38-
const targetRegEx = regexFromTarget(targetURL.host.replace(/%7B/g, '{').replace(/%7D/g, '}'));
42+
const targetRegEx = regexFromTarget(target.replace(/%7B/g, '{').replace(/%7D/g, '}'));
3943
const match = originalURL.toString().match(targetRegEx);
4044
// Null match indicates that this target is not relevant
4145
if (match == null) return originalURL;
4246
const newURL = new URL(originalURL.toString());
4347
newURL.host = prefixHost;
48+
// Remove port from new url (discord activities proxy doesn't listen on custom ports)
49+
newURL.port = '';
4450
newURL.pathname = prefix.replace(SUBSTITUTION_REGEX, (_, matchName) => {
4551
const replaceValue = match.groups?.[matchName];
4652
if (replaceValue == null) throw new Error('Misconfigured route.');

0 commit comments

Comments
 (0)