-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.lua
More file actions
522 lines (481 loc) · 15.4 KB
/
agent.lua
File metadata and controls
522 lines (481 loc) · 15.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
-- neosocksd (c) 2023-2026 He Xian <hexian000@outlook.com>
-- This code is licensed under MIT license (see LICENSE for details)
-- [[ agent.lua: implements peer discovery and connection relay based on RPC ]] --
_G.libruleset = require("libruleset")
local agent = {}
agent.running = true
agent.api_endpoint = "127.0.0.1:9080"
-- agent.peername = "peer0"
agent.peername = table.get(_G.agent, "peername")
-- agent.conns[id] = { proxy1, proxy2, ... }
agent.conns = table.get(_G.agent, "conns") or {}
-- agent.hosts = { "host1", "host2", ... }
agent.hosts = table.get(_G.agent, "hosts") or {}
local function connid_of(v)
for id, conn in pairs(agent.conns) do
if conn == v then
return tostring(id)
end
end
return "?"
end
-- _G.peerdb[peername] = { hosts = { hostname, "host1" }, conns = { [id] = { peername = "peer1", rtt = 0 } }, timestamp = os.time() }
_G.peerdb = _G.peerdb or {}
agent.API_ENDPOINT = "api.neosocksd.internal:80"
agent.INTERNAL_DOMAIN = ".internal"
-- <host>.peerN.peer2.peer1.relay.neosocksd.internal
agent.RELAY_DOMAIN = ".relay.neosocksd.internal"
agent.BOOTSTRAP_DELAY = 10
agent.GOSSIP_TARGET_COUNT = 2
agent.SYNC_INTERVAL_BASE = 300
agent.SYNC_INTERVAL_RANDOM = 300
agent.TIMESTAMP_TOLERANCE = 300
agent.PEERDB_EXPIRY_TIME = 7200
agent.verbose = table.get(_G.agent, "verbose")
local function is_valid(data, now)
local timestamp = data.timestamp
return
(type(timestamp) == "number") -- malformed peer data
and
(now - agent.PEERDB_EXPIRY_TIME < timestamp) -- lower bound to expire peer data
and
(timestamp < now + agent.TIMESTAMP_TOLERANCE) -- upper bound to detect clock skew
end
local strformat = string.format
local function format_path(path)
return list:new():append(path):reverse():map(function(s)
return strformat("%q", s)
end):concat("->")
end
local function dijkstra(peerdb, source)
local nodes = {}
for node, info in pairs(peerdb) do
nodes[node] = true
if info.conns then
for _, conn in pairs(info.conns) do
nodes[conn.peername] = true
end
end
end
if not nodes[source] then return {} end
local dist, prev, visited = {}, {}, {}
for node, _ in pairs(nodes) do
dist[node] = math.huge
prev[node] = nil
visited[node] = false
end
dist[source] = 0
while true do
-- find the unvisited node with the smallest distance
local min_dist, u = math.huge, nil
for node, _ in pairs(nodes) do
if not visited[node] and dist[node] < min_dist then
min_dist = dist[node]
u = node
end
end
-- all nodes visited or remaining nodes unreachable
if not u then break end
visited[u] = true
-- update neighbor nodes' distance
local u_info = peerdb[u]
if u_info and u_info.conns then
for _, conn in pairs(u_info.conns) do
local v = conn.peername
local weight = conn.rtt
if not visited[v] then
local alt = dist[u] + weight
if alt < dist[v] then
dist[v] = alt
prev[v] = u
end
end
end
end
end
-- build relay paths
local paths = {}
for node, _ in pairs(nodes) do
if node ~= source and dist[node] < math.huge then
local path, n = {}, 1
local curr = node
while curr ~= source do
path[n] = curr
n = n + 1
curr = prev[curr]
end
paths[node] = { path = path, rtt = dist[node] }
end
end
return paths
end
local function build_index(peerdb)
local hosts = {}
for peername, data in pairs(peerdb) do
for _, host in pairs(data.hosts or {}) do
hosts[host] = peername
end
end
local peers = {}
local selfinfo = peerdb[agent.peername] or {}
for connid, conninfo in pairs(selfinfo.conns or {}) do
local existing = peers[conninfo.peername]
if not existing or conninfo.rtt < existing.rtt then
peers[conninfo.peername] = { conn = agent.conns[connid], rtt = conninfo.rtt }
end
end
local routes = {}
local paths = dijkstra(peerdb, agent.peername)
for dest, info in pairs(paths) do
local path = info.path
local peername = path[#path]
local entry = peername and peers[peername]
if entry then
routes[dest] = { conn = entry.conn, path = path, rtt = info.rtt }
end
end
return hosts, routes
end
-- hosts[hostname] = peername
-- routes[peername] = { conn = conn, path = { peernameN, ..., peername2, peername1 }, rtt = rtt }
local hosts, routes = build_index(_G.peerdb)
-- extracts subdomain from FQDN
local function subdomain(fqdn, domain)
local n = domain:len()
if fqdn:sub(-n) ~= domain then
return nil
end
return fqdn:sub(1, -n - 1)
end
local ERR_UNKNOWN_HOST = "unknown host"
local ERR_NOT_REACHABLE = "peer not reachable"
local function resolve_internal(host, port)
local peername = hosts[host]
if not peername then
return ERR_UNKNOWN_HOST
end
assert(peername ~= agent.peername)
local route = routes[peername]
if not route then
return ERR_NOT_REACHABLE
end
local t = list:new():append(route.path)
peername = t[#t]
local conn = route.conn
t[1], t[#t] = host, nil
local addr
if peername == hosts[host] then
addr = strformat("%s%s:%s", host, agent.INTERNAL_DOMAIN, port)
else
addr = strformat("%s%s:%s", t:concat("."), agent.RELAY_DOMAIN, port)
end
return addr, conn
end
local splithostport = neosocksd.splithostport
function agent.proxy(...)
local t = { ... }
return function(addr)
local chain = list:new()
for i, proxy in ipairs(t) do
if i == 1 then
local proxyscheme, proxyaddr = proxy:match("^(%a[0-9A-Za-z+-.]*://)(.+)$")
assert(proxyscheme and proxyaddr)
local fqdn, port = splithostport(proxyaddr)
local sub = subdomain(fqdn, agent.INTERNAL_DOMAIN)
assert(sub)
local result, conn = resolve_internal(sub, port)
if not conn then
error(strformat("%s: %s", fqdn, result))
end
chain:append(conn)
chain:insert(proxyscheme .. result)
else
chain:insert(proxy)
end
end
return addr, chain:reverse():unpack()
end
end
function agent.rpcall(addr, func, ...)
return await.rpcall({ agent.proxy(addr)(agent.API_ENDPOINT) }, func, ...)
end
local function matcher(addr)
if addr == agent.API_ENDPOINT then
return function()
return agent.api_endpoint
end
end
local fqdn, port = splithostport(addr)
local sub = subdomain(fqdn, agent.RELAY_DOMAIN)
if not sub then
sub = subdomain(fqdn, agent.INTERNAL_DOMAIN)
if not sub then
return nil -- unhandled
end
local result, conn = resolve_internal(sub, port)
if not conn then
error(strformat("%s: %s", fqdn, result))
end
if agent.verbose then
if addr == result then
evlogf("agent [%s]: %s (passthrough)", connid_of(conn), addr)
else
evlogf("agent [%s]: %s -> %s", connid_of(conn), addr, result)
end
end
local proxies = list:new():append(conn):reverse()
return function()
return result, proxies:unpack()
end
end
-- resolve relay
local remain, peername = sub:match("^(.+)%.([^.]+)$")
local domain
if remain then
sub = remain
domain = agent.RELAY_DOMAIN
else
peername = hosts[sub]
domain = agent.INTERNAL_DOMAIN
end
local route = routes[peername]
if not route or #route.path ~= 1 then
error(strformat("%q: %s", peername, ERR_NOT_REACHABLE)) -- break matching
end
addr = strformat("%s%s:%s", sub, domain, port)
local conn = route.conn
if agent.verbose then
evlogf("relay [%s] %q: %s", connid_of(conn), peername, addr)
end
local proxies = list:new(conn):reverse()
return function()
return addr, proxies:unpack()
end
end
agent.chain = { { matcher } }
local function callbyconn(conn, func, ...)
if not agent.running then
error("cancelled")
end
local target = list:new():append(conn)
target:insert(agent.API_ENDPOINT)
target = target:reverse():totable()
return await.rpcall(target, func, ...)
end
local function update_peerdb(peername, peerdb)
local updated = false
local now = os.time()
-- remove stale data
for peer, data in pairs(_G.peerdb) do
if not is_valid(data, now) then
evlogf("peer expired: %q (time=%d)", peer, data.timestamp - now)
_G.peerdb[peer] = nil
end
end
-- update peerdb
for peer, data in pairs(peerdb) do
if peer == peername then
-- direct update from peer
local old = _G.peerdb[peer]
if not old or data.timestamp ~= old.timestamp then
updated = true
end
_G.peerdb[peer] = data
elseif peer == agent.peername then
-- ignore updates to self
elseif is_valid(data, now) then
local old = _G.peerdb[peer]
if not old or data.timestamp > old.timestamp then
updated = true
evlogf("peerdb: updated peer %q from %q (time=%d)",
peer, peername, data.timestamp - now)
_G.peerdb[peer] = data
end
end
end
return updated
end
local function sync_via(conn)
local ok, r1, r2 = callbyconn(conn, "sync", agent.peername, _G.peerdb)
if ok then
local peername, peerdb = r1, r2
update_peerdb(peername, peerdb)
end
return ok, r1
end
local function gossip(peername)
-- randomly pick connections to gossip (excluding the source)
local candidates = {}
for connid, conn in pairs(agent.conns) do
-- find the peername for this connection
local selfinfo = _G.peerdb[agent.peername]
if selfinfo and selfinfo.conns then
local conninfo = selfinfo.conns[connid]
if conninfo and conninfo.peername ~= peername then
table.insert(candidates, { connid = connid, conn = conn })
end
end
end
local n = #candidates
if n == 0 then
return -- no one to gossip to
end
-- shuffle candidates
for i = n, 2, -1 do
local j = math.random(i)
candidates[i], candidates[j] = candidates[j], candidates[i]
end
local n = 0
for _, target in pairs(candidates) do
if n >= agent.GOSSIP_TARGET_COUNT then break end
local ok, r1 = sync_via(target.conn)
if ok then
if agent.verbose then
evlogf("gossip: [%s] propagated update from %q to %q",
connid_of(target.conn), peername, r1)
end
n = n + 1
else
evlogf("gossip error: [%s] %s", connid_of(target.conn), r1)
end
end
hosts, routes = build_index(_G.peerdb)
end
function rpc.sync(peername, peerdb)
if type(peername) ~= "string" or type(peerdb) ~= "table" then
return agent.peername, _G.peerdb
end
local updated = update_peerdb(peername, peerdb)
if updated then
async(gossip, peername)
end
hosts, routes = build_index(_G.peerdb)
local peerdiff = {}
for name, info in pairs(_G.peerdb) do
if name == agent.peername then
peerdiff[name] = info
elseif name ~= peername then
local known = peerdb[name]
if not known or known.timestamp ~= info.timestamp then
peerdiff[name] = info
end
end
end
return agent.peername, peerdiff
end
function rpc.probe()
if not agent.peername then
error("peer is not available for relay")
end
return agent.peername
end
local function probe_via(conn)
assert(sync_via(conn))
local minrtt, peername, err
for _ = 1, 4 do
await.sleep(1)
local probe_start = time.monotonic()
local ok, result = callbyconn(conn, "probe")
local probe_end = time.monotonic()
if ok then
local rtt = probe_end - probe_start
if not minrtt or rtt < minrtt then
minrtt = rtt
end
peername = result
else
err = result
end
end
if not peername then error(err) end
evlogf("probe: [%s] %q %.0fms", connid_of(conn), peername, minrtt * 1e+3)
return { peername = peername, rtt = minrtt }
end
local function parallel_for(tag, t, func)
local tasks = {}
for k, v in pairs(t) do
tasks[k] = async(func, k, v)
end
for k, future in pairs(tasks) do
local ok, result = future:get()
if not ok then
evlogf("%s error: [%s] %s", tag, tostring(k), result:match("^(.-)\n") or result)
end
end
end
function agent.maintenance()
-- probe
local conns = {}
parallel_for("probe", agent.conns, function(connid, conn)
conns[connid] = probe_via(conn)
end)
-- update self
if agent.peername then
local info = _G.peerdb[agent.peername] or {}
info.hosts = agent.hosts
info.conns = conns
info.timestamp = os.time()
_G.peerdb[agent.peername] = info
end
if agent.verbose then
evlog("agent: probe finished")
end
-- sync
parallel_for("sync", agent.conns, function(_, conn)
assert(sync_via(conn))
end)
if agent.verbose then
evlog("agent: sync finished")
end
hosts, routes = build_index(_G.peerdb)
evlog("agent: maintenance finished")
end
local function mainloop()
await.sleep(agent.BOOTSTRAP_DELAY)
while agent.running do
agent.maintenance()
local update = table.get(_G, "ruleset", "update")
if update then
local ok, err = pcall(update)
if not ok then
evlogf("ruleset.update: %s", err)
end
end
await.sleep(agent.SYNC_INTERVAL_BASE + math.random(agent.SYNC_INTERVAL_RANDOM))
end
end
function agent.stats(dt)
if not agent.running then return "" end
local w = list:new()
for peername, data in pairs(_G.peerdb) do
local tag = strformat("%q", peername)
local timestamp = format_timestamp(data.timestamp)
local route = routes[peername]
if route then
w:insertf("%-16s: %s [%s] %4.0fms %s", tag, timestamp, connid_of(route.conn),
route.rtt * 1e+3, format_path(route.path))
elseif peername ~= agent.peername then
w:insertf("%-16s: %s (unreachable)", tag, timestamp)
end
end
local title = "> Peers"
if agent.peername then
title = title .. string.format(" (self=%q)", agent.peername)
end
return title .. "\n" .. w:sort():concat("\n")
end
function agent.stop()
agent.running = false
end
local function main(...)
local stop = table.get(_G, "agent", "stop")
if stop then
local ok, err = pcall(stop)
if not ok then
evlogf("agent.stop: %s", err)
end
end
async(mainloop)
return agent
end
return main(...)