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

Improved detection fixes #143

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions src/process/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const DetectableDB = JSON.parse(fs.readFileSync(join(__dirname, 'detectable.json
import * as Natives from './native/index.js';
const Native = Natives[process.platform];

function basename(str) {
return str.substr(str.lastIndexOf('/') + 1);
}

const timestamps = {}, names = {}, pids = {};
export default class ProcessServer {
Expand All @@ -34,11 +37,12 @@ export default class ProcessServer {

// log(`got processed in ${(performance.now() - startTime).toFixed(2)}ms`);

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

Expand All @@ -49,10 +53,32 @@ export default class ProcessServer {
toCompare.push(p.replace('_64', ''));
}

// performance: remove duplicate elements to process
const UniqueToCompare = []
for (const u of toCompare) {
if (!UniqueToCompare.includes(u)) {
UniqueToCompare.push(u)
}
}

if (UniqueToCompare.length < 1) { // performance: Skip empty path
continue
}

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;
for (const comp of UniqueToCompare) {
if (comp.includes('.exe')) {
if (x.name.includes('game/'+comp)) {
return true
}
if (x.name.includes('games/'+comp)) {
return true
}
}
}
if (x.name[0] === '>' ? x.name.substring(1) !== UniqueToCompare[0] : !UniqueToCompare.some(y => x.name === y || `${cwdPath}/${y}`.includes(`/${x.name}`))) return false;
if (args && x.arguments) return args.join(" ").indexOf(x.arguments) > -1;
return true;
})) {
Expand Down
10 changes: 8 additions & 2 deletions src/process/native/linux.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { readdir, readFile } from "fs/promises";
import { readdir, readFile, readlink } from "fs/promises";

export const getProcesses = async () => (await Promise.all(
(await readdir("/proc")).map(pid =>
(+pid > 0) && readFile(`/proc/${pid}/cmdline`, 'utf8')
.then(path => [+pid, path.split("\0")[0], path.split("\0").slice(1)], () => 0)
.then(async path => {
let cwdPath;
try {
cwdPath = await readlink(`/proc/${pid}/cwd`);
} catch (err) {};
return [+pid, path.split("\0")[0], path.split("\0").slice(1), cwdPath]
}, () => 0)
)
)).filter(x => x);