-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
84 lines (67 loc) · 1.91 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--[[lit-meta
name = "ccuser44/rapid-match-domain"
version = "1.0.4"
homepage = "https://github.com/ccuser44/rapid-match-domain"
description = "A fast Lua library to check if a domain name in a list of domain names. Wildcards, subdomains, portnumbers and IP addresses supported."
license = "MIT"
]]
local function getHostnames(hostnames)
local hostnames = {}
for hostname in string.gsub(hostnames, "[ \r\t\v\f]*([%w%-%.:%*]+)[ \r\t\v\f]*\r?\n?") do
hostnames[#hostnames + 1] = hostname
end
return hostnames
end
local function getDomains(hostname)
local domains = {}
for domain in string.gmatch(string.lower(hostname), "([%w%-%*])+[%.:]?") do
hostnames[#domains + 1] = domain
end
return domains
end
local function addUrls(tbl, domains, useSubdomains)
for _, hostname in ipairs(type(domains) == "string" and getHostnames(domains) or domains) do
hostname = getDomains(hostname)
for i = #hostname, 1, -1 do
local domain = hostname[i]
if not tbl[domain] then
if i == 1 then
tbl[domain] = (useSubdomains == true)
else
tbl[domain] = {}
end
elseif type(tbl[domain]) == "boolean" and (i ~= 1 or tbl[domain] ~= useSubdomains) then
tbl[domain] = {
[tbl[domain]] = true
}
end
tbl = tbl[domain]
if i == 1 and type(tbl) ~= "boolean" then
tbl[useSubdomains == true] = true
elseif domain == "**" and type(tbl) == "table" then -- Recursive wildcards
tbl["**"] = tbl
end
end
end
return tbl
end
local function match(tbl, hostname)
local domains = getDomains(hostname)
for i = #hostname, 1, -1 do
tbl = tbl[domains[i]] or tbl["*"] or tbl["**"]
if tbl == nil then
return false
if tbl == true or (tbl == false or tbl[false]) and i == 1 or tbl[true] then
return true
elseif tbl == false then
return false
end
end
return false
end
return {
getHostnames = getHostnames,
getDomains = getDomains,
addUrls = addUrls,
match = match
}