diff --git a/src/agent/errors.ts b/src/agent/errors.ts index b92f3b306e..e4db4293c8 100644 --- a/src/agent/errors.ts +++ b/src/agent/errors.ts @@ -1,4 +1,4 @@ -import { ErrorPolykey } from '../errors'; +import { ErrorPolykey, sysexits } from '../errors'; class ErrorAgent extends ErrorPolykey {} @@ -8,9 +8,15 @@ class ErrorAgentClientNotStarted extends ErrorAgent {} class ErrorAgentClientDestroyed extends ErrorAgent {} +class ErrorConnectionInfoMissing extends ErrorAgent { + description = 'Vault already exists'; + exitCode = sysexits.UNAVAILABLE; +} + export { ErrorAgent, ErrorAgentClientNotStarted, ErrorAgentRunning, ErrorAgentClientDestroyed, + ErrorConnectionInfoMissing, }; diff --git a/src/agent/service/echo.ts b/src/agent/service/echo.ts index 64b98dd55b..b99923bbbe 100644 --- a/src/agent/service/echo.ts +++ b/src/agent/service/echo.ts @@ -1,17 +1,13 @@ import type * as grpc from '@grpc/grpc-js'; -import type { ConnectionInfoGetter } from 'agent/types'; +import type { ConnectionInfoGet } from 'agent/types'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -function echo({ - connectionInfoGetter, -}: { - connectionInfoGetter: ConnectionInfoGetter; -}) { +function echo({ connectionInfoGet }: { connectionInfoGet: ConnectionInfoGet }) { return async ( call: grpc.ServerUnaryCall, callback: grpc.sendUnaryData, ): Promise => { - connectionInfoGetter(call.getPeer()); + connectionInfoGet(call); const response = new utilsPB.EchoMessage(); response.setChallenge(call.request.getChallenge()); callback(null, response); diff --git a/src/agent/service/index.ts b/src/agent/service/index.ts index 2ab0cbb91a..d7427d6012 100644 --- a/src/agent/service/index.ts +++ b/src/agent/service/index.ts @@ -36,12 +36,10 @@ function createService(container: { gestaltGraph: GestaltGraph; revProxy: ReverseProxy; }): IAgentServiceServer { - const connectionInfoGetter = agentUtils.connectionInfoGetter( - container.revProxy, - ); + const connectionInfoGet = agentUtils.connectionInfoGetter(container.revProxy); const container_ = { ...container, - connectionInfoGetter, + connectionInfoGet: connectionInfoGet, }; const service: IAgentServiceServer = { echo: echo(container_), diff --git a/src/agent/types.ts b/src/agent/types.ts index 9c9831b9a0..ced17bbf1b 100644 --- a/src/agent/types.ts +++ b/src/agent/types.ts @@ -1,5 +1,8 @@ import type { ConnectionInfo } from 'network/types'; +import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call'; -type ConnectionInfoGetter = (peerInfo: string) => ConnectionInfo | undefined; +type ConnectionInfoGet = ( + call: ServerSurfaceCall, +) => ConnectionInfo | undefined; -export type { ConnectionInfoGetter }; +export type { ConnectionInfoGet }; diff --git a/src/agent/utils.ts b/src/agent/utils.ts index 0a690159e4..6d6b6fdd2c 100644 --- a/src/agent/utils.ts +++ b/src/agent/utils.ts @@ -1,15 +1,17 @@ import type { Host, Port } from 'network/types'; import type ReverseProxy from 'network/ReverseProxy'; -import type { ConnectionInfoGetter } from './types'; +import type { ConnectionInfoGet } from './types'; +import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call'; -function connectionInfoGetter(revProxy: ReverseProxy): ConnectionInfoGetter { - return (peerInfo: string) => { - const address = peerInfo.split(':'); - const host = address[0] as Host; - const port = parseInt(address[1]) as Port; - // Return undefined in input was invalid - if (host == null || isNaN(port)) return; - return revProxy.getConnectionInfoByProxy(host, port); +function connectionInfoGetter(revProxy: ReverseProxy): ConnectionInfoGet { + return (call: ServerSurfaceCall) => { + let urlString = call.getPeer(); + if (!/^.*:\/\//.test(urlString)) urlString = 'pk://' + urlString; + const url = new URL(urlString); + return revProxy.getConnectionInfoByProxy( + url.hostname as Host, + parseInt(url.port) as Port, + ); }; }