@routr/processor 2.31.3 → 2.31.5

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.
@@ -37,8 +37,8 @@ export declare const isAnonymous: (uri: CommonTypes.SipURI) => boolean;
37
37
  */
38
38
  export declare const rewriteFromWithNumber: (route: Route) => (request: MessageRequest) => MessageRequest;
39
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
40
+ * The identity mapping that remembers how the identity alterations changed a
41
+ * request's From and To; addSelfRecordRoute writes it in its Record-Route URI. Both ends of the dialog copy Record-Route into the
42
42
  * Route of their in-dialog requests, so the mapping comes back to Routr on every
43
43
  * BYE, re-INVITE and ACK without any state in Routr (see swapIdentityFromRouteSet).
44
44
  *
@@ -53,9 +53,10 @@ export declare const identityRewriteParameters: (original: MessageRequest, alter
53
53
  * Trunk, whose To is the Number, gets the caller's own identity back. Some phones
54
54
  * answer 481 to an in-dialog request whose To or From URI changed.
55
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.
56
+ * The mapping is read from the first Route entry that carries one, in its URI
57
+ * user part or else its header parameters, and only when that entry points at
58
+ * this EdgePort: with two EdgePorts on the path, the one that rewrote the INVITE
59
+ * is the one that swaps, and the other leaves the request alone.
59
60
  *
60
61
  * @param {MessageRequest} request - an in-dialog request, with its Route set
61
62
  * @return {MessageRequest} the request with From and To swapped where they match
@@ -22,7 +22,7 @@ exports.swapIdentityFromRouteSet = exports.identityRewriteParameters = exports.r
22
22
  const common_1 = require("@routr/common");
23
23
  const phone_1 = require("phone");
24
24
  const helper_1 = require("./helper");
25
- const { IDENTITY_PARAMETERS, encodeIdentity, decodeIdentity } = common_1.Identity;
25
+ const { IDENTITY_PARAMETERS, encodeIdentity, encodeIdentityUser, decodeIdentity, decodeIdentityUser, portOf } = common_1.Identity;
26
26
  // Forces the requestURI to be the same as the trunk uri.
27
27
  // This is a workaround for Twilio.
28
28
  const fixRequestURI = (route) => (request) => {
@@ -146,10 +146,13 @@ const applyXHeaders = (route) => (request) => {
146
146
  return req;
147
147
  };
148
148
  exports.applyXHeaders = applyXHeaders;
149
- // The parameters argument is merged into every Record-Route entry this adds;
150
- // identityRewriteParameters uses it to carry a rewritten From or To.
149
+ // The parameters argument is the identity mapping from identityRewriteParameters.
150
+ // It goes in the URI user part of every Record-Route entry this adds, which a
151
+ // user agent keeps (RFC 3261 section 12.1.2), so a rewritten From or To comes
152
+ // back on the dialog's later requests (see swapIdentityFromRouteSet).
151
153
  const addSelfRecordRoute = (route, parameters = {}) => (request) => {
152
154
  const req = common_1.Helper.deepCopy(request);
155
+ const user = encodeIdentityUser(parameters);
153
156
  const originIntf = (0, helper_1.getEdgeInterface)({
154
157
  listeningPoints: request.listeningPoints,
155
158
  localnets: request.localnets,
@@ -169,12 +172,7 @@ const addSelfRecordRoute = (route, parameters = {}) => (request) => {
169
172
  });
170
173
  const originRoute = {
171
174
  address: {
172
- uri: {
173
- host: originIntf.host,
174
- port: originIntf.port,
175
- transportParam: originIntf.transport,
176
- lrParam: true
177
- }
175
+ uri: Object.assign(Object.assign({}, (user ? { user } : {})), { host: originIntf.host, port: originIntf.port, transportParam: originIntf.transport, lrParam: true })
178
176
  }
179
177
  };
180
178
  if (originIntf.host !== targetIntf.host ||
@@ -182,17 +180,16 @@ const addSelfRecordRoute = (route, parameters = {}) => (request) => {
182
180
  originIntf.transport !== targetIntf.transport) {
183
181
  const targetRoute = {
184
182
  address: {
185
- uri: {
186
- host: targetIntf.host,
187
- port: targetIntf.port,
188
- transportParam: targetIntf.transport,
189
- lrParam: true
190
- }
183
+ uri: Object.assign(Object.assign({}, (user ? { user } : {})), { host: targetIntf.host, port: targetIntf.port, transportParam: targetIntf.transport, lrParam: true })
191
184
  },
192
- parameters: Object.assign({ r2: "on" }, parameters)
185
+ parameters: {
186
+ r2: "on"
187
+ }
193
188
  };
194
189
  // Add r2 on to origin route
195
- originRoute.parameters = Object.assign({ r2: "on" }, parameters);
190
+ originRoute.parameters = {
191
+ r2: "on"
192
+ };
196
193
  req.message.recordRoute = [
197
194
  targetRoute,
198
195
  originRoute,
@@ -200,9 +197,6 @@ const addSelfRecordRoute = (route, parameters = {}) => (request) => {
200
197
  ];
201
198
  }
202
199
  else {
203
- if (Object.keys(parameters).length > 0) {
204
- originRoute.parameters = Object.assign({}, parameters);
205
- }
206
200
  req.message.recordRoute = [originRoute, ...req.message.recordRoute];
207
201
  }
208
202
  return req;
@@ -386,11 +380,12 @@ const changed = (a, b) => {
386
380
  var _a, _b, _c, _d;
387
381
  return ((_a = a.user) !== null && _a !== void 0 ? _a : "") !== ((_b = b.user) !== null && _b !== void 0 ? _b : "") ||
388
382
  a.host !== b.host ||
383
+ portOf(a) !== portOf(b) ||
389
384
  ((_c = a.userParam) !== null && _c !== void 0 ? _c : "") !== ((_d = b.userParam) !== null && _d !== void 0 ? _d : "");
390
385
  };
391
386
  /**
392
- * The Record-Route parameters that remember how the identity alterations changed
393
- * a request's From and To. Both ends of the dialog copy Record-Route into the
387
+ * The identity mapping that remembers how the identity alterations changed a
388
+ * request's From and To; addSelfRecordRoute writes it in its Record-Route URI. Both ends of the dialog copy Record-Route into the
394
389
  * Route of their in-dialog requests, so the mapping comes back to Routr on every
395
390
  * BYE, re-INVITE and ACK without any state in Routr (see swapIdentityFromRouteSet).
396
391
  *
@@ -425,10 +420,28 @@ const isOwnRoute = (request, route) => {
425
420
  return false;
426
421
  }
427
422
  };
428
- const applyIdentity = (uri, identity) => {
423
+ // The mapping a Route entry carries: in its URI user part, which a user agent
424
+ // keeps (RFC 3261 section 12.1.2), else in its header parameters, which it may
425
+ // drop. Only the user part carries the port, so only it is applied exactly.
426
+ const identityMapping = (route) => {
427
+ var _a, _b;
428
+ const hasMapping = (values) => !!(values === null || values === void 0 ? void 0 : values[IDENTITY_PARAMETERS.fromNew]) ||
429
+ !!(values === null || values === void 0 ? void 0 : values[IDENTITY_PARAMETERS.toNew]);
430
+ const inUser = decodeIdentityUser((_b = (_a = route.address) === null || _a === void 0 ? void 0 : _a.uri) === null || _b === void 0 ? void 0 : _b.user);
431
+ if (hasMapping(inUser))
432
+ return { values: inUser, exact: true };
433
+ if (hasMapping(route.parameters)) {
434
+ return { values: route.parameters, exact: false };
435
+ }
436
+ return undefined;
437
+ };
438
+ const applyIdentity = (uri, identity, exact) => {
439
+ var _a;
429
440
  uri.user = identity.user;
430
441
  uri.host = identity.host;
431
442
  uri.userParam = identity.userParam;
443
+ if (exact)
444
+ uri.port = (_a = identity.port) !== null && _a !== void 0 ? _a : -1;
432
445
  };
433
446
  /**
434
447
  * Keeps each side of a dialog seeing the identities it knows. A request from the
@@ -436,43 +449,41 @@ const applyIdentity = (uri, identity) => {
436
449
  * Trunk, whose To is the Number, gets the caller's own identity back. Some phones
437
450
  * answer 481 to an in-dialog request whose To or From URI changed.
438
451
  *
439
- * The mapping is read only from Route entries that point at this EdgePort: with
440
- * two EdgePorts on the path, the one that rewrote the INVITE is the one that
441
- * swaps, and the other leaves the request alone.
452
+ * The mapping is read from the first Route entry that carries one, in its URI
453
+ * user part or else its header parameters, and only when that entry points at
454
+ * this EdgePort: with two EdgePorts on the path, the one that rewrote the INVITE
455
+ * is the one that swaps, and the other leaves the request alone.
442
456
  *
443
457
  * @param {MessageRequest} request - an in-dialog request, with its Route set
444
458
  * @return {MessageRequest} the request with From and To swapped where they match
445
459
  */
446
460
  const swapIdentityFromRouteSet = (request) => {
447
461
  var _a;
448
- const own = ((_a = request.message.route) !== null && _a !== void 0 ? _a : []).find((r) => {
449
- var _a, _b;
450
- return ((_a = r.parameters) === null || _a === void 0 ? void 0 : _a[IDENTITY_PARAMETERS.fromNew]) ||
451
- ((_b = r.parameters) === null || _b === void 0 ? void 0 : _b[IDENTITY_PARAMETERS.toNew]);
452
- });
462
+ const own = ((_a = request.message.route) !== null && _a !== void 0 ? _a : []).find((r) => identityMapping(r));
453
463
  if (!own || !isOwnRoute(request, own))
454
464
  return request;
455
- const fromOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromOriginal]);
456
- const fromNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.fromNew]);
457
- const toOriginal = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toOriginal]);
458
- const toNew = decodeIdentity(own.parameters[IDENTITY_PARAMETERS.toNew]);
465
+ const { values, exact } = identityMapping(own);
466
+ const fromOriginal = decodeIdentity(values[IDENTITY_PARAMETERS.fromOriginal]);
467
+ const fromNew = decodeIdentity(values[IDENTITY_PARAMETERS.fromNew]);
468
+ const toOriginal = decodeIdentity(values[IDENTITY_PARAMETERS.toOriginal]);
469
+ const toNew = decodeIdentity(values[IDENTITY_PARAMETERS.toNew]);
459
470
  const req = common_1.Helper.deepCopy(request);
460
471
  const from = req.message.from.address.uri;
461
472
  const to = req.message.to.address.uri;
462
473
  // From the caller: From is the caller, To is the callee
463
474
  if (fromNew && sameIdentity(from, fromOriginal)) {
464
- applyIdentity(from, fromNew);
475
+ applyIdentity(from, fromNew, exact);
465
476
  }
466
477
  else if (toOriginal && sameIdentity(from, toNew)) {
467
478
  // From the Trunk: From is the callee as the Trunk knows it
468
- applyIdentity(from, toOriginal);
479
+ applyIdentity(from, toOriginal, exact);
469
480
  }
470
481
  if (toNew && sameIdentity(to, toOriginal)) {
471
- applyIdentity(to, toNew);
482
+ applyIdentity(to, toNew, exact);
472
483
  }
473
484
  else if (fromOriginal && sameIdentity(to, fromNew)) {
474
485
  // From the Trunk: To is the Number the caller was presented as
475
- applyIdentity(to, fromOriginal);
486
+ applyIdentity(to, fromOriginal, exact);
476
487
  }
477
488
  return req;
478
489
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/processor",
3
- "version": "2.31.3",
3
+ "version": "2.31.5",
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.31.3",
18
+ "@routr/common": "^2.31.5",
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": "7178a8d1ad402c3a00b31b55c8b63ecd2527c975"
39
+ "gitHead": "c0b54abe054aee1c28d6bc5c3402ba4bedba31a9"
40
40
  }