Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Config = {
- rpc_port : 12345,
- rpc_user : 'xxxxx',
- rpc_password : 'yyyyy',
- processes_whitelist : [
- ['cmd', 'args'],
- ]
- };
- var Client = require('bitcore-wallet-client');
- var fs = require('fs');
- var rpc = require('./jsonrpc.js');
- var lsof = require('./lsof.js');
- var ps = require('ps-node');
- var nodemailer = require('nodemailer');
- var BWS_INSTANCE_URL = 'http://127.0.0.1:3232/bws/api'
- var wallet = new Client({
- baseUrl: BWS_INSTANCE_URL,
- verbose: false
- });
- var server = new rpc.Server();
- var UNAUTHORIZED = "Unauthorized\n";
- server.authHandler = function(username, password) {
- if(username == Config.rpc_user && password == Config.rpc_password) {
- return true;
- } else {
- return false;
- }
- }
- wallet.import(fs.readFileSync('wallet.dat'));
- wallet.openWallet(function(err, ret) {
- if (err) {
- console.log('error: ', err);
- return
- };
- console.log('Wallet loaded correctly', '\n\n');
- var requestsHandler = server.handleHttp;
- server.handleHttp = function(req, res) {
- function authError(msg) {
- var transporter = nodemailer.createTransport();
- var mailOptions = {
- };
- transporter.sendMail(mailOptions, function(error, info){
- if(error){
- return console.log(error);
- }
- console.log('Message sent: ' + msg);
- });
- server.handleHttpError(req, res, 401, UNAUTHORIZED);
- }
- // Check authentication if we require it
- if (this.authHandler) {
- var authHeader = req.headers['authorization'] || '', // get the header
- authToken = authHeader.split(/\s+/).pop() || '', // get the token
- auth = new Buffer(authToken, 'base64').toString(), // base64 -> string
- parts = auth.split(/:/), // split on colon
- username = parts[0],
- password = parts[1];
- if (!this.authHandler(username, password)) {
- authError('Wrong username or password\n\n'+JSON.stringify(parts));
- return;
- }
- }
- var clientPort = req.connection.remotePort,
- that = this;
- lsof.rawTcpPort(clientPort, function(processes) {
- var processID = null;
- processes.forEach(function(proc) {
- var srcPort = proc['name'].split('->')[0].split(':')[1];
- if(srcPort == clientPort) {
- processID = proc['pid'];
- }
- });
- if(processID === null) {
- authError('Could not find PID of caller\n\n'+JSON.stringify(processes, null, 4));
- return;
- }
- ps.lookup({ pid: processID }, function(err, resultList ) {
- if (err) {
- throw new Error( err );
- }
- var process = resultList[ 0 ];
- if( process ){
- var allowed = false;
- Config['processes_whitelist'].forEach(function(allowed_process) {
- if(process.command.indexOf(allowed_process[0]) > -1) {
- console.log(process.arguments.join(' '));
- if(process.arguments.join(' ').indexOf(allowed_process[1]) > -1) {
- allowed = true;
- console.log(allowed_process.join(' '), 'connection accepted.');
- }
- }
- });
- if(allowed === false) {
- authError('Process not allowed\n\n'+JSON.stringify(process, null, 4));
- return;
- }
- // console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s, DEBUG: %s', process.pid, process.command, process.arguments, JSON.stringify(process, null, 4));
- requestsHandler.apply(that, [req, res]);
- }
- else {
- authError('No such process found!\n\n'+JSON.stringify(processes, null, 4));
- return;
- }
- });
- });
- }
- function checkProcess(portNo) {
- console.dir(portNo);
- }
- function getnewaddress(args, opt, callback) {
- var opts = {ignoreMaxGap: true};
- address = wallet.createAddress(opts, function(err, ret) {
- if('address' in ret)
- callback(null, ret['address']);
- else
- callback(true, false);
- });
- }
- server.expose('getnewaddress', getnewaddress);
- server.listen(Config['rpc_port'], 'localhost');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement