marknotes/client.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-09-26 00:49:24 +00:00
#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
// Don't modify the above code. Used to make the MN client universally executable.
// Establish constants and config variables.
require('./const.js');
var serverAddress = "http://127.0.0.1";
var port = 11111;
var endPoint = "/api";
// End config and constants
// some useful functions
function genChecksum(a, o, e) {
var sha = require('crypto').createHash('sha256');
var input = global.SECRET;
if (a) input += a;
if (o) input += o;
if (e) input += e;
//console.log(input);
return(sha.update(input).digest("hex"));
}
// end useful functions.
var a = process.argv[2];
var o = process.argv[3];
var e = "";
var argLength = process.argv.length;
if (argLength >= 4) {
for (var i = 4; i < argLength; i++) {
e += process.argv[i]
if (i < argLength-1 ) e += " ";
}
}
var checksum = genChecksum(a, o, e);
var http = require('http');
var qs = require("querystring");
var uri = serverAddress;
if (port) uri += ":" + port;
if (endPoint) uri += endPoint;
uri += "?" + qs.stringify({"a": a,"o": o, "e": e});
if (checksum) uri += "&cs=" + checksum;
//uri += '1'; // makes checksum fail
callback = function(response) {
if (response.statusCode == 200) {
console.log('SUCCESS: ' + response.statusCode);
} else {
console.log('FAILURE: ' + response.statusCode);
}
response.on('data', function(chunk) {
console.log('BODY: \n' + chunk);
});
}
console.log(uri);
var req = http.request(uri, callback).end();