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

Added Dynamic Analysis tool: Iroh #42

Open
wants to merge 5 commits into
base: f24
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
1 change: 1 addition & 0 deletions install/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"html-to-text": "9.0.5",
"imagesloaded": "5.0.0",
"ipaddr.js": "2.2.0",
"iroh": "^0.3.0",
"jquery": "3.7.1",
"jquery-deserialize": "2.0.0",
"jquery-form": "4.3.0",
Expand Down
56 changes: 56 additions & 0 deletions iroh/iroh-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const Iroh = require('iroh');

// Asked help from co-pilot for this function as I needed to simplify parsing for Iroh.
function monitorFunction(fn, name) {
console.log(`Setting up Iroh monitoring for ${name}`);

const stage = new Iroh.Stage(`
function monitored() {
try {
return target();
} catch(e) {
console.error(e);
throw e;
}
}
`);

// Using Iroh's listeners
stage.addListener(Iroh.CALL)
.on('before', (e) => {
console.log(`[IROH] ${name} called`);
e.setData('time', process.hrtime());
})
.on('after', (e) => {
const diff = process.hrtime(e.getData('time'));
const time = ((diff[0] * 1e9) + diff[1]) / 1e6;
console.log(`[IROH] ${name} took ${time}ms`);
});

// Return the original function with timing
return async function (...args) {
const start = process.hrtime();
console.log(`[START] ${name}`);

try {
const result = await fn.apply(this, args);
const [s, ns] = process.hrtime(start);
const ms = (s * 1000) + (ns / 1e6);
console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`);
console.log(`[END] ${name}`);
return result;
} catch (error) {
console.error(`[ERROR] ${name}:`, error);
throw error;
}
};
}

module.exports = {
monitorFunction: monitorFunction,
CALL: Iroh.CALL,
FUNCTION: Iroh.FUNCTION,
TRY: Iroh.TRY,
};
39 changes: 39 additions & 0 deletions iroh/iroh-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(base) jullia@jullias-air nodebb-f24-sweethearts % node app.js
2024-10-24T21:00:25.995Z [4567/64147] - info: NodeBB v3.8.4 Copyright (C) 2013-2024 NodeBB Inc.
2024-10-24T21:00:25.996Z [4567/64147] - info: This program comes with ABSOLUTELY NO WARRANTY.
2024-10-24T21:00:25.996Z [4567/64147] - info: This is free software, and you are welcome to redistribute it under certain conditions.
2024-10-24T21:00:25.997Z [4567/64147] - info:
Initializing Iroh monitoring...
Setting up Iroh monitoring for Topics.create
Setting up Iroh monitoring for Topics.reply
Iroh monitoring successfully initialized!!
2024-10-24T21:00:26.685Z [4567/64147] - info: Initializing NodeBB v3.8.4 http://localhost:4567
(node:64147) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
2024-10-24T21:00:26.892Z [4567/64147] - info: [socket.io] Restricting access to origin: http://localhost:*
2024-10-24T21:00:27.004Z [4567/64147] - info: [api] Adding 0 route(s) to `api/v3/plugins`
2024-10-24T21:00:27.007Z [4567/64147] - info: [router] Routes added
Tag "homework" successfully created!
Tag "assignment" successfully created!
2024-10-24T21:00:27.011Z [4567/64147] - info: 🎉 NodeBB Ready
2024-10-24T21:00:27.012Z [4567/64147] - info: 🤝 Enabling 'trust proxy'
2024-10-24T21:00:27.013Z [4567/64147] - info: 📡 NodeBB is now listening on: 0.0.0.0:4567
2024-10-24T21:00:27.013Z [4567/64147] - info: 🔗 Canonical URL: http://localhost:4567
WARNING: The keyword 'none' must be used as a single argument.
../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow()
../../../bootstrap/scss/forms/_form-control.scss 40:7 @import
bootstrap/scss/_forms.scss 3:9 @import
- 19:9 root stylesheet

WARNING: The keyword 'none' must be used as a single argument.
../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow()
../../../bootstrap/scss/forms/_form-select.scss 32:7 @import
bootstrap/scss/_forms.scss 4:9 @import
- 19:9 root stylesheet

[START] Topics.create
[TIME] Topics.create took 2.12ms
[END] Topics.create
[START] Topics.reply
[TIME] Topics.reply took 21.73ms
[END] Topics.reply
14 changes: 13 additions & 1 deletion src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@

const nconf = require('nconf');
const winston = require('winston');
const irohMonitor = require('../iroh/iroh-monitor');

const start = module.exports;

const db = require('./database');
const Topics = require('./topics');

start.start = async function () {
printStartupInfo();
try {
console.log('Initializing Iroh monitoring...');

// I chose to monitor core nodeBB topic functions (create and reply).
Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create');
Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply');

console.log('Iroh monitoring successfully initialized!!');
} catch (err) {
console.error('Failed to initialize Iroh monitoring:', err);
}

printStartupInfo();
addProcessHandlers();

try {
Expand Down