chylex / Discord-History-Tracker

Desktop app & browser script that saves Discord chat history into a file, and an offline viewer that displays the file.
https://dht.chylex.com
MIT License
475 stars 83 forks source link

Add CORS for Canary and PTB #196

Closed Matojeje closed 2 years ago

Matojeje commented 2 years ago

tl;dr Non-stable releases of Discord throw a hissy fit about CORS with the DHT internal server. It's because the app's (sub)domain is different - like https://canary.discord.com, and that's not included in this one file, so I modified it.


The problem

I kept getting the following console errors (on Desktop/Chromium and Firefox respectively) whenever I pressed Start Tracking:

Access to fetch at 'http://127.0.0.1:50000/track-channel' from origin 'https://canary.discord.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

POST http://127.0.0.1:50000/track-channel net::ERR_FAILED {status: "ERROR", message: "Failed to fetch"}

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:50000/track-channel. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Status code: (204). [Learn more]

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:50000/track-channel. (Reason: CORS request did not succeed). Status code: (null). [Learn more]

Object { status: "ERROR", message: "NetworkError when attempting to fetch resource." }

So I looked into the tracking script, but none of my attempts to fix it worked. After a bit more research, it turns out the problem is on the server side.

The fix

I'm not very experienced with C#, so I referred to this) to make sure that WithOrigins()'s parameters are just an array. I hope that's right.

Unfortunately I wasn't able to test out my code changes, since I kept running into problems with packages despite having the right version of the SDK, even trying to install them manually didn't work, but I'm sure that's just a skill issue on my part.

The only error code I kept getting was NU1100, saying Unable to resolve followed by whatever's included in each .csproj file as a <PackageReference>.

I didn't have time to look into this further to try and fix my dependencies, because I had to back up a server that was about to get deleted on a short notice. So I ran the tracker in this way instead:

The workaround

The current workaround for this CORS conundrum - without changing anything in DHT's code - is simply using the stable Discord web app or desktop client for running the tracking script. I went ahead and tested different release channels to see where the CORS stuff is okay:

So it's clear that the problem was just the domain of the Discord app, and I'm pretty sure this will fix it.

(I haven't tested if the CORS error occurs in the development version of desktop Discord because I had no clue that was even a thing until today.)

Also, would it be possible to use something like wildcards for this? (such as https://*.discord.com) I'm not sure if the ASP.NET builder stuff supports it, but it might be possible by just writing the response headers manually.

chylex commented 2 years ago

Thanks!

Also, would it be possible to use something like wildcards for this?

I will look into the possibility of using wildcards, but I'm wary of allowing all possible subdomains because some of them serve user-created content (i.e. cdn.discordapp.com).

Speaking off, it might be good to add the additional subdomains for both discord.com and discordapp.com, since the reason I added discordapp.com in the first place was because someone's Discord was still connecting to it instead of the new domain.

I will investigate and make the changes in your branch before I merge it.

chylex commented 2 years ago

Looks good, I'll just reformat the list of domains since it's quite a lot of them at this point and the line of code is getting long.

Side note: If you have the time and interest, please post more information about the errors when you tried to build the project. I want to make sure it's as easy as possible for people to fork and build the project, so I'd like to know if the error is something I can fix on my end or write documentation for.