@@ -7,6 +7,7 @@ import path from 'path';
77import process from 'process' ;
88import Logger from '@matrixai/logger' ;
99import { DB } from '@matrixai/db' ;
10+ import { MDNS } from '@matrixai/mdns' ;
1011import {
1112 CreateDestroyStartStop ,
1213 ready ,
@@ -83,6 +84,10 @@ type PolykeyAgentOptions = {
8384 rpcCallTimeoutTime : number ;
8485 rpcParserBufferSize : number ;
8586 } ;
87+ mdns : {
88+ groups : Array < string > ;
89+ port : number ;
90+ } ;
8691} ;
8792
8893interface PolykeyAgent extends CreateDestroyStartStop { }
@@ -167,6 +172,10 @@ class PolykeyAgent {
167172 connectionHolePunchIntervalTime :
168173 config . defaultsSystem . nodesConnectionHolePunchIntervalTime ,
169174 } ,
175+ mdns : {
176+ groups : config . defaultsSystem . mdnsGroups ,
177+ port : config . defaultsSystem . mdnsPort ,
178+ } ,
170179 } ) ;
171180 // This can only happen if the caller didn't specify the node path and the
172181 // automatic detection failed
@@ -201,6 +210,7 @@ class PolykeyAgent {
201210 let gestaltGraph : GestaltGraph | undefined ;
202211 let identitiesManager : IdentitiesManager | undefined ;
203212 let nodeGraph : NodeGraph | undefined ;
213+ let mdns : MDNS | undefined ;
204214 let nodeConnectionManager : NodeConnectionManager | undefined ;
205215 let nodeManager : NodeManager | undefined ;
206216 let discovery : Discovery | undefined ;
@@ -316,13 +326,23 @@ class PolykeyAgent {
316326 keyRing,
317327 logger : logger . getChild ( NodeGraph . name ) ,
318328 } ) ;
329+ mdns = new MDNS ( {
330+ logger : logger . getChild ( MDNS . name ) ,
331+ } ) ;
332+ await mdns . start ( {
333+ id : keyRing . getNodeId ( ) . toBuffer ( ) . readUint16BE ( ) ,
334+ hostname : nodesUtils . encodeNodeId ( keyRing . getNodeId ( ) ) ,
335+ groups : optionsDefaulted . mdns . groups ,
336+ port : optionsDefaulted . mdns . port ,
337+ } ) ;
319338 // Remove your own node ID if provided as a seed node
320339 const nodeIdOwnEncoded = nodesUtils . encodeNodeId ( keyRing . getNodeId ( ) ) ;
321340 delete optionsDefaulted . seedNodes [ nodeIdOwnEncoded ] ;
322341 nodeConnectionManager = new NodeConnectionManager ( {
323342 keyRing,
324343 nodeGraph,
325344 tlsConfig,
345+ mdns,
326346 seedNodes : optionsDefaulted . seedNodes ,
327347 connectionFindConcurrencyLimit :
328348 optionsDefaulted . nodes . connectionFindConcurrencyLimit ,
@@ -426,6 +446,7 @@ class PolykeyAgent {
426446 await discovery ?. stop ( ) ;
427447 await identitiesManager ?. stop ( ) ;
428448 await gestaltGraph ?. stop ( ) ;
449+ await mdns ?. stop ( ) ;
429450 await acl ?. stop ( ) ;
430451 await sigchain ?. stop ( ) ;
431452 await certManager ?. stop ( ) ;
@@ -448,6 +469,7 @@ class PolykeyAgent {
448469 acl,
449470 gestaltGraph,
450471 nodeGraph,
472+ mdns,
451473 taskManager,
452474 nodeConnectionManager,
453475 nodeManager,
@@ -486,6 +508,7 @@ class PolykeyAgent {
486508 public readonly acl : ACL ;
487509 public readonly gestaltGraph : GestaltGraph ;
488510 public readonly nodeGraph : NodeGraph ;
511+ public readonly mdns : MDNS ;
489512 public readonly taskManager : TaskManager ;
490513 public readonly nodeConnectionManager : NodeConnectionManager ;
491514 public readonly nodeManager : NodeManager ;
@@ -530,6 +553,7 @@ class PolykeyAgent {
530553 acl,
531554 gestaltGraph,
532555 nodeGraph,
556+ mdns,
533557 taskManager,
534558 nodeConnectionManager,
535559 nodeManager,
@@ -552,6 +576,7 @@ class PolykeyAgent {
552576 acl : ACL ;
553577 gestaltGraph : GestaltGraph ;
554578 nodeGraph : NodeGraph ;
579+ mdns : MDNS ;
555580 taskManager : TaskManager ;
556581 nodeConnectionManager : NodeConnectionManager ;
557582 nodeManager : NodeManager ;
@@ -576,6 +601,7 @@ class PolykeyAgent {
576601 this . gestaltGraph = gestaltGraph ;
577602 this . discovery = discovery ;
578603 this . nodeGraph = nodeGraph ;
604+ this . mdns = mdns ;
579605 this . taskManager = taskManager ;
580606 this . nodeConnectionManager = nodeConnectionManager ;
581607 this . nodeManager = nodeManager ;
@@ -624,6 +650,10 @@ class PolykeyAgent {
624650 | { recoveryCode : string }
625651 | { privateKey : Buffer }
626652 | { privateKeyPath : string } ;
653+ mdns : {
654+ groups : Array < string > ;
655+ port : number ;
656+ } ;
627657 } > ;
628658 workers ?: number ;
629659 fresh ?: boolean ;
@@ -637,6 +667,10 @@ class PolykeyAgent {
637667 agentServicePort : config . defaultsUser . agentServicePort ,
638668 workers : config . defaultsUser . workers ,
639669 ipv6Only : config . defaultsUser . ipv6Only ,
670+ mdns : {
671+ groups : config . defaultsSystem . mdnsGroups ,
672+ port : config . defaultsSystem . mdnsPort ,
673+ } ,
640674 } ) ;
641675 // Register event handlers
642676 this . certManager . addEventListener (
@@ -706,6 +740,12 @@ class PolykeyAgent {
706740 host : optionsDefaulted . clientServiceHost ,
707741 port : optionsDefaulted . clientServicePort ,
708742 } ) ;
743+ await this . mdns . start ( {
744+ id : this . keyRing . getNodeId ( ) . toBuffer ( ) . readUint16BE ( ) ,
745+ hostname : nodesUtils . encodeNodeId ( this . keyRing . getNodeId ( ) ) ,
746+ groups : optionsDefaulted . mdns . groups ,
747+ port : optionsDefaulted . mdns . port ,
748+ } ) ;
709749 await this . nodeManager . start ( ) ;
710750 await this . nodeConnectionManager . start ( {
711751 host : optionsDefaulted . agentServiceHost ,
@@ -768,6 +808,7 @@ class PolykeyAgent {
768808 await this . vaultManager ?. stop ( ) ;
769809 await this . discovery ?. stop ( ) ;
770810 await this . nodeGraph ?. stop ( ) ;
811+ await this . mdns ?. stop ( ) ;
771812 await this . nodeConnectionManager ?. stop ( ) ;
772813 await this . nodeManager ?. stop ( ) ;
773814 await this . clientService . stop ( { force : true } ) ;
@@ -810,6 +851,7 @@ class PolykeyAgent {
810851 await this . clientService . stop ( { force : true } ) ;
811852 await this . identitiesManager . stop ( ) ;
812853 await this . gestaltGraph . stop ( ) ;
854+ await this . mdns . stop ( ) ;
813855 await this . acl . stop ( ) ;
814856 await this . sigchain . stop ( ) ;
815857 await this . certManager . stop ( ) ;
0 commit comments