Skip to content

Commit

Permalink
feat: moved QUICServer into NodeConnectionManager
Browse files Browse the repository at this point in the history
* Related #527

[ci skip]
  • Loading branch information
tegefaulkes authored and CMCDragonkai committed Oct 4, 2023
1 parent 2d2d5e1 commit ed92634
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 182 deletions.
27 changes: 20 additions & 7 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type NetworkConfig = {
clientHost?: string;
clientPort?: number;
// Websocket server config
maxReadableStreamBytes?: number;
maxIdleTimeout?: number;
pingIntervalTime?: number;
pingTimeoutTimeTime?: number;
Expand Down Expand Up @@ -95,7 +96,8 @@ class PolykeyAgent {
keyRingConfig = {},
certManagerConfig = {},
networkConfig = {},
quicConfig = {},
quicServerConfig = {},
quicClientConfig = {},
nodeConnectionManagerConfig = {},
seedNodes = {},
workers,
Expand Down Expand Up @@ -147,7 +149,8 @@ class PolykeyAgent {
connectionHolePunchIntervalTime?: number;
};
networkConfig?: NetworkConfig;
quicConfig?: PolykeyQUICConfig;
quicServerConfig?: PolykeyQUICConfig;
quicClientConfig?: PolykeyQUICConfig;
seedNodes?: SeedNodes;
workers?: number;
status?: Status;
Expand Down Expand Up @@ -195,9 +198,14 @@ class PolykeyAgent {
...config.defaults.networkConfig,
...utils.filterEmptyObject(networkConfig),
};
const quicConfig_ = {
...config.defaults.quicConfig,
...utils.filterEmptyObject(quicConfig),
const quicServerConfig_ = {
...config.defaults.quicServerConfig,
...utils.filterEmptyObject(quicServerConfig),
};
// TODO: rename
const quicClientConfig_ = {
...config.defaults.quicClientConfig,
...utils.filterEmptyObject(quicClientConfig),
};
await utils.mkdirExists(fs, nodePath);
const statusPath = path.join(nodePath, config.defaults.statusBase);
Expand Down Expand Up @@ -393,7 +401,7 @@ class PolykeyAgent {
nodeGraph,
seedNodes,
quicSocket,
quicConfig: quicConfig_,
quicConfig: quicClientConfig_,
...nodeConnectionManagerConfig_,
tlsConfig,
crypto,
Expand Down Expand Up @@ -495,9 +503,11 @@ class PolykeyAgent {
(await WebSocketServer.createWebSocketServer({
connectionCallback: (rpcStream) =>
rpcServerClient!.handleStream(rpcStream),
fs,
host: networkConfig_.clientHost,
port: networkConfig_.clientPort,
tlsConfig,
maxReadableStreamBytes: networkConfig_.maxReadableStreamBytes,
maxIdleTimeout: networkConfig_.maxIdleTimeout,
pingIntervalTime: networkConfig_.pingIntervalTime,
pingTimeoutTimeTime: networkConfig_.pingTimeoutTimeTime,
Expand Down Expand Up @@ -739,7 +749,10 @@ class PolykeyAgent {
keyPrivatePem: keysUtils.privateKeyToPEM(data.keyPair.privateKey),
certChainPem: await this.certManager.getCertPEMsChainPEM(),
};
this.webSocketServerClient.setTlsConfig(tlsConfig);
// FIXME: Can we even support updating TLS config anymore?
// We would need to shut down the Websocket server and re-create it with the new config.
// Right now graceful shutdown is not supported.
// this.grpcServerClient.setTLSConfig(tlsConfig);
this.nodeConnectionManager.updateTlsConfig(tlsConfig);
this.logger.info(`${KeyRing.name} change propagated`);
},
Expand Down
18 changes: 14 additions & 4 deletions src/bootstrap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Sigchain } from '../sigchain';
import { ACL } from '../acl';
import { GestaltGraph } from '../gestalts';
import { KeyRing } from '../keys';
import { NodeGraph, NodeManager } from '../nodes';
import { NodeConnectionManager, NodeGraph, NodeManager } from '../nodes';
import { VaultManager } from '../vaults';
import { NotificationsManager } from '../notifications';
import { mkdirExists } from '../utils';
Expand Down Expand Up @@ -153,11 +153,21 @@ async function bootstrapState({
logger,
lazy: true,
});
const nodeConnectionManager = new NodeConnectionManager({
// No streams are created
handleStream: () => {},
keyRing,
nodeGraph,
quicClientConfig: {} as any, // No connections are attempted
crypto: {} as any, // No connections are attempted
quicSocket: {} as any, // No connections are attempted
logger: logger.getChild(NodeConnectionManager.name),
});
const nodeManager = new NodeManager({
db,
keyRing,
nodeGraph,
nodeConnectionManager: {} as any, // No connections are attempted
nodeConnectionManager,
sigchain,
taskManager,
gestaltGraph,
Expand All @@ -167,7 +177,7 @@ async function bootstrapState({
await NotificationsManager.createNotificationsManager({
acl,
db,
nodeConnectionManager: {} as any, // No connections are attempted
nodeConnectionManager,
nodeManager,
keyRing,
logger: logger.getChild(NotificationsManager.name),
Expand All @@ -178,7 +188,7 @@ async function bootstrapState({
db,
gestaltGraph,
keyRing,
nodeConnectionManager: {} as any, // No connections are attempted
nodeConnectionManager,
vaultsPath,
notificationsManager,
logger: logger.getChild(VaultManager.name),
Expand Down
23 changes: 11 additions & 12 deletions src/nodes/NodeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ContextTimed } from '@matrixai/contexts';
import type { PromiseCancellable } from '@matrixai/async-cancellable';
import type { NodeId, QuicConfig } from './types';
import type { Host, Hostname, Port, TLSConfig } from '../network/types';
import type { Certificate, CertificatePEM } from '../keys/types';
import type { CertificatePEM } from '../keys/types';
import type { ClientManifest, RPCStream } from '../rpc/types';
import type {
QUICSocket,
Expand All @@ -22,7 +22,6 @@ import RPCClient from '../rpc/RPCClient';
import * as networkUtils from '../network/utils';
import * as rpcUtils from '../rpc/utils';
import * as keysUtils from '../keys/utils';
import * as nodesUtils from '../nodes/utils';
import { never } from '../utils';

/**
Expand Down Expand Up @@ -184,7 +183,6 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {
if (certChain == null) never();
const nodeId = keysUtils.certNodeId(certChain[0]);
if (nodeId == null) never();
const newLogger = logger.getParent() ?? new Logger(this.name);
const nodeConnection = new this<M>({
validatedNodeId,
nodeId,
Expand All @@ -197,11 +195,7 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {
quicClient,
quicConnection,
rpcClient,
logger: newLogger.getChild(
`${this.name} [${nodesUtils.encodeNodeId(nodeId)}@${
quicConnection.remoteHost
}:${quicConnection.remotePort}]`,
),
logger,
});
quicClient.addEventListener(
'clientDestroy',
Expand All @@ -217,20 +211,25 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {

static async createNodeConnectionReverse<M extends ClientManifest>({
handleStream,
certChain,
nodeId,
quicConnection,
manifest,
logger = new Logger(this.name),
}: {
handleStream: (stream: RPCStream<Uint8Array, Uint8Array>) => void;
certChain: Array<Certificate>;
nodeId: NodeId;
quicConnection: QUICConnection;
manifest: M;
logger?: Logger;
}): Promise<NodeConnection<M>> {
logger.info(`Creating ${this.name}`);
// No specific error here, validation is handled by the QUICServer
const certChain = quicConnection.getRemoteCertsChain().map((pem) => {
const cert = keysUtils.certFromPEM(pem as CertificatePEM);
if (cert == null) never();
return cert;
});
if (certChain == null) never();
const nodeId = keysUtils.certNodeId(certChain[0]);
if (nodeId == null) never();
// Creating RPCClient
const rpcClient = await RPCClient.createRPCClient<M>({
manifest,
Expand Down
Loading

0 comments on commit ed92634

Please sign in to comment.