Skip to content

Commit

Permalink
Build min version
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Burkalev authored and Konstantin Burkalev committed Nov 8, 2016
1 parent f989ae7 commit 902c611
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 166 deletions.
52 changes: 52 additions & 0 deletions build/cleanup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--
-- Project: wiola
-- User: Konstantin Burkalev
-- Date: 08.11.16
--

local _M = {}

--
-- Cleans up wiola session data in redis store
--
-- redis - Redis instance on which to operate
-- regId - WAMP session registration ID
--
function _M.cleanupSession(redis, regId)

local session = redis:array_to_hash(redis:hgetall("wiSes" .. regId))
session.realm = session.realm or ""

local subscriptions = redis:array_to_hash(redis:hgetall("wiRealm" .. session.realm .. "Subs"))

for k, v in pairs(subscriptions) do
redis:srem("wiRealm" .. session.realm .. "Sub" .. k .. "Sessions", regId)
if redis:scard("wiRealm" .. session.realm .. "Sub" .. k .. "Sessions") == 0 then
redis:del("wiRealm" .. session.realm .. "Sub" .. k .. "Sessions")
redis:hdel("wiRealm" .. session.realm .. "Subs",k)
redis:hdel("wiRealm" .. session.realm .. "RevSubs",v)
end
end

local rpcs = redis:array_to_hash(redis:hgetall("wiSes" .. regId .. "RPCs"))

for k, v in pairs(rpcs) do
redis:srem("wiRealm" .. session.realm .. "RPCs",k)
redis:del("wiRPC" .. k)
end

redis:del("wiSes" .. regId .. "RPCs")
redis:del("wiSes" .. regId .. "RevRPCs")
redis:del("wiSes" .. regId .. "Challenge")

redis:srem("wiRealm" .. session.realm .. "Sessions", regId)
if redis:scard("wiRealm" .. session.realm .. "Sessions") == 0 then
redis:srem("wiolaRealms",session.realm)
end

redis:del("wiSes" .. regId .. "Data")
redis:del("wiSes" .. regId)
redis:srem("wiolaIds",regId)
end

return _M
120 changes: 120 additions & 0 deletions build/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
--
-- Project: wiola
-- User: Konstantin Burkalev
-- Date: 01.11.16
--


-- Redis connection configuration
local redisConf = {
host = "unix:/tmp/redis.sock",
port = nil,
db = nil
}

-- Wiola Runtime configuration
local wiolaConf = {
callerIdentification = "auto", -- auto | never | always
cookieAuth = {
authType = "none", -- none | static | dynamic
cookieName = "wampauth",
staticCredentials = nil, --{
-- "user1", "user2:password2", "secretkey3"
--},
authCallback = nil
},
wampCRA = {
authType = "none", -- none | static | dynamic
staticCredentials = nil, --{
-- user1 = { authrole = "userRole1", secret="secret1" },
-- user2 = { authrole = "userRole2", secret="secret2" }
--},
challengeCallback = nil,
authCallback = nil
}
}

local _M = {}

--
-- Get or set Wiola Runtime configuration
--
-- config - Configuration table with possible options:
-- {
-- redis = {
-- host = string - redis host or unix socket (default: "unix:/tmp/redis.sock"),
-- port = number - redis port in case of network use (default: nil),
-- db = number - redis database to select (default: nil)
-- },
-- callerIdentification = string - Disclose caller identification?
-- Possible values: auto | never | always. (default: "auto")
-- }
-- without params it just returns current configuration
--
function _M.config(config)

if not config then
local conf = wiolaConf
conf.redis = redisConf
return conf
end

if config.redis then

if config.redis.host ~= nil then
redisConf.host = config.redis.host
end

if config.redis.port ~= nil then
redisConf.port = config.redis.port
end

if config.redis.db ~= nil then
redisConf.db = config.redis.db
end
end

if config.callerIdentification ~= nil then
wiolaConf.callerIdentification = config.callerIdentification
end

if config.cookieAuth then

if config.cookieAuth.authType ~= nil then
wiolaConf.cookieAuth.authType = config.cookieAuth.authType
end

if config.cookieAuth.cookieName ~= nil then
wiolaConf.cookieAuth.cookieName = config.cookieAuth.cookieName
end

if config.cookieAuth.staticCredentials ~= nil then
wiolaConf.cookieAuth.staticCredentials = config.cookieAuth.staticCredentials
end

