and_cesbo

Astra Relay: auth_request

Jun 11th, 2014
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. channels = {
  2.     -- promo channel for auth_request redirect
  3.     ["promo"] = "...",
  4.     -- channel list
  5. }
  6.  
  7. access_list_file = "/etc/astra/access_list.txt"
  8. access_redirect = "/promo"
  9.  
  10. function ip_to_number(ip)
  11.     ip = split(ip, "%.")
  12.     if #ip ~= 4 then return 0 end
  13.     for k,v in ipairs(ip) do
  14.         ip[k] = tonumber(v)
  15.         if not ip[k] then return 0 end
  16.     end
  17.     return (ip[1] * 0x1000000) + (ip[2] * 0x10000) + (ip[3] * 0x100) + (ip[4])
  18. end
  19.  
  20. function mask_to_number(mask)
  21.     local a = 0xFFFFFFFF
  22.     local m = 32 - tonumber(mask)
  23.     if m > 0 then
  24.         a = bit32.rshift(a, m)
  25.         a = bit32.lshift(a, m)
  26.     end
  27.     return a
  28. end
  29.  
  30. function ip_check(ip)
  31.     ip = ip_to_number(ip)
  32.     for _,v in ipairs(access_list) do
  33.         if v[1] == bit32.band(ip, v[2]) then return true end
  34.     end
  35.     return false
  36. end
  37.  
  38. function load_access_list()
  39.     local r = {}
  40.     local f = io.open(access_list_file, "r")
  41.     for ip in f:lines() do
  42.         local b = ip:find("/")
  43.         if b then
  44.             local a = ip_to_number(ip:sub(1, b - 1))
  45.             local m = mask_to_number(ip:sub(b + 1))
  46.             a = bit32.band(a, m)
  47.             table.insert(r, { a, m })
  48.         else
  49.             local a = ip_to_number(ip)
  50.             local m = mask_to_number(32)
  51.             table.insert(r, { a, m })
  52.         end
  53.     end
  54.     f:close()
  55.     return r
  56. end
  57.  
  58. access_list = load_access_list()
  59.  
  60. function auth_request(client_id, request, auth_callback)
  61.     if not request then
  62.         return nil
  63.     end
  64.  
  65.     if request.path == access_redirect then
  66.         auth_callback(true)
  67.         return nil
  68.     end
  69.  
  70.     if ip_check(request.addr) then
  71.         auth_callback(true)
  72.     else
  73.         auth_callback(access_redirect)
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment