@routr/processor 2.22.0 → 2.23.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;
@@ -17,3 +17,47 @@ export declare const decreaseMaxForwards: (request: MessageRequest) => MessageRe
17
17
  export declare const fixInvalidContact: (request: MessageRequest) => MessageRequest;
18
18
  export declare const overwriteContactWithSenderInfo: (request: MessageRequest) => MessageRequest;
19
19
  export declare const enforceE164: (enforceE164: boolean, validateMobilePrefix: 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.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;
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,7 +297,9 @@ 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
300
+ // Will do the best effort to normalize from/to/requestUri users as e164 values.
301
+ // Connect applies it per Trunk (spec.enforceE164), on the way in before routing
302
+ // and on the way out through the tailor.
297
303
  const enforceE164 = (enforceE164, validateMobilePrefix) => (request) => {
298
304
  var _a, _b, _c;
299
305
  if (!enforceE164)
@@ -324,3 +330,175 @@ const enforceE164 = (enforceE164, validateMobilePrefix) => (request) => {
324
330
  return req;
325
331
  };
326
332
  exports.enforceE164 = enforceE164;
333
+ /**
334
+ * Whether a From is the anonymous identity (RFC 3323, SIP Connect 1.1 section 9).
335
+ * A caller that asked for privacy keeps it, so such a From is never rewritten.
336
+ *
337
+ * @param {CommonTypes.SipURI} uri - the From URI
338
+ * @return {boolean} true for anonymous@anonymous.invalid and its variants
339
+ */
340
+ const isAnonymous = (uri) => {
341
+ var _a, _b;
342
+ return ((_a = uri.user) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === common_1.CommonTypes.ANONYMOUS ||
343
+ ((_b = uri.host) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === "anonymous.invalid";
344
+ };
345
+ exports.isAnonymous = isAnonymous;
346
+ /**
347
+ * Presents the Number as the caller toward a Trunk with rewriteFromWithNumber.
348
+ * Carriers that authenticate by the From header (WhatsApp Business Calling, many
349
+ * SIPconnect carriers) only accept a From that is the business number at their
350
+ * domain. The tag and display name stay, so the dialog is unchanged for Routr,
351
+ * which matches on tags and Call-ID. An anonymous From is left as it is.
352
+ *
353
+ * @param {Route} route - the route; does nothing unless route.rewriteFrom is set
354
+ * @return {Function} the alteration
355
+ */
356
+ const rewriteFromWithNumber = (route) => (request) => {
357
+ if (!route.rewriteFrom)
358
+ return request;
359
+ if ((0, exports.isAnonymous)(request.message.from.address.uri))
360
+ return request;
361
+ const req = common_1.Helper.deepCopy(request);
362
+ const uri = req.message.from.address.uri;
363
+ uri.user = route.rewriteFrom.user;
364
+ uri.host = route.rewriteFrom.host;
365
+ // The URI names a telephone number (RFC 3261 section 19.1.1), as the
366
+ // P-Asserted-Identity Connect adds toward the Trunk does
367
+ uri.userParam = "phone";
368
+ // The caller's own port has no meaning at the carrier's domain; -1 is how
369
+ // the EdgePort's converter spells "no port"
370
+ uri.port = -1;
371
+ return req;
372
+ };
373
+ exports.rewriteFromWithNumber = rewriteFromWithNumber;
374
+ // The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
375
+ // [o]riginal/[n]ew. Not "rto": a parser that looks for a header by searching
376
+ // for its name (SIPp does) finds "To" inside "rto=" on the Record-Route line.
377
+ const IDENTITY_PARAMETERS = {
378
+ fromOriginal: "rfo",
379
+ fromNew: "rfn",
380
+ toOriginal: "rco",
381
+ toNew: "rcn"
382
+ };
383
+ // Base64url keeps the value a SIP token (RFC 3261 section 25.1), so it needs no
384
+ // quoting in the header and survives every hop unchanged
385
+ const encodeIdentity = (uri) => {
386
+ var _a;
387
+ return Buffer.from(`${(_a = uri.user) !== null && _a !== void 0 ? _a : ""}@${uri.host}${uri.userParam ? `;user=${uri.userParam}` : ""}`).toString("base64url");
388
+ };
389
+ const decodeIdentity = (value) => {
390
+ if (!value)
391
+ return undefined;
392
+ const decoded = Buffer.from(value, "base64url").toString("utf8");
393
+ const [address, userParam] = decoded.split(";user=");
394
+ const at = address.lastIndexOf("@");
395
+ if (at < 0)
396
+ return undefined;
397
+ const user = address.slice(0, at);
398
+ return {
399
+ user: user || undefined,
400
+ host: address.slice(at + 1),
401
+ userParam: userParam || undefined
402
+ };
403
+ };
404
+ const sameIdentity = (uri, identity) => {
405
+ var _a, _b, _c;
406
+ return !!identity &&
407
+ ((_a = uri.user) !== null && _a !== void 0 ? _a : "") === ((_b = identity.user) !== null && _b !== void 0 ? _b : "") &&
408
+ ((_c = uri.host) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === identity.host.toLowerCase();
409
+ };
410
+ const changed = (a, b) => {
411
+ var _a, _b, _c, _d;
412
+ return ((_a = a.user) !== null && _a !== void 0 ? _a : "") !== ((_b = b.user) !== null && _b !== void 0 ? _b : "") ||
413
+ a.host !== b.host ||
414
+ ((_c = a.userParam) !== null && _c !== void 0 ? _c : "") !== ((_d = b.userParam) !== null && _d !== void 0 ? _d : "");
415
+ };
416
+ /**
417
+ * The Record-Route parameters that remember how the identity alterations changed
418
+ * a request's From and To. Both ends of the dialog copy Record-Route into the
419
+ * Route of their in-dialog requests, so the mapping comes back to Routr on every
420
+ * BYE, re-INVITE and ACK without any state in Routr (see swapIdentityFromRouteSet).
421
+ *
422
+ * @param {MessageRequest} original - the request before the identity alterations
423
+ * @param {MessageRequest} altered - the request after them
424
+ * @return {Record<string, string>} the parameters, empty when nothing changed
425
+ */
426
+ const identityRewriteParameters = (original, altered) => {
427
+ const parameters = {};
428
+ const fromBefore = original.message.from.address.uri;
429
+ const fromAfter = altered.message.from.address.uri;
430
+ const toBefore = original.message.to.address.uri;
431
+ const toAfter = altered.message.to.address.uri;
432
+ if (changed(fromBefore, fromAfter)) {
433
+ parameters[IDENTITY_PARAMETERS.fromOriginal] = encodeIdentity(fromBefore);
434
+ parameters[IDENTITY_PARAMETERS.fromNew] = encodeIdentity(fromAfter);
435
+ }
436
+ if (changed(toBefore, toAfter)) {
437
+ parameters[IDENTITY_PARAMETERS.toOriginal] = encodeIdentity(toBefore);
438
+ parameters[IDENTITY_PARAMETERS.toNew] = encodeIdentity(toAfter);
439
+ }
440
+ return parameters;
441
+ };
442
+ exports.identityRewriteParameters = identityRewriteParameters;
443
+ // Like isSelfRoute, but a Route on a transport this EdgePort lacks is simply
444
+ // not ours, instead of an error that would stop the in-dialog request
445
+ const isOwnRoute = (request, route) => {
446
+ try {
447
+ return isSelfRoute(request)(route);
448
+ }
449
+ catch (_a) {
450
+ return false;
451
+ }
452
+ };
453
+ const applyIdentity = (uri, identity) => {
454
+ uri.user = identity.user;
455
+ uri.host = identity.host;
456
+ uri.userParam = identity.userParam;
457
+ };
458
+ /**
459
+ * Keeps each side of a dialog seeing the identities it knows. A request from the
460
+ * caller gets the From and To the Trunk saw on the INVITE; a request from the
461
+ * Trunk, whose To is the Number, gets the caller's own identity back. Some phones
462
+ * answer 481 to an in-dialog request whose To or From URI changed.
463
+ *
464
+ * The mapping is read only from Route entries that point at this EdgePort: with
465
+ * two EdgePorts on the path, the one that rewrote the INVITE is the one that
466
+ * swaps, and the other leaves the request alone.
467
+ *
468
+ * @param {MessageRequest} request - an in-dialog request, with its Route set
469
+ * @return {MessageRequest} the request with From and To swapped where they match
470
+ */
471
+ const swapIdentityFromRouteSet = (request) => {
472
+ var _a;
473
+ const own = ((_a = request.message.route) !== null && _a !== void 0 ? _a : []).find((r) => {
474
+ var _a, _b;
475
+ return ((_a = r.parameters) === null || _a === void 0 ? void 0 : _a[IDENTITY_PARAMETERS.fromNew]) ||
476
+ ((_b = r.parameters) === null || _b === void 0 ? void 0 : _b[IDENTITY_PARAMETERS.toNew]);
477
+ });
478
+ if (!own || !isOwnRoute(request, own))
479
+ return request;
480
+ const fromOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromOriginal]);
481
+ const fromNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromNew]);
482
+ const toOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toOriginal]);
483
+ const toNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toNew]);
484
+ const req = common_1.Helper.deepCopy(request);
485
+ const from = req.message.from.address.uri;
486
+ const to = req.message.to.address.uri;
487
+ // From the caller: From is the caller, To is the callee
488
+ if (fromNew && sameIdentity(from, fromOriginal)) {
489
+ applyIdentity(from, fromNew);
490
+ }
491
+ else if (toOriginal && sameIdentity(from, toNew)) {
492
+ // From the Trunk: From is the callee as the Trunk knows it
493
+ applyIdentity(from, toOriginal);
494
+ }
495
+ if (toNew && sameIdentity(to, toOriginal)) {
496
+ applyIdentity(to, toNew);
497
+ }
498
+ else if (fromOriginal && sameIdentity(to, fromNew)) {
499
+ // From the Trunk: To is the Number the caller was presented as
500
+ applyIdentity(to, fromOriginal);
501
+ }
502
+ return req;
503
+ };
504
+ 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.23.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.23.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": "47b6950fc8ce49832903f4fc6c7b75d561e536d4"
40
40
  }