old_contents: crypto = require 'crypto'
  os = require 'os'
  zmq = require 'zmq'
  
  exports.get_random = () ->
  	crypto.randomBytes(20).toString('hex')
  
  exports.list_local_ips = () ->
  	ips = []
  	for iface_name, addrs of os.networkInterfaces()
  		continue if /VMware/.test iface_name
  		ips.push a.address for a in addrs when not a.internal and a.family == 'IPv4'
  	return ips
  
  exports.get_local_endpoint = (sock) ->
  	addr = sock.getsockopt zmq.ZMQ_LAST_ENDPOINT
  	return addr if addr.indexOf('tcp://') != 0
  
  	# resolve to a locally reachable IP if tcp://
  	ip = this.list_local_ips()[0]
  	return addr.replace /0\.0\.0\.0/, "#{ip}"
  
  exports.get_port = (addr) ->
  	m = addr.match /tcp:\/\/[\d.]+:(\d+)\/?/
  	if m then Number(m[1]) else -1
  
  exports.bindAndGetAddr = (sock_type, addr) ->
  	sock = zmq.socket sock_type
  	sock.bindSync addr
  	addr = sock.getsockopt zmq.ZMQ_LAST_ENDPOINT
  	port = exports.get_port addr
  	return [sock, addr, port]
  
  
new_contents: 'use strict';
  const crypto = require('crypto');
  const os = require('os');
  const zmq = require('zmq');
  const tcp_exp = /tcp:\/\/[\d.]+:(\d+)\/?/;
  const vm_exp = /VMware/;
  const localip_exp = /0\.0\.0\.0/
  const tcp_proto = 'tcp://';
  
  exports.get_random = function(){
  	return crypto.randomBytes(20).toString('hex')
  };
  
  exports.list_local_ips = function(){
  	let ips = [];
  	let interfaces = os.networkInterfaces()
  	for(let iface_name in interfaces){
  		if(vm_exp.test(iface_name)){ continue }
  		let addrs = interfaces[ iface_name ];
  		for(let a in addrs){
  			let iface = addrs[a]
  			if(!iface.internal && iface.family == 'IPv4'){
  				ips.push(iface.address)
  			}
  		}
  	}
  	return ips;
  };
  exports.get_local_endpoint = function(sock){
  	let addr = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
  	if(tcp_proto.test( addr )){
  		return addr 
  	}
  
  	// resolve to a locally reachable IP if tcp://
  	let ip = this.list_local_ips()[0];
  	return addr.replace(localip_exp, `${ip}`);
  }
  exports.get_port = function(addr){
  	let m = addr.match(tcp_exp);
  	return m ? Number(m[1]) : -1
  }
  
  exports.bindAndGetAddr = function(sock_type, addr){
  	let sock = zmq.socket(sock_type);
  	sock.bindSync(addr);
  	let endpoint = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
  	let port = exports.get_port(addr);
  	return [sock, endpoint, port];
  }
  
subject: convert command from coffee script to js