|
1 |
| -// Generated by CoffeeScript 1.6.3 |
2 |
| -(function() { |
3 |
| - var crypto, os, zmq; |
| 1 | +/*jshint laxcomma: true, smarttabs: true, node: true, esnext: true*/ |
| 2 | +'use strict'; |
| 3 | +const crypto = require('crypto') |
| 4 | + , os = require('os') |
| 5 | + , zmq = require('zmq') |
| 6 | + , tcp_exp = /tcp:\/\/[\d.]+:(\d+)\/?/ |
| 7 | + , vm_exp = /VMware/ |
| 8 | + , localip_exp = /0\.0\.0\.0/ |
| 9 | + , tcp_proto = /^tcp\:\/\//; |
| 10 | + ; |
4 | 11 |
|
5 |
| - crypto = require('crypto'); |
6 |
| - |
7 |
| - os = require('os'); |
| 12 | +exports.get_random = function(){ |
| 13 | + return crypto.randomBytes(20).toString('hex'); |
| 14 | +}; |
8 | 15 |
|
9 |
| - zmq = require('zmq'); |
| 16 | +exports.isNumber = function(n){ |
| 17 | + return !isNaN(parseFloat(n)) && isFinite(n); |
| 18 | +}; |
10 | 19 |
|
11 |
| - exports.get_random = function() { |
12 |
| - return crypto.randomBytes(20).toString('hex'); |
13 |
| - }; |
14 |
| - |
15 |
| - exports.list_local_ips = function() { |
16 |
| - var a, addrs, iface_name, ips, _i, _len, _ref; |
17 |
| - ips = []; |
18 |
| - _ref = os.networkInterfaces(); |
19 |
| - for (iface_name in _ref) { |
20 |
| - addrs = _ref[iface_name]; |
21 |
| - if (/VMware/.test(iface_name)) { |
22 |
| - continue; |
23 |
| - } |
24 |
| - for (_i = 0, _len = addrs.length; _i < _len; _i++) { |
25 |
| - a = addrs[_i]; |
26 |
| - if (!a.internal && a.family === 'IPv4') { |
27 |
| - ips.push(a.address); |
| 20 | +exports.list_local_ips = function(){ |
| 21 | + let ips = []; |
| 22 | + let interfaces = os.networkInterfaces(); |
| 23 | + for(let iface_name in interfaces){ |
| 24 | + if(vm_exp.test(iface_name)){ continue; } |
| 25 | + let addrs = interfaces[ iface_name ]; |
| 26 | + for(let a in addrs){ |
| 27 | + let iface = addrs[a]; |
| 28 | + if(!iface.internal && iface.family == 'IPv4'){ |
| 29 | + ips.push(iface.address); |
| 30 | + } |
28 | 31 | }
|
29 |
| - } |
30 | 32 | }
|
31 | 33 | return ips;
|
32 |
| - }; |
33 |
| - |
34 |
| - exports.get_local_endpoint = function(sock) { |
35 |
| - var addr, ip; |
| 34 | +}; |
| 35 | +exports.get_local_endpoint = function(sock){ |
| 36 | + let addr, ip; |
36 | 37 | addr = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
|
37 |
| - if (addr.indexOf('tcp://') !== 0) { |
38 |
| - return addr; |
| 38 | + if(tcp_proto.test( addr )){ |
| 39 | + return addr; |
39 | 40 | }
|
| 41 | + |
| 42 | + // resolve to a locally reachable IP if tcp:// |
40 | 43 | ip = this.list_local_ips()[0];
|
41 |
| - return addr.replace(/0\.0\.0\.0/, "" + ip); |
42 |
| - }; |
43 |
| - |
44 |
| - exports.get_port = function(addr) { |
45 |
| - var m; |
46 |
| - m = addr.match(/tcp:\/\/[\d.]+:(\d+)\/?/); |
47 |
| - if (m) { |
48 |
| - return Number(m[1]); |
49 |
| - } else { |
50 |
| - return -1; |
51 |
| - } |
52 |
| - }; |
| 44 | + return addr.replace(localip_exp, `${ip}`); |
| 45 | +}; |
| 46 | + |
| 47 | +exports.get_port = function(addr){ |
| 48 | + let m = addr.match(tcp_exp); |
| 49 | + return m ? Number(m[1]) : -1; |
| 50 | +}; |
| 51 | + |
| 52 | +exports.bindAndGetAddr = function(sock_type, addr){ |
| 53 | + let sock, endpoint, port; |
53 | 54 |
|
54 |
| - exports.bindAndGetAddr = function(sock_type, addr) { |
55 |
| - var port, sock; |
56 | 55 | sock = zmq.socket(sock_type);
|
57 | 56 | sock.bindSync(addr);
|
58 |
| - addr = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT); |
| 57 | + endpoint = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT); |
59 | 58 | port = exports.get_port(addr);
|
60 |
| - return [sock, addr, port]; |
61 |
| - }; |
| 59 | + return [sock, endpoint, port]; |
| 60 | +}; |
| 61 | + |
| 62 | +exports.lpad = function(value, padding){ |
| 63 | + let zeroes = '0', idx = padding; |
| 64 | + while( idx ){ |
| 65 | + zeroes += '0'; |
| 66 | + idx--; |
| 67 | + } |
| 68 | + return (zeroes + value).slice(padding * - 1); |
| 69 | +}; |
62 | 70 |
|
63 |
| -}).call(this); |
| 71 | +exports.random = function( length = 10 ){ |
| 72 | + return Math.floor(Math.random() * 1024 * 1024); |
| 73 | +}; |
0 commit comments