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

Discord profile plugin example #15

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ build
tlsn/
zip
dist/

14 changes: 14 additions & 0 deletions examples/discord_profile/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module 'main' {
export function start(): I32;
export function two(): I32;
export function parseDiscordProfile(): I32;
export function three(): I32;
export function config(): I32;
}

declare module 'extism:host' {
interface user {
redirect(ptr: I64): void;
notarize(ptr: I64): I64;
}
}
142 changes: 142 additions & 0 deletions examples/discord_profile/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
function isValidHost(urlString) {
const url = new URL(urlString);
return url.hostname === 'discord.com' || url.hostname === 'discord.gg'
}


function gotoDiscord() {
const { redirect } = Host.getFunctions();
const mem = Memory.fromString('https://discord.com/channels/@me');
redirect(mem.offset);
}

function start() {
if (!isValidHost(Config.get('tabUrl'))) {
gotoDiscord();
Host.outputString(JSON.stringify(false));
return;
}
Host.outputString(JSON.stringify(true));
}



function two() {
const localStorage = JSON.parse(Config.get('localStorage'))['discord.com'];
const headers = JSON.parse(Config.get('headers'))['discord.com'];

if (
!localStorage.user_id_cache ||
!headers['Authorization']
) {
Host.outputString(JSON.stringify(false));
return;
}

let userId = localStorage.user_id_cache;

userId = userId.replace(/"/g, "");

Host.outputString(
JSON.stringify({
url: `https://discord.com/api/v9/users/${userId}/profile`,
method: 'GET',
headers: {
Host: 'discord.com',
Accept: '*/*',
'Accept-Encoding': 'identity',
'User-Agent': headers['User-Agent'],
Authorization: headers['Authorization'],
Connection: 'close'
},
secretHeaders: [
`Authorization: ${headers['Authorization']}`
]
}))
}

function parseDiscordProfile() {
const bodyString = Host.inputString();
const params = JSON.parse(bodyString);

if (params.user?.username) {
const revealed = JSON.stringify({
username: params.user.username,
global_name: params.user.global_name,
}).slice(1, -1);

const selectionStart = bodyString.indexOf(revealed);
if (selectionStart === -1) {
Host.outputString(JSON.stringify([bodyString]));
return;
}

const selectionEnd = selectionStart + revealed.length;
const secretResps = [
bodyString.substring(0, selectionStart),
bodyString.substring(selectionEnd),
];
Host.outputString(JSON.stringify(secretResps));
} else {
Host.outputString(JSON.stringify(false));
}
}
function three() {
const params = JSON.parse(Host.inputString());
const { notarize } = Host.getFunctions();

if (!params) {
Host.outputString(JSON.stringify(false))
} else {
const mem = Memory.fromString(JSON.stringify({
...params,
getSecretResponse: 'parseDiscordProfile'
}));
const idOffset = notarize(mem.offset);
const id = Memory.find(idOffset).readString();
Host.outputString(JSON.stringify(id));
}
}



function config() {
Host.outputString(
JSON.stringify({
title: 'Discord Profile',
description: 'Notarize your Discord Profile',

steps: [
{
title: "Goto Discord",
cta: "Go to discord.com",
action: 'start'
},
{
title: 'Collect credentials',
description: "Log in to your Discord if you haven't already",
cta: 'Check localstorage',
action: 'two',
},
{
title: 'Notarize Discord Profile',
cta: 'Notarize',
action: 'three',
prover: true,
}
],
hostFunctions: ['redirect', 'notarize'],
headers: ['discord.com'],
localStorage: ['discord.com'],
requests: [
{
url: `https://discord.com/api/v9/users/*/profile`,
method: 'GET',
},
],
}),
);
}


module.exports = { start, two, config, three, parseDiscordProfile };