Skip to content

Commit

Permalink
Not ready yet
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhoff committed Aug 13, 2015
1 parent 62cf9fc commit cb6d426
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
@@ -1 +1,30 @@
# Created by https://www.gitignore.io

### Node ###
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
26 changes: 26 additions & 0 deletions .jshintrc
@@ -0,0 +1,26 @@
{
"node": true,
"boss": true,
"bitwise": true,
"noempty": true,
"latedef": true,
"camelcase": false,
"eqeqeq": true,
"smarttabs": true,
"undef": true,
"unused": true,
"newcap": true,
"trailing": true,
"maxlen": 80,
"maxcomplexity": 5,
"indent": 2,
"quotmark": "single",
"strict": false,
"globals": {
"describe": true,
"before": true,
"after": true,
"it": true,
"test": true
}
}
5 changes: 5 additions & 0 deletions .npmignore
@@ -0,0 +1,5 @@
**/*
!bin/targz
!package.json
!index.js
!LICENSE
13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
Copyright (c) 2015, Alan Hoffmeister <alanhoffmeister@gmail.com>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
@@ -0,0 +1,48 @@
# tar.gz
[![Coverage Status](https://coveralls.io/repos/alanhoff/node-portastic/badge.svg?branch=master)][0]
[![Travis](https://travis-ci.org/alanhoff/node-portastic.svg)][1]
[![Dependencies](https://david-dm.org/alanhoff/node-portastic.svg)][2]

Pure javascript swiss knife for port management. Find open ports, monitor ports
and other port relates things.

### API

### Command line

It's also possible to use `portastic` as a command line utility, you just need
to install it globally with `npm install -g portastic`. Here is the help command
output.

```
```

### Testing

```bash
git clone git@github.com:alanhoff/node-portastic.git
cd node-portastic
npm install && npm test
```

### License (ISC)

```
Copyright (c) 2015, Alan Hoffmeister <alanhoffmeister@gmail.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
```

[0]: https://coveralls.io/github/alanhoff/node-portastic
[1]: https://travis-ci.org/alanhoff/node-portastic
[2]: https://david-dm.org/alanhoff/node-portastic
1 change: 1 addition & 0 deletions bin/portastic
@@ -0,0 +1 @@
#!/usr/bin/node
3 changes: 3 additions & 0 deletions history.md
@@ -0,0 +1,3 @@
### `v1.0.0`

* Full code refactoring, tests and integrations
4 changes: 4 additions & 0 deletions index.js
@@ -0,0 +1,4 @@
var portastic = require('./lib/portastic');
portastic.Monitor = require('./lib/monitor');

module.exports = portastic;
60 changes: 60 additions & 0 deletions lib/monitor.js
@@ -0,0 +1,60 @@
var portastic = require('./portastic');
var events = require('events');
var util = require('util');

var Monitor = function(ports, options) {
this._ports = ports;
this._options = options || {};
this._watchers = [];

if (this._options.autostart === false)
return;

this.start();
};
util.inherits(Monitor, events.EventEmitter);

Monitor.prototype.start = function() {
if (this._watchers.length)
return this.emit('error', new Error('Monitor already started'));

var that = this;
this._ports.forEach(function(port) {
that._watchers.push(that._watcher(port));
});
};

Monitor.prototype.stop = function() {
this._watchers.forEach(function(watcher) {
clearInterval(watcher.intervar);
});

this._watchers = [];
};

Monitor.prototype._watcher = function(port) {
var that = this;
var setup = {
state: null,
interval: setInterval(function() {
portastic.test(port)
.then(function(open) {
console.log(open);
var state = open.indexOf(port) !== -1;
if (setup.state !== state) {
that.emit(state ? 'open' : 'close', port);
setup.state = state;
}
})
.catch(function(err) {
process.nextTick(function() {
that.emit('error', err);
});
});
}, that._options.interval || 100)
};

return setup;
};

module.exports = Monitor;
133 changes: 133 additions & 0 deletions lib/portastic.js
@@ -0,0 +1,133 @@
var bluebird = require('bluebird');
var net = require('net');
var debug = require('debug');

module.exports = {

// Returns a promise that will be resolved with an array of open ports
test: bluebird.method(function(port, iface, callback) {
var log = debug('portastic:test');
var that = this;

if (typeof iface !== 'string' && !callback) {
callback = iface;
iface = null;
}

return bluebird.resolve([].concat(port))
.filter(function(port) {
var def = bluebird.defer();
var server = net.createServer();

server.on('error', function(err) {
if (err.code === 'EADDRINUSE') {
log('Port %s was in use', port);
return def.resolve(false);
}

def.reject(err);
});

log('Trying to test port %s', port);
server.listen(port, iface, function(err) {
if (err && err.code === 'EADDRINUSE') {
log('Port %s was in use', port);
return def.resolve(false);
}

if (err)
return def.reject(err);

server.close(function(err) {
if (err)
return def.reject(err);

log('Port %s was free', port);
log('TCP server on port %s closed', port);
def.resolve(true);
});
});

return def.promise;
})
.then(function(ports) {
if (!that._callback(callback, [ports]))
return ports;
})
.catch(function(err) {
if (!that._callback(callback, [err]))
throw err;
});
}),

// Find open ports in a range
find: bluebird.method(function(options, iface, callback) {
var log = debug('portastic:find');
var that = this;
var ports = [];
var result = [];

if (typeof iface !== 'string' && !callback) {
callback = iface;
iface = null;
}

for (var i = options.min; i <= options.max; i++)
ports.push(i);

log('Trying to find open ports between range %s and %s', options.min,
options.max);

var promise = bluebird.resolve(ports)
.each(function(port) {
return that.test(port, iface)
.then(function(open) {
var isOpen = open.indexOf(port) !== -1;

console.log(result);

if ((!options.retrieve && isOpen) ||
(result.length <= options.retrieve && isOpen)) {
log('Port %s was open, adding it to the result list', port);
return result.push(port);
}

if (!isOpen)
log('Port %s was not open', port);

if (options.retrieve && result.length <= options.retrieve) {
log('Result reached the maximum of %s ports, returning...',
options.retrieve);
promise.cancel();
}
});
})
.cancellable()
.catch(bluebird.CancellationError, function() {
return result;
})
.then(function(result) {
if (!that._callback(callback, [ports]))
return result;
})
.catch(function(err) {
if (!that._callback(callback, [err]))
throw err;
});

return promise;
}),

// Handles callbacks
_callback: function(cb, args) {
if (cb) {
// This will bypass promises errors catching
process.nextTick(function() {
cb.aplly(cb, args);
});
}

return !!cb;
}

};
40 changes: 40 additions & 0 deletions package.json
@@ -0,0 +1,40 @@
{
"name": "portastic",
"version": "1.0.0",
"description": "Pure javascript swiss knife for port management",
"main": "index.js",
"scripts": {
"test": "mocha --tdd --bail test/**/*-test.js",
"travis": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec test/**/*-test.js && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alanhoff/node-portastic.git"
},
"keywords": [
"port",
"management",
"open ports",
"find open",
"interface"
],
"author": "Alan Hoffmeister <alanhoffmeister@gmail.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/alanhoff/node-portastic/issues"
},
"bin": {
"portastic": "./bin/portastic"
},
"homepage": "https://github.com/alanhoff/node-portastic#readme",
"dependencies": {
"bluebird": "^2.9.34",
"debug": "^2.2.0"
},
"devDependencies": {
"chai": "^3.2.0",
"coveralls": "^2.11.4",
"istanbul": "^0.3.17",
"mocha": "^2.2.5"
}
}

0 comments on commit cb6d426

Please sign in to comment.