telnet-openvpn
Version:
Connect to OpenVPN management port through telnet.
218 lines (176 loc) • 9.3 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _telnetClient = require('telnet-client');
var _telnetClient2 = _interopRequireDefault(_telnetClient);
var _events = require('events');
var _events2 = _interopRequireDefault(_events);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _q = require('q');
var _q2 = _interopRequireDefault(_q);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
* Philip Smith
* Original repo at
* https://github.com/smithp1992/telnet-openvpn/
*/
var TelnetVPN = function (_EventEmitter) {
_inherits(TelnetVPN, _EventEmitter);
function TelnetVPN() {
_classCallCheck(this, TelnetVPN);
var _this = _possibleConstructorReturn(this, (TelnetVPN.__proto__ || Object.getPrototypeOf(TelnetVPN)).call(this));
_this.connection = new _telnetClient2.default();
// Telnet connected to management port
_this.connection.on('connect', function () {
_this.emit('connect');
});
// Sort and emit vpn console log
_this.connection.on('data', function (response) {
_this._emitData(response.toString());
});
// Telnet error
_this.connection.on('error', function (error) {
_this.disconnect();
_this.emit('error', error);
});
// Ending telnet session
_this.connection.on('end', function () {
_this.emit('end');
});
// Closing telnet session
_this.connection.on('close', function () {
_this.emit('close');
});
return _this;
}
// Connect To VPN through Telnet
_createClass(TelnetVPN, [{
key: 'connect',
value: function connect(options) {
var _this2 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
var params = _lodash2.default.defaults(options, {
host: '127.0.0.1',
port: 1337,
negotiationMandatory: true,
ors: '\r\n',
sendTimeout: 3000
});
resolve(_this2.connection.connect(params));
});
}
// Authenticate user credentials
}, {
key: 'authorize',
value: function authorize(auth) {
var _this3 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
_this3.exec('username "Auth" ' + auth.username).then(function () {
_this3.exec('password "Auth" ' + auth.password).then(function () {
resolve();
});
}).catch(function (error) {
reject(error);
});
});
}
// Disconnects from stream. Some data may still be sent
}, {
key: 'disconnect',
value: function disconnect() {
var _this4 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
if (_this4.connection) {
resolve(_this4.exec('signal SIGTERM'));
} else {
reject(new Error("Telnet connection undefined"));
}
});
}
// Removes all instances of connection input/output
}, {
key: 'destroy',
value: function destroy() {
var _this5 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
if (_this5.connection) {
resolve(_this5.connection.destroy());
} else {
reject(new Error("Telnet connection undefined"));
}
});
}
// Exit telnet terminal (Only closes telnet terminal)
}, {
key: 'end',
value: function end() {
var _this6 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
if (_this6.connection) {
resolve(_this6.connection.end());
} else {
reject(new Error("Telnet connection undefined"));
}
});
}
// Telnet OpenVPN Execution Commands
}, {
key: 'exec',
value: function exec(param) {
var _this7 = this;
// -> Promise
return _q2.default.Promise(function (resolve, reject, notify) {
if (_this7.connection) {
_this7.connection.send(param, function (error) {
if (error) reject(error);
resolve();
});
} else {
reject(_this7.emit('error', 'Error: connection Not Established'));
}
});
}
// Emit data from provided response string
}, {
key: '_emitData',
value: function _emitData(response) {
var _this8 = this;
_lodash2.default.each(response.split("\n"), function (data) {
if (data) {
if (data.substr(1, 5) === 'STATE') {
_this8.emit('data', { state: data.substr(7).split(",") });
} else if (data.substr(1, 4) === 'HOLD') {
_this8.emit('data', { hold: true });
} else if (data.substr(0, 7) === 'SUCCESS') {
if (data.substr(9, 3) === 'pid') {
_this8.emit('data', { pid: data.substr(13).trim() });
} else {
_this8.emit('data', { success: data.substr(8) });
}
} else if (data.substr(0, 5) === 'FATAL' || data && data.substr(0, 5) === 'ERROR') {
_this8.emit('error', data);
} else if (data.substr(1, 13) === 'BYTECOUNT_CLI') {
_this8.emit('data', { bytecount_cli: data.substr(15).trim().split(",") });
} else if (data.substr(1, 9) === 'BYTECOUNT') {
_this8.emit('data', { bytecount: data.substr(11).split(",") });
} else if (data.substr(1, 8) === 'PASSWORD') {
_this8.emit('data', { password: data.substr(10).split(",") });
} else if (data.substr(1, 3) === 'LOG') {
_this8.emit('log', data.substr(4).split(',')[2]);
} else {
_this8.emit('log', data);
}
}
});
}
}]);
return TelnetVPN;
}(_events2.default);
module.exports = TelnetVPN;