@ohos-ports/core 1.0.113-beta.0 → 1.16.33-beta.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.
- package/LICENSE +13 -0
- package/README.md +3 -0
- package/lib/api/node.js +176 -0
- package/lib/api/team.js +1889 -0
- package/lib/blocklet/downloader/blocklet-downloader.js +246 -0
- package/lib/blocklet/downloader/bundle-downloader.js +316 -0
- package/lib/blocklet/downloader/constants.js +3 -0
- package/lib/blocklet/downloader/resolve-download.js +187 -0
- package/lib/blocklet/extras.js +177 -0
- package/lib/blocklet/hooks.js +89 -0
- package/lib/blocklet/manager/base.js +49 -0
- package/lib/blocklet/manager/config-synchronizer.js +128 -0
- package/lib/blocklet/manager/disk.js +5170 -0
- package/lib/blocklet/manager/engine.js +124 -0
- package/lib/blocklet/manager/helper/install-application-from-backup.js +285 -0
- package/lib/blocklet/manager/helper/install-application-from-dev.js +72 -0
- package/lib/blocklet/manager/helper/install-application-from-general.js +209 -0
- package/lib/blocklet/manager/helper/install-component-from-dev.js +128 -0
- package/lib/blocklet/manager/helper/install-component-from-upload.js +197 -0
- package/lib/blocklet/manager/helper/install-component-from-url.js +214 -0
- package/lib/blocklet/manager/helper/migrate-application-to-struct-v2.js +557 -0
- package/lib/blocklet/manager/helper/rollback-cache.js +41 -0
- package/lib/blocklet/manager/helper/upgrade-components.js +179 -0
- package/lib/blocklet/manager/pm2-events.js +50 -0
- package/lib/blocklet/migration-dist/migration.cjs +38605 -0
- package/lib/blocklet/migration-dist/run-script.cjs +29 -0
- package/lib/blocklet/migration.js +145 -0
- package/lib/blocklet/project/connect-by-studio.js +91 -0
- package/lib/blocklet/project/connect-to-store.js +64 -0
- package/lib/blocklet/project/create-pack-release.js +293 -0
- package/lib/blocklet/project/disconnect-from-store.js +13 -0
- package/lib/blocklet/project/get-is-multiple-tenant.js +8 -0
- package/lib/blocklet/project/index.js +545 -0
- package/lib/blocklet/project/logo.png +0 -0
- package/lib/blocklet/project/publish-to-store.js +108 -0
- package/lib/blocklet/project/util.js +194 -0
- package/lib/blocklet/security/access-policy.js +131 -0
- package/lib/blocklet/security/index.js +309 -0
- package/lib/blocklet/security/response-header-policy.js +178 -0
- package/lib/blocklet/security/security-rule.js +127 -0
- package/lib/blocklet/security/utils.js +35 -0
- package/lib/blocklet/security/validator.js +40 -0
- package/lib/blocklet/storage/backup/base.js +149 -0
- package/lib/blocklet/storage/backup/blocklet-extras.js +110 -0
- package/lib/blocklet/storage/backup/blocklet.js +159 -0
- package/lib/blocklet/storage/backup/blocklets.js +109 -0
- package/lib/blocklet/storage/backup/data.js +35 -0
- package/lib/blocklet/storage/backup/disk.js +131 -0
- package/lib/blocklet/storage/backup/logs.js +24 -0
- package/lib/blocklet/storage/backup/routing-rule.js +47 -0
- package/lib/blocklet/storage/backup/spaces.js +345 -0
- package/lib/blocklet/storage/restore/base.js +74 -0
- package/lib/blocklet/storage/restore/blocklet-extras.js +89 -0
- package/lib/blocklet/storage/restore/blocklet.js +56 -0
- package/lib/blocklet/storage/restore/blocklets.js +43 -0
- package/lib/blocklet/storage/restore/disk.js +104 -0
- package/lib/blocklet/storage/restore/logs.js +21 -0
- package/lib/blocklet/storage/restore/spaces.js +174 -0
- package/lib/blocklet/storage/utils/disk.js +178 -0
- package/lib/blocklet/storage/utils/hash.js +53 -0
- package/lib/blocklet/storage/utils/zip.js +43 -0
- package/lib/cert.js +221 -0
- package/lib/crons/check-new-version.js +68 -0
- package/lib/crons/monitor-disk-usage.js +46 -0
- package/lib/crons/rotate-pm2-logs/index.js +41 -0
- package/lib/crons/rotate-pm2-logs/script.js +56 -0
- package/lib/event/auto-backup-handler.js +48 -0
- package/lib/event/index.js +520 -0
- package/lib/index.js +768 -364
- package/lib/locales/ar.js +18 -0
- package/lib/locales/de.js +21 -0
- package/lib/locales/en.js +19 -0
- package/lib/locales/es.js +21 -0
- package/lib/locales/fr.js +21 -0
- package/lib/locales/hi.js +18 -0
- package/lib/locales/i18n.db +0 -0
- package/lib/locales/id.js +20 -0
- package/lib/locales/index.js +54 -0
- package/lib/locales/ja.js +19 -0
- package/lib/locales/ko.js +18 -0
- package/lib/locales/pt.js +20 -0
- package/lib/locales/ru.js +21 -0
- package/lib/locales/th.js +18 -0
- package/lib/locales/vi.js +18 -0
- package/lib/locales/zh-tw.js +17 -0
- package/lib/locales/zh.js +17 -0
- package/lib/migrations/1.0.21-update-config.js +34 -0
- package/lib/migrations/1.0.22-max-memory.js +40 -0
- package/lib/migrations/1.0.25.js +36 -0
- package/lib/migrations/1.0.32-update-config.js +30 -0
- package/lib/migrations/1.0.33-blocklets.js +33 -0
- package/lib/migrations/1.0.36-snapshot.js +10 -0
- package/lib/migrations/1.1.10-team.js +26 -0
- package/lib/migrations/1.1.9-snapshot.js +7 -0
- package/lib/migrations/1.16.11-component-status.js +22 -0
- package/lib/migrations/1.16.33-blocklet-security-rule.js +17 -0
- package/lib/migrations/1.16.6-chain-info.js +78 -0
- package/lib/migrations/1.16.8-component-title.js +66 -0
- package/lib/migrations/1.2.11-blocklet-registry.js +10 -0
- package/lib/migrations/1.2.2-blocklet-registry.js +10 -0
- package/lib/migrations/1.3.1-disk-alert-threshold.js +10 -0
- package/lib/migrations/1.4.5-access-key.js +18 -0
- package/lib/migrations/1.5.0-site.js +34 -0
- package/lib/migrations/1.5.15-site.js +184 -0
- package/lib/migrations/1.5.20-registry.js +15 -0
- package/lib/migrations/1.6.17-blocklet-children.js +48 -0
- package/lib/migrations/1.6.21-rename-ip-echo-domain.js +35 -0
- package/lib/migrations/1.6.4-security.js +59 -0
- package/lib/migrations/1.6.5-security.js +60 -0
- package/lib/migrations/1.6.9-update-node-info-and-certificate.js +39 -0
- package/lib/migrations/1.7.1-blocklet-setup.js +18 -0
- package/lib/migrations/1.7.12-blocklet-meta.js +51 -0
- package/lib/migrations/1.7.15-blocklet-bundle-source.js +42 -0
- package/lib/migrations/1.7.20-blocklet-component.js +41 -0
- package/lib/migrations/1.8.33-blocklet-mem-limit.js +20 -0
- package/lib/migrations/README.md +71 -0
- package/lib/migrations/index.js +215 -0
- package/lib/monitor/blocklet-runtime-monitor.js +204 -0
- package/lib/monitor/get-history-list.js +37 -0
- package/lib/monitor/node-monit-sender.js +99 -0
- package/lib/monitor/node-runtime-monitor.js +205 -0
- package/lib/processes/updater.js +186 -0
- package/lib/router/helper.js +1653 -0
- package/lib/router/index.js +341 -0
- package/lib/router/manager.js +806 -0
- package/lib/states/README.md +38 -0
- package/lib/states/access-key.js +124 -0
- package/lib/states/access-policy.js +29 -0
- package/lib/states/audit-log.js +734 -0
- package/lib/states/backup.js +165 -0
- package/lib/states/base.js +25 -0
- package/lib/states/blocklet-extras.js +257 -0
- package/lib/states/blocklet.js +755 -0
- package/lib/states/cache.js +23 -0
- package/lib/states/connect-account.js +8 -0
- package/lib/states/index.js +76 -0
- package/lib/states/job.js +8 -0
- package/lib/states/migration.js +34 -0
- package/lib/states/node.js +324 -0
- package/lib/states/notification.js +81 -0
- package/lib/states/passport.js +8 -0
- package/lib/states/project.js +119 -0
- package/lib/states/release.js +50 -0
- package/lib/states/response-header-policy.js +30 -0
- package/lib/states/routing-snapshot.js +146 -0
- package/lib/states/runtime-insight.js +25 -0
- package/lib/states/security-rule.js +30 -0
- package/lib/states/session.js +53 -0
- package/lib/states/site.js +102 -0
- package/lib/states/tag.js +8 -0
- package/lib/states/traffic-insight.js +25 -0
- package/lib/states/user-session.js +8 -0
- package/lib/states/user.js +717 -0
- package/lib/states/verify-code.js +152 -0
- package/lib/states/webhook.js +66 -0
- package/lib/team/manager.js +508 -0
- package/lib/util/blocklet.js +2182 -0
- package/lib/util/cache.js +63 -0
- package/lib/util/chain.js +29 -0
- package/lib/util/default-node-config.js +46 -0
- package/lib/util/docker/blocklet-custom-dockerfile.js +92 -0
- package/lib/util/docker/check-docker-has-image.js +23 -0
- package/lib/util/docker/check-docker-installed.js +32 -0
- package/lib/util/docker/check-docker-run-history.js +18 -0
- package/lib/util/docker/check-need-run-docker.js +32 -0
- package/lib/util/docker/create-docker-image.js +65 -0
- package/lib/util/docker/debian-dockerfile.js +50 -0
- package/lib/util/docker/docker-container-prune.js +75 -0
- package/lib/util/docker/docker-exec-chown.js +39 -0
- package/lib/util/docker/docker-exec.js +68 -0
- package/lib/util/docker/docker-remove-by-name.js +20 -0
- package/lib/util/docker/get-docker-runtime-info.js +39 -0
- package/lib/util/docker/parse-docker-name.js +10 -0
- package/lib/util/docker/parse-docker-options-from-pm2.js +295 -0
- package/lib/util/docker/shard-network.js +10 -0
- package/lib/util/domain-status.js +118 -0
- package/lib/util/get-accessible-external-node-ip.js +92 -0
- package/lib/util/get-dynamic-components.js +47 -0
- package/lib/util/get-meta-from-url.js +30 -0
- package/lib/util/get-migration-scripts.js +36 -0
- package/lib/util/get-safe-env.js +12 -0
- package/lib/util/index.js +522 -0
- package/lib/util/install-external-dependencies.js +67 -0
- package/lib/util/ip.js +53 -0
- package/lib/util/launcher.js +475 -0
- package/lib/util/log.js +431 -0
- package/lib/util/maintain.js +178 -0
- package/lib/util/promise-debounce.js +29 -0
- package/lib/util/promise-once.js +14 -0
- package/lib/util/promise-spawn.js +93 -0
- package/lib/util/queue.js +44 -0
- package/lib/util/ready.js +89 -0
- package/lib/util/request.js +10 -0
- package/lib/util/requirement.js +61 -0
- package/lib/util/reset-node.js +166 -0
- package/lib/util/rotator.js +194 -0
- package/lib/util/router.js +122 -0
- package/lib/util/rpc.js +78 -0
- package/lib/util/spaces.js +102 -0
- package/lib/util/store.js +184 -0
- package/lib/util/sysinfo.js +46 -0
- package/lib/util/ua.js +54 -0
- package/lib/validators/backup.js +34 -0
- package/lib/validators/blocklet-extra.js +18 -0
- package/lib/validators/node.js +73 -0
- package/lib/validators/permission.js +38 -0
- package/lib/validators/project.js +39 -0
- package/lib/validators/role.js +64 -0
- package/lib/validators/router.js +192 -0
- package/lib/validators/space-gateway.js +33 -0
- package/lib/validators/trusted-factory.js +28 -0
- package/lib/validators/trusted-passport.js +32 -0
- package/lib/validators/user.js +62 -0
- package/lib/validators/util.js +92 -0
- package/lib/validators/webhook.js +42 -0
- package/lib/webhook/index.js +216 -0
- package/lib/webhook/reduce-queue.js +12 -0
- package/lib/webhook/sender/api/index.js +67 -0
- package/lib/webhook/sender/base.js +11 -0
- package/lib/webhook/sender/index.js +44 -0
- package/lib/webhook/sender/slack/index.js +167 -0
- package/lib/webhook/sender/wallet/index.js +62 -0
- package/package.json +107 -34
- package/examples/BatchClient.js +0 -61
- package/examples/compressor/example.js +0 -7
- package/examples/jsonrpcserver/rpcmethod.js +0 -21
- package/examples/jsonrpcserver/rpcrequest.js +0 -20
- package/examples/jsonrpcserver/rpctest.js +0 -6
- package/examples/loader-pipes/test-mc.js +0 -52
- package/examples/loader-pipes/test.js +0 -51
- package/examples/mc-test.js +0 -44
- package/examples/redisdebug.js +0 -80
- package/lib/cache/CFMemcache.js +0 -57
- package/lib/cache/CacheLoader.js +0 -217
- package/lib/cache/Memcache.js +0 -406
- package/lib/class/Class.js +0 -54
- package/lib/client/MemcacheAdapter.js +0 -139
- package/lib/client/Memcached.js +0 -142
- package/lib/client/MemcachedSocket.js +0 -570
- package/lib/client/MySQLPool.js +0 -153
- package/lib/client/PGPool.js +0 -296
- package/lib/client/Redis.js +0 -787
- package/lib/client/S3.js +0 -103
- package/lib/client/S3Client.js +0 -392
- package/lib/client/pg/CursorQueryWorker.js +0 -160
- package/lib/client/rabbitmq/RbmqChannel.js +0 -61
- package/lib/client/rabbitmq/RbmqConfig.js +0 -144
- package/lib/client/rabbitmq/RbmqConnection.js +0 -259
- package/lib/client/rabbitmq/RbmqExchange.js +0 -112
- package/lib/client/rabbitmq/RbmqIncomingMessage.js +0 -53
- package/lib/client/rabbitmq/RbmqMessage.js +0 -120
- package/lib/client/rabbitmq/RbmqOutcomingMessage.js +0 -47
- package/lib/client/rabbitmq/RbmqQueue.js +0 -87
- package/lib/client/rabbitmq/index.js +0 -8
- package/lib/common/Assertions.js +0 -184
- package/lib/common/Merge.js +0 -32
- package/lib/common/Net.js +0 -56
- package/lib/common/Process.js +0 -20
- package/lib/common/Random.js +0 -53
- package/lib/common/Sort.js +0 -19
- package/lib/common/Types.js +0 -163
- package/lib/credentials/CredentialsProvider.js +0 -45
- package/lib/credentials/Manager.js +0 -101
- package/lib/crypto/Crypto.js +0 -66
- package/lib/data/BinaryData.js +0 -363
- package/lib/data/Compressor.js +0 -83
- package/lib/data/FormData.js +0 -144
- package/lib/data/Mime.js +0 -45
- package/lib/event/ErrorEvent.js +0 -71
- package/lib/event/Event.js +0 -20
- package/lib/event/EventDispatcher.js +0 -157
- package/lib/http/ConnectionPool.js +0 -82
- package/lib/http/ExpressServer.js +0 -19
- package/lib/http/Headers.js +0 -165
- package/lib/http/Loader.js +0 -584
- package/lib/http/Message.js +0 -189
- package/lib/http/Request.js +0 -848
- package/lib/http/RequestProcessor.js +0 -150
- package/lib/http/Response.js +0 -850
- package/lib/http/Server.js +0 -267
- package/lib/http/Socket.js +0 -157
- package/lib/http/Url.js +0 -2
- package/lib/jsonrpc/JsonRpcAbstract.js +0 -109
- package/lib/jsonrpc/JsonRpcBatchRequest.js +0 -54
- package/lib/jsonrpc/JsonRpcBatchResponse.js +0 -73
- package/lib/jsonrpc/JsonRpcError.js +0 -191
- package/lib/jsonrpc/JsonRpcMethod.js +0 -67
- package/lib/jsonrpc/JsonRpcRequest.js +0 -224
- package/lib/jsonrpc/JsonRpcRequestProcessor.js +0 -366
- package/lib/jsonrpc/JsonRpcResponse.js +0 -175
- package/lib/jsonrpc/JsonRpcServer.js +0 -81
- package/lib/logger/EmptyLogger.js +0 -15
- package/lib/logger/LoggerFactory.js +0 -20
- package/lib/logger/WildFireLogger.js +0 -267
- package/lib/pattern/Pool.js +0 -187
- package/lib/pattern/decorator/AbstractDecorator.js +0 -49
- package/lib/pattern/decorator/DecoratorChain.js +0 -285
- package/lib/util/DeviceDetector.js +0 -153
- package/lib/yaml/YAML.js +0 -46
- package/patterns.conf +0 -6
- package/tests/cache/CacheLoaderSpec.js +0 -7
- package/tests/class/ClassSpec.js +0 -106
- package/tests/client/MemcachedSocketSpec.js +0 -1228
- package/tests/client/MemcachedSpec.js +0 -77
- package/tests/client/MySQLPoolSpec.js +0 -231
- package/tests/client/PGPoolSpec.js +0 -479
- package/tests/client/RedisSpec.js +0 -471
- package/tests/client/S3ClientSpec.js +0 -26
- package/tests/client/S3Spec.js +0 -129
- package/tests/client/rabbitmq/RbmqConnectionSpec.js +0 -71
- package/tests/common/AssertionsSpec.js +0 -278
- package/tests/common/NetSpec.js +0 -6
- package/tests/common/RandomSpec.js +0 -35
- package/tests/common/TypesSpec.js +0 -7
- package/tests/credentials/CredentialsProviderSpec.js +0 -46
- package/tests/crypto/CryptoSpec.js +0 -33
- package/tests/data/BinaryDataSpec.js +0 -50
- package/tests/data/CompressorSpec.js +0 -7
- package/tests/data/FormDataSpec.js +0 -7
- package/tests/data/MimeSpec.js +0 -7
- package/tests/event/ErrorEventSpec.js +0 -20
- package/tests/event/EventDispatcherSpec.js +0 -319
- package/tests/event/EventSpec.js +0 -17
- package/tests/http/ConnectionPoolSpec.js +0 -7
- package/tests/http/HeadersSpec.js +0 -79
- package/tests/http/LoaderSpec.js +0 -43
- package/tests/http/MessageSpec.js +0 -251
- package/tests/http/RequestProcessorSpec.js +0 -124
- package/tests/http/RequestSpec.js +0 -651
- package/tests/http/ResponseSpec.js +0 -491
- package/tests/http/ServerSpec.js +0 -7
- package/tests/http/SocketSpec.js +0 -7
- package/tests/http/UrlSpec.js +0 -14
- package/tests/jsonrpc/JsonRpcAbstractSpec.js +0 -20
- package/tests/jsonrpc/JsonRpcBatchRequestSpec.js +0 -31
- package/tests/jsonrpc/JsonRpcBatchResponseSpec.js +0 -38
- package/tests/jsonrpc/JsonRpcErrorSpec.js +0 -58
- package/tests/jsonrpc/JsonRpcMethodSpec.js +0 -7
- package/tests/jsonrpc/JsonRpcRequestProcessorSpec.js +0 -271
- package/tests/jsonrpc/JsonRpcRequestSpec.js +0 -70
- package/tests/jsonrpc/JsonRpcResponseSpec.js +0 -63
- package/tests/jsonrpc/JsonRpcServerSpec.js +0 -42
- package/tests/pattern/PoolSpec.js +0 -180
- package/tests/pattern/decorator/AbstractDecoratorSpec.js +0 -7
- package/tests/pattern/decorator/DecoratorChainSpec.js +0 -7
- package/tests/yaml/YAMLSpec.js +0 -132
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const dns = require('dns');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const shell = require('shelljs');
|
|
7
|
+
const camelCase = require('lodash/camelCase');
|
|
8
|
+
const get = require('lodash/get');
|
|
9
|
+
const uniq = require('lodash/uniq');
|
|
10
|
+
const { isFromPublicKey } = require('@arcblock/did');
|
|
11
|
+
const { joinURL } = require('ufo');
|
|
12
|
+
const { Certificate } = require('@fidm/x509');
|
|
13
|
+
const getPortLib = require('get-port');
|
|
14
|
+
const v8 = require('v8');
|
|
15
|
+
const StreamZip = require('node-stream-zip');
|
|
16
|
+
const normalizePathPrefix = require('@abtnode/util/lib/normalize-path-prefix');
|
|
17
|
+
const axios = require('@abtnode/util/lib/axios');
|
|
18
|
+
const { encode: encodeBase32 } = require('@abtnode/util/lib/base32');
|
|
19
|
+
const parseBlockletMeta = require('@blocklet/meta/lib/parse');
|
|
20
|
+
const { BlockletStatus } = require('@blocklet/constant');
|
|
21
|
+
const { replaceSlotToIp, isInProgress } = require('@blocklet/meta/lib/util');
|
|
22
|
+
const {
|
|
23
|
+
StatusCode,
|
|
24
|
+
DOMAIN_FOR_DEFAULT_SITE,
|
|
25
|
+
DOMAIN_FOR_IP_SITE,
|
|
26
|
+
DOMAIN_FOR_IP_SITE_REGEXP,
|
|
27
|
+
DEFAULT_HTTP_PORT,
|
|
28
|
+
DEFAULT_HTTPS_PORT,
|
|
29
|
+
SLOT_FOR_IP_DNS_SITE,
|
|
30
|
+
BLOCKLET_SITE_GROUP_SUFFIX,
|
|
31
|
+
} = require('@abtnode/constant');
|
|
32
|
+
|
|
33
|
+
const DEFAULT_WELLKNOWN_PORT = 8088;
|
|
34
|
+
|
|
35
|
+
const logger = require('@abtnode/logger')('@abtnode/core:util');
|
|
36
|
+
const getSafeEnv = require('./get-safe-env');
|
|
37
|
+
|
|
38
|
+
const validateOwner = (owner) => {
|
|
39
|
+
try {
|
|
40
|
+
return owner && owner.did && owner.pk && isFromPublicKey(owner.did, owner.pk);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const toStatus = (v) => Object.keys(StatusCode).find((x) => StatusCode[x] === Number(v));
|
|
47
|
+
const fromStatus = (v) => {
|
|
48
|
+
const match = Object.entries(StatusCode).find((x) => x[1] === Number(v));
|
|
49
|
+
return match ? match[0] : 'unknown';
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const replaceDomainSlot = ({ domain, context = {}, nodeIp }) => {
|
|
53
|
+
let processed = domain;
|
|
54
|
+
if (processed.includes(SLOT_FOR_IP_DNS_SITE)) {
|
|
55
|
+
const ipRegex = /\d+[-.]\d+[-.]\d+[-.]\d+/;
|
|
56
|
+
const match = ipRegex.exec(context.hostname);
|
|
57
|
+
if (match) {
|
|
58
|
+
const ip = match[0];
|
|
59
|
+
processed = replaceSlotToIp(processed, ip);
|
|
60
|
+
} else if (nodeIp) {
|
|
61
|
+
processed = replaceSlotToIp(processed, nodeIp);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return processed;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const getBlockletHost = ({ domain, context, nodeIp }) => {
|
|
68
|
+
const { protocol, port } = context || {};
|
|
69
|
+
|
|
70
|
+
if (domain === DOMAIN_FOR_DEFAULT_SITE) {
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let tmpDomain = domain;
|
|
75
|
+
if ([DOMAIN_FOR_IP_SITE, DOMAIN_FOR_IP_SITE_REGEXP].includes(domain) || !domain) {
|
|
76
|
+
tmpDomain = context.hostname || '';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
tmpDomain = replaceDomainSlot({ domain: tmpDomain, context, nodeIp });
|
|
80
|
+
|
|
81
|
+
if (!port) {
|
|
82
|
+
return tmpDomain;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (protocol === 'https' && Number(port) === 443) {
|
|
86
|
+
return tmpDomain; // 不展示 https 中的 443 端口
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (Number(port) === 80) {
|
|
90
|
+
return tmpDomain; // 不展示 http 中的 80 端
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return `${tmpDomain}:${port}`;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const isRoutingEnabled = (routing) => !!routing && !!routing.provider;
|
|
97
|
+
|
|
98
|
+
// Check whether a domain is setup
|
|
99
|
+
const checkDomainDNS = (domain) =>
|
|
100
|
+
new Promise((resolve) => {
|
|
101
|
+
dns.lookup(domain, (error, address) => {
|
|
102
|
+
const data = {};
|
|
103
|
+
if (error) {
|
|
104
|
+
data.resolved = false;
|
|
105
|
+
} else {
|
|
106
|
+
data.resolved = true;
|
|
107
|
+
data.ip = address;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
resolve(data);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// async and promise style of shelljs.exec
|
|
115
|
+
const asyncExec = (command, options) =>
|
|
116
|
+
new Promise((resolve) => {
|
|
117
|
+
shell.exec(command, { async: true, ...options }, (code, stdout, stderr) => {
|
|
118
|
+
resolve({ code, stdout, stderr });
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const getProviderFromNodeInfo = (info) => get(info, 'routing.provider', 'default');
|
|
123
|
+
|
|
124
|
+
const isCLI = () => !process.env.ABT_NODE_SK;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Returns the digest of the data with given hash algorithm.
|
|
128
|
+
* @param {Buffer} data
|
|
129
|
+
* @param {string} alg
|
|
130
|
+
*/
|
|
131
|
+
const getFingerprint = (data, alg) => {
|
|
132
|
+
const shasum = crypto.createHash(alg);
|
|
133
|
+
// eslint-disable-next-line newline-per-chained-call
|
|
134
|
+
return shasum.update(data).digest('hex').match(/.{2}/g).join(':').toUpperCase();
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const getHttpsCertInfo = (certificate) => {
|
|
138
|
+
const info = Certificate.fromPEM(certificate);
|
|
139
|
+
|
|
140
|
+
const domain = info.subject.commonName.valueOf();
|
|
141
|
+
const validFrom = info.validFrom.valueOf();
|
|
142
|
+
const validTo = info.validTo.valueOf();
|
|
143
|
+
const issuer = {
|
|
144
|
+
countryName: info.issuer.countryName.valueOf(),
|
|
145
|
+
organizationName: info.issuer.organizationName.valueOf(),
|
|
146
|
+
commonName: info.issuer.commonName.valueOf(),
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
domain,
|
|
151
|
+
validFrom,
|
|
152
|
+
validTo,
|
|
153
|
+
issuer,
|
|
154
|
+
sans: info.dnsNames,
|
|
155
|
+
validityPeriod: info.validTo - info.validFrom,
|
|
156
|
+
fingerprintAlg: 'SHA256',
|
|
157
|
+
fingerprint: getFingerprint(info.raw, 'sha256'),
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const getDataDirs = (dataDir) => ({
|
|
162
|
+
core: path.join(dataDir, 'core'),
|
|
163
|
+
cache: path.join(dataDir, 'cache'),
|
|
164
|
+
data: path.join(dataDir, 'data'),
|
|
165
|
+
logs: path.join(dataDir, 'logs'),
|
|
166
|
+
router: path.join(dataDir, 'router'),
|
|
167
|
+
tmp: path.join(dataDir, 'tmp'),
|
|
168
|
+
blocklets: path.join(dataDir, 'blocklets'),
|
|
169
|
+
services: path.join(dataDir, 'services'),
|
|
170
|
+
modules: path.join(dataDir, 'modules'),
|
|
171
|
+
certManagerModule: path.join(dataDir, 'modules', 'certificate-manager'),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Ensure data dir for Blocklet Server exists
|
|
175
|
+
const ensureDataDirs = (dataDir) => {
|
|
176
|
+
try {
|
|
177
|
+
logger.info('ensure data dir', { dataDir });
|
|
178
|
+
fs.mkdirSync(dataDir, { recursive: true });
|
|
179
|
+
const ignore = 'logs\ncache\nrouter\nblocklets\nservices';
|
|
180
|
+
fs.writeFileSync(path.join(dataDir, '.gitignore'), ignore);
|
|
181
|
+
} catch (err) {
|
|
182
|
+
// Do nothing
|
|
183
|
+
}
|
|
184
|
+
const dataDirs = getDataDirs(dataDir);
|
|
185
|
+
|
|
186
|
+
Object.keys(dataDirs).forEach((x) => fs.mkdirSync(dataDirs[x], { recursive: true }));
|
|
187
|
+
|
|
188
|
+
return dataDirs;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const formatEnvironments = (environments) => Object.keys(environments).map((x) => ({ key: x, value: environments[x] }));
|
|
192
|
+
|
|
193
|
+
const transformIPToDomain = (ip) => ip.split('.').join('-');
|
|
194
|
+
|
|
195
|
+
const getBaseUrls = async (node, ips) => {
|
|
196
|
+
const info = await node.getNodeInfo();
|
|
197
|
+
const { https, httpPort, httpsPort } = info.routing;
|
|
198
|
+
const getPort = (port, defaultPort) => (port && port !== defaultPort ? `:${port}` : '');
|
|
199
|
+
const availableIps = uniq(ips.filter(Boolean));
|
|
200
|
+
|
|
201
|
+
const getHttpInfo = async (domain) => {
|
|
202
|
+
const certificate = https ? await node.certManager.getNormalByDomain(domain) : null;
|
|
203
|
+
const protocol = certificate ? 'https' : 'http';
|
|
204
|
+
const port = certificate ? getPort(httpsPort, DEFAULT_HTTPS_PORT) : getPort(httpPort, DEFAULT_HTTP_PORT);
|
|
205
|
+
|
|
206
|
+
return { protocol, port };
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
if (info.routing.provider && info.routing.snapshotHash && info.routing.adminPath) {
|
|
210
|
+
const sites = await node.getSitesFromSnapshot();
|
|
211
|
+
const { ipWildcardDomain } = info.routing;
|
|
212
|
+
const adminPath = normalizePathPrefix(info.routing.adminPath);
|
|
213
|
+
const tmpHttpPort = getPort(httpPort, DEFAULT_HTTP_PORT);
|
|
214
|
+
|
|
215
|
+
const httpUrls = availableIps.map((ip) => {
|
|
216
|
+
return {
|
|
217
|
+
url: `http://${ip}${tmpHttpPort}${adminPath}`,
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
if (ipWildcardDomain) {
|
|
222
|
+
const site = sites.find((c) => (c.domainAliases || []).find((item) => item.value === ipWildcardDomain));
|
|
223
|
+
if (site) {
|
|
224
|
+
const httpInfo = await getHttpInfo(ipWildcardDomain);
|
|
225
|
+
const httpsUrls = availableIps.map((ip) => ({
|
|
226
|
+
url: `${httpInfo.protocol}://${transformIPToDomain(ip)}.${ipWildcardDomain.substring(2)}${
|
|
227
|
+
httpInfo.port
|
|
228
|
+
}${adminPath}`,
|
|
229
|
+
}));
|
|
230
|
+
|
|
231
|
+
// add did domain access url
|
|
232
|
+
const didDomainAlias = site.domainAliases.find((item) => item.value.endsWith(info.didDomain));
|
|
233
|
+
if (didDomainAlias) {
|
|
234
|
+
const didDomain = didDomainAlias.value;
|
|
235
|
+
const didDomainHttpInfo = await getHttpInfo(didDomain);
|
|
236
|
+
|
|
237
|
+
httpsUrls.push({
|
|
238
|
+
url: `${didDomainHttpInfo.protocol}://${didDomain}${didDomainHttpInfo.port}${adminPath}`,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return httpUrls.concat(httpsUrls);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return httpUrls;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// port urls
|
|
250
|
+
return availableIps.map((ip) => ({
|
|
251
|
+
url: `http://${ip}:${info.port}`,
|
|
252
|
+
}));
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const expandBundle = async (bundlePath, destDir) => {
|
|
256
|
+
// eslint-disable-next-line new-cap
|
|
257
|
+
const zip = new StreamZip.async({ file: bundlePath });
|
|
258
|
+
await zip.extract(null, destDir);
|
|
259
|
+
await zip.close();
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// eslint-disable-next-line no-shadow
|
|
263
|
+
const findInterfaceByName = (blocklet, name) => {
|
|
264
|
+
const { interfaces = [] } = blocklet.meta;
|
|
265
|
+
if (!Array.isArray(interfaces)) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return interfaces.find((x) => x.name === name);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const findInterfacePortByName = (blocklet, name) => {
|
|
273
|
+
const found = findInterfaceByName(blocklet, name);
|
|
274
|
+
const { ports } = blocklet;
|
|
275
|
+
|
|
276
|
+
if (!found || !ports) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (typeof found.port === 'string') {
|
|
281
|
+
return ports[found.port];
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return ports[found.port.internal];
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const getWellknownSitePort = async () => {
|
|
288
|
+
if (process.env.NODE_ENV === 'test') {
|
|
289
|
+
const port = await getPortLib();
|
|
290
|
+
return port;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return DEFAULT_WELLKNOWN_PORT;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const shouldUpdateBlockletStatus = (blockletStatus) =>
|
|
297
|
+
isInProgress(blockletStatus) === false && blockletStatus !== BlockletStatus.error;
|
|
298
|
+
|
|
299
|
+
const getQueueConcurrencyByMem = () => {
|
|
300
|
+
const totalMem = os.totalmem();
|
|
301
|
+
const heapSizeLimit = v8.getHeapStatistics().heap_size_limit;
|
|
302
|
+
|
|
303
|
+
const heapSizeRatio = heapSizeLimit / totalMem;
|
|
304
|
+
|
|
305
|
+
if (heapSizeRatio <= 0.2) {
|
|
306
|
+
return 4;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (heapSizeRatio <= 0.3) {
|
|
310
|
+
return 3;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (heapSizeRatio <= 0.6) {
|
|
314
|
+
return 2;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return 1;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// For performance sake, we only support 1 param here, and the cache is flushed every minute
|
|
321
|
+
const memoizeAsync = (fn, flushInterval = 60000) => {
|
|
322
|
+
const cache = new Map();
|
|
323
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
324
|
+
setInterval(() => cache.clear(), flushInterval);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return (arg) => {
|
|
328
|
+
if (cache.has(arg)) {
|
|
329
|
+
return cache.get(arg);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// eslint-disable-next-line prefer-spread
|
|
333
|
+
const result = fn.apply(null, [arg]);
|
|
334
|
+
cache.set(arg, result);
|
|
335
|
+
|
|
336
|
+
return result;
|
|
337
|
+
};
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const getStateCrons = (states) => [
|
|
341
|
+
{
|
|
342
|
+
name: 'cleanup-audit-logs',
|
|
343
|
+
time: '0 0 6 * * *', // cleanup old logs every day at 6am
|
|
344
|
+
// time: '0 */5 * * * *', // cleanup every 5 minutes
|
|
345
|
+
options: { runOnInit: false },
|
|
346
|
+
fn: async () => {
|
|
347
|
+
const removed = await states.auditLog.remove({
|
|
348
|
+
createdAt: { $lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 365) },
|
|
349
|
+
});
|
|
350
|
+
logger.info(`Removed ${removed} audit logs`);
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
const getDelegateState = async (chainHost, address) => {
|
|
356
|
+
const result = await axios.post(
|
|
357
|
+
joinURL(chainHost, '/gql/'),
|
|
358
|
+
JSON.stringify({
|
|
359
|
+
query: `{
|
|
360
|
+
getDelegateState(address: "${address}") {
|
|
361
|
+
state {
|
|
362
|
+
address
|
|
363
|
+
ops {
|
|
364
|
+
key
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}`,
|
|
369
|
+
}),
|
|
370
|
+
{
|
|
371
|
+
headers: {
|
|
372
|
+
'Content-Type': 'application/json',
|
|
373
|
+
Accept: 'application/json',
|
|
374
|
+
},
|
|
375
|
+
timeout: 60 * 1000,
|
|
376
|
+
}
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
return get(result.data, 'data.getDelegateState.state');
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const getNFTState = async (chainHost, nftId) => {
|
|
383
|
+
const url = joinURL(new URL(chainHost).origin, '/api/gql/');
|
|
384
|
+
|
|
385
|
+
const result = await axios.post(
|
|
386
|
+
url,
|
|
387
|
+
JSON.stringify({
|
|
388
|
+
query: `{
|
|
389
|
+
getAssetState(address: "${nftId}") {
|
|
390
|
+
state {
|
|
391
|
+
address
|
|
392
|
+
data {
|
|
393
|
+
typeUrl
|
|
394
|
+
value
|
|
395
|
+
}
|
|
396
|
+
display {
|
|
397
|
+
type
|
|
398
|
+
content
|
|
399
|
+
}
|
|
400
|
+
issuer
|
|
401
|
+
owner
|
|
402
|
+
parent
|
|
403
|
+
tags
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}`,
|
|
407
|
+
}),
|
|
408
|
+
{
|
|
409
|
+
headers: {
|
|
410
|
+
'Content-Type': 'application/json',
|
|
411
|
+
Accept: 'application/json',
|
|
412
|
+
},
|
|
413
|
+
timeout: 60 * 1000,
|
|
414
|
+
}
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
const state = get(result, 'data.data.getAssetState.state');
|
|
418
|
+
if (state && state.data.typeUrl === 'json') {
|
|
419
|
+
state.data.value = JSON.parse(state.data.value);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return state;
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const getServerDidDomain = (nodeInfo) => `${encodeBase32(nodeInfo.did)}.${nodeInfo.didDomain}`;
|
|
426
|
+
|
|
427
|
+
const prettyURL = (url, isHttps = true) => {
|
|
428
|
+
if (typeof url !== 'string') {
|
|
429
|
+
return url;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (url.toLowerCase().startsWith('http')) {
|
|
433
|
+
return url;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return isHttps ? `https://${url}` : `http://${url}`;
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
const templateReplace = (str, vars = {}) => {
|
|
440
|
+
if (typeof str === 'string') {
|
|
441
|
+
return str.replace(/{([.\w]+)}/g, (m, key) => get(vars, key));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return str;
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const isGatewayCacheEnabled = (info) => {
|
|
448
|
+
const cacheEnabled = get(info, 'routing.cacheEnabled');
|
|
449
|
+
if (typeof cacheEnabled === 'boolean') {
|
|
450
|
+
return cacheEnabled;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return true;
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const isBlockletSite = (domain) => domain.endsWith(BLOCKLET_SITE_GROUP_SUFFIX);
|
|
457
|
+
const isServerSite = (domain) =>
|
|
458
|
+
[DOMAIN_FOR_DEFAULT_SITE, DOMAIN_FOR_IP_SITE, DOMAIN_FOR_IP_SITE_REGEXP].includes(domain);
|
|
459
|
+
|
|
460
|
+
const getDbFilePath = (filePath) => (process.env.NODE_ENV === 'test' ? `${filePath}:memory:` : filePath);
|
|
461
|
+
|
|
462
|
+
const toCamelCase = (obj) => {
|
|
463
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
464
|
+
return obj;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (Array.isArray(obj)) {
|
|
468
|
+
return obj.map(toCamelCase);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const converted = {};
|
|
472
|
+
|
|
473
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
474
|
+
for (const key in obj) {
|
|
475
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
476
|
+
converted[camelCase(key)] = toCamelCase(obj[key]);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return converted;
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const lib = {
|
|
484
|
+
validateOwner,
|
|
485
|
+
getProviderFromNodeInfo,
|
|
486
|
+
toStatus,
|
|
487
|
+
fromStatus,
|
|
488
|
+
formatEnvironments,
|
|
489
|
+
replaceDomainSlot,
|
|
490
|
+
getBlockletHost,
|
|
491
|
+
isRoutingEnabled,
|
|
492
|
+
checkDomainDNS,
|
|
493
|
+
asyncExec,
|
|
494
|
+
isCLI,
|
|
495
|
+
getHttpsCertInfo,
|
|
496
|
+
ensureDataDirs,
|
|
497
|
+
getDataDirs,
|
|
498
|
+
getBaseUrls,
|
|
499
|
+
getBlockletMeta: parseBlockletMeta,
|
|
500
|
+
expandBundle,
|
|
501
|
+
findInterfaceByName,
|
|
502
|
+
findInterfacePortByName,
|
|
503
|
+
getWellknownSitePort,
|
|
504
|
+
shouldUpdateBlockletStatus,
|
|
505
|
+
transformIPToDomain,
|
|
506
|
+
getQueueConcurrencyByMem,
|
|
507
|
+
getSafeEnv,
|
|
508
|
+
memoizeAsync,
|
|
509
|
+
getStateCrons,
|
|
510
|
+
getDelegateState,
|
|
511
|
+
getNFTState,
|
|
512
|
+
getServerDidDomain,
|
|
513
|
+
prettyURL,
|
|
514
|
+
templateReplace,
|
|
515
|
+
isGatewayCacheEnabled,
|
|
516
|
+
isBlockletSite,
|
|
517
|
+
isServerSite,
|
|
518
|
+
getDbFilePath,
|
|
519
|
+
toCamelCase,
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
module.exports = lib;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const { spawn } = require('child_process');
|
|
2
|
+
const fs = require('fs-extra');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
function isDependencyInstalled(appDir, dependency) {
|
|
6
|
+
return fs.existsSync(path.resolve(appDir, 'node_modules', dependency));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function installExternalDependencies({ appDir, forceInstall = false } = {}) {
|
|
10
|
+
if (!appDir) {
|
|
11
|
+
throw new Error('appDir is required');
|
|
12
|
+
}
|
|
13
|
+
if (!fs.existsSync(appDir)) {
|
|
14
|
+
throw new Error(`not a correct appDir directory: ${appDir}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 读取 BLOCKLET_APP_DIR 的 package.json
|
|
18
|
+
const packageJsonPath = path.resolve(appDir, 'package.json');
|
|
19
|
+
|
|
20
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const packageJson = fs.readJsonSync(packageJsonPath);
|
|
25
|
+
|
|
26
|
+
const { blockletExternalDependencies: externals = [], externalManager = 'npm' } = packageJson;
|
|
27
|
+
if (!Array.isArray(externals) || !externals.length) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const isNeedInstall = forceInstall || externals.some((dependency) => !isDependencyInstalled(appDir, dependency));
|
|
32
|
+
if (!isNeedInstall) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// eslint-disable-next-line consistent-return
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const child = spawn(externalManager, ['install'], {
|
|
39
|
+
cwd: appDir,
|
|
40
|
+
stdio: 'pipe',
|
|
41
|
+
shell: true,
|
|
42
|
+
env: {
|
|
43
|
+
...process.env,
|
|
44
|
+
NODE_ENV: 'production',
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
let errorOutput = '';
|
|
49
|
+
child.stderr.on('data', (data) => {
|
|
50
|
+
errorOutput += data.toString();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
child.on('close', (code) => {
|
|
54
|
+
if (code !== 0 && errorOutput.trim()) {
|
|
55
|
+
reject(new Error(errorOutput));
|
|
56
|
+
} else {
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
child.on('error', (err) => {
|
|
62
|
+
reject(err);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { installExternalDependencies };
|
package/lib/util/ip.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const getIP = require('@abtnode/util/lib/get-ip');
|
|
2
|
+
const { encode } = require('@abtnode/util/lib/base32');
|
|
3
|
+
const { DEFAULT_DID_DOMAIN } = require('@abtnode/constant');
|
|
4
|
+
const logger = require('@abtnode/logger')('@abtnode/core:ip');
|
|
5
|
+
const dns = require('dns');
|
|
6
|
+
const { promisify } = require('util');
|
|
7
|
+
|
|
8
|
+
const lookup = promisify(dns.lookup);
|
|
9
|
+
|
|
10
|
+
let cache = null;
|
|
11
|
+
const fetch = async (args = {}) => {
|
|
12
|
+
try {
|
|
13
|
+
const start = Date.now();
|
|
14
|
+
const result = await getIP({ includeV6: true, timeout: 5000, ...args });
|
|
15
|
+
cache = result;
|
|
16
|
+
logger.info('refetch ip', { result, duration: Date.now() - start });
|
|
17
|
+
} catch (err) {
|
|
18
|
+
logger.error('failed to refetch ip', { error: err });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return cache;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const get = (args) => cache || fetch(args);
|
|
25
|
+
const cron = {
|
|
26
|
+
name: 'refetch-ip',
|
|
27
|
+
time: '0 */30 * * * *', // refetch every 30 minutes
|
|
28
|
+
fn: fetch,
|
|
29
|
+
options: { runOnInit: true, runInService: true },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @description
|
|
34
|
+
* @param {string} did
|
|
35
|
+
* @return {Promise<boolean>}
|
|
36
|
+
*/
|
|
37
|
+
async function isDnsIpMappingCorrect(did) {
|
|
38
|
+
try {
|
|
39
|
+
const { internal, external } = await getIP();
|
|
40
|
+
const didDomain = `${encode(did)}.${DEFAULT_DID_DOMAIN}`;
|
|
41
|
+
const { address } = await lookup(didDomain, { rrtype: 'A' });
|
|
42
|
+
|
|
43
|
+
return internal === address || external === address;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
logger.error(error);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports.fetch = fetch;
|
|
51
|
+
module.exports.get = get;
|
|
52
|
+
module.exports.cron = cron;
|
|
53
|
+
module.exports.isDnsIpMappingCorrect = isDnsIpMappingCorrect;
|