1- import type { FileSystem } from './types' ;
1+ import type { FileSystem , PromiseDeconstructed } from './types' ;
22import type { PolykeyWorkerManagerInterface } from './workers/types' ;
33import type { ConnectionData , Host , Port , TLSConfig } from './network/types' ;
44import type { SeedNodes } from './nodes/types' ;
@@ -10,6 +10,10 @@ import process from 'process';
1010import Logger from '@matrixai/logger' ;
1111import { DB } from '@matrixai/db' ;
1212import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop' ;
13+ import RPCServer from './rpc/RPCServer' ;
14+ import WebSocketServer from './websockets/WebSocketServer' ;
15+ import * as middlewareUtils from './rpc/utils/middleware' ;
16+ import * as authMiddleware from './client/utils/authenticationMiddleware' ;
1317import { WorkerManager } from './workers' ;
1418import * as networkUtils from './network/utils' ;
1519import KeyRing from './keys/KeyRing' ;
@@ -32,14 +36,14 @@ import { providers } from './identities';
3236import Proxy from './network/Proxy' ;
3337import { EventBus , captureRejectionSymbol } from './events' ;
3438import createAgentService , { AgentServiceService } from './agent/service' ;
35- import createClientService , { ClientServiceService } from './client/service' ;
3639import config from './config' ;
3740import * as errors from './errors' ;
3841import * as utils from './utils' ;
3942import * as keysUtils from './keys/utils' ;
4043import * as nodesUtils from './nodes/utils' ;
4144import * as workersUtils from './workers/utils' ;
4245import TaskManager from './tasks/TaskManager' ;
46+ import { serverManifest } from './client/handlers' ;
4347
4448type NetworkConfig = {
4549 forwardHost ?: Host ;
@@ -49,9 +53,13 @@ type NetworkConfig = {
4953 // GRPCServer for agent service
5054 agentHost ?: Host ;
5155 agentPort ?: Port ;
52- // GRPCServer for client service
56+ // RPCServer for client service
5357 clientHost ?: Host ;
5458 clientPort ?: Port ;
59+ maxReadBufferBytes ?: number ;
60+ idleTimeout ?: number ;
61+ pingInterval ?: number ;
62+ pingTimeout ?: number ;
5563} ;
5664
5765interface PolykeyAgent extends CreateDestroyStartStop { }
@@ -103,8 +111,9 @@ class PolykeyAgent {
103111 vaultManager,
104112 notificationsManager,
105113 sessionManager,
106- grpcServerClient ,
114+ rpcServerClient ,
107115 grpcServerAgent,
116+ webSocketServerClient,
108117 fs = require ( 'fs' ) ,
109118 logger = new Logger ( this . name ) ,
110119 fresh = false ,
@@ -156,8 +165,9 @@ class PolykeyAgent {
156165 vaultManager ?: VaultManager ;
157166 notificationsManager ?: NotificationsManager ;
158167 sessionManager ?: SessionManager ;
159- grpcServerClient ?: GRPCServer ;
168+ rpcServerClient ?: RPCServer ;
160169 grpcServerAgent ?: GRPCServer ;
170+ webSocketServerClient ?: WebSocketServer ;
161171 fs ?: FileSystem ;
162172 logger ?: Logger ;
163173 fresh ?: boolean ;
@@ -183,6 +193,10 @@ class PolykeyAgent {
183193 ...config . defaults . nodeConnectionManagerConfig ,
184194 ...utils . filterEmptyObject ( nodeConnectionManagerConfig ) ,
185195 } ;
196+ const _networkConfig = {
197+ ...config . defaults . networkConfig ,
198+ ...utils . filterEmptyObject ( networkConfig ) ,
199+ } ;
186200 await utils . mkdirExists ( fs , nodePath ) ;
187201 const statusPath = path . join ( nodePath , config . defaults . statusBase ) ;
188202 const statusLockPath = path . join ( nodePath , config . defaults . statusLockBase ) ;
@@ -193,6 +207,7 @@ class PolykeyAgent {
193207 const events = new EventBus ( {
194208 captureRejections : true ,
195209 } ) ;
210+ let pkAgentProm : PromiseDeconstructed < PolykeyAgent > | undefined ;
196211 try {
197212 status =
198213 status ??
@@ -401,18 +416,65 @@ class PolykeyAgent {
401416 // If a recovery code is provided then we reset any sessions in case the
402417 // password changed.
403418 if ( keyRingConfig . recoveryCode != null ) await sessionManager . resetKey ( ) ;
404- grpcServerClient =
405- grpcServerClient ??
406- new GRPCServer ( {
407- logger : logger . getChild ( GRPCServer . name + 'Client' ) ,
419+ if ( rpcServerClient == null ) {
420+ pkAgentProm = utils . promise ( ) ;
421+ rpcServerClient = await RPCServer . createRPCServer ( {
422+ manifest : serverManifest ( {
423+ acl : acl ,
424+ certManager : certManager ,
425+ db : db ,
426+ discovery : discovery ,
427+ fs : fs ,
428+ gestaltGraph : gestaltGraph ,
429+ identitiesManager : identitiesManager ,
430+ keyRing : keyRing ,
431+ logger : logger ,
432+ nodeConnectionManager : nodeConnectionManager ,
433+ nodeGraph : nodeGraph ,
434+ nodeManager : nodeManager ,
435+ notificationsManager : notificationsManager ,
436+ pkAgentProm : pkAgentProm . p ,
437+ sessionManager : sessionManager ,
438+ vaultManager : vaultManager ,
439+ } ) ,
440+ middlewareFactory : middlewareUtils . defaultServerMiddlewareWrapper (
441+ authMiddleware . authenticationMiddlewareServer (
442+ sessionManager ,
443+ keyRing ,
444+ ) ,
445+ ) ,
446+ sensitive : false ,
447+ logger : logger . getChild ( 'RPCServerClient' ) ,
408448 } ) ;
449+ }
450+ const tlsConfig : TLSConfig = {
451+ keyPrivatePem : keysUtils . privateKeyToPEM ( keyRing . keyPair . privateKey ) ,
452+ certChainPem : await certManager . getCertPEMsChainPEM ( ) ,
453+ } ;
454+ webSocketServerClient =
455+ webSocketServerClient ??
456+ ( await WebSocketServer . createWebSocketServer ( {
457+ connectionCallback : ( streamPair ) =>
458+ rpcServerClient ! . handleStream ( streamPair , { } ) ,
459+ fs,
460+ host : _networkConfig . clientHost ,
461+ port : _networkConfig . clientPort ,
462+ tlsConfig,
463+ maxReadBufferBytes : _networkConfig . maxReadBufferBytes ,
464+ idleTimeout : _networkConfig . idleTimeout ,
465+ pingInterval : _networkConfig . pingInterval ,
466+ pingTimeout : _networkConfig . pingTimeout ,
467+ logger : logger . getChild ( 'WebSocketServer' ) ,
468+ } ) ) ;
409469 grpcServerAgent =
410470 grpcServerAgent ??
411471 new GRPCServer ( {
412472 logger : logger . getChild ( GRPCServer . name + 'Agent' ) ,
413473 } ) ;
414474 } catch ( e ) {
415475 logger . warn ( `Failed Creating ${ this . name } ` ) ;
476+ await rpcServerClient ?. destroy ( ) ;
477+ await webSocketServerClient ?. stop ( true ) ;
416478 await sessionManager ?. stop ( ) ;
417479 await notificationsManager ?. stop ( ) ;
418480 await vaultManager ?. stop ( ) ;
@@ -450,12 +512,14 @@ class PolykeyAgent {
450512 vaultManager,
451513 notificationsManager,
452514 sessionManager,
515+ rpcServerClient,
453516 grpcServerAgent,
454- grpcServerClient ,
517+ webSocketServerClient ,
455518 events,
456519 fs,
457520 logger,
458521 } ) ;
522+ pkAgentProm ?. resolveP ( pkAgent ) ;
459523 await pkAgent . start ( {
460524 password,
461525 networkConfig,
@@ -486,10 +550,11 @@ class PolykeyAgent {
486550 public readonly notificationsManager : NotificationsManager ;
487551 public readonly sessionManager : SessionManager ;
488552 public readonly grpcServerAgent : GRPCServer ;
489- public readonly grpcServerClient : GRPCServer ;
490553 public readonly events : EventBus ;
491554 public readonly fs : FileSystem ;
492555 public readonly logger : Logger ;
556+ public readonly rpcServerClient : RPCServer ;
557+ public readonly webSocketServerClient : WebSocketServer ;
493558 protected workerManager : PolykeyWorkerManagerInterface | undefined ;
494559
495560 constructor ( {
@@ -512,8 +577,9 @@ class PolykeyAgent {
512577 vaultManager,
513578 notificationsManager,
514579 sessionManager,
515- grpcServerClient ,
580+ rpcServerClient ,
516581 grpcServerAgent,
582+ webSocketServerClient,
517583 events,
518584 fs,
519585 logger,
@@ -537,8 +603,9 @@ class PolykeyAgent {
537603 vaultManager : VaultManager ;
538604 notificationsManager : NotificationsManager ;
539605 sessionManager : SessionManager ;
540- grpcServerClient : GRPCServer ;
606+ rpcServerClient : RPCServer ;
541607 grpcServerAgent : GRPCServer ;
608+ webSocketServerClient : WebSocketServer ;
542609 events : EventBus ;
543610 fs : FileSystem ;
544611 logger : Logger ;
@@ -563,7 +630,8 @@ class PolykeyAgent {
563630 this . vaultManager = vaultManager ;
564631 this . notificationsManager = notificationsManager ;
565632 this . sessionManager = sessionManager ;
566- this . grpcServerClient = grpcServerClient ;
633+ this . rpcServerClient = rpcServerClient ;
634+ this . webSocketServerClient = webSocketServerClient ;
567635 this . grpcServerAgent = grpcServerAgent ;
568636 this . events = events ;
569637 this . fs = fs ;
@@ -614,7 +682,8 @@ class PolykeyAgent {
614682 keyPrivatePem : keysUtils . privateKeyToPEM ( data . keyPair . privateKey ) ,
615683 certChainPem : await this . certManager . getCertPEMsChainPEM ( ) ,
616684 } ;
617- this . grpcServerClient . setTLSConfig ( tlsConfig ) ;
685+ // FIXME: Can we even support updating TLS config anymore?
686+ // this.grpcServerClient.setTLSConfig(tlsConfig);
618687 this . proxy . setTLSConfig ( tlsConfig ) ;
619688 this . logger . info ( `${ KeyRing . name } change propagated` ) ;
620689 } ,
@@ -641,7 +710,7 @@ class PolykeyAgent {
641710 }
642711 } ,
643712 ) ;
644- const networkConfig_ = {
713+ const _networkConfig = {
645714 ...config . defaults . networkConfig ,
646715 ...utils . filterEmptyObject ( networkConfig ) ,
647716 } ;
@@ -661,28 +730,6 @@ class PolykeyAgent {
661730 proxy : this . proxy ,
662731 logger : this . logger . getChild ( 'GRPCClientAgentService' ) ,
663732 } ) ;
664- const clientService = createClientService ( {
665- pkAgent : this ,
666- db : this . db ,
667- certManager : this . certManager ,
668- discovery : this . discovery ,
669- gestaltGraph : this . gestaltGraph ,
670- identitiesManager : this . identitiesManager ,
671- keyRing : this . keyRing ,
672- nodeGraph : this . nodeGraph ,
673- nodeConnectionManager : this . nodeConnectionManager ,
674- nodeManager : this . nodeManager ,
675- notificationsManager : this . notificationsManager ,
676- sessionManager : this . sessionManager ,
677- vaultManager : this . vaultManager ,
678- sigchain : this . sigchain ,
679- acl : this . acl ,
680- grpcServerClient : this . grpcServerClient ,
681- grpcServerAgent : this . grpcServerAgent ,
682- proxy : this . proxy ,
683- fs : this . fs ,
684- logger : this . logger . getChild ( 'GRPCClientClientService' ) ,
685- } ) ;
686733 // Starting modules
687734 await this . keyRing . start ( {
688735 password,
@@ -726,25 +773,26 @@ class PolykeyAgent {
726773 certChainPem : await this . certManager . getCertPEMsChainPEM ( ) ,
727774 } ;
728775 // Client server
729- await this . grpcServerClient . start ( {
730- services : [ [ ClientServiceService , clientService ] ] ,
731- host : networkConfig_ . clientHost ,
732- port : networkConfig_ . clientPort ,
776+ await this . webSocketServerClient . start ( {
733777 tlsConfig,
778+ host : _networkConfig . clientHost ,
779+ port : _networkConfig . clientPort ,
780+ connectionCallback : ( streamPair ) =>
781+ this . rpcServerClient . handleStream ( streamPair , { } ) ,
734782 } ) ;
735783 // Agent server
736784 await this . grpcServerAgent . start ( {
737785 services : [ [ AgentServiceService , agentService ] ] ,
738- host : networkConfig_ . agentHost ,
739- port : networkConfig_ . agentPort ,
786+ host : _networkConfig . agentHost ,
787+ port : _networkConfig . agentPort ,
740788 } ) ;
741789 await this . proxy . start ( {
742- forwardHost : networkConfig_ . forwardHost ,
743- forwardPort : networkConfig_ . forwardPort ,
790+ forwardHost : _networkConfig . forwardHost ,
791+ forwardPort : _networkConfig . forwardPort ,
744792 serverHost : this . grpcServerAgent . getHost ( ) ,
745793 serverPort : this . grpcServerAgent . getPort ( ) ,
746- proxyHost : networkConfig_ . proxyHost ,
747- proxyPort : networkConfig_ . proxyPort ,
794+ proxyHost : _networkConfig . proxyHost ,
795+ proxyPort : _networkConfig . proxyPort ,
748796 tlsConfig,
749797 } ) ;
750798 await this . nodeManager . start ( ) ;
@@ -768,10 +816,10 @@ class PolykeyAgent {
768816 await this . status . finishStart ( {
769817 pid : process . pid ,
770818 nodeId : this . keyRing . getNodeId ( ) ,
771- clientHost : this . grpcServerClient . getHost ( ) ,
772- clientPort : this . grpcServerClient . getPort ( ) ,
819+ clientHost : this . webSocketServerClient . getHost ( ) ,
820+ clientPort : this . webSocketServerClient . getPort ( ) ,
773821 agentHost : this . grpcServerAgent . getHost ( ) ,
774- agentPort : this . grpcServerClient . getPort ( ) ,
822+ agentPort : this . grpcServerAgent . getPort ( ) ,
775823 forwardHost : this . proxy . getForwardHost ( ) ,
776824 forwardPort : this . proxy . getForwardPort ( ) ,
777825 proxyHost : this . proxy . getProxyHost ( ) ,
@@ -793,7 +841,7 @@ class PolykeyAgent {
793841 await this . nodeManager ?. stop ( ) ;
794842 await this . proxy ?. stop ( ) ;
795843 await this . grpcServerAgent ?. stop ( ) ;
796- await this . grpcServerClient ? .stop ( ) ;
844+ await this . webSocketServerClient . stop ( true ) ;
797845 await this . identitiesManager ?. stop ( ) ;
798846 await this . gestaltGraph ?. stop ( ) ;
799847 await this . acl ?. stop ( ) ;
@@ -829,7 +877,7 @@ class PolykeyAgent {
829877 await this . nodeManager . stop ( ) ;
830878 await this . proxy . stop ( ) ;
831879 await this . grpcServerAgent . stop ( ) ;
832- await this . grpcServerClient . stop ( ) ;
880+ await this . webSocketServerClient . stop ( true ) ;
833881 await this . identitiesManager . stop ( ) ;
834882 await this . gestaltGraph . stop ( ) ;
835883 await this . acl . stop ( ) ;
@@ -877,6 +925,7 @@ class PolykeyAgent {
877925 await this . vaultManager . destroy ( ) ;
878926 await this . discovery . destroy ( ) ;
879927 await this . nodeGraph . destroy ( ) ;
928+ await this . rpcServerClient . destroy ( ) ;
880929 await this . identitiesManager . destroy ( ) ;
881930 await this . gestaltGraph . destroy ( ) ;
882931 await this . acl . destroy ( ) ;
0 commit comments