1
1
import type { DeepPartial , FileSystem , PromiseDeconstructed } from './types' ;
2
2
import type { PolykeyWorkerManagerInterface } from './workers/types' ;
3
- import type { ConnectionData , TLSConfig } from './network/types' ;
3
+ import type { TLSConfig } from './network/types' ;
4
4
import type { SeedNodes } from './nodes/types' ;
5
5
import type { CertManagerChangeData , Key } from './keys/types' ;
6
6
import type { RecoveryCode , PrivateKey } from './keys/types' ;
@@ -10,13 +10,11 @@ import process from 'process';
10
10
import Logger from '@matrixai/logger' ;
11
11
import { DB } from '@matrixai/db' ;
12
12
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop' ;
13
- import { QUICServer } from '@matrixai/quic' ;
14
13
import RPCServer from './rpc/RPCServer' ;
15
14
import WebSocketServer from './websockets/WebSocketServer' ;
16
15
import * as rpcUtilsMiddleware from './rpc/utils/middleware' ;
17
16
import * as clientUtilsMiddleware from './client/utils/middleware' ;
18
17
import { WorkerManager } from './workers' ;
19
- import * as networkUtils from './network/utils' ;
20
18
import KeyRing from './keys/KeyRing' ;
21
19
import CertManager from './keys/CertManager' ;
22
20
import Status from './status/Status' ;
@@ -39,6 +37,7 @@ import * as errors from './errors';
39
37
import * as utils from './utils' ;
40
38
import * as keysUtils from './keys/utils' ;
41
39
import * as nodesUtils from './nodes/utils' ;
40
+ import * as nodesEvents from './nodes/events' ;
42
41
import * as workersUtils from './workers/utils' ;
43
42
import TaskManager from './tasks/TaskManager' ;
44
43
import { serverManifest as clientServerManifest } from './client/handlers' ;
@@ -85,6 +84,15 @@ type PolykeyAgentOptions = {
85
84
} ;
86
85
} ;
87
86
87
+ type PolykeyAgentStartOptions = {
88
+ clientServiceHost : string ;
89
+ clientServicePort : number ;
90
+ agentServiceHost : string ;
91
+ agentServicePort : number ;
92
+ ipv6Only : boolean ;
93
+ workers : number ;
94
+ } ;
95
+
88
96
interface PolykeyAgent extends CreateDestroyStartStop { }
89
97
@CreateDestroyStartStop (
90
98
new errors . ErrorPolykeyAgentRunning ( ) ,
@@ -172,50 +180,63 @@ class PolykeyAgent {
172
180
const umask = 0o077 ;
173
181
logger . info ( `Setting umask to ${ umask . toString ( 8 ) . padStart ( 3 , '0' ) } ` ) ;
174
182
process . umask ( umask ) ;
175
- const optionsDefaulted = utils . mergeObjects (
176
- options ,
177
- {
178
- nodePath : config . defaultsUser . nodePath ,
179
- clientServiceHost : config . defaultsUser . clientServiceHost ,
180
- clientServicePort : config . defaultsUser . clientServicePort ,
181
- agentServiceHost : config . defaultsUser . agentServiceHost ,
182
- agentServicePort : config . defaultsUser . agentServicePort ,
183
- seedNodes : config . defaultsUser . seedNodes ,
184
- workers : config . defaultsUser . workers ,
185
- ipv6Only : config . defaultsUser . ipv6Only ,
186
- keys : {
187
- certDuration : config . defaultsUser . certDuration ,
188
- certRenewLeadTime : config . defaultsUser . certRenewLeadTime ,
189
- } ,
190
- rpc : {
191
- callTimeoutTime : config . defaultsSystem . rpcCallTimeoutTime ,
192
- parserBufferSize : config . defaultsSystem . rpcParserBufferSize ,
193
- } ,
194
- client : {
195
- connectTimoutTime : config . defaultsSystem . clientConnectTimeoutTime ,
196
- keepAliveTimeoutTime : config . defaultsSystem . clientKeepAliveTimeoutTime ,
197
- keepAliveIntervalTime : config . defaultsSystem . clientKeepAliveIntervalTime ,
198
- } ,
199
- nodes : {
200
- connectionIdleTimeoutTime : config . defaultsSystem . nodesConnectionIdleTimeoutTime ,
201
- connectionFindConcurrencyLimit : config . defaultsSystem . nodesConnectionFindConcurrencyLimit ,
202
- connectionConnectTimeoutTime : config . defaultsSystem . nodesConnectionConnectTimeoutTime ,
203
- connectionKeepAliveTimeoutTime : config . defaultsSystem . nodesConnectionKeepAliveTimeoutTime ,
204
- connectionKeepAliveIntervalTime : config . defaultsSystem . nodesConnectionKeepAliveIntervalTime ,
205
- connectionHolePunchIntervalTime : config . defaultsSystem . nodesConnectionHolePunchIntervalTime ,
206
- } ,
183
+ const optionsDefaulted = utils . mergeObjects ( options , {
184
+ nodePath : config . defaultsUser . nodePath ,
185
+ clientServiceHost : config . defaultsUser . clientServiceHost ,
186
+ clientServicePort : config . defaultsUser . clientServicePort ,
187
+ agentServiceHost : config . defaultsUser . agentServiceHost ,
188
+ agentServicePort : config . defaultsUser . agentServicePort ,
189
+ seedNodes : config . defaultsUser . seedNodes ,
190
+ workers : config . defaultsUser . workers ,
191
+ ipv6Only : config . defaultsUser . ipv6Only ,
192
+ keys : {
193
+ certDuration : config . defaultsUser . certDuration ,
194
+ certRenewLeadTime : config . defaultsUser . certRenewLeadTime ,
207
195
} ,
208
- ) ;
196
+ rpc : {
197
+ callTimeoutTime : config . defaultsSystem . rpcCallTimeoutTime ,
198
+ parserBufferSize : config . defaultsSystem . rpcParserBufferSize ,
199
+ } ,
200
+ client : {
201
+ connectTimoutTime : config . defaultsSystem . clientConnectTimeoutTime ,
202
+ keepAliveTimeoutTime : config . defaultsSystem . clientKeepAliveTimeoutTime ,
203
+ keepAliveIntervalTime :
204
+ config . defaultsSystem . clientKeepAliveIntervalTime ,
205
+ } ,
206
+ nodes : {
207
+ connectionIdleTimeoutTime :
208
+ config . defaultsSystem . nodesConnectionIdleTimeoutTime ,
209
+ connectionFindConcurrencyLimit :
210
+ config . defaultsSystem . nodesConnectionFindConcurrencyLimit ,
211
+ connectionConnectTimeoutTime :
212
+ config . defaultsSystem . nodesConnectionConnectTimeoutTime ,
213
+ connectionKeepAliveTimeoutTime :
214
+ config . defaultsSystem . nodesConnectionKeepAliveTimeoutTime ,
215
+ connectionKeepAliveIntervalTime :
216
+ config . defaultsSystem . nodesConnectionKeepAliveIntervalTime ,
217
+ connectionHolePunchIntervalTime :
218
+ config . defaultsSystem . nodesConnectionHolePunchIntervalTime ,
219
+ } ,
220
+ } ) ;
209
221
// This can only happen if the caller didn't specify the node path and the
210
222
// automatic detection failed
211
223
if ( optionsDefaulted . nodePath == null ) {
212
224
throw new errors . ErrorUtilsNodePath ( ) ;
213
225
}
214
226
logger . info ( `Setting node path to ${ optionsDefaulted . nodePath } ` ) ;
215
227
await utils . mkdirExists ( fs , optionsDefaulted . nodePath ) ;
216
- const statusPath = path . join ( optionsDefaulted . nodePath , config . paths . statusBase ) ;
217
- const statusLockPath = path . join ( optionsDefaulted . nodePath , config . paths . statusLockBase ) ;
218
- const statePath = path . join ( optionsDefaulted . nodePath , config . paths . stateBase ) ;
228
+ const statusPath = path . join (
229
+ optionsDefaulted . nodePath ,
230
+ config . paths . statusBase ,
231
+ ) ;
232
+ const statusLockPath = path . join (
233
+ optionsDefaulted . nodePath ,
234
+ config . paths . statusLockBase ,
235
+ ) ;
236
+ const statePath = path . join (
237
+ optionsDefaulted . nodePath ,
238
+ config . paths . stateBase ,
239
+ ) ;
219
240
const dbPath = path . join ( statePath , config . paths . dbBase ) ;
220
241
const keysPath = path . join ( statePath , config . paths . keysBase ) ;
221
242
const vaultsPath = path . join ( statePath , config . paths . vaultsBase ) ;
@@ -362,12 +383,18 @@ class PolykeyAgent {
362
383
nodeGraph,
363
384
tlsConfig,
364
385
seedNodes : optionsDefaulted . seedNodes ,
365
- connectionFindConcurrencyLimit : optionsDefaulted . nodes . connectionFindConcurrencyLimit ,
366
- connectionIdleTimeoutTime : optionsDefaulted . nodes . connectionIdleTimeoutTime ,
367
- connectionConnectTimeoutTime : optionsDefaulted . nodes . connectionConnectTimeoutTime ,
368
- connectionKeepAliveTimeoutTime : optionsDefaulted . nodes . connectionKeepAliveTimeoutTime ,
369
- connectionKeepAliveIntervalTime : optionsDefaulted . nodes . connectionKeepAliveIntervalTime ,
370
- connectionHolePunchIntervalTime : optionsDefaulted . nodes . connectionHolePunchIntervalTime ,
386
+ connectionFindConcurrencyLimit :
387
+ optionsDefaulted . nodes . connectionFindConcurrencyLimit ,
388
+ connectionIdleTimeoutTime :
389
+ optionsDefaulted . nodes . connectionIdleTimeoutTime ,
390
+ connectionConnectTimeoutTime :
391
+ optionsDefaulted . nodes . connectionConnectTimeoutTime ,
392
+ connectionKeepAliveTimeoutTime :
393
+ optionsDefaulted . nodes . connectionKeepAliveTimeoutTime ,
394
+ connectionKeepAliveIntervalTime :
395
+ optionsDefaulted . nodes . connectionKeepAliveIntervalTime ,
396
+ connectionHolePunchIntervalTime :
397
+ optionsDefaulted . nodes . connectionHolePunchIntervalTime ,
371
398
logger : logger . getChild ( NodeConnectionManager . name ) ,
372
399
} ) ;
373
400
nodeManager =
@@ -429,7 +456,9 @@ class PolykeyAgent {
429
456
} ) ) ;
430
457
// If a recovery code is provided then we reset any sessions in case the
431
458
// password changed.
432
- if ( optionsDefaulted . keys . recoveryCode != null ) await sessionManager . resetKey ( ) ;
459
+ if ( optionsDefaulted . keys . recoveryCode != null ) {
460
+ await sessionManager . resetKey ( ) ;
461
+ }
433
462
if ( rpcServerClient == null ) {
434
463
pkAgentProm = utils . promise ( ) ;
435
464
rpcServerClient = await RPCServer . createRPCServer ( {
@@ -550,10 +579,14 @@ class PolykeyAgent {
550
579
551
580
await pkAgent . start ( {
552
581
password,
553
- host,
554
- port
555
- // networkConfig,
556
- workers : optionsDefaulted . workers ,
582
+ options : {
583
+ clientServiceHost : optionsDefaulted . clientServiceHost ,
584
+ clientServicePort : optionsDefaulted . clientServicePort ,
585
+ agentServiceHost : optionsDefaulted . agentServiceHost ,
586
+ agentServicePort : optionsDefaulted . agentServicePort ,
587
+ workers : optionsDefaulted . workers ,
588
+ ipv6Only : optionsDefaulted . ipv6Only ,
589
+ } ,
557
590
fresh,
558
591
} ) ;
559
592
logger . info ( `Created ${ this . name } ` ) ;
@@ -586,6 +619,11 @@ class PolykeyAgent {
586
619
public readonly rpcServerAgent : RPCServer ;
587
620
protected workerManager : PolykeyWorkerManagerInterface | undefined ;
588
621
622
+ protected handleEventNodeStream = ( e : nodesEvents . EventNodeStream ) => {
623
+ const stream = e . detail ;
624
+ this . rpcServerAgent . handleStream ( stream ) ;
625
+ } ;
626
+
589
627
constructor ( {
590
628
nodePath,
591
629
status,
@@ -670,10 +708,18 @@ class PolykeyAgent {
670
708
fresh = false ,
671
709
} : {
672
710
password : string ;
673
- options ,
711
+ options : Partial < PolykeyAgentStartOptions > ;
674
712
workers ?: number ;
675
713
fresh ?: boolean ;
676
714
} ) {
715
+ const optionsDefaulted = utils . mergeObjects ( options , {
716
+ clientServiceHost : config . defaultsUser . clientServiceHost ,
717
+ clientServicePort : config . defaultsUser . clientServicePort ,
718
+ agentServiceHost : config . defaultsUser . agentServiceHost ,
719
+ agentServicePort : config . defaultsUser . agentServicePort ,
720
+ workers : config . defaultsUser . workers ,
721
+ ipv6Only : config . defaultsUser . ipv6Only ,
722
+ } ) ;
677
723
try {
678
724
this . logger . info ( `Starting ${ this . constructor . name } ` ) ;
679
725
// Set up error handling for event handlers
@@ -713,10 +759,6 @@ class PolykeyAgent {
713
759
this . logger . info ( `${ KeyRing . name } change propagated` ) ;
714
760
} ,
715
761
) ;
716
- const _networkConfig = {
717
- ...config . defaults . networkConfig ,
718
- ...utils . filterEmptyObject ( networkConfig ) ,
719
- } ;
720
762
await this . status . start ( { pid : process . pid } ) ;
721
763
await this . schema . start ( { fresh } ) ;
722
764
// Starting modules
@@ -763,15 +805,20 @@ class PolykeyAgent {
763
805
// Client server
764
806
await this . webSocketServerClient . start ( {
765
807
tlsConfig,
766
- host : _networkConfig . clientHost ,
767
- port : _networkConfig . clientPort ,
808
+ host : optionsDefaulted . clientServiceHost ,
809
+ port : optionsDefaulted . clientServicePort ,
768
810
connectionCallback : ( streamPair ) =>
769
811
this . rpcServerClient . handleStream ( streamPair ) ,
770
812
} ) ;
771
813
await this . nodeManager . start ( ) ;
814
+ this . nodeConnectionManager . addEventListener (
815
+ nodesEvents . EventNodeStream . name ,
816
+ this . handleEventNodeStream ,
817
+ ) ;
772
818
await this . nodeConnectionManager . start ( {
773
- host : optionss ,
774
- handleStream : ( stream ) => this . rpcServerAgent . handleStream ( stream ) ,
819
+ host : optionsDefaulted . agentServiceHost ,
820
+ port : optionsDefaulted . agentServicePort ,
821
+ ipv6Only : optionsDefaulted . ipv6Only ,
775
822
} ) ;
776
823
await this . nodeGraph . start ( { fresh } ) ;
777
824
await this . nodeManager . syncNodeGraph ( false ) ;
@@ -812,6 +859,10 @@ class PolykeyAgent {
812
859
await this . discovery ?. stop ( ) ;
813
860
await this . nodeGraph ?. stop ( ) ;
814
861
await this . nodeConnectionManager ?. stop ( ) ;
862
+ this . nodeConnectionManager . removeEventListener (
863
+ nodesEvents . EventNodeStream . name ,
864
+ this . handleEventNodeStream ,
865
+ ) ;
815
866
await this . nodeManager ?. stop ( ) ;
816
867
await this . webSocketServerClient . stop ( true ) ;
817
868
await this . identitiesManager ?. stop ( ) ;
@@ -845,6 +896,10 @@ class PolykeyAgent {
845
896
await this . vaultManager . stop ( ) ;
846
897
await this . discovery . stop ( ) ;
847
898
await this . nodeConnectionManager . stop ( ) ;
899
+ this . nodeConnectionManager . removeEventListener (
900
+ nodesEvents . EventNodeStream . name ,
901
+ this . handleEventNodeStream ,
902
+ ) ;
848
903
await this . nodeGraph . stop ( ) ;
849
904
await this . nodeManager . stop ( ) ;
850
905
await this . webSocketServerClient . stop ( true ) ;
0 commit comments