Last active
October 7, 2015 03:38
-
-
Save jas-/3099437 to your computer and use it in GitHub Desktop.
Threaded TLS server
This file contains hidden or 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
module.exports = { | |
/* returns tls authorization */ | |
_a: function(o) | |
{ | |
return o.authorized ? 'authorized' : 'unauthorized: '+o.authorizationError; | |
}, | |
/* returns connecting host */ | |
_host: function(o) | |
{ | |
return o.remoteAddress; | |
}, | |
/* returns server bound address */ | |
_srv: function(server) | |
{ | |
var _s = server.address(); | |
return _s.address; | |
}, | |
/* return sserver bound port */ | |
_prt: function(server) | |
{ | |
var _p = server.address(); | |
return _p.port; | |
}, | |
/* returns date as MM/DD/YYYY */ | |
_date: function() | |
{ | |
var _d = new Date(); | |
return _d.getMonth()+'/'+_d.getDate()+'/'+_d.getFullYear(); | |
} | |
}; |
This file contains hidden or 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
/* include libraries */ | |
var cluster = require('cluster'); | |
/* main */ | |
if (cluster.isMaster) { | |
require('os').cpus().forEach(function(){ | |
cluster.fork(); | |
}); | |
} else { | |
require('./worker.js'); | |
} |
This file contains hidden or 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
/* included libraries */ | |
var tls = require('tls'); | |
var fs = require('fs'); | |
var lib = require('./lib'); | |
/* tls server options */ | |
var opts = { | |
key: fs.readFileSync('certificate.key'), | |
cert: fs.readFileSync('certificate.cer'), | |
ca: [fs.readFileSync('authority.cer')], | |
passphrase: 'password', | |
ciphers: 'RC4-SHA:RC4:ECDHE-RSA-AES256-SHA:AES256-SHA:HIGH:!MD5:!aNULL:!EDH:!AESGCM', | |
honorCipherOrder: true, | |
requestCert: true | |
}; | |
/* create tls server */ | |
var server = tls.createServer(opts, function(cleartextStream) { | |
console.log('[%s] [%d] %s %s', lib._date(), process.pid, lib._host(cleartextStream), lib._a(cleartextStream)); | |
cleartextStream.on('data', function(data) { | |
console.log('[%s] [%d] - recieved "%s"', lib._date(), process.pid, data); | |
}); | |
cleartextStream.on('error', function() { | |
console.log('[%d] [%d] - '); | |
}); | |
cleartextStream.on('end', function() { | |
server.close(); | |
}); | |
cleartextStream.setEncoding('utf8'); | |
cleartextStream.pipe(cleartextStream); | |
}); | |
server.listen(8000, function() { | |
console.log('[%s] [%d] - worker started on %s %d...', lib._date(), process.pid, lib._srv(server), lib._prt(server)); | |
}); |
This file contains hidden or 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
def set_options(opt): | |
opt.tool_options("compiler_cxx") | |
def configure(conf): | |
conf.check_tool("compiler_cxx") | |
conf.check_tool("node_addon") | |
def build(bld): | |
obj = bld.new_task_gen("cxx", "shlib", "node_addon") | |
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"] | |
obj.target = "xtension" | |
obj.source = "src/xtension.cpp" |
This file contains hidden or 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
#include <v8.h> | |
#include <node.h> | |
#include <string> | |
using namespace v8; | |
class Xtension : node::ObjectWrap { | |
private: | |
public: | |
std::string txt; | |
static v8::Persistent<FunctionTemplate> pft; | |
static void Init(Handle<Object> target) { | |
v8::HandleScope scope; | |
v8::Local<FunctionTemplate> lft = v8::FunctionTemplate::New(New); | |
Xtension::pft = v8::Persistent<FunctionTemplate>::New(lft); | |
Xtension::pft->InstanceTemplate()->SetInternalFieldCount(1); | |
Xtension::pft->SetClassName(v8::String::NewSymbol("Xtension")); | |
Xtension::pft->InstanceTemplate()->SetAccessor(String::New("txt"), GetTxt, SetTxt); | |
NODE_SET_PROTOTYPE_METHOD(Xtension::pft, "options", Options); | |
target->Set(String::NewSymbol("xtension"), Xtension::pft->GetFunction()); | |
} | |
static Handle<Value> New(const Arguments& args) { | |
HandleScope scope; | |
Xtension * inst = new Xtension(); | |
inst->Wrap(args.This()); | |
return args.This(); | |
} | |
static v8::Handle<Value> Options(const Arguments& args) { | |
v8::HandleScope scope; | |
Xtension * inst = node::ObjectWrap::Unwrap<Xtension>(args.This()); | |
if (args.Length() < 0 || !args[0]->IsObject()) { | |
} | |
inst->Wrap(args.This()); | |
return args.This(); | |
} | |
static v8::Handle<Value> GetTxt(v8::Local<v8::String> property, const v8::AccessorInfo& info) { | |
Xtension * inst = node::ObjectWrap::Unwrap<Xtension>(info.Holder()); | |
return v8::String::New(inst->txt.c_str()); | |
} | |
static void SetTxt(Local<String> property, Local<Value> value, const AccessorInfo& info) { | |
Xtension * inst = node::ObjectWrap::Unwrap<Xtension>(info.Holder()); | |
v8::String::Utf8Value v8str(value); | |
inst->txt = *v8str; | |
} | |
}; | |
v8::Persistent<FunctionTemplate> Xtension::pft; | |
extern "C" { | |
static void init(Handle<Object> target) { | |
Xtension::Init(target); | |
} | |
NODE_MODULE(xtension, init); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment