@@ -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,40 @@ 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 recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
178+ if ( recipientPeer === undefined ) {
179+ throw new RequestError ( `Unknown recipient ${ recipientID } ` , 400 ) ;
180+ }
181+ if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
182+ throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) ?? 'none' } recieved=${ recipientDestination } ` , 400 ) ;
164183 }
165184 let requestId = uuidV4 ( ) ;
166185 if ( typeof req . body . requestId === 'string' ) {
167186 requestId = req . body . requestId ;
168187 }
169- messagesHandler . sendMessage ( req . body . message , recipientID , recipientURL , requestId , senderDestination , recipientDestination ) ;
188+ messagesHandler . sendMessage ( req . body . message , recipientID , recipientPeer . endpoint , requestId , senderDestination , recipientDestination ) ;
170189 res . send ( { requestId } ) ;
171190 } catch ( err ) {
172191 next ( err ) ;
@@ -231,40 +250,40 @@ router.post('/transfers', async (req, res, next) => {
231250 await blobsHandler . retrieveMetadata ( req . body . path ) ;
232251 let senderDestination : string | undefined = undefined ;
233252 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 ;
253+ const segments = req . body . sender . split ( '/' ) ;
254+ if ( segments [ 0 ] !== peerID ) {
255+ throw new RequestError ( `Sender ID mismatch expected=${ peerID } recieved=${ segments [ 0 ] } ` , 400 ) ;
256+ }
257+ if ( segments . length > 1 ) {
258+ if ( ! config . destinations ?. includes ( segments [ 1 ] ) ) {
259+ throw new RequestError ( `Unknown sender destination expected=${ config . destinations ?. join ( '|' ) } recieved=${ segments [ 1 ] } ` , 400 ) ;
240260 }
261+ senderDestination = segments [ 1 ] ;
241262 }
242263 }
243264 let recipientID : string ;
244265 let recipientDestination : string | undefined = undefined ;
245266 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 ;
267+ const segments = req . body . recipient . split ( '/' ) ;
268+ recipientID = segments [ 0 ] ;
269+ if ( segments . length > 1 ) {
270+ recipientDestination = segments [ 1 ] ;
255271 }
256272 } else {
257273 throw new RequestError ( 'Missing recipient' , 400 ) ;
258274 }
259- let recipientURL = config . peers . find ( peer => peer . id === recipientID ) ?. endpoint ;
260- if ( recipientURL === undefined ) {
275+ let recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
276+ if ( recipientPeer === undefined ) {
261277 throw new RequestError ( `Unknown recipient` , 400 ) ;
262278 }
279+ if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
280+ throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) } recieved=${ recipientDestination } ` , 400 ) ;
281+ }
263282 let requestId = uuidV4 ( ) ;
264283 if ( typeof req . body . requestId === 'string' ) {
265284 requestId = req . body . requestId ;
266285 }
267- blobsHandler . sendBlob ( req . body . path , recipientID , recipientURL , requestId , senderDestination , recipientDestination ) ;
286+ blobsHandler . sendBlob ( req . body . path , recipientID , recipientPeer . endpoint , requestId , senderDestination , recipientDestination ) ;
268287 res . send ( { requestId } ) ;
269288 } catch ( err ) {
270289 next ( err ) ;
0 commit comments