tls = require('tls'),
util = require('util'),
EventEmitter = require('events').EventEmitter,
+ crypto = require('crypto'),
ipaddr = require('ipaddr.js');
var SocksConnection = function (destination, socks) {
this.socksSocket = net.connect({host: socks.host, port: socks.port}, socksConnected.bind(this));
this.socksSocket.once('data', socksAuth.bind(this));
+ this.socksSocket.on('error', socksError);
};
util.inherits(SocksConnection, EventEmitter);
}
};
-var emitSocket = function () {
+var starttls = function () {
var that = this;
- this.socksSocket.setEncoding('utf8');
- this.emit('socksConnect', this.socksSocket);
-};
\ No newline at end of file
+
+ var pair = tls.createSecurePair(crypto.createCredentials(), false);
+ pair.encrypted.pipe(this.socksSocket);
+ this.socksSocket.pipe(pair.encrypted);
+
+ pair.cleartext.socket = this.socksSocket;
+ pair.cleartext.encrypted = pair.encrypted;
+ pair.cleartext.authorised = false;
+
+ pair.on('secure', function () {
+ that.emit('socksConnect', pair.cleartext, pair.encrypted);
+ });
+}
+
+var socksError = function (err) {
+ console.log(err);
+}
+
+var emitSocket = function () {
+ if (this.destination.ssl) {
+ starttls.call(this);
+ } else {
+ this.emit('socksConnect', this.socksSocket);
+ }
+};