@@ -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+
1822export 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 */
3438export 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 ( / % 7 B / g, '{' ) . replace ( / % 7 D / g, '}' ) ) ;
42+ const targetRegEx = regexFromTarget ( target . replace ( / % 7 B / g, '{' ) . replace ( / % 7 D / 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