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: rewrite requests through /.proxy/ using patchUrlMappings #222

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/nested-messages/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
</head>
<body>
<div>Parent iframe</div>
<iframe id="child-iframe" src="/nested" width="250px" height="250px"></iframe>
<iframe id="child-iframe" src="/.proxy/nested" width="250px" height="250px"></iframe>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const start = async () => {
});

// Get guild specific nickname and avatar, and fallback to user name and avatar
const guildMember = await fetch(`/discord/api/users/@me/guilds/${discordSdk.guildId}/member`, {
const guildMember = await fetch(`https://discord.com/api/users/@me/guilds/${discordSdk.guildId}/member`, {
method: 'get',
headers: {Authorization: `Bearer ${access_token}`},
})
Expand Down
56 changes: 35 additions & 21 deletions src/utils/__tests__/matchAndRewriteRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ describe('matchAndRewriteURL', () => {
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/',
result: 'https://123456789012345678.discordsays.com/.proxy/',
},
{
originalURL: new URL('https://discord.com/test/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/test/?foo=bar',
result: 'https://123456789012345678.discordsays.com/.proxy/test/?foo=bar',
},
{
originalURL: new URL('https://discord.com/test?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/test?foo=bar',
result: 'https://123456789012345678.discordsays.com/.proxy/test?foo=bar',
},
{
originalURL: new URL('wss://discord.com/test/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'wss://123456789012345678.discordsays.com/test/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/test/?foo=bar',
},
{
originalURL: new URL('wss://discord.com/test?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com',
result: 'wss://123456789012345678.discordsays.com/test?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/test?foo=bar',
},
{
originalURL: new URL('wss://discord.com/foo/bar/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com/foo/bar',
result: 'wss://123456789012345678.discordsays.com/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/?foo=bar',
},
{
originalURL: new URL('wss://discord.com/foo/bar/test/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/',
target: 'discord.com/foo/bar',
result: 'wss://123456789012345678.discordsays.com/test/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/test/?foo=bar',
},
];
for (const {result, ...rest} of TEST_CASES) {
Expand All @@ -68,49 +68,49 @@ describe('matchAndRewriteURL', () => {
prefixHost: '123456789012345678.discordsays.com',
prefix: '/discord',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/discord/',
result: 'https://123456789012345678.discordsays.com/.proxy/discord/',
},
{
originalURL: new URL('https://discord.com/test?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/discord',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/discord/test?foo=bar',
result: 'https://123456789012345678.discordsays.com/.proxy/discord/test?foo=bar',
},
{
originalURL: new URL('https://discord.com/test/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/discord',
target: 'discord.com',
result: 'https://123456789012345678.discordsays.com/discord/test/?foo=bar',
result: 'https://123456789012345678.discordsays.com/.proxy/discord/test/?foo=bar',
},
{
originalURL: new URL('wss://discord.com/test?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/discord',
target: 'discord.com',
result: 'wss://123456789012345678.discordsays.com/discord/test?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/discord/test?foo=bar',
},
{
originalURL: new URL('wss://discord.com/test/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/discord',
target: 'discord.com',
result: 'wss://123456789012345678.discordsays.com/discord/test/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/discord/test/?foo=bar',
},
{
originalURL: new URL('wss://test-hyphen.discord.com/baz/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/foo/{parameter}',
target: '{parameter}.discord.com/baz',
result: 'wss://123456789012345678.discordsays.com/foo/test-hyphen/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/foo/test-hyphen/?foo=bar',
},
{
originalURL: new URL('wss://test-hyphen.discord.com/baz/bar/?foo=bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/foo/{parameter}',
target: '{parameter}.discord.com/baz',
result: 'wss://123456789012345678.discordsays.com/foo/test-hyphen/bar/?foo=bar',
result: 'wss://123456789012345678.discordsays.com/.proxy/foo/test-hyphen/bar/?foo=bar',
},
];
for (const {result, ...rest} of TEST_CASES) {
Expand All @@ -126,21 +126,21 @@ describe('matchAndRewriteURL', () => {
prefixHost: '123456789012345678.discordsays.com',
prefix: '/foo/{parameter}',
target: '{parameter}.discord.com',
result: 'https://123456789012345678.discordsays.com/foo/test/',
result: 'https://123456789012345678.discordsays.com/.proxy/foo/test/',
},
{
originalURL: new URL('https://test.discord.com/bar'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/foo/{parameter}',
target: '{parameter}.discord.com',
result: 'https://123456789012345678.discordsays.com/foo/test/bar',
result: 'https://123456789012345678.discordsays.com/.proxy/foo/test/bar',
},
{
originalURL: new URL('https://test1-hyphen.discord.com/test2-hyphen/'),
prefixHost: '123456789012345678.discordsays.com',
prefix: '/foo/{parameter}',
target: '{parameter}.discord.com',
result: 'https://123456789012345678.discordsays.com/foo/test1-hyphen/test2-hyphen/',
result: 'https://123456789012345678.discordsays.com/.proxy/foo/test1-hyphen/test2-hyphen/',
},
];

Expand Down Expand Up @@ -202,6 +202,20 @@ describe('matchAndRewriteURL', () => {
expect(url4.toString()).toEqual('https://localhost/googleapis/foo/v1/test:url?key=abc123');
});

it('Applies the /.proxy/ mapping without any mappings', () => {
const url = attemptRemap({
url: new URL('https://1234567890.discordsays.com/api/token'),
mappings: [],
});
expect(url.toString()).toEqual('https://1234567890.discordsays.com/.proxy/api/token');

const base = attemptRemap({
url: new URL('https://1234567890.discordsays.com/'),
mappings: [],
});
expect(base.toString()).toEqual('https://1234567890.discordsays.com/.proxy/');
});

it("Doesn't apply trailing slash to complete filenames", () => {
const prefixHost = '123456789012345678.discordsays.com';
const target = 'domain.com';
Expand All @@ -211,28 +225,28 @@ describe('matchAndRewriteURL', () => {
prefixHost,
prefix: '/',
target,
result: 'https://123456789012345678.discordsays.com/file/music.mp3',
result: 'https://123456789012345678.discordsays.com/.proxy/file/music.mp3',
},
{
originalURL: new URL('https://domain.com/file/text.txt'),
prefixHost,
prefix: '/',
target,
result: 'https://123456789012345678.discordsays.com/file/text.txt',
result: 'https://123456789012345678.discordsays.com/.proxy/file/text.txt',
},
{
originalURL: new URL('https://domain.com/a.b/c.html'),
prefixHost,
prefix: '/path/to/files',
target,
result: 'https://123456789012345678.discordsays.com/path/to/files/a.b/c.html',
result: 'https://123456789012345678.discordsays.com/.proxy/path/to/files/a.b/c.html',
},
{
originalURL: new URL('https://domain.com/file/scene.bundle'),
prefixHost,
prefix: '/',
target,
result: 'https://123456789012345678.discordsays.com/file/scene.bundle',
result: 'https://123456789012345678.discordsays.com/.proxy/file/scene.bundle',
},
];

Expand Down
8 changes: 6 additions & 2 deletions src/utils/patchUrlMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ function attemptSetNodeSrc(node: Node, mappings: Mapping[]) {
}

export function attemptRemap({url, mappings}: RemapInput): URL {
const newURL = new URL(url.toString());
if (newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) {
newURL.pathname = '/.proxy' + newURL.pathname;
}
for (const mapping of mappings) {
const mapped = matchAndRewriteURL({
originalURL: url,
originalURL: newURL,
prefix: mapping.prefix,
target: mapping.target,
prefixHost: window.location.host,
Expand All @@ -142,5 +146,5 @@ export function attemptRemap({url, mappings}: RemapInput): URL {
return mapped;
}
}
return url;
return newURL;
}
4 changes: 4 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function matchAndRewriteURL({originalURL, prefix, prefixHost, target}: Ma

// Append the original path
newURL.pathname += newURL.pathname === '/' ? originalURL.pathname.slice(1) : originalURL.pathname;
// prepend /.proxy/ to path if using discord activities proxy
if (newURL.hostname.includes('discordsays.com') || newURL.hostname.includes('discordsez.com')) {
newURL.pathname = '/.proxy' + newURL.pathname;
}
// Remove the target's path from the new url path
newURL.pathname = newURL.pathname.replace(targetURL.pathname, '');
// Add a trailing slash if original url had it, and if it doesn't already have one or if matches filename regex
Expand Down