Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. var Config = {
  2. rpc_port : 12345,
  3. rpc_user : 'xxxxx',
  4. rpc_password : 'yyyyy',
  5. processes_whitelist : [
  6. ['cmd', 'args'],
  7. ]
  8. };
  9. var Client = require('bitcore-wallet-client');
  10.  
  11. var fs = require('fs');
  12. var rpc = require('./jsonrpc.js');
  13. var lsof = require('./lsof.js');
  14. var ps = require('ps-node');
  15. var nodemailer = require('nodemailer');
  16. var BWS_INSTANCE_URL = 'http://127.0.0.1:3232/bws/api'
  17.  
  18. var wallet = new Client({
  19. baseUrl: BWS_INSTANCE_URL,
  20. verbose: false
  21. });
  22. var server = new rpc.Server();
  23. var UNAUTHORIZED = "Unauthorized\n";
  24. server.authHandler = function(username, password) {
  25. if(username == Config.rpc_user && password == Config.rpc_password) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31.  
  32. wallet.import(fs.readFileSync('wallet.dat'));
  33.  
  34. wallet.openWallet(function(err, ret) {
  35. if (err) {
  36. console.log('error: ', err);
  37. return
  38. };
  39.  
  40. console.log('Wallet loaded correctly', '\n\n');
  41.  
  42. var requestsHandler = server.handleHttp;
  43. server.handleHttp = function(req, res) {
  44. function authError(msg) {
  45. var transporter = nodemailer.createTransport();
  46.  
  47. var mailOptions = {
  48. };
  49.  
  50. transporter.sendMail(mailOptions, function(error, info){
  51. if(error){
  52. return console.log(error);
  53. }
  54. console.log('Message sent: ' + msg);
  55. });
  56.  
  57. server.handleHttpError(req, res, 401, UNAUTHORIZED);
  58. }
  59.  
  60. // Check authentication if we require it
  61. if (this.authHandler) {
  62. var authHeader = req.headers['authorization'] || '', // get the header
  63. authToken = authHeader.split(/\s+/).pop() || '', // get the token
  64. auth = new Buffer(authToken, 'base64').toString(), // base64 -> string
  65. parts = auth.split(/:/), // split on colon
  66. username = parts[0],
  67. password = parts[1];
  68. if (!this.authHandler(username, password)) {
  69. authError('Wrong username or password\n\n'+JSON.stringify(parts));
  70. return;
  71. }
  72. }
  73.  
  74. var clientPort = req.connection.remotePort,
  75. that = this;
  76.  
  77. lsof.rawTcpPort(clientPort, function(processes) {
  78. var processID = null;
  79. processes.forEach(function(proc) {
  80. var srcPort = proc['name'].split('->')[0].split(':')[1];
  81. if(srcPort == clientPort) {
  82. processID = proc['pid'];
  83. }
  84. });
  85.  
  86. if(processID === null) {
  87. authError('Could not find PID of caller\n\n'+JSON.stringify(processes, null, 4));
  88. return;
  89. }
  90.  
  91. ps.lookup({ pid: processID }, function(err, resultList ) {
  92. if (err) {
  93. throw new Error( err );
  94. }
  95.  
  96. var process = resultList[ 0 ];
  97.  
  98. if( process ){
  99. var allowed = false;
  100.  
  101. Config['processes_whitelist'].forEach(function(allowed_process) {
  102. if(process.command.indexOf(allowed_process[0]) > -1) {
  103. console.log(process.arguments.join(' '));
  104. if(process.arguments.join(' ').indexOf(allowed_process[1]) > -1) {
  105. allowed = true;
  106. console.log(allowed_process.join(' '), 'connection accepted.');
  107. }
  108. }
  109. });
  110.  
  111. if(allowed === false) {
  112. authError('Process not allowed\n\n'+JSON.stringify(process, null, 4));
  113. return;
  114. }
  115.  
  116. // console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s, DEBUG: %s', process.pid, process.command, process.arguments, JSON.stringify(process, null, 4));
  117. requestsHandler.apply(that, [req, res]);
  118. }
  119. else {
  120. authError('No such process found!\n\n'+JSON.stringify(processes, null, 4));
  121. return;
  122. }
  123. });
  124. });
  125. }
  126.  
  127. function checkProcess(portNo) {
  128. console.dir(portNo);
  129. }
  130.  
  131. function getnewaddress(args, opt, callback) {
  132. var opts = {ignoreMaxGap: true};
  133.  
  134. address = wallet.createAddress(opts, function(err, ret) {
  135. if('address' in ret)
  136. callback(null, ret['address']);
  137. else
  138. callback(true, false);
  139. });
  140. }
  141.  
  142. server.expose('getnewaddress', getnewaddress);
  143.  
  144. server.listen(Config['rpc_port'], 'localhost');
  145. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement