Skip to content

Commit 94837e7

Browse files
committed
Implement decai -e to configure endpoint and commands
1 parent bc1dcee commit 94837e7

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

r2plugin/decai.r2.js

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(function () {
22
const command = "decai";
3+
let decaiHost = "http://localhost:8080";
4+
let decaiCommands = "pdc";
35
let decprompt = "optimize this pseudodisasm into high level quality decompiled code,";
46
decprompt += "replace goto with proper control flow statements,";
57
decprompt += "use better names for variables,";
@@ -11,8 +13,22 @@
1113
decprompt += "remove unnecessary assignments inlining them into the function argument calls, add a comment on top explaining whats the function for in one sentence";
1214
// simplest the better
1315
// decprompt = "do not explain, just improve and merge the following decompiled functions, remove gotos and use better names for variables. optimize for readability, assume calling conventions to fill function arguments";
14-
decprompt = "do not explain, just improve and merge the following decompiled functions, remove gotos and use better names for variables, focus on readability";
16+
decprompt = "show only the code without explanations, just improve and merge the following decompiled functions, remove gotos and use better names for variables, focus on readability";
1517

18+
function decaiEval(arg) {
19+
const [k, v] = arg.split("=");
20+
switch (k) {
21+
case "decai.cmds":
22+
decaiCommands = v;
23+
break;
24+
case "decai.prompt":
25+
decprompt = v;
26+
break;
27+
case "decai.host":
28+
decaiHost = v;
29+
break;
30+
}
31+
}
1632
function usage() {
1733
console.error("Usage: " + command + " (-h) [prompt]");
1834
console.error(" " + command + " -d - decompile");
@@ -26,7 +42,7 @@
2642
console.error(" " + command + " -V - find vulnerabilities");
2743
}
2844
function r2ai(s) {
29-
const host = "http://localhost:8080/cmd";
45+
const host = decaiHost + "/cmd"; // "http://localhost:8080/cmd";
3046
const ss = s.replace(/ /g, "%20").replace(/'/g, "\\'");
3147
r2.cmd0 ('\'!curl -s "' + host + '/' + ss + '" > .pdc.txt || echo cannot curl. Is "r2ai -w" running?');
3248
return r2.cmd ('cat .pdc.txt');
@@ -79,9 +95,14 @@
7995
r2aidec("-d find vulnerabilities, dont show the code, only show the response");
8096
break;
8197
case "e": // "-e"
82-
console.log("e decai.host=http://localhost:8080");
83-
console.log("e decai.prompt=" + decprompt);
84-
// eval
98+
args = args.slice(2).trim();
99+
if (args) {
100+
decaiEval(args);
101+
} else {
102+
console.log("e decai.host=" + decaiHost);
103+
console.log("e decai.prompt=" + decprompt);
104+
console.log("e decai.cmds=" + decaiCommands);
105+
}
85106
break;
86107
case "x": // "-x"
87108
r2.cmd ("pdsf > /tmp/.pdc.txt");
@@ -109,7 +130,16 @@
109130
case "d": // "-d"
110131
try {
111132
args = args.slice(2).trim();
112-
r2.cmd ("pdc > /tmp/.pdc.txt");
133+
r2.call ("rm /tmp/.pdc.txt");
134+
r2.call ("rm .pdc.txt");
135+
r2.cmd ("echo > /tmp/.pdc.txt");
136+
for (const c of decaiCommands.split(",")) {
137+
console.error("Running " + c);
138+
r2.cmd ("echo Output from " + c + ": >> /tmp/.pdc.txt");
139+
r2.cmd ("echo BEGIN >> /tmp/.pdc.txt");
140+
r2.cmd (c + " >> /tmp/.pdc.txt");
141+
r2.cmd ("echo END >> /tmp/.pdc.txt");
142+
}
113143
r2ai("-R");
114144
out = r2ai("-i /tmp/.pdc.txt " + (decprompt + " " + args).trim());
115145
} catch (e) {

0 commit comments

Comments
 (0)