Skip to content

Commit 2eef18b

Browse files
committed
wip: fixing up PolykeyAgent options
[ci skip]
1 parent 3a6e29e commit 2eef18b

File tree

5 files changed

+225
-130
lines changed

5 files changed

+225
-130
lines changed

src/PolykeyAgent.ts

Lines changed: 114 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DeepPartial, FileSystem, PromiseDeconstructed } from './types';
22
import type { PolykeyWorkerManagerInterface } from './workers/types';
3-
import type { ConnectionData, TLSConfig } from './network/types';
3+
import type { TLSConfig } from './network/types';
44
import type { SeedNodes } from './nodes/types';
55
import type { CertManagerChangeData, Key } from './keys/types';
66
import type { RecoveryCode, PrivateKey } from './keys/types';
@@ -10,13 +10,11 @@ import process from 'process';
1010
import Logger from '@matrixai/logger';
1111
import { DB } from '@matrixai/db';
1212
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop';
13-
import { QUICServer } from '@matrixai/quic';
1413
import RPCServer from './rpc/RPCServer';
1514
import WebSocketServer from './websockets/WebSocketServer';
1615
import * as rpcUtilsMiddleware from './rpc/utils/middleware';
1716
import * as clientUtilsMiddleware from './client/utils/middleware';
1817
import { WorkerManager } from './workers';
19-
import * as networkUtils from './network/utils';
2018
import KeyRing from './keys/KeyRing';
2119
import CertManager from './keys/CertManager';
2220
import Status from './status/Status';
@@ -39,6 +37,7 @@ import * as errors from './errors';
3937
import * as utils from './utils';
4038
import * as keysUtils from './keys/utils';
4139
import * as nodesUtils from './nodes/utils';
40+
import * as nodesEvents from './nodes/events';
4241
import * as workersUtils from './workers/utils';
4342
import TaskManager from './tasks/TaskManager';
4443
import { serverManifest as clientServerManifest } from './client/handlers';
@@ -85,6 +84,15 @@ type PolykeyAgentOptions = {
8584
};
8685
};
8786

87+
type PolykeyAgentStartOptions = {
88+
clientServiceHost: string;
89+
clientServicePort: number;
90+
agentServiceHost: string;
91+
agentServicePort: number;
92+
ipv6Only: boolean;
93+
workers: number;
94+
};
95+
8896
interface PolykeyAgent extends CreateDestroyStartStop {}
8997
@CreateDestroyStartStop(
9098
new errors.ErrorPolykeyAgentRunning(),
@@ -172,50 +180,63 @@ class PolykeyAgent {
172180
const umask = 0o077;
173181
logger.info(`Setting umask to ${umask.toString(8).padStart(3, '0')}`);
174182
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,
207195
},
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+
});
209221
// This can only happen if the caller didn't specify the node path and the
210222
// automatic detection failed
211223
if (optionsDefaulted.nodePath == null) {
212224
throw new errors.ErrorUtilsNodePath();
213225
}
214226
logger.info(`Setting node path to ${optionsDefaulted.nodePath}`);
215227
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+
);
219240
const dbPath = path.join(statePath, config.paths.dbBase);
220241
const keysPath = path.join(statePath, config.paths.keysBase);
221242
const vaultsPath = path.join(statePath, config.paths.vaultsBase);
@@ -362,12 +383,18 @@ class PolykeyAgent {
362383
nodeGraph,
363384
tlsConfig,
364385
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,
371398
logger: logger.getChild(NodeConnectionManager.name),
372399
});
373400
nodeManager =
@@ -429,7 +456,9 @@ class PolykeyAgent {
429456
}));
430457
// If a recovery code is provided then we reset any sessions in case the
431458
// password changed.
432-
if (optionsDefaulted.keys.recoveryCode != null) await sessionManager.resetKey();
459+
if (optionsDefaulted.keys.recoveryCode != null) {
460+
await sessionManager.resetKey();
461+
}
433462
if (rpcServerClient == null) {
434463
pkAgentProm = utils.promise();
435464
rpcServerClient = await RPCServer.createRPCServer({
@@ -550,10 +579,14 @@ class PolykeyAgent {
550579

551580
await pkAgent.start({
552581
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+
},
557590
fresh,
558591
});
559592
logger.info(`Created ${this.name}`);
@@ -586,6 +619,11 @@ class PolykeyAgent {
586619
public readonly rpcServerAgent: RPCServer;
587620
protected workerManager: PolykeyWorkerManagerInterface | undefined;
588621

622+
protected handleEventNodeStream = (e: nodesEvents.EventNodeStream) => {
623+
const stream = e.detail;
624+
this.rpcServerAgent.handleStream(stream);
625+
};
626+
589627
constructor({
590628
nodePath,
591629
status,
@@ -670,10 +708,18 @@ class PolykeyAgent {
670708
fresh = false,
671709
}: {
672710
password: string;
673-
options,
711+
options: Partial<PolykeyAgentStartOptions>;
674712
workers?: number;
675713
fresh?: boolean;
676714
}) {
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+
});
677723
try {
678724
this.logger.info(`Starting ${this.constructor.name}`);
679725
// Set up error handling for event handlers
@@ -713,10 +759,6 @@ class PolykeyAgent {
713759
this.logger.info(`${KeyRing.name} change propagated`);
714760
},
715761
);
716-
const _networkConfig = {
717-
...config.defaults.networkConfig,
718-
...utils.filterEmptyObject(networkConfig),
719-
};
720762
await this.status.start({ pid: process.pid });
721763
await this.schema.start({ fresh });
722764
// Starting modules
@@ -763,15 +805,20 @@ class PolykeyAgent {
763805
// Client server
764806
await this.webSocketServerClient.start({
765807
tlsConfig,
766-
host: _networkConfig.clientHost,
767-
port: _networkConfig.clientPort,
808+
host: optionsDefaulted.clientServiceHost,
809+
port: optionsDefaulted.clientServicePort,
768810
connectionCallback: (streamPair) =>
769811
this.rpcServerClient.handleStream(streamPair),
770812
});
771813
await this.nodeManager.start();
814+
this.nodeConnectionManager.addEventListener(
815+
nodesEvents.EventNodeStream.name,
816+
this.handleEventNodeStream,
817+
);
772818
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,
775822
});
776823
await this.nodeGraph.start({ fresh });
777824
await this.nodeManager.syncNodeGraph(false);
@@ -812,6 +859,10 @@ class PolykeyAgent {
812859
await this.discovery?.stop();
813860
await this.nodeGraph?.stop();
814861
await this.nodeConnectionManager?.stop();
862+
this.nodeConnectionManager.removeEventListener(
863+
nodesEvents.EventNodeStream.name,
864+
this.handleEventNodeStream,
865+
);
815866
await this.nodeManager?.stop();
816867
await this.webSocketServerClient.stop(true);
817868
await this.identitiesManager?.stop();
@@ -845,6 +896,10 @@ class PolykeyAgent {
845896
await this.vaultManager.stop();
846897
await this.discovery.stop();
847898
await this.nodeConnectionManager.stop();
899+
this.nodeConnectionManager.removeEventListener(
900+
nodesEvents.EventNodeStream.name,
901+
this.handleEventNodeStream,
902+
);
848903
await this.nodeGraph.stop();
849904
await this.nodeManager.stop();
850905
await this.webSocketServerClient.stop(true);

0 commit comments

Comments
 (0)