Skip to content

Commit

Permalink
upload report to api
Browse files Browse the repository at this point in the history
  • Loading branch information
muromec committed Jan 22, 2022
1 parent fd709c6 commit b4be8b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
31 changes: 22 additions & 9 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ function readFile(filename) {
});
}

function output(filename, data, isWin) {
if (typeof filename === "string" && filename !== "-") {
async function output(filename, data, isWin, uploadUrl) {
if (uploadUrl) {
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
const uploadData = JSON.stringify([{
fname: filename,
contentBase64: data.toString('base64'),
}]);
const ret = await http.query_promise('POST', uploadUrl, headers, uploadData);
console.log(ret.toString());
} else if (typeof filename === "string" && filename !== "-") {
io.writeFileSync(filename, data);
} else {
io.stdout.write(isWin ? encoding.convert(data, "utf-8", "cp1251") : data);
Expand Down Expand Up @@ -111,6 +122,7 @@ async function do_sc(
box,
inputF,
outputF,
uploadUrl,
certRecF,
edrpou,
email,
Expand Down Expand Up @@ -197,7 +209,7 @@ async function do_sc(
error("Error occured inside the pipeline.");
return false;
}
output(outputF, tb);
await output(outputF, tb, null, uploadUrl);
return true;
}

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

if (isErr === false) {
output(outputF, textinfo.content, isWin);
await output(outputF, textinfo.content, isWin);
}

return true;
}

function unprotect(key, outputF) {
async function unprotect(key, outputF) {
key = key_param_parse(key);
const buf = fs.readFileSync(key.path);
const store = Priv.from_protected(buf, key.pw, algos());

store.keys.forEach(function (key) {
output(outputF, key.as_pem());
});
for(let key of store.keys) {
await output(outputF, key.as_pem());
}

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

if (argv.unprotect) {
return unprotect(argv.key, argv.output);
return await unprotect(argv.key, argv.output);
}

let box;
Expand All @@ -323,6 +335,7 @@ async function main(argv, setIo) {
box,
argv.input,
argv.output,
argv.upload_url,
argv.recipient_cert,
argv.edrpou,
argv.email,
Expand Down
12 changes: 10 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const http = require("http");
const https = require("https");
const url = require("url");

var query = function(method, toUrl, headers, payload, cb) {
var parsed = url.parse(toUrl);
var req = http.request({
var module = {'http:': http, 'https:': https}[parsed.protocol];
var req = module.request({
host: parsed.host,
path: parsed.path,
headers: headers,
Expand All @@ -24,4 +26,10 @@ var query = function(method, toUrl, headers, payload, cb) {
req.end();
};

module.exports = {query};
function query_promise(...args) {
return new Promise((resolve)=> {
query(...[...args, resolve]);
});
}

module.exports = {query, query_promise};

0 comments on commit b4be8b1

Please sign in to comment.