Skip to content

Instantly share code, notes, and snippets.

@creationix creationix/tls-connect-luvit.lua Secret
Last active Aug 29, 2015

Embed
What would you like to do?
local openssl = require('openssl')
local uv = require('uv')
local bit = require('bit')
local pathjoin = require('luvi').path.join
local ssl = openssl.ssl
local x509 = openssl.x509
local bio = openssl.bio
-- Sync readfile using uv apis
local function readfile(path)
local fd, stat, chunk, err
fd = assert(uv.fs_open(path, "r", 420)) -- 420 = 0644)
stat, err = uv.fs_fstat(fd)
if stat then
chunk, err = uv.fs_read(fd, stat.size, 0)
end
uv.fs_close(fd)
if chunk then return chunk end
error(err)
end
local start, onAddress, onConnect
local address, input, output
local xcert = x509.read(readfile(pathjoin(module.dir, "ca.cer")))
local ctx = ssl.ctx_new("TLSv1_2")
ctx:set_verify({"none"})
ctx:options(bit.bor(ssl.no_sslv2, ssl.no_sslv3, ssl.no_compression))
local client = uv.new_tcp()
function start()
uv.getaddrinfo("luvit.io", "https", {
socktype= "STREAM",
family = "INET"
}, onAddress)
end
function onAddress(err, res)
assert(not err, err)
assert(#res > 0)
address = res[1]
uv.tcp_connect(client, address.addr, address.port, onConnect)
end
function onConnect(client, err)
assert(not err, err)
input, output = bio.mem(8192), bio.mem(8192)
p {
input = input,
output = output,
address = address,
xcert = xcert,
ctx = ctx,
client = client,
}
end
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.