@abtnode/router-provider 1.4.12 → 1.5.1

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.
Files changed (2) hide show
  1. package/lib/nginx/index.js +29 -7
  2. package/package.json +5 -5
@@ -16,6 +16,7 @@ const {
16
16
  ROUTING_RULE_TYPES,
17
17
  CONFIG_FOLDER_NAME,
18
18
  DEFAULT_ADMIN_PATH,
19
+ SLOT_FOR_IP_DNS_SITE,
19
20
  } = require('@abtnode/constant');
20
21
  const md5 = require('@abtnode/util/lib/md5');
21
22
 
@@ -48,6 +49,23 @@ const REQUIRED_MODULES = [{ configName: 'with-stream', moduleBinaryName: 'ngx_st
48
49
  // TODO: move the did-auth-path-prefix to a constant
49
50
  const DID_AUTH_PATH_PREFIX = `${DEFAULT_ADMIN_PATH}/api/did`;
50
51
 
52
+ // convert wildcard domain and ipDnsDomain template to regex
53
+ const parseServerName = (serverName) => {
54
+ // ipDnsDomain template
55
+ if (serverName.includes(SLOT_FOR_IP_DNS_SITE)) {
56
+ const name = serverName.replace(/\./g, '\\.').replace(SLOT_FOR_IP_DNS_SITE, '\\d+-\\d+-\\d+-\\d+');
57
+ return `~^${name}$`;
58
+ }
59
+
60
+ // wildcard domain
61
+ if (serverName.startsWith('*.')) {
62
+ const name = serverName.replace('*', '').replace(/\./g, '\\.');
63
+ return `~.+${name}$`;
64
+ }
65
+
66
+ return serverName;
67
+ };
68
+
51
69
  class NginxProvider extends BaseProvider {
52
70
  /**
53
71
  * @param {string} configDirectory
@@ -204,6 +222,7 @@ class NginxProvider extends BaseProvider {
204
222
  const certificate = findCertificate(certificates, serverName);
205
223
  const isHttps = !!certificate;
206
224
 
225
+ const parsedServerName = parseServerName(serverName);
207
226
  const sortedLocations = this._sortLocations(locations);
208
227
  if (isHttps) {
209
228
  // HTTPS configurations
@@ -220,7 +239,7 @@ class NginxProvider extends BaseProvider {
220
239
  conf,
221
240
  locations: sortedLocations,
222
241
  certificateFileName: certificate.domain,
223
- serverName,
242
+ serverName: parsedServerName,
224
243
  corsAllowedOrigins,
225
244
  daemonPort: nodeInfo.port,
226
245
  });
@@ -228,7 +247,7 @@ class NginxProvider extends BaseProvider {
228
247
  this._addHttpServer({
229
248
  conf,
230
249
  locations: sortedLocations,
231
- serverName,
250
+ serverName: parsedServerName,
232
251
  corsAllowedOrigins,
233
252
  port,
234
253
  daemonPort: nodeInfo.port,
@@ -540,7 +559,9 @@ class NginxProvider extends BaseProvider {
540
559
 
541
560
  _addCommonHeader(location) {
542
561
  location._add('set', '$abt_proto $scheme');
543
- location._add('set', '$abt_host $host');
562
+ // use $http_host to keep port when redirecting
563
+ // https://stackoverflow.com/questions/43397365/nginx-keep-port-number-when-301-redirecting
564
+ location._add('set', '$abt_host $http_host');
544
565
  location._add('set', '$abt_query_string ""');
545
566
  location._addVerbatimBlock('if ($http_x_forwarded_proto)', 'set $abt_proto $http_x_forwarded_proto;');
546
567
  location._addVerbatimBlock('if ($http_x_forwarded_host)', 'set $abt_host $http_x_forwarded_host;');
@@ -758,16 +779,17 @@ class NginxProvider extends BaseProvider {
758
779
  siteCorsConfigs.forEach((corsConfig) => {
759
780
  if (Array.isArray(corsConfig.corsAllowedOrigins) && corsConfig.corsAllowedOrigins.length > 0) {
760
781
  const allowedOrigins = corsConfig.corsAllowedOrigins.map((x) => {
761
- if (x.startsWith('~')) {
762
- return `${x} $http_origin;`;
782
+ const y = parseServerName(x);
783
+ if (y.startsWith('~')) {
784
+ return `${y} $http_origin;`;
763
785
  }
764
786
 
765
- return `~${x} $http_origin;`;
787
+ return `~${y} $http_origin;`;
766
788
  });
767
789
 
768
790
  allowedOrigins.push('default "";');
769
791
  conf.nginx.http._addVerbatimBlock(
770
- `map $http_origin $allow_origin_${md5(corsConfig.serverName)}`,
792
+ `map $http_origin $allow_origin_${md5(parseServerName(corsConfig.serverName))}`,
771
793
  allowedOrigins.join(' ')
772
794
  );
773
795
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/router-provider",
3
- "version": "1.4.12",
3
+ "version": "1.5.1",
4
4
  "description": "Routing engine implementations for abt node",
5
5
  "author": "polunzh <polunzh@gmail.com>",
6
6
  "homepage": "https://github.com/ArcBlock/abt-node#readme",
@@ -32,9 +32,9 @@
32
32
  "url": "https://github.com/ArcBlock/abt-node/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@abtnode/constant": "1.4.12",
36
- "@abtnode/logger": "1.4.12",
37
- "@abtnode/util": "1.4.12",
35
+ "@abtnode/constant": "1.5.1",
36
+ "@abtnode/logger": "1.5.1",
37
+ "@abtnode/util": "1.5.1",
38
38
  "axios": "^0.21.4",
39
39
  "debug": "^4.3.2",
40
40
  "find-process": "^1.4.3",
@@ -51,5 +51,5 @@
51
51
  "devDependencies": {
52
52
  "fs-extra": "^10.0.0"
53
53
  },
54
- "gitHead": "677dcef3be9130feeca3334d475202a5e11a7323"
54
+ "gitHead": "8219f2128aaa199a52c100e6c90ea4a90d15ad15"
55
55
  }