if config.cookieAuth.authCallback ~= nil then
wiolaConf.cookieAuth.authCallback = config.cookieAuth.authCallback
end
end

if config.wampCRA then

if config.wampCRA.authType ~= nil then
wiolaConf.wampCRA.authType = config.wampCRA.authType
end

if config.wampCRA.staticCredentials ~= nil then
wiolaConf.wampCRA.staticCredentials = config.wampCRA.staticCredentials
end

if config.wampCRA.challengeCallback ~= nil then
wiolaConf.wampCRA.challengeCallback = config.wampCRA.challengeCallback
end

if config.wampCRA.authCallback ~= nil then
wiolaConf.wampCRA.authCallback = config.wampCRA.authCallback
end
end
end

return _M
53 changes: 37 additions & 16 deletions build/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ local wiola = require "wiola"
local wampServer = wiola:new()

local webSocket, err = wsServer:new({
timeout = 5000,
max_payload_len = 65535
timeout = tonumber(ngx.var.wiola_socket_timeout, 10) or 100,
max_payload_len = tonumber(ngx.var.wiola_max_payload_len, 10) or 65535
})

if not webSocket then
Expand All @@ -23,11 +23,36 @@ end

local sessionId, dataType = wampServer:addConnection(ngx.var.connection, ngx.header["Sec-WebSocket-Protocol"])

local cleanExit = false
local function removeConnection(premature, sessionId)

while true do
local data, typ, err = webSocket:recv_frame()
local redisOk, redisErr
local redisLib = require "resty.redis"
local wiola_config = require "wiola.config"

local redis = redisLib:new()
local conf = wiola_config.config()

if conf.redis.port == nil then
redisOk, redisErr = redis:connect(conf.redis.host)
else
redisOk, redisErr = redis:connect(conf.redis.host, conf.redis.port)
end

if redisOk and conf.redis.db ~= nil then
redis:select(conf.redis.db)
end

local wiola_cleanup = require "wiola.cleanup"
wiola_cleanup.cleanupSession(redis, sessionId)

end

--local ok, err = ngx.on_abort(removeConnection)
--if not ok then
-- ngx.exit(444)
--end

while true do
local cliData, cliErr = wampServer:getPendingData(sessionId)

while cliData ~= ngx.null do
Expand All @@ -45,32 +70,34 @@ while true do
end

if webSocket.fatal then
wampServer:removeConnection(sessionId)
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
end

local data, typ, err = webSocket:recv_frame()

if not data then

local bytes, err = webSocket:send_ping()
if not bytes then
wampServer:removeConnection(sessionId)
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
end

elseif typ == "close" then
wampServer:removeConnection(sessionId)
local bytes, err = webSocket:send_close(1000, "Closing connection")
if not bytes then
return
end
cleanExit = true
ngx.timer.at(0, removeConnection, sessionId)
webSocket:send_close()
break

elseif typ == "ping" then

local bytes, err = webSocket:send_pong()
if not bytes then
wampServer:removeConnection(sessionId)
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
end

Expand All @@ -84,9 +111,3 @@ while true do

end
end

-- Just for clearance
if not cleanExit then
webSocket:send_close()
wampServer:removeConnection(sessionId)
end
37 changes: 36 additions & 1 deletion build/headers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,42 @@
-- Date: 16.03.14
--

ngx.header["Server"] = "wiola/Lua v0.5.0"
ngx.header["Server"] = "wiola/Lua v0.6.0"

function has(tab, val)
for index, value in ipairs (tab) do
if value == val then
return true
end
end

return false
end

local wiola_config = require "wiola.config"
local conf = wiola_config.config()

if conf.cookieAuth.authType ~= "none" then

local cookieValue = ngx.unescape_uri(ngx.var["cookie_" .. conf.cookieAuth.cookieName])

if not cookieValue then
return ngx.exit(ngx.HTTP_FORBIDDEN)
end

if conf.cookieAuth.authType == "static" then

if not has(conf.cookieAuth.staticCredentials, cookieValue) then
return ngx.exit(ngx.HTTP_FORBIDDEN)
end

elseif conf.cookieAuth.authType == "dynamic" then

if not conf.cookieAuth.authCallback(cookieValue) then
return ngx.exit(ngx.HTTP_FORBIDDEN)
end
end
end

local wsProto = ngx.req.get_headers()["Sec-WebSocket-Protocol"]

Expand Down
Loading

0 comments on commit 902c611

Please sign in to comment.