@abtnode/router-provider 1.16.33-beta-20241024-064549-2c1ad302 → 1.16.33-beta-20241028-164124-17cf3c21
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.
- package/lib/default/daemon.js +1 -46
- package/lib/default/proxy.js +0 -10
- package/lib/nginx/includes/proxy +0 -1
- package/lib/nginx/index.js +3 -53
- package/lib/nginx/util.js +0 -1
- package/package.json +6 -6
- package/lib/nginx/includes/cors-loose +0 -8
- package/lib/nginx/includes/cors-strict +0 -6
- package/lib/nginx/includes/security +0 -3
package/lib/default/daemon.js
CHANGED
|
@@ -162,50 +162,6 @@ const onError = (err, req, res) => {
|
|
|
162
162
|
}
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
-
const corsHandler = (host, req, res) => {
|
|
166
|
-
if (req.method === 'OPTIONS') {
|
|
167
|
-
const domain = toSlotDomain(host);
|
|
168
|
-
const site = config.sites.find((x) => x.domain === domain);
|
|
169
|
-
if (!site) {
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const allowedOrigins = site.corsAllowedOrigins;
|
|
174
|
-
const currentOrigin = req.headers.origin;
|
|
175
|
-
if (allowedOrigins.includes('*')) {
|
|
176
|
-
res.writeHead(204, {
|
|
177
|
-
Vary: 'Origin',
|
|
178
|
-
// TODO: @zhanghan 需要优先读取应用代码透传的 header
|
|
179
|
-
'Access-Control-Allow-Origin': '*',
|
|
180
|
-
'Access-Control-Allow-Credentials': false,
|
|
181
|
-
'Access-Control-Allow-Methods': 'POST, GET, HEAD, PUT, DELETE, OPTIONS',
|
|
182
|
-
'Access-Control-Allow-Headers':
|
|
183
|
-
'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,$http_access_control_request_headers',
|
|
184
|
-
'Access-Control-Max-Age': 1800,
|
|
185
|
-
});
|
|
186
|
-
res.end();
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (allowedOrigins.some((x) => checkDomainMatch(x, currentOrigin))) {
|
|
191
|
-
res.writeHead(204, {
|
|
192
|
-
Vary: 'Origin',
|
|
193
|
-
// TODO: @zhanghan 需要优先读取应用代码透传的 header
|
|
194
|
-
'Access-Control-Allow-Origin': currentOrigin,
|
|
195
|
-
'Access-Control-Allow-Credentials': false,
|
|
196
|
-
'Access-Control-Allow-Methods': 'POST, GET, HEAD, PUT, DELETE, OPTIONS',
|
|
197
|
-
'Access-Control-Allow-Headers':
|
|
198
|
-
'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,$http_access_control_request_headers',
|
|
199
|
-
'Access-Control-Max-Age': 1800,
|
|
200
|
-
});
|
|
201
|
-
res.end();
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return true;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
165
|
// internal servers
|
|
210
166
|
const internalServers = {};
|
|
211
167
|
const ensureInternalServer = (port) => {
|
|
@@ -213,7 +169,7 @@ const ensureInternalServer = (port) => {
|
|
|
213
169
|
return;
|
|
214
170
|
}
|
|
215
171
|
|
|
216
|
-
const server = new ProxyServer({ xfwd: false, internal: true, port, headers: config.headers, onError
|
|
172
|
+
const server = new ProxyServer({ xfwd: false, internal: true, port, headers: config.headers, onError });
|
|
217
173
|
server.addResolver(sharedResolver);
|
|
218
174
|
internalServers[port] = server;
|
|
219
175
|
logger.info('internal server ready on port', { port });
|
|
@@ -295,7 +251,6 @@ const defaultCert = config.certs.find((x) => x.domain.endsWith(DEFAULT_IP_DOMAIN
|
|
|
295
251
|
const main = new ProxyServer({
|
|
296
252
|
xfwd: true,
|
|
297
253
|
onError,
|
|
298
|
-
corsHandler,
|
|
299
254
|
port: httpPort,
|
|
300
255
|
headers: config.headers,
|
|
301
256
|
ssl: {
|
package/lib/default/proxy.js
CHANGED
|
@@ -59,9 +59,6 @@ module.exports = class ReverseProxy {
|
|
|
59
59
|
if (!this.opts.onError) {
|
|
60
60
|
this.opts.onError = (err) => console.error(err);
|
|
61
61
|
}
|
|
62
|
-
if (!this.opts.corsHandler) {
|
|
63
|
-
this.opts.corsHandler = () => true;
|
|
64
|
-
}
|
|
65
62
|
|
|
66
63
|
const websocketUpgrade = (req, socket, head) => {
|
|
67
64
|
socket.on('error', (err) => logger.error('WebSockets error', { error: err }));
|
|
@@ -105,7 +102,6 @@ module.exports = class ReverseProxy {
|
|
|
105
102
|
|
|
106
103
|
// @link: https://github.com/http-party/node-http-proxy/issues/1401
|
|
107
104
|
this.proxy.on('proxyRes', (proxyRes) => {
|
|
108
|
-
delete proxyRes.headers['x-powered-by'];
|
|
109
105
|
this.opts.headers.forEach((x) => {
|
|
110
106
|
proxyRes.headers[x.key] = x.value;
|
|
111
107
|
});
|
|
@@ -133,9 +129,6 @@ module.exports = class ReverseProxy {
|
|
|
133
129
|
setupHttpProxy(proxy, websocketUpgrade, opts) {
|
|
134
130
|
const server = http.createServer((req, res) => {
|
|
135
131
|
const src = this._getSource(req);
|
|
136
|
-
if (this.opts.corsHandler(req, res) === false) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
132
|
|
|
140
133
|
this._getTarget(src, req, res).then((target) => {
|
|
141
134
|
if (target) {
|
|
@@ -198,9 +191,6 @@ module.exports = class ReverseProxy {
|
|
|
198
191
|
|
|
199
192
|
this.httpsServer = https.createServer(ssl, (req, res) => {
|
|
200
193
|
const src = this._getSource(req);
|
|
201
|
-
if (this.opts.corsHandler(req, res) === false) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
194
|
|
|
205
195
|
const httpProxyOpts = Object.assign({}, this.opts.httpProxy);
|
|
206
196
|
this._getTarget(src, req, res).then((target) => {
|
package/lib/nginx/includes/proxy
CHANGED
package/lib/nginx/index.js
CHANGED
|
@@ -12,7 +12,6 @@ const camelCase = require('lodash/camelCase');
|
|
|
12
12
|
const isEmpty = require('lodash/isEmpty');
|
|
13
13
|
const formatBackSlash = require('@abtnode/util/lib/format-back-slash');
|
|
14
14
|
const {
|
|
15
|
-
DOMAIN_FOR_DEFAULT_SITE,
|
|
16
15
|
ROUTING_RULE_TYPES,
|
|
17
16
|
CONFIG_FOLDER_NAME,
|
|
18
17
|
SLOT_FOR_IP_DNS_SITE,
|
|
@@ -21,7 +20,6 @@ const {
|
|
|
21
20
|
LOG_RETAIN_IN_DAYS,
|
|
22
21
|
ROUTER_CACHE_GROUPS,
|
|
23
22
|
} = require('@abtnode/constant');
|
|
24
|
-
const md5 = require('@abtnode/util/lib/md5');
|
|
25
23
|
|
|
26
24
|
const promiseRetry = require('promise-retry');
|
|
27
25
|
|
|
@@ -186,9 +184,8 @@ class NginxProvider extends BaseProvider {
|
|
|
186
184
|
conf.on('flushed', () => resolve());
|
|
187
185
|
conf.live(this.configPath);
|
|
188
186
|
|
|
189
|
-
const { sites, cacheGroups
|
|
187
|
+
const { sites, cacheGroups } = formatRoutingTable(routingTable);
|
|
190
188
|
|
|
191
|
-
this._addCorsMap(conf, siteCorsConfigs);
|
|
192
189
|
if (this.cacheEnabled) {
|
|
193
190
|
this._addCacheGroups(conf, cacheGroups);
|
|
194
191
|
}
|
|
@@ -477,7 +474,6 @@ class NginxProvider extends BaseProvider {
|
|
|
477
474
|
suffix,
|
|
478
475
|
did,
|
|
479
476
|
componentId,
|
|
480
|
-
corsAllowedOrigins,
|
|
481
477
|
target,
|
|
482
478
|
targetPrefix, // used to strip prefix from target
|
|
483
479
|
ruleId,
|
|
@@ -490,9 +486,6 @@ class NginxProvider extends BaseProvider {
|
|
|
490
486
|
|
|
491
487
|
const location = this._getLastLocation(server);
|
|
492
488
|
|
|
493
|
-
// Note: 下面这段代码比较 tricky,不要在这段代码之前添加任何 add_header, proxy_set_header, proxy_hide_header 的语句,否则 nginx 配置可能无法按预期工作
|
|
494
|
-
this._addCors({ location, corsAllowedOrigins });
|
|
495
|
-
|
|
496
489
|
this._addCommonResHeaders(location, commonHeaders);
|
|
497
490
|
if (!cacheGroup && !suffix) {
|
|
498
491
|
this._addTailSlashRedirection(location, prefix); // Note: 末尾 "/" 的重定向要放在 CORS(OPTIONS) 响应之后, 这样不会影响 OPTIONS 的响应
|
|
@@ -559,13 +552,11 @@ class NginxProvider extends BaseProvider {
|
|
|
559
552
|
location._add('proxy_pass', `http://${getUpstreamName(port)}`);
|
|
560
553
|
}
|
|
561
554
|
|
|
562
|
-
_addRedirectTypeLocation({ server, url, redirectCode, prefix, suffix
|
|
555
|
+
_addRedirectTypeLocation({ server, url, redirectCode, prefix, suffix }) {
|
|
563
556
|
const cleanUrl = trimEndSlash(url);
|
|
564
557
|
server._add('location', `${concatPath(prefix, suffix)}`);
|
|
565
558
|
const location = this._getLastLocation(server);
|
|
566
559
|
|
|
567
|
-
this._addCors({ location, corsAllowedOrigins });
|
|
568
|
-
|
|
569
560
|
location._add('set $abt_query_string', '""');
|
|
570
561
|
location._addVerbatimBlock('if ($query_string)', 'set $abt_query_string "?$query_string";');
|
|
571
562
|
|
|
@@ -582,10 +573,9 @@ class NginxProvider extends BaseProvider {
|
|
|
582
573
|
}
|
|
583
574
|
}
|
|
584
575
|
|
|
585
|
-
_addRewriteTypeLocation({ server, url, prefix, suffix
|
|
576
|
+
_addRewriteTypeLocation({ server, url, prefix, suffix }) {
|
|
586
577
|
server._add('location', concatPath(prefix, suffix));
|
|
587
578
|
const location = this._getLastLocation(server);
|
|
588
|
-
this._addCors({ location, corsAllowedOrigins });
|
|
589
579
|
location._add('rewrite', `^${prefix}(.*) ${url}$1 last`);
|
|
590
580
|
}
|
|
591
581
|
|
|
@@ -896,46 +886,6 @@ class NginxProvider extends BaseProvider {
|
|
|
896
886
|
});
|
|
897
887
|
}
|
|
898
888
|
|
|
899
|
-
_addCorsMap(conf, siteCorsConfigs) {
|
|
900
|
-
siteCorsConfigs.forEach((corsConfig) => {
|
|
901
|
-
if (Array.isArray(corsConfig.corsAllowedOrigins) && corsConfig.corsAllowedOrigins.length > 0) {
|
|
902
|
-
const allowedOrigins = corsConfig.corsAllowedOrigins.map((x) => {
|
|
903
|
-
const y = parseServerName(x);
|
|
904
|
-
if (y.startsWith('~')) {
|
|
905
|
-
return `${y} $http_origin;`;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
return `~${y} $http_origin;`;
|
|
909
|
-
});
|
|
910
|
-
|
|
911
|
-
allowedOrigins.push('default "";');
|
|
912
|
-
conf.nginx.http._addVerbatimBlock(
|
|
913
|
-
`map $http_origin $allow_origin_${md5(parseServerName(corsConfig.domain))}`,
|
|
914
|
-
allowedOrigins.join(' ')
|
|
915
|
-
);
|
|
916
|
-
}
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
_addCors({ location, corsAllowedOrigins }) {
|
|
921
|
-
if (!isEmpty(corsAllowedOrigins)) {
|
|
922
|
-
if (corsAllowedOrigins.includes(DOMAIN_FOR_DEFAULT_SITE)) {
|
|
923
|
-
location._add('include', 'includes/cors-loose');
|
|
924
|
-
location._add('include', 'includes/security');
|
|
925
|
-
} else {
|
|
926
|
-
// TODO: @zhanghan 此处是否需要变更默认的值,如果在逻辑代码中有指定的 Access-Control-Allow-Origin,则应该优先遵守逻辑代码中的配置
|
|
927
|
-
// location._add('add_header', `Access-Control-Allow-Origin $allow_origin_${md5(serverName)} always`); // TODO: zhenqiang 下面去掉了 OPTIONS 请求逻辑,这里也不应该处理 Access-Control-Allow-Origin
|
|
928
|
-
location._add('include', 'includes/cors-strict');
|
|
929
|
-
location._add('include', 'includes/security');
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
// TODO: @zhanghan 统一登录需要自行处理 options 请求的逻辑
|
|
933
|
-
// location._addVerbatimBlock('if ($request_method = "OPTIONS")', 'return 204;');
|
|
934
|
-
} else {
|
|
935
|
-
location._add('include', 'includes/security');
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
889
|
addGlobalReqLimit(block, limit) {
|
|
940
890
|
const key = limit.ipHeader ? `$http_${limit.ipHeader}` : '$binary_remote_addr';
|
|
941
891
|
block._add('limit_req_zone', `${key} zone=ip_limit:20m rate=${limit.rate || 5}r/s`);
|
package/lib/nginx/util.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abtnode/router-provider",
|
|
3
|
-
"version": "1.16.33-beta-
|
|
3
|
+
"version": "1.16.33-beta-20241028-164124-17cf3c21",
|
|
4
4
|
"description": "Routing engine implementations for abt node",
|
|
5
5
|
"author": "polunzh <polunzh@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/ArcBlock/blocklet-server#readme",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"url": "https://github.com/ArcBlock/blocklet-server/issues"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@abtnode/constant": "1.16.33-beta-
|
|
36
|
-
"@abtnode/logger": "1.16.33-beta-
|
|
37
|
-
"@abtnode/router-templates": "1.16.33-beta-
|
|
38
|
-
"@abtnode/util": "1.16.33-beta-
|
|
35
|
+
"@abtnode/constant": "1.16.33-beta-20241028-164124-17cf3c21",
|
|
36
|
+
"@abtnode/logger": "1.16.33-beta-20241028-164124-17cf3c21",
|
|
37
|
+
"@abtnode/router-templates": "1.16.33-beta-20241028-164124-17cf3c21",
|
|
38
|
+
"@abtnode/util": "1.16.33-beta-20241028-164124-17cf3c21",
|
|
39
39
|
"@arcblock/http-proxy": "^1.19.1",
|
|
40
40
|
"@arcblock/is-valid-domain": "^1.0.5",
|
|
41
41
|
"axios": "^1.7.5",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"bluebird": "^3.7.2",
|
|
60
60
|
"fs-extra": "^11.2.0"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "df5cccba1192375274247a8770fe07f9dc248994"
|
|
63
63
|
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
# TODO: 需要优先读取应用代码透传的 header
|
|
2
|
-
# add_header Access-Control-Allow-Credentials false always;
|
|
3
|
-
add_header Vary Origin always;
|
|
4
|
-
add_header Access-Control-Allow-Methods "POST, GET, HEAD, PUT, DELETE, OPTIONS" always;
|
|
5
|
-
add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,$http_access_control_request_headers";
|
|
6
|
-
# TODO: 需要优先读取应用代码透传的 header
|
|
7
|
-
# add_header Access-Control-Allow-Origin * always;
|
|
8
|
-
add_header Access-Control-Max-Age 1800;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
# TODO: 需要优先读取应用代码透传的 header
|
|
2
|
-
# add_header Access-Control-Allow-Credentials false always;
|
|
3
|
-
add_header Vary Origin always;
|
|
4
|
-
add_header Access-Control-Allow-Methods "POST, GET, HEAD, PUT, DELETE, OPTIONS" always;
|
|
5
|
-
add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,$http_access_control_request_headers";
|
|
6
|
-
add_header Access-Control-Max-Age 1800;
|