Skip to content

Instantly share code, notes, and snippets.

@mattcollier
Created April 26, 2015 21:42
Show Gist options
  • Save mattcollier/aab47ea932a56365f5d0 to your computer and use it in GitHub Desktop.
Save mattcollier/aab47ea932a56365f5d0 to your computer and use it in GitHub Desktop.
ES6 Class Example
"use strict";
var net = require('net');
class TorControlClient {
constructor() {
this.torHost = '127.0.0.1';
this.torControlPort = 9998;
this.torControlPassword = 'torControl2015';
this.initialized = false;
this.sendLocked = false;
this.controlSocket;
}
_sendLockAcquire() {
if(this.sendLocked) {
// already locked
return false;
} else {
this.sendLocked = true;
return true;
}
}
_sendLockRelease() {
this.sendLocked = false;
}
init() {
if(!this.initialized && this._sendLockAcquire()) {
this.controlSocket = net.createConnection({host: this.torHost,
port: this.torControlPort});
this.controlSocket.on('connect', (function() {
this.controlSocket.write('AUTHENTICATE "' + this.torControlPassword + '"\r\n');
}).bind(this));
this.controlSocket.on('data', (function(data) {
console.log('response:', data.toString());
this._sendLockRelease();
}).bind(this));
}
}
}
module.exports = TorControlClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment