@@ -23,7 +23,7 @@ import * as blobsHandler from '../handlers/blobs';
2323import * as eventsHandler from '../handlers/events' ;
2424import * as messagesHandler from '../handlers/messages' ;
2525import { ca , cert , certBundle , key , peerID } from '../lib/cert' ;
26- import { config , persistPeers } from '../lib/config' ;
26+ import { config , persistDestinations , persistPeers } from '../lib/config' ;
2727import { IStatus } from '../lib/interfaces' ;
2828import RequestError from '../lib/request-error' ;
2929import * as utils from '../lib/utils' ;
@@ -41,7 +41,8 @@ router.get('/id', async (_req, res, next) => {
4141 res . send ( {
4242 id : peerID ,
4343 endpoint : config . p2p . endpoint ?? `https://${ config . p2p . hostname } :${ config . p2p . port } ` ,
44- cert : certBundle
44+ cert : certBundle ,
45+ destinations : config . destinations
4546 } ) ;
4647 } catch ( err ) {
4748 next ( err ) ;
@@ -82,24 +83,42 @@ router.get('/peers', (_req, res) => {
8283 res . send ( config . peers ) ;
8384} ) ;
8485
85- router . put ( '/peers/:id' , async ( req , res , next ) => {
86+ router . put ( '/peers/:id/:destination? ' , async ( req , res , next ) => {
8687 try {
87- if ( req . body . endpoint === undefined ) {
88- throw new RequestError ( 'Missing endpoint' , 400 ) ;
89- }
90- if ( req . body . cert !== undefined ) {
91- await fs . writeFile ( path . join ( utils . constants . DATA_DIRECTORY , utils . constants . PEER_CERTS_SUBDIRECTORY , `${ req . params . id } .pem` ) , req . body . cert ) ;
92- }
93- let peer = config . peers . find ( peer => peer . id === req . params . id ) ;
94- if ( peer === undefined ) {
95- peer = {
96- id : req . params . id ,
97- endpoint : req . body . endpoint
98- } ;
99- config . peers . push ( peer ) ;
88+ if ( req . params . id === peerID ) {
89+ if ( req . params . destination !== undefined ) {
90+ if ( config . destinations === undefined ) {
91+ config . destinations = [ req . params . destination ]
92+ } else if ( ! config . destinations . includes ( req . params . destination ) ) {
93+ config . destinations . push ( req . params . destination ) ;
94+ }
95+ await persistDestinations ( ) ;
96+ }
97+ } else {
98+ let peer = config . peers . find ( peer => peer . id === req . params . id ) ;
99+ if ( peer === undefined ) {
100+ if ( req . body . endpoint === undefined ) {
101+ throw new RequestError ( 'Missing endpoint' , 400 ) ;
102+ }
103+ peer = {
104+ id : req . params . id ,
105+ endpoint : req . body . endpoint
106+ } ;
107+ config . peers . push ( peer ) ;
108+ }
109+ if ( req . params . destination !== undefined ) {
110+ if ( peer . destinations === undefined ) {
111+ peer . destinations = [ req . params . destination ] ;
112+ } else if ( ! peer . destinations . includes ( req . params . destination ) ) {
113+ peer . destinations . push ( req . params . destination ) ;
114+ }
115+ }
116+ await persistPeers ( ) ;
117+ if ( req . body . cert !== undefined ) {
118+ await fs . writeFile ( path . join ( utils . constants . DATA_DIRECTORY , utils . constants . PEER_CERTS_SUBDIRECTORY , `${ req . params . id } .pem` ) , req . body . cert ) ;
119+ await refreshCACerts ( ) ;
120+ }
100121 }
101- await persistPeers ( ) ;
102- await refreshCACerts ( ) ;
103122 res . send ( { status : 'added' } ) ;
104123 } catch ( err ) {
105124 next ( err ) ;
@@ -133,40 +152,46 @@ router.post('/messages', async (req, res, next) => {
133152 }
134153 let senderDestination : string | undefined = undefined ;
135154 if ( typeof req . body . sender === 'string' ) {
136- if ( ! req . body . sender . startsWith ( peerID ) ) {
137- throw new RequestError ( 'Invalid sender' ) ;
138- } else {
139- const destination = req . body . sender . substring ( peerID . length + 1 ) ;
140- if ( destination . length > 0 ) {
141- senderDestination = destination ;
155+ const segments = req . body . sender . split ( '/' ) ;
156+ if ( segments [ 0 ] !== peerID ) {
157+ throw new RequestError ( `Sender ID mismatch expected=${ peerID } recieved=${ segments [ 0 ] } ` , 400 ) ;
158+ }
159+ if ( segments . length > 1 ) {
160+ if ( ! config . destinations ?. includes ( segments [ 1 ] ) ) {
161+ throw new RequestError ( `Unknown sender destination expected=${ config . destinations ?. join ( '|' ) ?? 'none' } recieved=${ segments [ 1 ] } ` , 400 ) ;
142162 }
163+ senderDestination = segments [ 1 ] ;
143164 }
144165 }
145166 let recipientID : string ;
146167 let recipientDestination : string | undefined = undefined ;
147168 if ( typeof req . body . recipient === 'string' ) {
148- const index = req . body . recipient . indexOf ( utils . constants . ID_SEGMENT_SEPARATOR ) ;
149- if ( index !== - 1 ) {
150- recipientID = req . body . recipient . substring ( 0 , index ) ;
151- const destination = req . body . recipient . substring ( index + 1 ) ;
152- if ( destination . length > 0 ) {
153- recipientDestination = destination ;
154- }
155- } else {
156- recipientID = req . body . recipient ;
169+ const segments = req . body . recipient . split ( '/' ) ;
170+ recipientID = segments [ 0 ] ;
171+ if ( segments . length > 1 ) {
172+ recipientDestination = segments [ 1 ] ;
157173 }
158174 } else {
159175 throw new RequestError ( 'Missing recipient' , 400 ) ;
160176 }
161- let recipientURL = config . peers . find ( peer => peer . id === recipientID ) ?. endpoint ;
162- if ( recipientURL === undefined ) {
163- throw new RequestError ( `Unknown recipient` , 400 ) ;
177+ let recipientEndpoint : string ;
178+ if ( recipientID === peerID ) {
179+ recipientEndpoint = config . p2p . endpoint ?? `https://${ config . p2p . hostname } :${ config . p2p . port } ` ;
180+ } else {
181+ let recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
182+ if ( recipientPeer === undefined ) {
183+ throw new RequestError ( `Unknown recipient ${ recipientID } ` , 400 ) ;
184+ }
185+ recipientEndpoint = recipientPeer . endpoint ;
186+ if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
187+ throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) ?? 'none' } recieved=${ recipientDestination } ` , 400 ) ;
188+ }
164189 }
165190 let requestId = uuidV4 ( ) ;
166191 if ( typeof req . body . requestId === 'string' ) {
167192 requestId = req . body . requestId ;
168193 }
169- messagesHandler . sendMessage ( req . body . message , recipientID , recipientURL , requestId , senderDestination , recipientDestination ) ;
194+ messagesHandler . sendMessage ( req . body . message , recipientID , recipientEndpoint , requestId , senderDestination , recipientDestination ) ;
170195 res . send ( { requestId } ) ;
171196 } catch ( err ) {
172197 next ( err ) ;
@@ -231,40 +256,46 @@ router.post('/transfers', async (req, res, next) => {
231256 await blobsHandler . retrieveMetadata ( req . body . path ) ;
232257 let senderDestination : string | undefined = undefined ;
233258 if ( typeof req . body . sender === 'string' ) {
234- if ( ! req . body . sender . startsWith ( peerID ) ) {
235- throw new RequestError ( 'Invalid sender' ) ;
236- } else {
237- const destination = req . body . sender . substring ( peerID . length + 1 ) ;
238- if ( destination . length > 0 ) {
239- senderDestination = destination ;
259+ const segments = req . body . sender . split ( '/' ) ;
260+ if ( segments [ 0 ] !== peerID ) {
261+ throw new RequestError ( `Sender ID mismatch expected=${ peerID } recieved=${ segments [ 0 ] } ` , 400 ) ;
262+ }
263+ if ( segments . length > 1 ) {
264+ if ( ! config . destinations ?. includes ( segments [ 1 ] ) ) {
265+ throw new RequestError ( `Unknown sender destination expected=${ config . destinations ?. join ( '|' ) } recieved=${ segments [ 1 ] } ` , 400 ) ;
240266 }
267+ senderDestination = segments [ 1 ] ;
241268 }
242269 }
243270 let recipientID : string ;
244271 let recipientDestination : string | undefined = undefined ;
245272 if ( typeof req . body . recipient === 'string' ) {
246- const index = req . body . recipient . indexOf ( utils . constants . ID_SEGMENT_SEPARATOR ) ;
247- if ( index !== - 1 ) {
248- recipientID = req . body . recipient . substring ( 0 , index ) ;
249- const destination = req . body . recipient . substring ( index + 1 ) ;
250- if ( destination . length > 0 ) {
251- recipientDestination = destination ;
252- }
253- } else {
254- recipientID = req . body . recipient ;
273+ const segments = req . body . recipient . split ( '/' ) ;
274+ recipientID = segments [ 0 ] ;
275+ if ( segments . length > 1 ) {
276+ recipientDestination = segments [ 1 ] ;
255277 }
256278 } else {
257279 throw new RequestError ( 'Missing recipient' , 400 ) ;
258280 }
259- let recipientURL = config . peers . find ( peer => peer . id === recipientID ) ?. endpoint ;
260- if ( recipientURL === undefined ) {
261- throw new RequestError ( `Unknown recipient` , 400 ) ;
281+ let recipientEndpoint : string ;
282+ if ( recipientID === peerID ) {
283+ recipientEndpoint = config . p2p . endpoint ?? `https://${ config . p2p . hostname } :${ config . p2p . port } ` ;
284+ } else {
285+ let recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
286+ if ( recipientPeer === undefined ) {
287+ throw new RequestError ( `Unknown recipient` , 400 ) ;
288+ }
289+ if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
290+ throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) } recieved=${ recipientDestination } ` , 400 ) ;
291+ }
292+ recipientEndpoint = recipientPeer . endpoint ;
262293 }
263294 let requestId = uuidV4 ( ) ;
264295 if ( typeof req . body . requestId === 'string' ) {
265296 requestId = req . body . requestId ;
266297 }
267- blobsHandler . sendBlob ( req . body . path , recipientID , recipientURL , requestId , senderDestination , recipientDestination ) ;
298+ blobsHandler . sendBlob ( req . body . path , recipientID , recipientEndpoint , requestId , senderDestination , recipientDestination ) ;
268299 res . send ( { requestId } ) ;
269300 } catch ( err ) {
270301 next ( err ) ;
0 commit comments