Skip to content

Commit b4be8b1

Browse files
committed
upload report to api
1 parent fd709c6 commit b4be8b1

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

agent.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,19 @@ function readFile(filename) {
4141
});
4242
}
4343

44-
function output(filename, data, isWin) {
45-
if (typeof filename === "string" && filename !== "-") {
44+
async function output(filename, data, isWin, uploadUrl) {
45+
if (uploadUrl) {
46+
const headers = {
47+
'Content-Type': 'application/json',
48+
'Accept': 'application/json',
49+
};
50+
const uploadData = JSON.stringify([{
51+
fname: filename,
52+
contentBase64: data.toString('base64'),
53+
}]);
54+
const ret = await http.query_promise('POST', uploadUrl, headers, uploadData);
55+
console.log(ret.toString());
56+
} else if (typeof filename === "string" && filename !== "-") {
4657
io.writeFileSync(filename, data);
4758
} else {
4859
io.stdout.write(isWin ? encoding.convert(data, "utf-8", "cp1251") : data);
@@ -111,6 +122,7 @@ async function do_sc(
111122
box,
112123
inputF,
113124
outputF,
125+
uploadUrl,
114126
certRecF,
115127
edrpou,
116128
email,
@@ -197,7 +209,7 @@ async function do_sc(
197209
error("Error occured inside the pipeline.");
198210
return false;
199211
}
200-
output(outputF, tb);
212+
await output(outputF, tb, null, uploadUrl);
201213
return true;
202214
}
203215

@@ -276,20 +288,20 @@ async function do_parse(inputF, outputF, box, tsp, ocsp) {
276288
});
277289

278290
if (isErr === false) {
279-
output(outputF, textinfo.content, isWin);
291+
await output(outputF, textinfo.content, isWin);
280292
}
281293

282294
return true;
283295
}
284296

285-
function unprotect(key, outputF) {
297+
async function unprotect(key, outputF) {
286298
key = key_param_parse(key);
287299
const buf = fs.readFileSync(key.path);
288300
const store = Priv.from_protected(buf, key.pw, algos());
289301

290-
store.keys.forEach(function (key) {
291-
output(outputF, key.as_pem());
292-
});
302+
for(let key of store.keys) {
303+
await output(outputF, key.as_pem());
304+
}
293305

294306
return true;
295307
}
@@ -301,7 +313,7 @@ async function main(argv, setIo) {
301313
jk.Curve.only_known = argv.only_known;
302314

303315
if (argv.unprotect) {
304-
return unprotect(argv.key, argv.output);
316+
return await unprotect(argv.key, argv.output);
305317
}
306318

307319
let box;
@@ -323,6 +335,7 @@ async function main(argv, setIo) {
323335
box,
324336
argv.input,
325337
argv.output,
338+
argv.upload_url,
326339
argv.recipient_cert,
327340
argv.edrpou,
328341
argv.email,

lib/http.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const http = require("http");
2+
const https = require("https");
23
const url = require("url");
34

45
var query = function(method, toUrl, headers, payload, cb) {
56
var parsed = url.parse(toUrl);
6-
var req = http.request({
7+
var module = {'http:': http, 'https:': https}[parsed.protocol];
8+
var req = module.request({
79
host: parsed.host,
810
path: parsed.path,
911
headers: headers,
@@ -24,4 +26,10 @@ var query = function(method, toUrl, headers, payload, cb) {
2426
req.end();
2527
};
2628

27-
module.exports = {query};
29+
function query_promise(...args) {
30+
return new Promise((resolve)=> {
31+
query(...[...args, resolve]);
32+
});
33+
}
34+
35+
module.exports = {query, query_promise};

0 commit comments

Comments
 (0)