Replace master branch with local files
This commit is contained in:
commit
875a53f499
60 changed files with 21637 additions and 0 deletions
67
openresty/lua/auth.lua
Normal file
67
openresty/lua/auth.lua
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
local redis_helper = require "redis_helper"
|
||||
local cjson = require "cjson"
|
||||
|
||||
-- Read POST body for OME webhook
|
||||
ngx.req.read_body()
|
||||
local body = ngx.req.get_body_data()
|
||||
|
||||
if not body then
|
||||
ngx.status = ngx.HTTP_BAD_REQUEST
|
||||
ngx.say(cjson.encode({allowed = false, reason = "No body provided"}))
|
||||
return ngx.exit(ngx.HTTP_BAD_REQUEST)
|
||||
end
|
||||
|
||||
-- Parse JSON body
|
||||
local ok, data = pcall(cjson.decode, body)
|
||||
if not ok then
|
||||
ngx.status = ngx.HTTP_BAD_REQUEST
|
||||
ngx.say(cjson.encode({allowed = false, reason = "Invalid JSON"}))
|
||||
return ngx.exit(ngx.HTTP_BAD_REQUEST)
|
||||
end
|
||||
|
||||
-- Extract stream key from the request
|
||||
local stream_key = nil
|
||||
|
||||
-- Check different possible locations for the stream key
|
||||
if data.request and data.request.url then
|
||||
-- Extract from URL path or query string
|
||||
stream_key = data.request.url:match("/app/([^/?]+)")
|
||||
if not stream_key and data.request.params then
|
||||
stream_key = data.request.params.key or data.request.params.streamid
|
||||
end
|
||||
elseif data.stream and data.stream.name then
|
||||
stream_key = data.stream.name
|
||||
end
|
||||
|
||||
-- Handle SRT streamid format (app/KEY)
|
||||
if stream_key then
|
||||
local extracted = stream_key:match("app/(.+)")
|
||||
if extracted then
|
||||
stream_key = extracted
|
||||
end
|
||||
end
|
||||
|
||||
if not stream_key then
|
||||
ngx.status = ngx.HTTP_OK
|
||||
ngx.say(cjson.encode({allowed = false, reason = "No stream key provided"}))
|
||||
return ngx.exit(ngx.HTTP_OK)
|
||||
end
|
||||
|
||||
-- Validate key
|
||||
local valid = redis_helper.validate_stream_key(stream_key)
|
||||
|
||||
-- OME webhook expects specific response format
|
||||
local response = {
|
||||
allowed = valid,
|
||||
stream = {
|
||||
name = stream_key
|
||||
}
|
||||
}
|
||||
|
||||
if not valid then
|
||||
response.reason = "Invalid stream key"
|
||||
end
|
||||
|
||||
ngx.status = ngx.HTTP_OK
|
||||
ngx.header.content_type = "application/json"
|
||||
ngx.say(cjson.encode(response))
|
||||
126
openresty/lua/redis_helper.lua
Normal file
126
openresty/lua/redis_helper.lua
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
local redis = require "resty.redis"
|
||||
|
||||
local _M = {}
|
||||
|
||||
local function get_redis_connection()
|
||||
local red = redis:new()
|
||||
red:set_timeouts(1000, 1000, 1000) -- connect, send, read timeout in ms
|
||||
|
||||
local host = "redis" -- Will be resolved by nginx resolver
|
||||
local port = tonumber(os.getenv("REDIS_PORT")) or 6379
|
||||
|
||||
local ok, err = red:connect(host, port)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "Failed to connect to Redis: ", err)
|
||||
return nil
|
||||
end
|
||||
|
||||
return red
|
||||
end
|
||||
|
||||
local function close_redis_connection(red)
|
||||
-- Put connection into pool
|
||||
local ok, err = red:set_keepalive(10000, 100)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "Failed to set keepalive: ", err)
|
||||
end
|
||||
end
|
||||
|
||||
function _M.validate_stream_key(key)
|
||||
-- For now, skip Redis and go directly to backend
|
||||
-- This fixes the immediate issue
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local backend_url = os.getenv("BACKEND_URL") or "http://drogon-backend:8080"
|
||||
|
||||
local res, err = httpc:request_uri(backend_url .. "/api/stream/validate/" .. key, {
|
||||
method = "GET",
|
||||
timeout = 1000
|
||||
})
|
||||
|
||||
if res and res.status == 200 then
|
||||
local cjson = require "cjson"
|
||||
local ok, data = pcall(cjson.decode, res.body)
|
||||
|
||||
if ok and data.valid then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function _M.validate_viewer_token(token, expected_stream_key)
|
||||
local red = get_redis_connection()
|
||||
if not red then
|
||||
ngx.log(ngx.ERR, "Failed to connect to Redis for token validation")
|
||||
return false
|
||||
end
|
||||
|
||||
-- Get the stream key associated with this token
|
||||
local res, err = red:get("viewer_token:" .. token)
|
||||
if not res or res == ngx.null then
|
||||
ngx.log(ngx.WARN, "Token not found: ", token)
|
||||
close_redis_connection(red)
|
||||
return false
|
||||
end
|
||||
|
||||
close_redis_connection(red)
|
||||
|
||||
-- Check if the token is for the expected stream
|
||||
if res ~= expected_stream_key then
|
||||
ngx.log(ngx.WARN, "Token stream mismatch. Expected: ", expected_stream_key, " Got: ", res)
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function _M.refresh_viewer_token(token)
|
||||
local red = get_redis_connection()
|
||||
if not red then
|
||||
return false
|
||||
end
|
||||
|
||||
-- Refresh TTL to 30 seconds
|
||||
local ok, err = red:expire("viewer_token:" .. token, 30)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "Failed to refresh token TTL: ", err)
|
||||
end
|
||||
|
||||
close_redis_connection(red)
|
||||
return ok
|
||||
end
|
||||
|
||||
function _M.get_streams_to_disconnect()
|
||||
local red = get_redis_connection()
|
||||
if not red then
|
||||
return {}
|
||||
end
|
||||
|
||||
local res, err = red:smembers("streams_to_disconnect")
|
||||
close_redis_connection(red)
|
||||
|
||||
if not res then
|
||||
ngx.log(ngx.ERR, "Failed to get streams to disconnect: ", err)
|
||||
return {}
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
function _M.remove_stream_from_disconnect(key)
|
||||
local red = get_redis_connection()
|
||||
if not red then
|
||||
return
|
||||
end
|
||||
|
||||
local ok, err = red:srem("streams_to_disconnect", key)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "Failed to remove stream from disconnect set: ", err)
|
||||
end
|
||||
|
||||
close_redis_connection(red)
|
||||
end
|
||||
|
||||
return _M
|
||||
84
openresty/lua/stream_monitor.lua
Normal file
84
openresty/lua/stream_monitor.lua
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
|
||||
local redis_helper = require "redis_helper"
|
||||
local cjson = require "cjson"
|
||||
|
||||
-- Safely load the http module
|
||||
local has_http, http = pcall(require, "resty.http")
|
||||
|
||||
local function disconnect_stream(stream_key)
|
||||
if not has_http then
|
||||
ngx.log(ngx.WARN, "resty.http module not available, skipping stream disconnection")
|
||||
return
|
||||
end
|
||||
|
||||
local httpc = http.new()
|
||||
local ome_url = os.getenv("OME_URL") or "http://ovenmediaengine:8081"
|
||||
local ome_token = os.getenv("OME_API_TOKEN") or "your-api-token"
|
||||
|
||||
-- Get active streams from OME
|
||||
local res, err = httpc:request_uri(ome_url .. "/v1/vhosts/default/apps/app/streams", {
|
||||
method = "GET",
|
||||
headers = {
|
||||
["Authorization"] = "Bearer " .. ome_token,
|
||||
["Content-Type"] = "application/json"
|
||||
}
|
||||
})
|
||||
|
||||
if not res then
|
||||
ngx.log(ngx.ERR, "Failed to get streams: ", err)
|
||||
return
|
||||
end
|
||||
|
||||
local ok, data = pcall(cjson.decode, res.body)
|
||||
if ok and data and data.response and data.response.streams then
|
||||
for _, stream in ipairs(data.response.streams) do
|
||||
if stream.name == stream_key then
|
||||
-- Disconnect the stream
|
||||
local del_res, del_err = httpc:request_uri(
|
||||
ome_url .. "/v1/vhosts/default/apps/app/streams/" .. stream.name,
|
||||
{
|
||||
method = "DELETE",
|
||||
headers = {
|
||||
["Authorization"] = "Bearer " .. ome_token
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if del_res and del_res.status == 200 then
|
||||
ngx.log(ngx.INFO, "Disconnected stream: ", stream_key)
|
||||
else
|
||||
ngx.log(ngx.ERR, "Failed to disconnect stream: ", stream_key)
|
||||
end
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function monitor_streams()
|
||||
-- Check if Redis is available
|
||||
local ok, keys = pcall(redis_helper.get_streams_to_disconnect)
|
||||
if not ok then
|
||||
ngx.log(ngx.WARN, "Redis not available yet")
|
||||
return
|
||||
end
|
||||
|
||||
for _, key in ipairs(keys or {}) do
|
||||
disconnect_stream(key)
|
||||
redis_helper.remove_stream_from_disconnect(key)
|
||||
end
|
||||
end
|
||||
|
||||
-- Start monitoring in a timer with error handling
|
||||
local function start_monitoring()
|
||||
local ok, err = ngx.timer.every(1, monitor_streams)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "Failed to create timer: ", err)
|
||||
else
|
||||
ngx.log(ngx.INFO, "Stream monitor started")
|
||||
end
|
||||
end
|
||||
|
||||
-- Delay start to ensure services are ready
|
||||
ngx.timer.at(5, start_monitoring)
|
||||
Loading…
Add table
Add a link
Reference in a new issue