Skip to content

Commit

Permalink
formatting :trolley:
Browse files Browse the repository at this point in the history
  • Loading branch information
wont-stream committed Sep 26, 2024
1 parent 4b3dfc4 commit 97efbea
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 161 deletions.
4 changes: 2 additions & 2 deletions src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const bridge = Bun.serve({
return Response.redirect("https://github.com/wont-stream/bunRPC", 301);
},
websocket: {
message() { },
message() {},
open(ws) {
clients.add(ws);

Expand All @@ -34,4 +34,4 @@ const bridge = Bun.serve({
},
});

console.log(`[bunRPC] [Bridge] listening on ws://localhost:${bridge.port}`);
console.log(`[bunRPC] [Bridge] listening on ws://localhost:${bridge.port}`);
212 changes: 114 additions & 98 deletions src/process/index.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,121 @@
import * as Natives from './native/index.js';
import * as Natives from "./native/index.js";
const Native = Natives[process.platform];

const timestamps = {};
const names = {};
const pids = {};

export default class {
constructor(handlers) {
if (!Native) return;

this.handlers = handlers;

this.scan = this.scan.bind(this);

this.getDB();

this.scan();
setInterval(this.scan, 10000); // every 10 seconds instead of 5

console.log("[bunRPC] [Process] Scanner started");
}

async getDB() {
if (this.DetectableDB) return this.DetectableDB;

const data = await fetch("https://discord.com/api/v9/applications/detectable")

this.DetectableDB = await data.json();

return this.DetectableDB;
}

async scan() {
const processes = await Native.getProcesses();
const ids = [];

const DetectableDB = await this.getDB();

for (const [pid, _path, args] of processes) {
const path = _path.toLowerCase().replaceAll('\\', '/');
const toCompare = [];
const splitPath = path.split('/');
for (let i = 1; i < splitPath.length; i++) {
toCompare.push(splitPath.slice(-i).join('/'));
}

for (const p of toCompare.slice()) {
toCompare.push(p.replace('64', ''));
toCompare.push(p.replace('.x64', ''));
toCompare.push(p.replace('x64', ''));
toCompare.push(p.replace('_64', ''));
}

for (const { executables, id, name } of DetectableDB) {
if (executables?.some(x => {
if (x.is_launcher) return false;
if (x.name[0] === '>' ? x.name.substring(1) !== toCompare[0] : !toCompare.some(y => x.name === y)) return false;
if (args && x.arguments) return args.join(" ").indexOf(x.arguments) > -1;
return true;
})) {
names[id] = name;
pids[id] = pid;

ids.push(id);
if (!timestamps[id]) {
timestamps[id] = Date.now();
}

this.handlers.message({
socketId: id
}, {
cmd: 'SET_ACTIVITY',
args: {
activity: {
application_id: id,
name,
timestamps: {
start: timestamps[id]
}
},
pid
}
});
}
}
}

for (const id in timestamps) {
if (!ids.includes(id)) {
delete timestamps[id];

this.handlers.message({
socketId: id
}, {
cmd: 'SET_ACTIVITY',
args: {
activity: null,
pid: pids[id]
}
});
}
}
}
}
constructor(handlers) {
if (!Native) return;

this.handlers = handlers;

this.scan = this.scan.bind(this);

this.getDB();

this.scan();
setInterval(this.scan, 10000); // every 10 seconds instead of 5

console.log("[bunRPC] [Process] Scanner started");
}

async getDB() {
if (this.DetectableDB) return this.DetectableDB;

const data = await fetch(
"https://discord.com/api/v9/applications/detectable",
);

this.DetectableDB = await data.json();

return this.DetectableDB;
}

async scan() {
const processes = await Native.getProcesses();
const ids = [];

const DetectableDB = await this.getDB();

for (const [pid, _path, args] of processes) {
const path = _path.toLowerCase().replaceAll("\\", "/");
const toCompare = [];
const splitPath = path.split("/");
for (let i = 1; i < splitPath.length; i++) {
toCompare.push(splitPath.slice(-i).join("/"));
}

for (const p of toCompare.slice()) {
toCompare.push(p.replace("64", ""));
toCompare.push(p.replace(".x64", ""));
toCompare.push(p.replace("x64", ""));
toCompare.push(p.replace("_64", ""));
}

for (const { executables, id, name } of DetectableDB) {
if (
executables?.some((x) => {
if (x.is_launcher) return false;
if (
x.name[0] === ">"
? x.name.substring(1) !== toCompare[0]
: !toCompare.some((y) => x.name === y)
)
return false;
if (args && x.arguments)
return args.join(" ").indexOf(x.arguments) > -1;
return true;
})
) {
names[id] = name;
pids[id] = pid;

ids.push(id);
if (!timestamps[id]) {
timestamps[id] = Date.now();
}

this.handlers.message(
{
socketId: id,
},
{
cmd: "SET_ACTIVITY",
args: {
activity: {
application_id: id,
name,
timestamps: {
start: timestamps[id],
},
},
pid,
},
},
);
}
}
}

for (const id in timestamps) {
if (!ids.includes(id)) {
delete timestamps[id];

this.handlers.message(
{
socketId: id,
},
{
cmd: "SET_ACTIVITY",
args: {
activity: null,
pid: pids[id],
},
},
);
}
}
}
}
32 changes: 16 additions & 16 deletions src/process/native/darwin/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { exec } from 'node:child_process';
import { exec } from "node:child_process";

export const getProcesses = () => {
return new Promise((resolve, reject) => {
exec("ps -axo pid,comm", (error, stdout) => {
if (error) {
return reject(error); // Handle errors
}
return new Promise((resolve, reject) => {
exec("ps -axo pid,comm", (error, stdout) => {
if (error) {
return reject(error); // Handle errors
}

const processes = stdout
.toString()
.split('\n') // Split by new lines
.slice(1) // Skip the header line
.map(line => line.trim().split(/\s+/, 2)) // Split into PID and command
.filter(parts => parts.length === 2) // Ensure both PID and command exist
.map(([pid, command]) => [Number(pid), command]);
const processes = stdout
.toString()
.split("\n") // Split by new lines
.slice(1) // Skip the header line
.map((line) => line.trim().split(/\s+/, 2)) // Split into PID and command
.filter((parts) => parts.length === 2) // Ensure both PID and command exist
.map(([pid, command]) => [Number(pid), command]);

resolve(processes);
});
});
resolve(processes);
});
});
};
6 changes: 3 additions & 3 deletions src/process/native/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * as darwin from './darwin/index.js';
export * as linux from './linux/index.js';
export * as win32 from './win32/index.js';
export * as darwin from "./darwin/index.js";
export * as linux from "./linux/index.js";
export * as win32 from "./win32/index.js";
30 changes: 15 additions & 15 deletions src/process/native/linux/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { readdir } from "node:fs/promises";
import { file } from "bun"
import { file } from "bun";

export const getProcesses = async () => {
const directories = await readdir("/proc");
const directories = await readdir("/proc");

const processPromises = directories
.filter(pid => +pid > 0) // Filter valid PIDs upfront
.map(async (pid) => {
try {
const cmdline = await file(`/proc/${pid}/cmdline`).text();
const [command, ...args] = cmdline.split("\0"); // Destructure for clarity
return [+pid, command, args];
} catch {
return null; // Return null on failure
}
});
const processPromises = directories
.filter((pid) => +pid > 0) // Filter valid PIDs upfront
.map(async (pid) => {
try {
const cmdline = await file(`/proc/${pid}/cmdline`).text();
const [command, ...args] = cmdline.split("\0"); // Destructure for clarity
return [+pid, command, args];
} catch {
return null; // Return null on failure
}
});

const processes = await Promise.all(processPromises);
return processes.filter(Boolean); // Filter out null entries
const processes = await Promise.all(processPromises);
return processes.filter(Boolean); // Filter out null entries
};
33 changes: 19 additions & 14 deletions src/process/native/win32/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { exec } from 'node:child_process';
import { exec } from "node:child_process";

export const getProcesses = () => {
return new Promise((resolve) => {
exec("wmic process get ProcessID,ExecutablePath /format:csv", (error, output) => {
return new Promise((resolve) => {
exec(
"wmic process get ProcessID,ExecutablePath /format:csv",
(error, output) => {
const processes = output
.toString()
.split("\r\n") // Split by new lines
.slice(2) // Remove headers
.map((line) => line.trim().split(",").reverse()) // Split, reverse, and trim
.filter((parsed) => parsed[1]) // Filter out invalid paths
.map(([executablePath, processId]) => [
Number(processId) || processId,
executablePath,
]); // Parse IDs

const processes = output
.toString()
.split('\r\n') // Split by new lines
.slice(2) // Remove headers
.map(line => line.trim().split(',').reverse()) // Split, reverse, and trim
.filter(parsed => parsed[1]) // Filter out invalid paths
.map(([executablePath, processId]) => [Number(processId) || processId, executablePath]); // Parse IDs

resolve(processes);
});
});
resolve(processes);
},
);
});
};
8 changes: 4 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventEmitter } from "node:events";
import ipc from "./transports/ipc.js";
import ws from "./transports/ws.js";

import process from "./process/index.js"
import process from "./process/index.js";

let socketId = 0;

Expand Down Expand Up @@ -168,9 +168,9 @@ export default class extends EventEmitter {
data: isValid
? { code }
: {
code: isInvite ? 4011 : 4017,
message: `Invalid ${isInvite ? "invite" : "guild template"} id: ${code}`,
},
code: isInvite ? 4011 : 4017,
message: `Invalid ${isInvite ? "invite" : "guild template"} id: ${code}`,
},
evt: isValid ? null : "ERROR",
nonce,
});
Expand Down
Loading

0 comments on commit 97efbea

Please sign in to comment.