@routr/processor 2.22.0 → 2.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { MessageRequest, Route } from "@routr/common";
1
+ import { MessageRequest, Route, CommonTypes } from "@routr/common";
2
2
  export declare const fixRequestURI: (route: Route) => (request: MessageRequest) => MessageRequest;
3
3
  export declare const addSelfVia: (route: Route) => (request: MessageRequest) => MessageRequest;
4
4
  export declare const addSelfViaUsingTheRouteHeaders: (request: MessageRequest) => MessageRequest;
@@ -6,7 +6,7 @@ export declare const addSelfViaUsingExternalAddrs: (request: MessageRequest) =>
6
6
  export declare const addRouteToNextHop: (route: Route) => (request: MessageRequest) => MessageRequest;
7
7
  export declare const addRouteToPeerEdgePort: (route: Route) => (request: MessageRequest) => MessageRequest;
8
8
  export declare const applyXHeaders: (route: Route) => (request: MessageRequest) => MessageRequest;
9
- export declare const addSelfRecordRoute: (route: Route) => (request: MessageRequest) => MessageRequest;
9
+ export declare const addSelfRecordRoute: (route: Route, parameters?: Record<string, string>) => (request: MessageRequest) => MessageRequest;
10
10
  export declare const addXEdgePortRef: (request: MessageRequest) => MessageRequest;
11
11
  export declare const removeXEdgePortRef: (request: MessageRequest) => MessageRequest;
12
12
  export declare const removeHeaders: (names: string[]) => (request: MessageRequest) => MessageRequest;
@@ -16,4 +16,48 @@ export declare const removeTopVia: (request: MessageRequest) => MessageRequest;
16
16
  export declare const decreaseMaxForwards: (request: MessageRequest) => MessageRequest;
17
17
  export declare const fixInvalidContact: (request: MessageRequest) => MessageRequest;
18
18
  export declare const overwriteContactWithSenderInfo: (request: MessageRequest) => MessageRequest;
19
- export declare const enforceE164: (enforceE164: boolean, validateMobilePrefix: boolean) => (request: MessageRequest) => MessageRequest;
19
+ export declare const normalizeToE164: (normalizeToE164: boolean) => (request: MessageRequest) => MessageRequest;
20
+ /**
21
+ * Whether a From is the anonymous identity (RFC 3323, SIP Connect 1.1 section 9).
22
+ * A caller that asked for privacy keeps it, so such a From is never rewritten.
23
+ *
24
+ * @param {CommonTypes.SipURI} uri - the From URI
25
+ * @return {boolean} true for anonymous@anonymous.invalid and its variants
26
+ */
27
+ export declare const isAnonymous: (uri: CommonTypes.SipURI) => boolean;
28
+ /**
29
+ * Presents the Number as the caller toward a Trunk with rewriteFromWithNumber.
30
+ * Carriers that authenticate by the From header (WhatsApp Business Calling, many
31
+ * SIPconnect carriers) only accept a From that is the business number at their
32
+ * domain. The tag and display name stay, so the dialog is unchanged for Routr,
33
+ * which matches on tags and Call-ID. An anonymous From is left as it is.
34
+ *
35
+ * @param {Route} route - the route; does nothing unless route.rewriteFrom is set
36
+ * @return {Function} the alteration
37
+ */
38
+ export declare const rewriteFromWithNumber: (route: Route) => (request: MessageRequest) => MessageRequest;
39
+ /**
40
+ * The Record-Route parameters that remember how the identity alterations changed
41
+ * a request's From and To. Both ends of the dialog copy Record-Route into the
42
+ * Route of their in-dialog requests, so the mapping comes back to Routr on every
43
+ * BYE, re-INVITE and ACK without any state in Routr (see swapIdentityFromRouteSet).
44
+ *
45
+ * @param {MessageRequest} original - the request before the identity alterations
46
+ * @param {MessageRequest} altered - the request after them
47
+ * @return {Record<string, string>} the parameters, empty when nothing changed
48
+ */
49
+ export declare const identityRewriteParameters: (original: MessageRequest, altered: MessageRequest) => Record<string, string>;
50
+ /**
51
+ * Keeps each side of a dialog seeing the identities it knows. A request from the
52
+ * caller gets the From and To the Trunk saw on the INVITE; a request from the
53
+ * Trunk, whose To is the Number, gets the caller's own identity back. Some phones
54
+ * answer 481 to an in-dialog request whose To or From URI changed.
55
+ *
56
+ * The mapping is read only from Route entries that point at this EdgePort: with
57
+ * two EdgePorts on the path, the one that rewrote the INVITE is the one that
58
+ * swaps, and the other leaves the request alone.
59
+ *
60
+ * @param {MessageRequest} request - an in-dialog request, with its Route set
61
+ * @return {MessageRequest} the request with From and To swapped where they match
62
+ */
63
+ export declare const swapIdentityFromRouteSet: (request: MessageRequest) => MessageRequest;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.enforceE164 = exports.overwriteContactWithSenderInfo = exports.fixInvalidContact = exports.decreaseMaxForwards = exports.removeTopVia = exports.removeSelfRoutes = exports.removeAuthorization = exports.removeHeaders = exports.removeXEdgePortRef = exports.addXEdgePortRef = exports.addSelfRecordRoute = exports.applyXHeaders = exports.addRouteToPeerEdgePort = exports.addRouteToNextHop = exports.addSelfViaUsingExternalAddrs = exports.addSelfViaUsingTheRouteHeaders = exports.addSelfVia = exports.fixRequestURI = void 0;
3
+ exports.swapIdentityFromRouteSet = exports.identityRewriteParameters = exports.rewriteFromWithNumber = exports.isAnonymous = exports.normalizeToE164 = exports.overwriteContactWithSenderInfo = exports.fixInvalidContact = exports.decreaseMaxForwards = exports.removeTopVia = exports.removeSelfRoutes = exports.removeAuthorization = exports.removeHeaders = exports.removeXEdgePortRef = exports.addXEdgePortRef = exports.addSelfRecordRoute = exports.applyXHeaders = exports.addRouteToPeerEdgePort = exports.addRouteToNextHop = exports.addSelfViaUsingExternalAddrs = exports.addSelfViaUsingTheRouteHeaders = exports.addSelfVia = exports.fixRequestURI = void 0;
4
4
  /*
5
5
  * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
6
  * http://github.com/fonoster/routr
@@ -145,7 +145,9 @@ const applyXHeaders = (route) => (request) => {
145
145
  return req;
146
146
  };
147
147
  exports.applyXHeaders = applyXHeaders;
148
- const addSelfRecordRoute = (route) => (request) => {
148
+ // The parameters argument is merged into every Record-Route entry this adds;
149
+ // identityRewriteParameters uses it to carry a rewritten From or To.
150
+ const addSelfRecordRoute = (route, parameters = {}) => (request) => {
149
151
  const req = common_1.Helper.deepCopy(request);
150
152
  const originIntf = (0, helper_1.getEdgeInterface)({
151
153
  listeningPoints: request.listeningPoints,
@@ -186,14 +188,10 @@ const addSelfRecordRoute = (route) => (request) => {
186
188
  lrParam: true
187
189
  }
188
190
  },
189
- parameters: {
190
- r2: "on"
191
- }
191
+ parameters: Object.assign({ r2: "on" }, parameters)
192
192
  };
193
193
  // Add r2 on to origin route
194
- originRoute.parameters = {
195
- r2: "on"
196
- };
194
+ originRoute.parameters = Object.assign({ r2: "on" }, parameters);
197
195
  req.message.recordRoute = [
198
196
  targetRoute,
199
197
  originRoute,
@@ -201,6 +199,9 @@ const addSelfRecordRoute = (route) => (request) => {
201
199
  ];
202
200
  }
203
201
  else {
202
+ if (Object.keys(parameters).length > 0) {
203
+ originRoute.parameters = Object.assign({}, parameters);
204
+ }
204
205
  req.message.recordRoute = [originRoute, ...req.message.recordRoute];
205
206
  }
206
207
  return req;
@@ -239,18 +240,21 @@ const removeAuthorization = (request) => {
239
240
  return req;
240
241
  };
241
242
  exports.removeAuthorization = removeAuthorization;
243
+ // Whether a Route entry points at this EdgePort (one of its local or external
244
+ // addresses, on the port of the matching listening point)
245
+ const isSelfRoute = (request) => (r) => {
246
+ var _a;
247
+ const localIps = request.localnets.map((ln) => ln.split("/")[0]);
248
+ const lp = common_1.Helper.getListeningPoint(request.listeningPoints, ((_a = r.address.uri.transportParam) === null || _a === void 0 ? void 0 : _a.toUpperCase()) ||
249
+ common_1.Transport.UDP);
250
+ const routeUri = r.address.uri;
251
+ return ((localIps.includes(routeUri.host) ||
252
+ request.externalAddrs.includes(routeUri.host)) &&
253
+ routeUri.port === (lp === null || lp === void 0 ? void 0 : lp.port));
254
+ };
242
255
  const removeSelfRoutes = (request) => {
243
256
  const req = common_1.Helper.deepCopy(request);
244
- const localIps = request.localnets.map((ln) => ln.split("/")[0]);
245
- req.message.route = req.message.route.filter((r) => {
246
- var _a;
247
- const lp = common_1.Helper.getListeningPoint(request.listeningPoints, ((_a = r.address.uri.transportParam) === null || _a === void 0 ? void 0 : _a.toUpperCase()) ||
248
- common_1.Transport.UDP);
249
- const routeUri = r.address.uri;
250
- return !((localIps.includes(routeUri.host) ||
251
- request.externalAddrs.includes(routeUri.host)) &&
252
- routeUri.port === lp.port);
253
- });
257
+ req.message.route = req.message.route.filter((r) => !isSelfRoute(request)(r));
254
258
  return req;
255
259
  };
256
260
  exports.removeSelfRoutes = removeSelfRoutes;
@@ -293,10 +297,17 @@ const overwriteContactWithSenderInfo = (request) => {
293
297
  return req;
294
298
  };
295
299
  exports.overwriteContactWithSenderInfo = overwriteContactWithSenderInfo;
296
- // Will do the best effort to normalize from/to/requestUri users as e164 values
297
- const enforceE164 = (enforceE164, validateMobilePrefix) => (request) => {
300
+ // Will do the best effort to normalize from/to/requestUri users as e164 values.
301
+ // Connect applies it per Trunk (spec.normalizeToE164), on the way in before routing
302
+ // and on the way out through the tailor.
303
+ //
304
+ // The phone library validates mobile prefixes by default, which rejects
305
+ // landlines; that check is off everywhere, so any valid number is normalized.
306
+ // (The first version did the same; a later port turned it on for From alone.)
307
+ const normalize = (user) => (0, phone_1.phone)(`+${user}`, { validateMobilePrefix: false });
308
+ const normalizeToE164 = (normalizeToE164) => (request) => {
298
309
  var _a, _b, _c;
299
- if (!enforceE164)
310
+ if (!normalizeToE164)
300
311
  return request;
301
312
  const req = common_1.Helper.deepCopy(request);
302
313
  // Removes the + to ensure not having duplicates
@@ -304,13 +315,9 @@ const enforceE164 = (enforceE164, validateMobilePrefix) => (request) => {
304
315
  const toUser = (_b = req.message.to.address.uri.user) === null || _b === void 0 ? void 0 : _b.replace("+", "");
305
316
  const requestUriUser = (_c = req.message.requestUri.user) === null || _c === void 0 ? void 0 : _c.replace("+", "");
306
317
  // Normalizing as e164
307
- const normalizedTo = (0, phone_1.phone)(`+${toUser}`, { validateMobilePrefix });
308
- const normalizedFrom = (0, phone_1.phone)(`+${fromUser}`, {
309
- validateMobilePrefix: true
310
- });
311
- const normalizedReqUri = (0, phone_1.phone)(`+${requestUriUser}`, {
312
- validateMobilePrefix
313
- });
318
+ const normalizedTo = normalize(toUser);
319
+ const normalizedFrom = normalize(fromUser);
320
+ const normalizedReqUri = normalize(requestUriUser);
314
321
  // If the normalization worked we overwrite the original values
315
322
  if (normalizedFrom.isValid) {
316
323
  req.message.from.address.uri.user = normalizedFrom.phoneNumber;
@@ -323,4 +330,176 @@ const enforceE164 = (enforceE164, validateMobilePrefix) => (request) => {
323
330
  }
324
331
  return req;
325
332
  };
326
- exports.enforceE164 = enforceE164;
333
+ exports.normalizeToE164 = normalizeToE164;
334
+ /**
335
+ * Whether a From is the anonymous identity (RFC 3323, SIP Connect 1.1 section 9).
336
+ * A caller that asked for privacy keeps it, so such a From is never rewritten.
337
+ *
338
+ * @param {CommonTypes.SipURI} uri - the From URI
339
+ * @return {boolean} true for anonymous@anonymous.invalid and its variants
340
+ */
341
+ const isAnonymous = (uri) => {
342
+ var _a, _b;
343
+ return ((_a = uri.user) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === common_1.CommonTypes.ANONYMOUS ||
344
+ ((_b = uri.host) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === "anonymous.invalid";
345
+ };
346
+ exports.isAnonymous = isAnonymous;
347
+ /**
348
+ * Presents the Number as the caller toward a Trunk with rewriteFromWithNumber.
349
+ * Carriers that authenticate by the From header (WhatsApp Business Calling, many
350
+ * SIPconnect carriers) only accept a From that is the business number at their
351
+ * domain. The tag and display name stay, so the dialog is unchanged for Routr,
352
+ * which matches on tags and Call-ID. An anonymous From is left as it is.
353
+ *
354
+ * @param {Route} route - the route; does nothing unless route.rewriteFrom is set
355
+ * @return {Function} the alteration
356
+ */
357
+ const rewriteFromWithNumber = (route) => (request) => {
358
+ if (!route.rewriteFrom)
359
+ return request;
360
+ if ((0, exports.isAnonymous)(request.message.from.address.uri))
361
+ return request;
362
+ const req = common_1.Helper.deepCopy(request);
363
+ const uri = req.message.from.address.uri;
364
+ uri.user = route.rewriteFrom.user;
365
+ uri.host = route.rewriteFrom.host;
366
+ // The URI names a telephone number (RFC 3261 section 19.1.1), as the
367
+ // P-Asserted-Identity Connect adds toward the Trunk does
368
+ uri.userParam = "phone";
369
+ // The caller's own port has no meaning at the carrier's domain; -1 is how
370
+ // the EdgePort's converter spells "no port"
371
+ uri.port = -1;
372
+ return req;
373
+ };
374
+ exports.rewriteFromWithNumber = rewriteFromWithNumber;
375
+ // The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
376
+ // [o]riginal/[n]ew. Not "rto": a parser that looks for a header by searching
377
+ // for its name (SIPp does) finds "To" inside "rto=" on the Record-Route line.
378
+ const IDENTITY_PARAMETERS = {
379
+ fromOriginal: "rfo",
380
+ fromNew: "rfn",
381
+ toOriginal: "rco",
382
+ toNew: "rcn"
383
+ };
384
+ // Base64url keeps the value a SIP token (RFC 3261 section 25.1), so it needs no
385
+ // quoting in the header and survives every hop unchanged
386
+ const encodeIdentity = (uri) => {
387
+ var _a;
388
+ return Buffer.from(`${(_a = uri.user) !== null && _a !== void 0 ? _a : ""}@${uri.host}${uri.userParam ? `;user=${uri.userParam}` : ""}`).toString("base64url");
389
+ };
390
+ const decodeIdentity = (value) => {
391
+ if (!value)
392
+ return undefined;
393
+ const decoded = Buffer.from(value, "base64url").toString("utf8");
394
+ const [address, userParam] = decoded.split(";user=");
395
+ const at = address.lastIndexOf("@");
396
+ if (at < 0)
397
+ return undefined;
398
+ const user = address.slice(0, at);
399
+ return {
400
+ user: user || undefined,
401
+ host: address.slice(at + 1),
402
+ userParam: userParam || undefined
403
+ };
404
+ };
405
+ const sameIdentity = (uri, identity) => {
406
+ var _a, _b, _c;
407
+ return !!identity &&
408
+ ((_a = uri.user) !== null && _a !== void 0 ? _a : "") === ((_b = identity.user) !== null && _b !== void 0 ? _b : "") &&
409
+ ((_c = uri.host) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === identity.host.toLowerCase();
410
+ };
411
+ const changed = (a, b) => {
412
+ var _a, _b, _c, _d;
413
+ return ((_a = a.user) !== null && _a !== void 0 ? _a : "") !== ((_b = b.user) !== null && _b !== void 0 ? _b : "") ||
414
+ a.host !== b.host ||
415
+ ((_c = a.userParam) !== null && _c !== void 0 ? _c : "") !== ((_d = b.userParam) !== null && _d !== void 0 ? _d : "");
416
+ };
417
+ /**
418
+ * The Record-Route parameters that remember how the identity alterations changed
419
+ * a request's From and To. Both ends of the dialog copy Record-Route into the
420
+ * Route of their in-dialog requests, so the mapping comes back to Routr on every
421
+ * BYE, re-INVITE and ACK without any state in Routr (see swapIdentityFromRouteSet).
422
+ *
423
+ * @param {MessageRequest} original - the request before the identity alterations
424
+ * @param {MessageRequest} altered - the request after them
425
+ * @return {Record<string, string>} the parameters, empty when nothing changed
426
+ */
427
+ const identityRewriteParameters = (original, altered) => {
428
+ const parameters = {};
429
+ const fromBefore = original.message.from.address.uri;
430
+ const fromAfter = altered.message.from.address.uri;
431
+ const toBefore = original.message.to.address.uri;
432
+ const toAfter = altered.message.to.address.uri;
433
+ if (changed(fromBefore, fromAfter)) {
434
+ parameters[IDENTITY_PARAMETERS.fromOriginal] = encodeIdentity(fromBefore);
435
+ parameters[IDENTITY_PARAMETERS.fromNew] = encodeIdentity(fromAfter);
436
+ }
437
+ if (changed(toBefore, toAfter)) {
438
+ parameters[IDENTITY_PARAMETERS.toOriginal] = encodeIdentity(toBefore);
439
+ parameters[IDENTITY_PARAMETERS.toNew] = encodeIdentity(toAfter);
440
+ }
441
+ return parameters;
442
+ };
443
+ exports.identityRewriteParameters = identityRewriteParameters;
444
+ // Like isSelfRoute, but a Route on a transport this EdgePort lacks is simply
445
+ // not ours, instead of an error that would stop the in-dialog request
446
+ const isOwnRoute = (request, route) => {
447
+ try {
448
+ return isSelfRoute(request)(route);
449
+ }
450
+ catch (_a) {
451
+ return false;
452
+ }
453
+ };
454
+ const applyIdentity = (uri, identity) => {
455
+ uri.user = identity.user;
456
+ uri.host = identity.host;
457
+ uri.userParam = identity.userParam;
458
+ };
459
+ /**
460
+ * Keeps each side of a dialog seeing the identities it knows. A request from the
461
+ * caller gets the From and To the Trunk saw on the INVITE; a request from the
462
+ * Trunk, whose To is the Number, gets the caller's own identity back. Some phones
463
+ * answer 481 to an in-dialog request whose To or From URI changed.
464
+ *
465
+ * The mapping is read only from Route entries that point at this EdgePort: with
466
+ * two EdgePorts on the path, the one that rewrote the INVITE is the one that
467
+ * swaps, and the other leaves the request alone.
468
+ *
469
+ * @param {MessageRequest} request - an in-dialog request, with its Route set
470
+ * @return {MessageRequest} the request with From and To swapped where they match
471
+ */
472
+ const swapIdentityFromRouteSet = (request) => {
473
+ var _a;
474
+ const own = ((_a = request.message.route) !== null && _a !== void 0 ? _a : []).find((r) => {
475
+ var _a, _b;
476
+ return ((_a = r.parameters) === null || _a === void 0 ? void 0 : _a[IDENTITY_PARAMETERS.fromNew]) ||
477
+ ((_b = r.parameters) === null || _b === void 0 ? void 0 : _b[IDENTITY_PARAMETERS.toNew]);
478
+ });
479
+ if (!own || !isOwnRoute(request, own))
480
+ return request;
481
+ const fromOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromOriginal]);
482
+ const fromNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromNew]);
483
+ const toOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toOriginal]);
484
+ const toNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toNew]);
485
+ const req = common_1.Helper.deepCopy(request);
486
+ const from = req.message.from.address.uri;
487
+ const to = req.message.to.address.uri;
488
+ // From the caller: From is the caller, To is the callee
489
+ if (fromNew && sameIdentity(from, fromOriginal)) {
490
+ applyIdentity(from, fromNew);
491
+ }
492
+ else if (toOriginal && sameIdentity(from, toNew)) {
493
+ // From the Trunk: From is the callee as the Trunk knows it
494
+ applyIdentity(from, toOriginal);
495
+ }
496
+ if (toNew && sameIdentity(to, toOriginal)) {
497
+ applyIdentity(to, toNew);
498
+ }
499
+ else if (fromOriginal && sameIdentity(to, fromNew)) {
500
+ // From the Trunk: To is the Number the caller was presented as
501
+ applyIdentity(to, fromOriginal);
502
+ }
503
+ return req;
504
+ };
505
+ exports.swapIdentityFromRouteSet = swapIdentityFromRouteSet;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/processor",
3
- "version": "2.22.0",
3
+ "version": "2.24.0",
4
4
  "description": "Processor server and API",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -15,7 +15,7 @@
15
15
  "@fonoster/logger": "0.6.0",
16
16
  "@grpc/grpc-js": "~1.14.6",
17
17
  "@grpc/proto-loader": "~0.7.10",
18
- "@routr/common": "^2.22.0",
18
+ "@routr/common": "^2.24.0",
19
19
  "fp-ts": "^2.11.8",
20
20
  "phone": "^3.1.68"
21
21
  },
@@ -36,5 +36,5 @@
36
36
  "bugs": {
37
37
  "url": "https://github.com/fonoster/routr/issues"
38
38
  },
39
- "gitHead": "43868fbe7fb81a31b7eadd88d6847778d10f1754"
39
+ "gitHead": "168637a54c6390bb7c79dc77a6e9f763d2ce2da2"
40
40
  }