Skip to content

Instantly share code, notes, and snippets.

@rphillips
Created November 18, 2014 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rphillips/cec93f314e9418e63ffb to your computer and use it in GitHub Desktop.
Save rphillips/cec93f314e9418e63ffb to your computer and use it in GitHub Desktop.
exports.connect = function(options, callback)
local defaults, hostname, context, conn, cleartext, tlsChain, tls
local port, onConnect
-- Setup options
defaults = {
ciphers = DEFAULT_CIPHERS,
rejectUnauthorized = false,
-- TODO checkServerIdentity
}
options = extend(defaults, options or {})
hostname = options.servername or options.host or
(options.socket and options.socket._host)
port = options.port
cleartext = Emitter:new()
tls = tlsCodec(options)
tlsChain = chain(tls.decoder, codec.wrapEmitter(cleartext), tls.encoder)
function onConnect()
local read, write = wrapStream(conn._handle)
tls.emitter:on('secureConnect', callback)
tlsChain(read, write)
end
conn = net.create(port, hostname, onConnect)
cleartext._socket = conn
cleartext._tls = tls
return cleartext
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment