Skip to content

Commit

Permalink
something something idk
Browse files Browse the repository at this point in the history
  • Loading branch information
RoootTheFox committed Aug 23, 2022
1 parent 069b4ba commit b6bbc9a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 25 deletions.
111 changes: 96 additions & 15 deletions discord-proxy.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
from textwrap import indent
from mitmproxy import http
from bs4 import BeautifulSoup
import json
import json
import os
import re
import requests

pronoundb_pronouns = {
"unspecified": "Unspecified",
"hh": "he/him",
"hi": "he/it",
"hs": "he/she",
"ht": "he/they",
"ih": "it/him",
"ii": "it/its",
"is": "it/she",
"it": "it/they",
"shh": "she/he",
"sh": "she/her",
"si": "she/it",
"st": "she/they",
"th": "they/he",
"ti": "they/it",
"ts": "they/she",
"tt": "they/them",
"any": "Any pronouns",
"other": "Other pronouns",
"ask": "Ask me my pronouns",
"avoid": "Avoid pronouns, use my name"
}
def startswith_discord_endpoint(url, endpoint):
match = re.search("^https://(canary.discord.com|discord.com|ptb.discord.com)/", url)
if match is not None:
url_endpoint = str(url).replace(str(match.group(0)), "", 1)
if url_endpoint.startswith(endpoint):
return True
else:
return False
else:
return False

def startswith_discord_api_endpoint(url, endpoint):
match = re.search("^https://(canary.discord.com|discord.com|ptb.discord.com)/api/v\d?\d/", url)
if match is not None:
url_endpoint = str(url).replace(str(match.group(0)), "", 1)
if url_endpoint.startswith(endpoint):
return True
else:
return False
else:
return False

def response(flow: http.HTTPFlow):
flow.response.headers["content-security-policy"] = "default-src * 'unsafe-inline' 'unsafe-eval' data:; img-src * blob: data:; media-src * blob: data:"
flow.response.headers["access-control-allow-origin"] = "*"
startswith_discord_endpoint(flow.request.pretty_url, "")

flow.response.headers["content-security-policy"] = "default-src * 'unsafe-inline' 'unsafe-eval' data:; " \
"img-src * blob: data:; media-src * blob: data: "
if "content-type" in flow.response.headers:
if flow.response.headers["content-type"] == "text/html" and (flow.request.pretty_url.startswith("https://discord.com/app") or flow.request.pretty_url.startswith("https://discord.com/channels")):
if flow.response.headers["content-type"] == "text/html" and (startswith_discord_endpoint(flow.request.pretty_url, "app") or startswith_discord_endpoint(flow.request.pretty_url, "channels")):
print("Received Discord App Response!")
print("Parsing HTML")
content = flow.response.content
soup = BeautifulSoup(content, 'html.parser')

print("Loading script")
script_file = open("loader.js")
script = script_file.read()
Expand All @@ -25,8 +74,38 @@ def response(flow: http.HTTPFlow):
soup.find("head").append(tag)

flow.response.text = soup.prettify()
# https://canary.discord.com/api/v9/users/512242962407882752/profile?with_mutual_guilds=false&guild_id=264801645370671114
if startswith_discord_api_endpoint(flow.request.pretty_url, "users/") and flow.request.pretty_url.__contains__("/profile"):
url = flow.request.pretty_url

match = re.search("users/(\d{8,20})/profile", url)
data = json.loads(flow.response.content.decode())
#data["user"]["avatar_decoration"] = "rainbow"
if match is not None and match.groupdict().__sizeof__() > 1:
print("profile request!!!")
user_id = match.group(1)
print("user id: " + user_id)

pndb_response = requests.get("https://pronoundb.org/api/v1/lookup", params={'platform': 'discord', 'id':user_id})
print("pndb: " + pndb_response.text)
if pndb_response.status_code == 200:
print("has pronoundb entry!")
pndb_data = json.loads(pndb_response.text)
pndb_entry = pndb_data["pronouns"]
print("entry: " + pndb_entry)
pronouns = pronoundb_pronouns.get(pndb_entry)
if pronouns != "Unspecified":
data["user_profile"]["pronouns"] = pronouns

flow.response.text = json.dumps(data)

def request(flow: http.HTTPFlow):
#if flow.request.pretty_url.startswith("https://cdn.discordapp.com/avatar-decorations/"):
# deco_file = open("rainbow.png", mode="rb")
# deco = deco_file.read()
# deco_file.close()
# flow.response = http.Response.make(200, deco, {"Access-Control-Allow-Origin": "*"})

if flow.request.pretty_url.startswith("https://discord.com/themeldr"):
# currently unused, might use this to make installing the loader easier in case injecting js at load is against discord tos
if flow.request.pretty_url == "https://discord.com/themeldr/getldr":
Expand All @@ -38,48 +117,50 @@ def request(flow: http.HTTPFlow):
loader,
{"Content-Type": "text/javascript"}
)
flow.response.headers["access-control-allow-origin"] = "*"
if flow.request.pretty_url == "https://discord.com/themeldr/modifytheme":
data = flow.request.content.decode('utf-8')
data_json = json.loads(data)
print("Theme: " + data_json["name"], "Enabled: " + str(data_json["enabled"]))

datastore = {}
if os.path.exists("datastore.json"):
datastore_file = open("datastore.json", "r")
datastore = json.load(datastore_file)
else:
print ("no datastore.json")
datastore = {}

print(datastore)
if "themes" not in datastore:
print("no themes in datastore")
datastore["themes"] = []

already_indexed = False
for i in range(len(datastore["themes"])):
if datastore["themes"][i]["name"] == data_json["name"]:
already_indexed = True
datastore["themes"][i]["enabled"] = data_json["enabled"]
datastore["themes"][i]["url"] = data_json["url"]
datastore["themes"][i]["data"] = data_json["data"]

if not already_indexed:
datastore["themes"].append({"name": data_json["name"], "enabled": data_json["enabled"], "url": data_json["url"], "data": data_json["data"]})
with open('datastore.json', 'w') as outfile:

with open('datastore.json', 'w') as outfile:
json.dump(datastore, outfile, indent=4)

flow.response = http.Response.make(
200,
"",
{"Content-Type": "text/plain"}
)

flow.response.headers["access-control-allow-origin"] = "*"

if flow.request.pretty_url == "https://discord.com/themeldr/getthemes":
if os.path.exists("datastore.json"):
datastore_file = open("datastore.json", "r")
datastore = json.load(datastore_file)
flow.response = http.Response.make(200, json.dumps(datastore["themes"], indent=4), {"Content-Type": "application/json"})
flow.response = http.Response.make(200, json.dumps(datastore["themes"], indent=4), {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"})
else:
flow.response = http.Response.make(200,"[]",{"Content-Type": "application/json"})
flow.response = http.Response.make(200,"[]",{"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"})
21 changes: 12 additions & 9 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,18 @@ async function themeldr_init() {
}

function enableDevMode() {
Object.defineProperty((webpackChunkdiscord_app.push([
[''], {},
e => {
m = [];
for (let c in e.c) m.push(e.c[c])
}
]), m).find(m => m ?.exports ?.default ?.isDeveloper !== void 0).exports.default, "isDeveloper", {
get: () => true
});
let wpRequire;
window.webpackChunkdiscord_app.push([[ Math.random() ], {}, (req) => { wpRequire = req; }]);
mod = Object.values(wpRequire.c).find(x => typeof x?.exports?.default?.isDeveloper !== "undefined")
usermod = Object.values(wpRequire.c).find(x => x?.exports?.default?.getUsers)
nodes = Object.values(mod.exports.default._dispatcher._actionHandlers._dependencyGraph.nodes);
try {
nodes.find(x => x.name == "ExperimentStore").actionHandler["OVERLAY_INITIALIZE"]({user: {flags: 1}})
} catch (e) {}
oldGetUser = usermod.exports.default.__proto__.getCurrentUser;
usermod.exports.default.__proto__.getCurrentUser = () => ({hasFlag: () => true})
nodes.find(x => x.name == "DeveloperExperimentStore").actionHandler["CONNECTION_OPEN"]()
usermod.exports.default.__proto__.getCurrentUser = oldGetUser
}

setTimeout(function () {
Expand Down
Binary file added rainbow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/bin/bash
discord_executable=$0

if [ $discord_executable == "" ]; then
discord_executable=discord
fi

echo "discordthemeldr"
this_path=$(dirname $(realpath $0))
cd "$this_path"
echo "Starting Discord"
discord --proxy-server="127.0.0.1:8080" &
$discord_executable proxy-server="127.0.0.1:8080" &
pid=$!

echo "Starting Proxy"
Expand Down

0 comments on commit b6bbc9a

Please sign in to comment.