-
Notifications
You must be signed in to change notification settings - Fork 30
/
mesh.js
869 lines (780 loc) · 21.9 KB
/
mesh.js
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
import db from './db.js'
export default function (config) {
var meshName = config.name
var username
var caCert
var agentCert
var agentKey
var agentLog = []
var meshErrors = []
var services = []
var ports = {}
var exited = false
if (config.ca) {
try {
caCert = new crypto.Certificate(config.ca)
} catch {
meshError('Invalid CA certificate')
}
} else {
meshError('Missing CA certificate')
}
if (config.agent.certificate) {
try {
agentCert = new crypto.Certificate(config.agent.certificate)
username = agentCert.subject?.commonName
} catch {
meshError('Invalid agent certificate')
}
} else {
meshError('Missing agent certificate')
}
if (config.agent.privateKey) {
try {
agentKey = new crypto.PrivateKey(config.agent.privateKey)
} catch {
meshError('Invalid agent private key')
}
} else {
meshError('Missing agent private key')
}
var tlsOptions = {
certificate: agentCert && agentKey ? {
cert: agentCert,
key: agentKey,
} : null,
trusted: caCert ? [caCert] : null,
}
var hubAddresses = config.bootstraps.map(
function (addr) {
if (addr.startsWith('localhost:')) addr = '127.0.0.1:' + addr.substring(10)
return addr
}
)
//
// Utility pipelies
//
var bypass = pipeline($=>$)
var wrapUDP = pipeline($=>$
.replaceData(data => data.size > 0 ? new Message(data) : undefined)
.encodeWebSocket()
)
var unwrapUDP = pipeline($=>$
.decodeWebSocket()
.replaceMessage(msg => msg.body)
)
//
// Class Hub
// Management of the interaction with a single hub instance
//
function Hub(address) {
var connections = new Set
var closed = false
var serviceList = null
var serviceListUpdateTime = 0
var serviceListSendTime = 0
//
// requestHub ---\
// \-->
// hubSession <---> Hub
// /---
// reverseServer <--/
//
var $response
var $serviceListTime
// Long-lived agent-to-hub connection, multiplexed with HTTP/2
var hubSession = pipeline($=>$
.muxHTTP(() => '', { version: 2 }).to($=>$
.connectTLS({
...tlsOptions,
onState: (session) => {
var err = session.error
if (err) meshError(err)
}
}).to($=>$
.onStart(() => { meshErrors.length = 0 })
.connect(address, {
onState: function (conn) {
if (conn.state === 'connected') {
logInfo(`Connected to hub ${address}`)
meshErrors.length = 0
connections.add(conn)
if (serviceList) updateServiceList(serviceList)
} else if (conn.state === 'closed') {
connections.delete(conn)
}
}
})
.handleStreamEnd(
(eos) => meshError(`Connection to hub ${address} closed, error = ${eos.error}`)
)
)
)
)
// Send a request to the hub
var requestHub = pipeline($=>$
.onStart(msg => msg)
.pipe(hubSession)
.handleMessage(msg => $response = msg)
.replaceMessage(new StreamEnd)
.onEnd(() => $response)
)
// Hook up to the hub and receive orders
var reverseServer = pipeline($=>$
.onStart(new Data)
.repeat(() => new Timeout(5).wait().then(() => !closed)).to($=>$
.loop($=>$
.connectHTTPTunnel(
new Message({
method: 'CONNECT',
path: `/api/endpoints/${config.agent.id}`,
})
)
.to(hubSession)
.pipe(serveHub)
)
)
)
// Establish a pull session to the hub
reverseServer.spawn()
// Start sending service list updates
pipeline($=>$
.onStart(new Data)
.repeat(() => new Timeout(1).wait().then(() => !closed)).to($=>$
.forkJoin().to($=>$
.pipe(
() => {
if (serviceListUpdateTime > serviceListSendTime) {
$serviceListTime = serviceListUpdateTime
return 'send'
}
return 'wait'
}, {
'wait': ($=>$.replaceStreamStart(new StreamEnd)),
'send': ($=>$.replaceStreamStart(
() => requestHub.spawn(
new Message(
{
method: 'POST',
path: `/api/services`,
},
JSON.encode({
time: $serviceListTime,
services: serviceList || [],
})
)
).then(
function (res) {
if (res && res.head.status === 201) {
if (serviceListSendTime < $serviceListTime) {
serviceListSendTime = $serviceListTime
}
}
return new StreamEnd
}
)
)),
}
)
)
.replaceStreamStart(new StreamEnd)
)
).spawn()
function updateServiceList(list) {
serviceList = list
serviceListUpdateTime = Date.now()
}
function heartbeat() {
if (closed) return
requestHub.spawn(
new Message(
{ method: 'POST', path: '/api/status' },
JSON.encode({ name: config.agent.name })
)
)
}
function discoverEndpoints() {
return requestHub.spawn(
new Message({ method: 'GET', path: '/api/endpoints' })
).then(
function (res) {
if (res && res.head.status === 200) {
return JSON.decode(res.body)
} else {
return []
}
}
)
}
function discoverServices(ep) {
return requestHub.spawn(
new Message({ method: 'GET', path: ep ? `/api/endpoints/${ep}/services` : '/api/services' })
).then(
function (res) {
if (res && res.head.status === 200) {
return JSON.decode(res.body)
} else {
return []
}
}
)
}
function findEndpoint(ep) {
return requestHub.spawn(
new Message({ method: 'GET', path: `/api/endpoints/${ep}`})
).then(
function (res) {
if (res && res.head.status === 200) {
return JSON.decode(res.body)
} else {
return null
}
}
)
}
function findService(proto, svc) {
return requestHub.spawn(
new Message({ method: 'GET', path: `/api/services/${proto}/${svc}`})
).then(
function (res) {
if (res && res.head.status === 200) {
return JSON.decode(res.body)
} else {
return null
}
}
)
}
function leave() {
closed = true
connections.forEach(
conn => conn.close()
)
}
return {
isConnected: () => connections.size > 0,
address,
heartbeat,
updateServiceList,
discoverEndpoints,
discoverServices,
findEndpoint,
findService,
leave,
}
} // End of class Hub
var matchServices = new http.Match('/api/services/{proto}/{svc}')
var response200 = new Message({ status: 200 })
var response404 = new Message({ status: 404 })
var $requestedService
var $selectedEp
var $selectedHub
//
// Agent serving requests from the hubs
//
// Hub ----\
// Hub -----)----> Agent
// Hub ----/
//
var serveHub = pipeline($=>$
.demuxHTTP().to($=>$
.pipe(
function (evt) {
if (evt instanceof MessageStart) {
if (evt.head.method === 'CONNECT') {
var params = matchServices(evt.head.path)
if (params) return proxyToLocal
}
return serveOtherAgents
}
}
)
)
)
//
// Agent handling hub-forwarded requests from other agents
//
// Remote Agent ----> Hub ----\
// Remote Agent ----> Hub -----)----> Agent
// Remote Agent ----> Hub ----/
//
var serveOtherAgents = (function() {
var routes = Object.entries({
'/api/ping': {
'GET': () => response(200)
},
'/api/services': {
'GET': function () {
return response(200, db.allServices(meshName))
},
},
'/api/services/{proto}/{svc}': {
'GET': function () {
return response(200, db.getService(meshName, params.proto, params.svc))
},
'POST': function (params, req) {
db.setService(meshName, params.proto, params.svc, JSON.decode(req.body))
var s = db.getService(meshName, params.proto, params.svc)
publishService(params.proto, params.svc, s.host, s.port, s.users)
return response(201, s)
},
'DELETE': function (params) {
deleteService(params.proto, params.svc)
db.delService(meshName, params.proto, params.svc)
return response(204)
},
},
'/api/ports': {
'GET': function () {
return response(200, db.allPorts(meshName).map(
p => Object.assign(p, checkPort(p.listen.ip, p.protocol, p.listen.port))
))
},
},
'/api/ports/{ip}/{proto}/{port}': {
'GET': function (params) {
var ip = params.ip
var proto = params.proto
var port = Number.parseInt(params.port)
return response(200, Object.assign(
db.getPort(meshName, ip, proto, port),
checkPort(ip, proto, port),
))
},
'POST': function (params, req) {
var port = Number.parseInt(params.port)
var body = JSON.decode(req.body)
var target = body.target
openPort(params.ip, params.proto, port, target.service, target.endpoint)
db.setPort(meshName, params.ip, params.proto, port, body)
return response(201, db.getPort(meshName, params.ip, params.proto, port))
},
'DELETE': function (params) {
var port = Number.parseInt(params.port)
closePort(params.ip, params.proto, port)
db.delPort(meshName, params.ip, params.proto, port)
return response(204)
},
},
'/api/log': {
'GET': function () {
return response(200, getLog())
}
},
}).map(
function ([path, methods]) {
var match = new http.Match(path)
var handler = function (params, req) {
var f = methods[req.head.method]
if (f) return f(params, req)
return response(405)
}
return { match, handler }
}
)
return pipeline($=>$
.replaceMessage(
function (req) {
var params
var path = req.head.path
var route = routes.find(r => Boolean(params = r.match(path)))
if (route) return route.handler(params, req)
return response(404)
}
)
)
})()
//
// Agent proxying to local services: mesh -> local
//
// Remote Client ----> Remote Agent ----> Hub ----\ /----> Local Service
// Remote Client ----> Remote Agent ----> Hub -----)----> Agent ----(-----> Local Service
// Remote Client ----> Remote Agent ----> Hub ----/ \----> Local Service
//
var proxyToLocal = pipeline($=>$
.acceptHTTPTunnel(
function (req) {
var params = matchServices(req.head.path)
if (params) {
var protocol = params.proto
var name = params.svc
$requestedService = services.find(s => s.protocol === protocol && s.name === name)
if ($requestedService) {
logInfo(`Proxy to local service ${name}`)
return response200
}
logError(`Local service ${name} not found`)
}
return response404
}
).to($=>$
.pipe(() => $requestedService.protocol, {
'tcp': ($=>$.connect(() => `${$requestedService.host}:${$requestedService.port}`)),
'udp': ($=>$
.pipe(unwrapUDP)
.connect(() => `${$requestedService.host}:${$requestedService.port}`, { protocol: 'udp' })
.pipe(wrapUDP)
)
})
.onEnd(() => logInfo(`Proxy to local service ${$requestedService.name} ended`))
)
)
//
// Agent proxying to remote services: local -> mesh
//
// Local Client ----\ /----> Hub ----> Remote Agent ----> Remote Service
// Local Client -----)----> Agent ----(-----> Hub ----> Remote Agent ----> Remote Service
// Local Client ----/ \----> Hub ----> Remote Agent ----> Remote Service
//
var proxyToMesh = (proto, svc, ep) => pipeline($=>$
.onStart(() => {
if (ep) {
$selectedEp = ep
return selectHub(ep).then(hub => {
$selectedHub = hub
return new Data
})
} else {
return selectEndpoint(proto, svc).then(ep => {
if (!ep) return new Data
$selectedEp = ep
return selectHub(ep).then(hub => {
$selectedHub = hub
return new Data
})
})
}
})
.pipe(() => $selectedHub ? 'proxy' : 'deny', {
'proxy': ($=>$
.onStart(() => logInfo(`Proxy to ${svc} at endpoint ${$selectedEp} via ${$selectedHub}`))
.pipe(proto === 'udp' ? wrapUDP : bypass)
.connectHTTPTunnel(() => (
new Message({
method: 'CONNECT',
path: `/api/endpoints/${$selectedEp}/services/${proto}/${svc}`,
})
)).to($=>$
.muxHTTP(() => $selectedHub, { version: 2 }).to($=>$
.connectTLS(tlsOptions).to($=>$
.connect(() => $selectedHub)
)
)
)
.pipe(proto === 'udp' ? unwrapUDP : bypass)
.onEnd(() => logInfo(`Proxy to ${svc} at endpoint ${$selectedEp} via ${$selectedHub} ended`))
),
'deny': ($=>$
.onStart(() => logError($selectedEp ? `No route to endpoint ${$selectedEp}` : `No endpoint found for ${svc}`))
.replaceData(new StreamEnd)
),
})
)
// HTTP agents for ad-hoc agent-to-hub sessions
var httpAgents = new algo.Cache(
target => new http.Agent(target, { tls: tlsOptions })
)
// Connect to all hubs
var hubs = config.bootstraps.map(
addr => Hub(addr)
)
// Start sending heartbeats
heartbeat()
function heartbeat() {
if (!exited) {
hubs.forEach(h => h.heartbeat())
new Timeout(15).wait().then(heartbeat)
}
}
// Publish services
db.allServices(meshName).forEach(
function (s) {
publishService(s.protocol, s.name, s.host, s.port, s.users)
}
)
// Open local ports
db.allPorts(meshName).forEach(
function (p) {
var listen = p.listen
var target = p.target
openPort(listen.ip, p.protocol, listen.port, target.service, target.endpoint)
}
)
logInfo(`Joined ${meshName} as ${config.agent.name} (uuid = ${config.agent.id})`)
function selectEndpoint(proto, svc) {
return hubs[0].findService(proto, svc).then(
function (service) {
if (!service) return null
var ep = service.endpoints[0]
return ep ? ep.id : null
}
)
}
function selectHub(ep) {
return hubs[0].findEndpoint(ep).then(
function (endpoint) {
if (!endpoint) return null
var addresses = endpoint.hubs || []
return addresses.find(addr => hubAddresses.indexOf(addr) >= 0) || hubs[0].address
}
)
}
function selectHubWithThrow(ep) {
return selectHub(ep).then(hub => {
if (!hub) throw `No hub for endpoint ${ep}`
return hub
})
}
function findEndpoint(ep) {
return hubs[0].findEndpoint(ep)
}
function discoverEndpoints() {
return hubs[0].discoverEndpoints()
}
function discoverServices(ep) {
return hubs[0].discoverServices(ep)
}
function publishService(protocol, name, host, port, users) {
users = users || null
var old = services.find(s => s.name === name && s.protocol === protocol)
if (old) {
old.host = host
old.port = port
old.users = users
} else {
services.push({
name,
protocol,
host,
port,
users,
})
}
updateServiceList()
}
function deleteService(protocol, name) {
var old = services.find(s => s.name === name && s.protocol === protocol)
if (old) {
services.splice(services.indexOf(old), 1)
updateServiceList()
}
}
function updateServiceList() {
var list = services.map(({ name, protocol, users }) => ({ name, protocol, users }))
hubs.forEach(hub => hub.updateServiceList(list))
}
function portName(ip, protocol, port) {
return `${ip}/${protocol}/${port}`
}
function openPort(ip, protocol, port, service, endpoint) {
var key = portName(ip, protocol, port)
try {
switch (protocol) {
case 'tcp':
case 'udp':
pipy.listen(`${ip}:${port}`, protocol, proxyToMesh(protocol, service, endpoint))
break
default: throw `Invalid protocol: ${protocol}`
}
ports[key] = { open: true }
} catch (err) {
ports[key] = { open: false, error: err.toString() }
}
}
function closePort(ip, protocol, port) {
var key = portName(ip, protocol, port)
pipy.listen(`${ip}:${port}`, protocol, null)
delete ports[key]
}
function checkPort(ip, protocol, port) {
var key = portName(ip, protocol, port)
return ports[key]
}
function remoteQueryServices(ep) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'GET', `/api/forward/${ep}/services`
).then(
res => {
remoteCheckResponse(res, 200)
return JSON.decode(res.body)
}
)
)
}
function remotePublishService(ep, proto, name, host, port, users) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'POST', `/api/forward/${ep}/services/${proto}/${name}`,
{}, JSON.encode({ host, port, users })
).then(
res => {
remoteCheckResponse(res, 201)
return JSON.decode(res.body)
}
)
)
}
function remoteDeleteService(ep, proto, name) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'DELETE', `/api/forward/${ep}/services/${proto}/${name}`
).then(
res => {
remoteCheckResponse(res, 204)
}
)
)
}
function remoteQueryPorts(ep) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'GET', `/api/forward/${ep}/ports`
).then(
res => {
remoteCheckResponse(res, 200)
return JSON.decode(res.body)
}
)
)
}
function remoteOpenPort(ep, ip, proto, port, target) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'POST', `/api/forward/${ep}/ports/${ip}/${proto}/${port}`,
{}, JSON.encode({ target })
).then(
res => {
remoteCheckResponse(res, 201)
return JSON.decode(res.body)
}
)
)
}
function remoteClosePort(ep, ip, proto, port) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'DELETE', `/api/forward/${ep}/ports/${ip}/${proto}/${port}`
).then(
res => {
remoteCheckResponse(res, 204)
}
)
)
}
function remoteQueryLog(ep) {
return selectHubWithThrow(ep).then(
(hub) => httpAgents.get(hub).request(
'GET', `/api/forward/${ep}/log`
).then(
res => {
remoteCheckResponse(res, 200)
return JSON.decode(res.body)
}
)
)
}
function remoteCheckResponse(res, expected) {
var status = res?.head?.status
if (status !== expected) {
throw { status: status || 500, message: res?.head?.statusText }
}
}
function leave() {
db.allPorts(meshName).forEach(
function ({ protocol, listen }) {
closePort(listen.ip, protocol, listen.port)
}
)
hubs.forEach(hub => hub.leave())
exited = true
logInfo(`Left ${meshName} as ${config.agent.name} (uuid = ${config.agent.id})`)
}
function isConnected() {
return hubs.some(h => h.isConnected())
}
function getStatus() {
return {
name: meshName,
ca: config.ca,
agent: {
id: config.agent.id,
name: config.agent.name,
username,
certificate: config.agent.certificate,
},
bootstraps: [...config.bootstraps],
connected: isConnected(),
errors: getErrors(),
}
}
function getLog() {
return [...agentLog]
}
function getErrors() {
return [...meshErrors]
}
function log(type, msg) {
if (agentLog.length > 100) {
agentLog.splice(0, agentLog.length - 100)
}
agentLog.push({
time: new Date().toISOString(),
type,
message: msg,
})
}
function logInfo(msg) {
log('info', msg)
console.info(msg)
}
function logError(msg) {
log('error', msg)
console.error(msg)
}
function meshError(msg) {
logError(msg)
meshErrors.push({
time: new Date().toISOString(),
message: msg,
})
}
return {
config,
username,
isConnected,
getStatus,
getLog,
getErrors,
findEndpoint,
discoverEndpoints,
discoverServices,
publishService,
deleteService,
openPort,
closePort,
checkPort,
remoteQueryServices,
remotePublishService,
remoteDeleteService,
remoteQueryPorts,
remoteOpenPort,
remoteClosePort,
remoteQueryLog,
leave,
}
}
function response(status, body) {
if (!body) return new Message({ status })
if (typeof body === 'string') return responseCT(status, 'text/plain', body)
return responseCT(status, 'application/json', JSON.encode(body))
}
function responseCT(status, ct, body) {
return new Message(
{
status,
headers: { 'content-type': ct }
},
body
)
}