Skip to content

Commit

Permalink
add https client and server support by bios
Browse files Browse the repository at this point in the history
  • Loading branch information
lvzixun committed Mar 14, 2019
1 parent 1422cae commit 7746193
Show file tree
Hide file tree
Showing 6 changed files with 634 additions and 20 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ update3rd :
CSERVICE = snlua logger gate harbor
LUA_CLIB = skynet \
client \
bson md5 sproto lpeg
bson md5 sproto lpeg ltls

LUA_CLIB_SKYNET = \
lua-skynet.c lua-seri.c \
Expand Down Expand Up @@ -106,6 +106,9 @@ $(LUA_CLIB_PATH)/client.so : lualib-src/lua-clientsocket.c lualib-src/lua-crypt.
$(LUA_CLIB_PATH)/sproto.so : lualib-src/sproto/sproto.c lualib-src/sproto/lsproto.c | $(LUA_CLIB_PATH)
$(CC) $(CFLAGS) $(SHARED) -Ilualib-src/sproto $^ -o $@

$(LUA_CLIB_PATH)/ltls.so : lualib-src/ltls.c | $(LUA_CLIB_PATH)
$(CC) $(CFLAGS) $(SHARED) -Iskynet-src -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include $^ -o $@ -lssl

$(LUA_CLIB_PATH)/lpeg.so : 3rd/lpeg/lpcap.c 3rd/lpeg/lpcode.c 3rd/lpeg/lpprint.c 3rd/lpeg/lptree.c 3rd/lpeg/lpvm.c | $(LUA_CLIB_PATH)
$(CC) $(CFLAGS) $(SHARED) -I3rd/lpeg $^ -o $@

Expand Down
58 changes: 50 additions & 8 deletions examples/simpleweb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,64 @@ local urllib = require "http.url"
local table = table
local string = string

local mode = ...
local mode, protocol = ...
protocol = protocol or "http"

if mode == "agent" then

local function response(id, ...)
local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
local function response(id, write, ...)
local ok, err = httpd.write_response(write, ...)
if not ok then
-- if err == sockethelper.socket_error , that means socket closed.
skynet.error(string.format("fd = %d, %s", id, err))
end
end


local SSLCTX_SERVER = nil
local function gen_interface(protocol, fd)
if protocol == "http" then
return {
init = nil,
close = nil,
read = sockethelper.readfunc(fd),
write = sockethelper.writefunc(fd),
}
elseif protocol == "https" then
local tls = require "http.tlshelper"
if not SSLCTX_SERVER then
SSLCTX_SERVER = tls.newctx()
-- gen cert and key
-- openssl req -x509 -newkey rsa:2048 -days 3650 -nodes -keyout server-key.pem -out server-cert.pem
local certfile = skynet.getenv("certfile") or "./server-cert.pem"
local keyfile = skynet.getenv("keyfile") or "./server-key.pem"
print(certfile, keyfile)
SSLCTX_SERVER:set_cert(certfile, keyfile)
end
local tls_ctx = tls.newtls("server", SSLCTX_SERVER)
return {
init = tls.init_responsefunc(fd, tls_ctx),
close = tls.closefunc(tls_ctx),
read = tls.readfunc(fd, tls_ctx),
write = tls.writefunc(fd, tls_ctx),
}
else
error(string.format("Invalid protocol: %s", protocol))
end
end

skynet.start(function()
skynet.dispatch("lua", function (_,_,id)
socket.start(id)
local interface = gen_interface(protocol, id)
if interface.init then
interface.init()
end
-- limit request body size to 8192 (you can pass nil to unlimit)
local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
local code, url, method, header, body = httpd.read_request(interface.read, 8192)
if code then
if code ~= 200 then
response(id, code)
response(id, interface.write, code)
else
local tmp = {}
if header.host then
Expand All @@ -44,7 +82,7 @@ skynet.start(function()
table.insert(tmp, string.format("%s = %s",k,v))
end
table.insert(tmp, "-----body----\n" .. body)
response(id, code, table.concat(tmp,"\n"))
response(id, interface.write, code, table.concat(tmp,"\n"))
end
else
if url == sockethelper.socket_error then
Expand All @@ -54,19 +92,23 @@ skynet.start(function()
end
end
socket.close(id)
if interface.close then
interface.close()
end
end)
end)

else

skynet.start(function()
local agent = {}
local protocol = "http"
for i= 1, 20 do
agent[i] = skynet.newservice(SERVICE_NAME, "agent")
agent[i] = skynet.newservice(SERVICE_NAME, "agent", protocol)
end
local balance = 1
local id = socket.listen("0.0.0.0", 8001)
skynet.error("Listen web port 8001")
skynet.error(string.format("Listen web port 8001 protocol:%s", protocol))
socket.start(id , function(id, addr)
skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance]))
skynet.send(agent[balance], "lua", id)
Expand Down
Loading

0 comments on commit 7746193

Please sign in to comment.