|
| 1 | +import type Logger from '@matrixai/logger'; |
| 2 | +import type { |
| 3 | + AgentRPCRequestParams, |
| 4 | + AgentRPCResponseResult, |
| 5 | + HolePunchRequestMessage, |
| 6 | +} from '../types'; |
| 7 | +import type NodeConnectionManager from '../../NodeConnectionManager'; |
| 8 | +import type { Host, Port } from '../../../network/types'; |
| 9 | +import { UnaryHandler } from '@matrixai/rpc'; |
| 10 | +import * as keysUtils from '../../../keys/utils'; |
| 11 | +import * as ids from '../../../ids'; |
| 12 | +import * as agentErrors from '../errors'; |
| 13 | +import * as agentUtils from '../utils'; |
| 14 | + |
| 15 | +class NodesConnectionSignalFinal extends UnaryHandler< |
| 16 | + { |
| 17 | + nodeConnectionManager: NodeConnectionManager; |
| 18 | + logger: Logger; |
| 19 | + }, |
| 20 | + AgentRPCRequestParams<HolePunchRequestMessage>, |
| 21 | + AgentRPCResponseResult |
| 22 | +> { |
| 23 | + public handle = async ( |
| 24 | + input: AgentRPCRequestParams<HolePunchRequestMessage>, |
| 25 | + _cancel, |
| 26 | + meta, |
| 27 | + ): Promise<AgentRPCResponseResult> => { |
| 28 | + const { nodeConnectionManager, logger } = this.container; |
| 29 | + // Connections should always be validated |
| 30 | + const sourceNodeId = ids.parseNodeId(input.sourceNodeIdEncoded); |
| 31 | + const targetNodeId = ids.parseNodeId(input.targetNodeIdEncoded); |
| 32 | + const relayingNodeId = agentUtils.nodeIdFromMeta(meta); |
| 33 | + if (relayingNodeId == null) { |
| 34 | + throw new agentErrors.ErrorAgentNodeIdMissing(); |
| 35 | + } |
| 36 | + const requestSignature = Buffer.from(input.requestSignature, 'base64url'); |
| 37 | + // Checking request requestSignature, requestData is just `<sourceNodeId><targetNodeId>` concatenated |
| 38 | + const requestData = Buffer.concat([sourceNodeId, targetNodeId]); |
| 39 | + const sourcePublicKey = keysUtils.publicKeyFromNodeId(sourceNodeId); |
| 40 | + if ( |
| 41 | + !keysUtils.verifyWithPublicKey( |
| 42 | + sourcePublicKey, |
| 43 | + requestData, |
| 44 | + requestSignature, |
| 45 | + ) |
| 46 | + ) { |
| 47 | + throw new agentErrors.ErrorNodesConnectionSignalRequestVerificationFailed(); |
| 48 | + } |
| 49 | + // Checking relay message relaySignature. |
| 50 | + // relayData is just `<sourceNodeId><targetNodeId><Address><requestSignature>` concatenated. |
| 51 | + const relayData = Buffer.concat([ |
| 52 | + sourceNodeId, |
| 53 | + targetNodeId, |
| 54 | + Buffer.from(JSON.stringify(input.address), 'utf-8'), |
| 55 | + requestSignature, |
| 56 | + ]); |
| 57 | + const relayPublicKey = keysUtils.publicKeyFromNodeId(relayingNodeId); |
| 58 | + const relaySignature = Buffer.from(input.relaySignature, 'base64url'); |
| 59 | + if ( |
| 60 | + !keysUtils.verifyWithPublicKey(relayPublicKey, relayData, relaySignature) |
| 61 | + ) { |
| 62 | + throw new agentErrors.ErrorNodesConnectionSignalRelayVerificationFailed(); |
| 63 | + } |
| 64 | + |
| 65 | + const host = input.address.host as Host; |
| 66 | + const port = input.address.port as Port; |
| 67 | + logger.debug(`Received signaling message to target ${host}:${port}`); |
| 68 | + nodeConnectionManager.handleNodesConnectionSignalFinal(host, port); |
| 69 | + return {}; |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +export default NodesConnectionSignalFinal; |
0 commit comments