-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleset.lua
177 lines (160 loc) · 6.02 KB
/
ruleset.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
_G.libruleset = require("libruleset")
-- [[ configurations ]] --
local function is_disabled()
local now = os.date("*t")
if not (2 <= now.wday and now.wday <= 6) then
return true
end
return not (9 <= now.hour and now.hour < 18)
end
-- 1. ordered redirect rules (matched as string)
-- in {matcher, action, optional log tag}
-- matching stops after a match is found
-- redirect_name: for requests with name string
_G.redirect_name = {
-- access mDNS sites directly
{ match.domain(".local"), rule.direct() },
-- self-assignment
{ match.exact("api.neosocksd.lan:80"), rule.redirect("127.0.1.1:9080") },
-- admin routes
{ match.exact("localhost:22"), rule.redirect("127.0.0.1:22"), "ssh" },
{ match.exact("region1.lan:22"), rule.redirect("localhost:22", "socks5://192.168.32.1:1080"), "region1" },
{ match.exact("region2.lan:22"), rule.redirect("localhost:22", "socks5://192.168.33.1:1080"), "region2" },
-- global condition
{ is_disabled, rule.reject(), "off" },
-- proxy routes
{ match.exact("region1.neosocksd.lan:1080"), rule.redirect("socks4a://192.168.32.1:1080") },
{ match.exact("region2.neosocksd.lan:1080"), rule.redirect("socks4a://192.168.33.1:1080") },
-- dynamically loaded big domains list
{ composite.maybe(_G, "domains"), rule.proxy("socks4a://proxy.lan:1080"), "biglist" },
-- if in _G.hosts, go to _G.route/_G.route6
-- otherwise, go to _G.route_default
}
-- redirect: for requests with IPv4 address
_G.redirect = {
-- redirect TCP DNS to local cache
{ match.exact("1.1.1.1:53"), rule.redirect("127.0.0.53:53") },
{ match.exact("1.0.0.1:53"), rule.redirect("127.0.0.53:53") },
-- global condition
{ is_disabled, rule.reject(), "off" },
-- go to _G.route
}
-- redirect6: for requests with IPv6 address
_G.redirect6 = {
-- global condition
{ is_disabled, rule.reject(), "off" },
-- go to _G.route6
}
-- 2. unordered hosts map
_G.hosts = {
["gateway.region1.lan"] = "192.168.32.1",
["host123.region1.lan"] = "192.168.32.123",
["gateway.region2.lan"] = "192.168.33.1",
["host123.region2.lan"] = "192.168.33.123"
}
-- 3. ordered routes (matched as address)
_G.route = {
-- reject loopback or link-local
{ inet.subnet("127.0.0.0/8"), rule.reject() },
{ inet.subnet("169.254.0.0/16"), rule.reject() },
-- region1 proxy
{ inet.subnet("192.168.32.0/24"), rule.proxy("socks4a://192.168.32.1:1080"), "region1" },
-- jump to region2 through region1 proxy (for a fancy demo)
{ inet.subnet("192.168.33.0/24"), rule.proxy("socks4a://192.168.32.1:1080", "socks4a://192.168.33.1:1080"), "region2" },
-- access other lan addresses directly
{ inet.subnet("192.168.0.0/16"), rule.direct(), "lan" },
-- dynamically loaded big IP ranges list
{ composite.maybe(_G, "countryip"), rule.proxy("socks4a://proxy.lan:1080"), "biglist" },
-- go to _G.route_default
}
_G.route6 = {
-- reject loopback or link-local
{ inet6.subnet("::1/128"), rule.reject() },
{ inet6.subnet("fe80::/10"), rule.reject() },
{ inet6.subnet("::ffff:127.0.0.0/104"), rule.reject() },
{ inet6.subnet("::ffff:169.254.0.0/112"), rule.reject() },
-- dynamically loaded big IP ranges list
{ composite.maybe(_G, "countryip6"), rule.proxy("socks4a://proxy.lan:1080"), "biglist" },
-- go to _G.route_default
}
-- 4. the global default applies to any unmatched requests
-- in {action, optional log tag}
_G.route_index = 1
_G.route_list = {
[1] = { rule.proxy("socks4a://127.0.0.1:1081"), "default1" },
[2] = { rule.proxy("socks4a://127.0.0.2:1081"), "default2" }
}
_G.route_default = route_index[1]
function _G.set_route(i, ...)
_G.route_index = i
if select("#", ...) > 0 then
_G.route_list[route_index] = { rule.proxy(...) }
end
_G.route_default = route_list[route_index]
end
-- support reloading libruleset
local ruleset = setmetatable({}, {
__index = function(t, k)
return _G.libruleset[k]
end
})
neosocksd.setinterval(60.0)
local function ping(target)
local lasterr
local rtt = {}
for i = 1, 4 do
await.sleep(1)
local begin = neosocksd.now()
local ok, result = await.rpcall(target, "echo", string.rep(" ", 32))
if ok then
table.insert(rtt, neosocksd.now() - begin)
else
lasterr = result
end
end
if rtt[1] then
rtt = math.min(table.unpack(rtt))
return true, string.format("%dms", math.ceil(rtt * 1e+3))
end
return false, lasterr
end
if _G.ruleset then
-- stop old ruleset routines when reloading
_G.ruleset.running = false
end
ruleset.running = true
ruleset.server_rtt = {}
local function keepalive(target, tag)
while ruleset.running do
local interval = 60
if not is_disabled() then
local ok, result = ping(target)
logf("ping %q: %s", tag, result)
ruleset.server_rtt[tag] = result
if ok then interval = 3600 end
end
await.sleep(interval)
end
end
for k, v in pairs(route_list) do
local route, tag = v[1], v[2]
local target = table.pack(route("api.neosocksd.lan:80"))
async(keepalive, target, tag)
end
local function format_rtt()
local w = list:new()
for tag, result in pairs(ruleset.server_rtt) do
w:insertf("[%s] %s", tag, result)
end
w:sort()
return w:concat(", ")
end
function ruleset.stats(dt)
local w = list:new()
w:insertf("%-20s: %d", "Default Route", route_index)
w:insertf("%-20s: %s", "Server RTT", format_rtt())
w:insert(libruleset.stats(dt))
return w:concat("\n")
end
logf("ruleset loaded, interpreter: %s", _VERSION)
return ruleset