Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/* | ||
!bin/targz | ||
!package.json | ||
!index.js | ||
!LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#!/usr/bin/node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### `v1.0.0` | ||
|
||
* Full code refactoring, tests and integrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var portastic = require('./lib/portastic'); | ||
portastic.Monitor = require('./lib/monitor'); | ||
|
||
module.exports = portastic; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Oops, something went wrong.