project Websites / videos.bentasker.co.uk avatar

Websites / videos.bentasker.co.uk: af9ff643




VID-11 Check for black listed IPs

VID-11 Check for black listed IPs

If the user's IP is in the table {{blacklisted_ips}} refuse to serve them a token and include error message "Users IP is blacklisted"

Commit af9ff643.

Authored 2019-04-16T17:29:03.000+01:00 by B Tasker in project Websites / videos.bentasker.co.uk

+30 lines -7 lines

Changes

diff --git a/resources/tokenisation/minter/token_gen.lua b/resources/tokenisation/minter/token_gen.lua
--- a/resources/tokenisation/minter/token_gen.lua
+++ b/resources/tokenisation/minter/token_gen.lua
# @@ -27,12 +27,24 @@ local excluded_ips = {"127.0.0.1"}
# local permitted_referrers = {"*.bentasker.co.uk"}
#
# -- Blacklisted clients
# -local blacklisted_ips = {}
# +local blacklisted_ips = {'127.0.1.1'}
#
#
#
# -- Functions
#
# +function table.contains(table, element)
# + -- From https://stackoverflow.com/questions/2282444/how-to-check-if-a-table-contains-an-element-in-lua
# + for _, value in pairs(table) do
# + if value == element then
# + return true
# + end
# + end
# + return false
# +end
# +
# +
# +
# local function sendSuccessful(tok,expires,debug)
# -- response format should be
# -- {"status": true, "token": "OqBvCvqg49QJgGKDHFbdNccB", "exp": 0}
# @@ -49,11 +61,16 @@ local function mint_token(path,expires,ip,secret)
# end
#
#
# -local function sendFailed()
# - -- response format should be
# - -- {"status": true, "token": "OqBvCvqg49QJgGKDHFbdNccB", "exp": 0}
# - local r = {status = false, token = 'failed', exp = '-1'}
# - return json.encode(r)
# +local function sendFailed(reason)
# + local r = {status = false, token = 'failed', exp = '-1', msg = reason}
# +
# + -- ngx.status = 403
# + -- ngx.say(json.encode(r))
# + -- ngx.exit(403)
# +
# + -- Offline only
# + print(json.encode(r))
# +
# end
#
#
# @@ -69,7 +86,7 @@ local vidpath = '2019/08/16.m3u8' -- for offline testing only
#
# -- Get the user's IP
# -- local ip = ngx.var.remote_addr
# -local ip = '127.0.0.1' -- for offline testing only
# +local ip = '127.0.1.1' -- for offline testing only
#
#
# -- Grab the HTTP referrer header if present
# @@ -89,6 +106,12 @@ local httpref = 'https://snippets.bentasker.co.uk/foobar'
# -- If the above passes, or the ip is in excluded_ips, then we should mint the token
# -- otherwise, call sendFailed()
#
# +if table.contains(blacklisted_ips,ip)
# +then
# + sendFailed("Users IP is blacklisted")
# + return
# +end
# +
#
#
# -- Calculate when the token should expire
#