Created
April 26, 2015 21:42
-
-
Save mattcollier/aab47ea932a56365f5d0 to your computer and use it in GitHub Desktop.
ES6 Class Example
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
"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