Tuesday 23 May 2017

Node.js async module value return

I'm struggling to grasp how to use asynchronous programming. I have a tcp server that listens for connections, accepts packets, then passes the contained data to a module that generates keypairs and stores the whole mess in postgresql. I need to return the public key to the tcp server to pass back to the original packet sender. What I have now works but I believe it leaves the public key behind and passes undefined to the client. I can't decided if I'm just not understanding the process I'm using quite right or if I'm completely misunderstanding the whole process.

Here's my code

server:

    var net = require('net');
    var keys = require('./genKey.js');

    var HOST = '10.0.0.1';
    var PORT = 5555;

    net.createServer(function(sock) {

        console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

        sock.on('data', function(data) {

            // console.log('DATA ' + sock.remoteAddress + ': ' + data);

            keys.genKey(data, out);

            //var public = console.log(String(keys.pub));
            **function out (pk) {
            console.log('out function called');
            var public = keys.pub;
            sock.write(public);**
            console.log(public);
            }

            //var promise = keys.genKey(data);
            //promise.then(console.log, console.error);

            //console.log(public);

            //sock.write(String(keys.pub));

        });
        sock.on('error', function (exc) {
        console.log("ignoring exception: " + exc);
    });

        sock.on('close', function(data) {
            console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
        });

    }).listen(PORT, HOST);

    console.log('Server listening on ' + HOST +':'+ PORT);

module:

    var Promise = require('promise');
    var cp = require('child_process')
      , assert = require('assert')
      ;
    const pool = require('./db');

    var prkey = '';
    var pukey = '';

    var privateKey, publicKey;
    publicKey = '';

    module.exports = {
    genKey: function(input) {
    cp.exec('openssl genrsa 2048', function(err, stdout, stderr) {
      assert.ok(!err);
      prkey = stdout;
      privateKey = stdout;
      //console.log(privateKey);
      //console.log('stdout: ', stdout);
      //prkey = privateKey;
      //console.log(prkey);
      makepub = cp.spawn('openssl', ['rsa', '-pubout']);
      makepub.on('exit', function(code) {
        assert.equal(code, 0);
        //console.log(publicKey);
        //pukey = publicKey;
        //console.log(pukey);
      });
      makepub.stdout.on('data', function(data) {
        publicKey += data;
        pukey = String(publicKey);
        //console.log('stdout: ', publicKey);
        writeSQL(input, prkey, pukey);
        //console.log(pukey);
        //return {
        //pub: () => pukey
        // };
        **return pukey;**
      });
      makepub.stdout.setEncoding('ascii');
      makepub.stdin.write(privateKey);
      makepub.stdin.end();
    //console.log('stdout: ', stdout);
    });

    //console.log('output keys');
    //console.log(prkey);
    //console.log(pukey);
      //writeSQL(prkey, pukey);
    }
    };

    function writeSQL(uuid, private, public) {
      //console.log(private);
      //console.log(public);
      //console.log(uuid);
      console.log('write sql');
      //pool.query('INSERT INTO uuidkeys(uuid, public, private) values($1,$2, $3)',
      //  [uuid, public, private]);

    };

I apologize for the messy code, I've tried so many different things it has become very ugly. If someone could tell me how to get my public key back to my server at the right point in my server function that would be great. Hopefully that will be sufficient to get me on the right track to understanding this.



via Bronston Hansen

No comments:

Post a Comment