@reclaimprotocol/attestor-core 5.0.4 → 5.0.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.
Files changed (122) hide show
  1. package/LICENSE +660 -660
  2. package/browser/resources/attestor-browser.min.mjs +31 -31
  3. package/lib/server/tee/acme-http-server.d.ts +13 -0
  4. package/lib/server/tee/attestation-generate.d.ts +29 -0
  5. package/lib/server/tee/bootstrap.d.ts +11 -0
  6. package/lib/server/tee/cert-manager.d.ts +24 -0
  7. package/lib/server/tee/cloud-logging.d.ts +23 -0
  8. package/lib/server/tee/secret-loader.d.ts +10 -0
  9. package/lib/server/tee/secret-manager.d.ts +3 -0
  10. package/lib/utils/gcp-attestation.d.ts +23 -0
  11. package/package.json +3 -3
  12. package/lib/avs/abis/avsDirectoryABI.js +0 -340
  13. package/lib/avs/abis/delegationABI.js +0 -1
  14. package/lib/avs/abis/registryABI.js +0 -725
  15. package/lib/avs/client/create-claim-on-avs.js +0 -138
  16. package/lib/avs/config.js +0 -20
  17. package/lib/avs/contracts/ReclaimServiceManager.js +0 -1
  18. package/lib/avs/contracts/common.js +0 -1
  19. package/lib/avs/contracts/factories/ReclaimServiceManager__factory.js +0 -1169
  20. package/lib/avs/contracts/factories/index.js +0 -4
  21. package/lib/avs/contracts/index.js +0 -2
  22. package/lib/avs/types/index.js +0 -1
  23. package/lib/avs/utils/contracts.js +0 -33
  24. package/lib/avs/utils/register.js +0 -78
  25. package/lib/avs/utils/tasks.js +0 -40
  26. package/lib/client/create-claim.js +0 -433
  27. package/lib/client/index.js +0 -3
  28. package/lib/client/tunnels/make-rpc-tcp-tunnel.js +0 -51
  29. package/lib/client/tunnels/make-rpc-tls-tunnel.js +0 -131
  30. package/lib/client/utils/attestor-pool.js +0 -25
  31. package/lib/client/utils/client-socket.js +0 -98
  32. package/lib/client/utils/message-handler.js +0 -87
  33. package/lib/config/index.js +0 -44
  34. package/lib/external-rpc/benchmark.js +0 -69
  35. package/lib/external-rpc/event-bus.js +0 -14
  36. package/lib/external-rpc/handle-incoming-msg.js +0 -233
  37. package/lib/external-rpc/jsc-polyfills/1.js +0 -82
  38. package/lib/external-rpc/jsc-polyfills/2.js +0 -20
  39. package/lib/external-rpc/jsc-polyfills/event.js +0 -14
  40. package/lib/external-rpc/jsc-polyfills/index.js +0 -2
  41. package/lib/external-rpc/jsc-polyfills/ws.js +0 -81
  42. package/lib/external-rpc/setup-browser.js +0 -33
  43. package/lib/external-rpc/setup-jsc.js +0 -22
  44. package/lib/external-rpc/types.js +0 -1
  45. package/lib/external-rpc/utils.js +0 -100
  46. package/lib/external-rpc/zk.js +0 -63
  47. package/lib/mechain/abis/governanceABI.js +0 -458
  48. package/lib/mechain/abis/taskABI.js +0 -509
  49. package/lib/mechain/client/create-claim-on-mechain.js +0 -28
  50. package/lib/mechain/client/index.js +0 -1
  51. package/lib/mechain/constants/index.js +0 -3
  52. package/lib/mechain/index.js +0 -2
  53. package/lib/mechain/types/index.js +0 -1
  54. package/lib/proto/api.js +0 -4273
  55. package/lib/proto/tee-bundle.js +0 -1316
  56. package/lib/providers/http/index.js +0 -658
  57. package/lib/providers/http/patch-parse5-tree.js +0 -33
  58. package/lib/providers/http/utils.js +0 -324
  59. package/lib/providers/index.js +0 -4
  60. package/lib/scripts/fetch-ec2-metadata.d.ts +0 -1
  61. package/lib/server/create-server.js +0 -103
  62. package/lib/server/handlers/claimTeeBundle.js +0 -252
  63. package/lib/server/handlers/claimTunnel.js +0 -73
  64. package/lib/server/handlers/completeClaimOnChain.js +0 -22
  65. package/lib/server/handlers/createClaimOnChain.js +0 -26
  66. package/lib/server/handlers/createTaskOnMechain.js +0 -47
  67. package/lib/server/handlers/createTunnel.js +0 -93
  68. package/lib/server/handlers/disconnectTunnel.js +0 -5
  69. package/lib/server/handlers/fetchCertificateBytes.js +0 -41
  70. package/lib/server/handlers/index.js +0 -22
  71. package/lib/server/handlers/init.js +0 -32
  72. package/lib/server/handlers/toprf.js +0 -16
  73. package/lib/server/index.js +0 -4
  74. package/lib/server/socket.js +0 -109
  75. package/lib/server/tunnels/make-tcp-tunnel.js +0 -177
  76. package/lib/server/utils/apm.js +0 -36
  77. package/lib/server/utils/assert-valid-claim-request.js +0 -204
  78. package/lib/server/utils/config-env.js +0 -4
  79. package/lib/server/utils/dns.js +0 -18
  80. package/lib/server/utils/gcp-attestation.js +0 -289
  81. package/lib/server/utils/generics.js +0 -51
  82. package/lib/server/utils/iso.js +0 -256
  83. package/lib/server/utils/keep-alive.js +0 -38
  84. package/lib/server/utils/nitro-attestation.js +0 -325
  85. package/lib/server/utils/process-handshake.js +0 -215
  86. package/lib/server/utils/proxy-session.js +0 -6
  87. package/lib/server/utils/tee-oprf-mpc-verification.js +0 -90
  88. package/lib/server/utils/tee-oprf-verification.js +0 -174
  89. package/lib/server/utils/tee-transcript-reconstruction.js +0 -187
  90. package/lib/server/utils/tee-verification.js +0 -421
  91. package/lib/server/utils/validation.js +0 -38
  92. package/lib/types/bgp.js +0 -1
  93. package/lib/types/claims.js +0 -1
  94. package/lib/types/client.js +0 -1
  95. package/lib/types/general.js +0 -1
  96. package/lib/types/handlers.js +0 -1
  97. package/lib/types/index.js +0 -10
  98. package/lib/types/providers.gen.js +0 -10
  99. package/lib/types/providers.js +0 -1
  100. package/lib/types/rpc.js +0 -1
  101. package/lib/types/signatures.js +0 -1
  102. package/lib/types/tunnel.js +0 -1
  103. package/lib/types/zk.js +0 -1
  104. package/lib/utils/auth.js +0 -59
  105. package/lib/utils/b64-json.js +0 -17
  106. package/lib/utils/bgp-listener.js +0 -119
  107. package/lib/utils/claims.js +0 -99
  108. package/lib/utils/env.js +0 -15
  109. package/lib/utils/error.js +0 -50
  110. package/lib/utils/generics.js +0 -317
  111. package/lib/utils/http-parser.js +0 -246
  112. package/lib/utils/index.js +0 -13
  113. package/lib/utils/logger.js +0 -91
  114. package/lib/utils/prepare-packets.js +0 -62
  115. package/lib/utils/redactions.js +0 -148
  116. package/lib/utils/retries.js +0 -24
  117. package/lib/utils/signatures/eth.js +0 -29
  118. package/lib/utils/signatures/index.js +0 -7
  119. package/lib/utils/socket-base.js +0 -90
  120. package/lib/utils/tls.js +0 -58
  121. package/lib/utils/ws.js +0 -22
  122. package/lib/utils/zk.js +0 -577
@@ -1,289 +0,0 @@
1
- /**
2
- * GCP attestation validation utilities
3
- * Validates JWT tokens from Google Confidential Computing
4
- */
5
- import crypto, { X509Certificate } from 'crypto';
6
- // Cache for Google's public keys
7
- let gcpKeysCache = null;
8
- let gcpKeysCacheTime = 0;
9
- const GCP_KEYS_CACHE_TTL = 3600000; // 1 hour in milliseconds
10
- // GCP Confidential Space Root CA
11
- const GCP_CONFIDENTIAL_SPACE_ROOT_CA = `-----BEGIN CERTIFICATE-----
12
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- uiHvXeSJBRlRxQ8Vu+t6J7hBdl0etWBKAu9Vti46af5cjC03dspkHR3MAUGcrLWE
29
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
44
- uyT5CnJulxSC5CT1
45
- -----END CERTIFICATE-----`;
46
- /**
47
- * Base64url decode (RFC 4648, no padding)
48
- */
49
- function base64urlDecode(input) {
50
- // Add padding if needed
51
- let base64 = input.replace(/-/g, '+').replace(/_/g, '/');
52
- while (base64.length % 4) {
53
- base64 += '=';
54
- }
55
- return Buffer.from(base64, 'base64');
56
- }
57
- /**
58
- * Fetch Google's public keys (with caching)
59
- */
60
- async function fetchGooglePublicKeys(logger) {
61
- const now = Date.now();
62
- // Return cached keys if still valid
63
- if (gcpKeysCache && (now - gcpKeysCacheTime) < GCP_KEYS_CACHE_TTL) {
64
- if (logger) {
65
- logger.debug('Using cached Google public keys');
66
- }
67
- return gcpKeysCache;
68
- }
69
- // Fetch fresh keys
70
- if (logger) {
71
- logger.info('Fetching Google public keys from https://www.googleapis.com/oauth2/v3/certs');
72
- }
73
- const response = await fetch('https://www.googleapis.com/oauth2/v3/certs');
74
- if (!response.ok) {
75
- throw new Error(`Failed to fetch Google keys: ${response.status} ${response.statusText}`);
76
- }
77
- const keys = await response.json();
78
- // Update cache
79
- gcpKeysCache = keys;
80
- gcpKeysCacheTime = now;
81
- if (logger) {
82
- logger.info(`Fetched ${keys.keys.length} Google public keys`);
83
- }
84
- return keys;
85
- }
86
- /**
87
- * Convert JWK to RSA public key
88
- */
89
- function jwkToPublicKey(jwk) {
90
- // Create RSA public key from modulus and exponent
91
- return crypto.createPublicKey({
92
- key: {
93
- kty: 'RSA',
94
- n: jwk.n,
95
- e: jwk.e,
96
- },
97
- format: 'jwk'
98
- });
99
- }
100
- /**
101
- * Verify x5c certificate chain and return leaf certificate's public key
102
- */
103
- function verifyX5cChain(x5cChain, logger) {
104
- if (!x5cChain || x5cChain.length === 0) {
105
- throw new Error('Empty x5c certificate chain');
106
- }
107
- // Parse leaf certificate (first in chain)
108
- const leafCertPem = `-----BEGIN CERTIFICATE-----\n${x5cChain[0]}\n-----END CERTIFICATE-----`;
109
- const leafCert = new X509Certificate(leafCertPem);
110
- if (logger) {
111
- logger.info(`x5c leaf certificate: subject=${leafCert.subject}, issuer=${leafCert.issuer}`);
112
- }
113
- // Parse root CA
114
- const rootCert = new X509Certificate(GCP_CONFIDENTIAL_SPACE_ROOT_CA);
115
- // For chain verification with Node.js X509Certificate, we need to verify each cert in sequence
116
- // Start with leaf and work up to root
117
- let currentCert = leafCert;
118
- // Verify intermediate certificates if present
119
- for (let i = 1; i < x5cChain.length; i++) {
120
- const intermediatePem = `-----BEGIN CERTIFICATE-----\n${x5cChain[i]}\n-----END CERTIFICATE-----`;
121
- const intermediateCert = new X509Certificate(intermediatePem);
122
- // Verify current cert was signed by intermediate
123
- const isValid = currentCert.verify(intermediateCert.publicKey);
124
- if (!isValid) {
125
- throw new Error(`Certificate chain verification failed at level ${i}`);
126
- }
127
- if (logger) {
128
- logger.debug(`Verified cert level ${i}: ${intermediateCert.subject}`);
129
- }
130
- currentCert = intermediateCert;
131
- }
132
- // Verify the top cert was signed by root CA
133
- const isRootValid = currentCert.verify(rootCert.publicKey);
134
- if (!isRootValid) {
135
- throw new Error('Certificate chain does not root to GCP Confidential Space Root CA');
136
- }
137
- if (logger) {
138
- logger.info('x5c certificate chain verified successfully');
139
- }
140
- // Return leaf certificate's public key for signature verification
141
- return leafCert.publicKey;
142
- }
143
- /**
144
- * Validates GCP JWT attestation and extracts ETH address
145
- */
146
- export async function validateGcpAttestationAndExtractKey(attestationBytes, logger) {
147
- const errors = [];
148
- try {
149
- // 1. Parse JWT structure
150
- const jwtString = Buffer.from(attestationBytes).toString('utf8');
151
- const parts = jwtString.split('.');
152
- if (parts.length !== 3) {
153
- errors.push('Invalid JWT format: expected 3 parts');
154
- return { isValid: false, errors };
155
- }
156
- const [headerB64, payloadB64, signatureB64] = parts;
157
- // Decode header and payload
158
- const headerJson = base64urlDecode(headerB64).toString('utf8');
159
- const payloadJson = base64urlDecode(payloadB64).toString('utf8');
160
- const header = JSON.parse(headerJson);
161
- const payload = JSON.parse(payloadJson);
162
- if (logger) {
163
- logger.info(`GCP JWT header: kid=${header.kid}, alg=${header.alg}`);
164
- logger.info(`GCP JWT payload: iss=${payload.iss}, aud=${payload.aud}`);
165
- }
166
- // 2. Verify claims
167
- const now = Math.floor(Date.now() / 1000);
168
- // Check issuer - accept both Google accounts and Confidential Computing
169
- const validIssuers = [
170
- 'https://accounts.google.com',
171
- 'https://confidentialcomputing.googleapis.com'
172
- ];
173
- if (!validIssuers.includes(payload.iss)) {
174
- errors.push(`Invalid issuer: expected one of ${validIssuers.join(', ')}, got "${payload.iss}"`);
175
- }
176
- // Check expiration
177
- if (payload.exp <= now) {
178
- errors.push(`Token expired: exp=${payload.exp}, now=${now}`);
179
- }
180
- // Check issued at (allow 60 second clock skew)
181
- if (payload.iat > now + 60) {
182
- errors.push(`Token issued in future: iat=${payload.iat}, now=${now}`);
183
- }
184
- // Audience can be:
185
- // 1. Custom Reclaim audience with data param: https://reclaimprotocol.org/attestation?data=tee_k_public_key:0x...
186
- // 2. Reclaim domain only: https://reclaim-protocol.com (address in eat_nonce)
187
- // 3. GCP STS audience: https://sts.googleapis.com (for Confidential Space)
188
- const hasReclaimAudience = payload.aud?.includes('reclaimprotocol.org');
189
- const hasGcpStsAudience = payload.aud?.includes('sts.googleapis.com');
190
- if (!hasReclaimAudience && !hasGcpStsAudience) {
191
- errors.push(`Invalid audience: expected "reclaimprotocol.org" or "sts.googleapis.com", got "${payload.aud}"`);
192
- }
193
- if (errors.length > 0) {
194
- return { isValid: false, errors };
195
- }
196
- // 3. Get public key - either from x5c chain or JWKS
197
- let publicKey;
198
- if (header.x5c && header.x5c.length > 0) {
199
- // PKI token with certificate chain
200
- if (logger) {
201
- logger.info(`Using x5c certificate chain (${header.x5c.length} certificates)`);
202
- }
203
- publicKey = verifyX5cChain(header.x5c, logger);
204
- }
205
- else if (header.kid) {
206
- // OIDC token with kid
207
- if (logger) {
208
- logger.info(`Using OIDC token with kid: ${header.kid}`);
209
- }
210
- // Fetch Google's public keys
211
- const jwks = await fetchGooglePublicKeys(logger);
212
- // Find matching key
213
- const jwk = jwks.keys.find(k => k.kid === header.kid);
214
- if (!jwk) {
215
- errors.push(`No public key found for kid: ${header.kid}`);
216
- return { isValid: false, errors };
217
- }
218
- publicKey = jwkToPublicKey(jwk);
219
- }
220
- else {
221
- errors.push('JWT header must contain either x5c or kid field');
222
- return { isValid: false, errors };
223
- }
224
- // 4. Verify signature
225
- const signedData = `${headerB64}.${payloadB64}`;
226
- const signature = base64urlDecode(signatureB64);
227
- const verify = crypto.createVerify('RSA-SHA256');
228
- verify.update(signedData);
229
- const isSignatureValid = verify.verify(publicKey, signature);
230
- if (!isSignatureValid) {
231
- errors.push('Signature verification failed');
232
- return { isValid: false, errors };
233
- }
234
- if (logger) {
235
- logger.info('GCP JWT signature verified successfully');
236
- }
237
- // 5. Extract ETH address from eat_nonce
238
- if (!payload.eat_nonce) {
239
- errors.push('No eat_nonce field found in JWT payload');
240
- return { isValid: false, errors };
241
- }
242
- // Format: "tee_k_public_key:0x..." or "tee_t_public_key:0x..."
243
- const match = payload.eat_nonce.match(/^(tee_[kt])_public_key:0x([0-9a-fA-F]{40})$/);
244
- if (!match) {
245
- errors.push(`Invalid eat_nonce format: ${payload.eat_nonce}`);
246
- return { isValid: false, errors };
247
- }
248
- const userDataType = match[1]; // "tee_k" or "tee_t"
249
- const hexAddress = match[2];
250
- const ethAddress = new Uint8Array(Buffer.from(hexAddress, 'hex'));
251
- if (logger) {
252
- logger.info(`Extracted address from eat_nonce: ${payload.eat_nonce}`);
253
- }
254
- // Extract image digest from JWT payload (GCP's equivalent to PCR0)
255
- let pcr0 = 'gcp-no-digest';
256
- if (payload.google?.compute_engine?.image_digest) {
257
- pcr0 = payload.google.compute_engine.image_digest;
258
- }
259
- else if (payload.submods?.container?.image_digest) {
260
- pcr0 = payload.submods.container.image_digest;
261
- }
262
- // Add debug prefix if debug mode is enabled
263
- if (payload.dbgstat === 'enabled' && pcr0.startsWith('sha256:')) {
264
- pcr0 = 'debug_' + pcr0;
265
- }
266
- // Extract environment variables if present
267
- const envVars = payload.submods?.container?.env || {};
268
- if (logger) {
269
- const hexAddr = Buffer.from(ethAddress).toString('hex');
270
- logger.info(`Extracted ETH address from GCP attestation: 0x${hexAddr}, type: ${userDataType}, pcr0: ${pcr0}`);
271
- if (Object.keys(envVars).length > 0) {
272
- logger.debug(`Environment variables: ${Object.keys(envVars).join(', ')}`);
273
- }
274
- }
275
- return {
276
- isValid: true,
277
- errors: [],
278
- ethAddress,
279
- userDataType,
280
- pcr0,
281
- envVars
282
- };
283
- }
284
- catch (error) {
285
- const errorMsg = error instanceof Error ? error.message : String(error);
286
- errors.push(`GCP attestation validation error: ${errorMsg}`);
287
- return { isValid: false, errors };
288
- }
289
- }
@@ -1,51 +0,0 @@
1
- import { RPCMessages } from "../../proto/api.js";
2
- import { getEnvVariable } from "../../utils/env.js";
3
- import { AttestorError, strToUint8Array } from "../../utils/index.js";
4
- import { SIGNATURES } from "../../utils/signatures/index.js";
5
- const PRIVATE_KEY = getEnvVariable('PRIVATE_KEY');
6
- /**
7
- * Sign message using the PRIVATE_KEY env var.
8
- */
9
- export function signAsAttestor(data, scheme) {
10
- const { sign } = SIGNATURES[scheme];
11
- return sign(typeof data === 'string' ? strToUint8Array(data) : data, PRIVATE_KEY);
12
- }
13
- /**
14
- * Obtain the address on chain, from the PRIVATE_KEY env var.
15
- */
16
- export function getAttestorAddress(scheme) {
17
- const { getAddress, getPublicKey } = SIGNATURES[scheme];
18
- const publicKey = getPublicKey(PRIVATE_KEY);
19
- return getAddress(publicKey);
20
- }
21
- /**
22
- * Nice parse JSON with a key.
23
- * If the data is empty, returns an empty object.
24
- * And if the JSON is invalid, throws a bad request error,
25
- * with the key in the error message.
26
- */
27
- export function niceParseJsonObject(data, key) {
28
- if (!data) {
29
- return {};
30
- }
31
- try {
32
- return JSON.parse(data);
33
- }
34
- catch (e) {
35
- throw AttestorError.badRequest(`Invalid JSON in ${key}: ${e.message}`);
36
- }
37
- }
38
- /**
39
- * Extract any initial messages sent via the query string,
40
- * in the `messages` parameter.
41
- */
42
- export function getInitialMessagesFromQuery(req) {
43
- const url = new URL(req.url, 'http://localhost');
44
- const messagesB64 = url.searchParams.get('messages');
45
- if (!messagesB64?.length) {
46
- return [];
47
- }
48
- const msgsBytes = Buffer.from(messagesB64, 'base64');
49
- const msgs = RPCMessages.decode(msgsBytes);
50
- return msgs.messages;
51
- }
@@ -1,256 +0,0 @@
1
- const countries = {
2
- AF: 'Afghanistan',
3
- AX: 'Åland Islands',
4
- AL: 'Albania',
5
- DZ: 'Algeria',
6
- AS: 'American Samoa',
7
- AD: 'Andorra',
8
- AO: 'Angola',
9
- AI: 'Anguilla',
10
- AQ: 'Antarctica',
11
- AG: 'Antigua and Barbuda',
12
- AR: 'Argentina',
13
- AM: 'Armenia',
14
- AW: 'Aruba',
15
- AU: 'Australia',
16
- AT: 'Austria',
17
- AZ: 'Azerbaijan',
18
- BS: 'Bahamas',
19
- BH: 'Bahrain',
20
- BD: 'Bangladesh',
21
- BB: 'Barbados',
22
- BY: 'Belarus',
23
- BE: 'Belgium',
24
- BZ: 'Belize',
25
- BJ: 'Benin',
26
- BM: 'Bermuda',
27
- BT: 'Bhutan',
28
- BO: 'Bolivia, Plurinational State of',
29
- BQ: 'Bonaire, Sint Eustatius and Saba',
30
- BA: 'Bosnia and Herzegovina',
31
- BW: 'Botswana',
32
- BV: 'Bouvet Island',
33
- BR: 'Brazil',
34
- IO: 'British Indian Ocean Territory',
35
- BN: 'Brunei Darussalam',
36
- BG: 'Bulgaria',
37
- BF: 'Burkina Faso',
38
- BI: 'Burundi',
39
- KH: 'Cambodia',
40
- CM: 'Cameroon',
41
- CA: 'Canada',
42
- CV: 'Cape Verde',
43
- KY: 'Cayman Islands',
44
- CF: 'Central African Republic',
45
- TD: 'Chad',
46
- CL: 'Chile',
47
- CN: 'China',
48
- CX: 'Christmas Island',
49
- CC: 'Cocos (Keeling) Islands',
50
- CO: 'Colombia',
51
- KM: 'Comoros',
52
- CG: 'Congo',
53
- CD: 'Congo, the Democratic Republic of the',
54
- CK: 'Cook Islands',
55
- CR: 'Costa Rica',
56
- CI: "Côte d'Ivoire",
57
- HR: 'Croatia',
58
- CU: 'Cuba',
59
- CW: 'Curaçao',
60
- CY: 'Cyprus',
61
- CZ: 'Czech Republic',
62
- DK: 'Denmark',
63
- DJ: 'Djibouti',
64
- DM: 'Dominica',
65
- DO: 'Dominican Republic',
66
- EC: 'Ecuador',
67
- EG: 'Egypt',
68
- SV: 'El Salvador',
69
- GQ: 'Equatorial Guinea',
70
- ER: 'Eritrea',
71
- EE: 'Estonia',
72
- ET: 'Ethiopia',
73
- FK: 'Falkland Islands (Malvinas)',
74
- FO: 'Faroe Islands',
75
- FJ: 'Fiji',
76
- FI: 'Finland',
77
- FR: 'France',
78
- GF: 'French Guiana',
79
- PF: 'French Polynesia',
80
- TF: 'French Southern Territories',
81
- GA: 'Gabon',
82
- GM: 'Gambia',
83
- GE: 'Georgia',
84
- DE: 'Germany',
85
- GH: 'Ghana',
86
- GI: 'Gibraltar',
87
- GR: 'Greece',
88
- GL: 'Greenland',
89
- GD: 'Grenada',
90
- GP: 'Guadeloupe',
91
- GU: 'Guam',
92
- GT: 'Guatemala',
93
- GG: 'Guernsey',
94
- GN: 'Guinea',
95
- GW: 'Guinea-Bissau',
96
- GY: 'Guyana',
97
- HT: 'Haiti',
98
- HM: 'Heard Island and McDonald Mcdonald Islands',
99
- VA: 'Holy See (Vatican City State)',
100
- HN: 'Honduras',
101
- HK: 'Hong Kong',
102
- HU: 'Hungary',
103
- IS: 'Iceland',
104
- IN: 'India',
105
- ID: 'Indonesia',
106
- IR: 'Iran, Islamic Republic of',
107
- IQ: 'Iraq',
108
- IE: 'Ireland',
109
- IM: 'Isle of Man',
110
- IL: 'Israel',
111
- IT: 'Italy',
112
- JM: 'Jamaica',
113
- JP: 'Japan',
114
- JE: 'Jersey',
115
- JO: 'Jordan',
116
- KZ: 'Kazakhstan',
117
- KE: 'Kenya',
118
- KI: 'Kiribati',
119
- XK: 'Kosovo',
120
- KP: "Korea, Democratic People's Republic of",
121
- KR: 'Korea, Republic of',
122
- KW: 'Kuwait',
123
- KG: 'Kyrgyzstan',
124
- LA: "Lao People's Democratic Republic",
125
- LV: 'Latvia',
126
- LB: 'Lebanon',
127
- LS: 'Lesotho',
128
- LR: 'Liberia',
129
- LY: 'Libya',
130
- LI: 'Liechtenstein',
131
- LT: 'Lithuania',
132
- LU: 'Luxembourg',
133
- MO: 'Macao',
134
- MK: 'North Macedonia',
135
- MG: 'Madagascar',
136
- MW: 'Malawi',
137
- MY: 'Malaysia',
138
- MV: 'Maldives',
139
- ML: 'Mali',
140
- MT: 'Malta',
141
- MH: 'Marshall Islands',
142
- MQ: 'Martinique',
143
- MR: 'Mauritania',
144
- MU: 'Mauritius',
145
- YT: 'Mayotte',
146
- MX: 'Mexico',
147
- FM: 'Micronesia, Federated States of',
148
- MD: 'Moldova, Republic of',
149
- MC: 'Monaco',
150
- MN: 'Mongolia',
151
- ME: 'Montenegro',
152
- MS: 'Montserrat',
153
- MA: 'Morocco',
154
- MZ: 'Mozambique',
155
- MM: 'Myanmar',
156
- NA: 'Namibia',
157
- NR: 'Nauru',
158
- NP: 'Nepal',
159
- NL: 'Netherlands',
160
- AN: 'Netherlands Antilles',
161
- NC: 'New Caledonia',
162
- NZ: 'New Zealand',
163
- NI: 'Nicaragua',
164
- NE: 'Niger',
165
- NG: 'Nigeria',
166
- NU: 'Niue',
167
- NF: 'Norfolk Island',
168
- MP: 'Northern Mariana Islands',
169
- NO: 'Norway',
170
- OM: 'Oman',
171
- PK: 'Pakistan',
172
- PW: 'Palau',
173
- PS: 'Palestine, State of',
174
- PA: 'Panama',
175
- PG: 'Papua New Guinea',
176
- PY: 'Paraguay',
177
- PE: 'Peru',
178
- PH: 'Philippines',
179
- PN: 'Pitcairn',
180
- PL: 'Poland',
181
- PT: 'Portugal',
182
- PR: 'Puerto Rico',
183
- QA: 'Qatar',
184
- RE: 'Réunion',
185
- RO: 'Romania',
186
- RU: 'Russian Federation',
187
- RW: 'Rwanda',
188
- BL: 'Saint Barthélemy',
189
- SH: 'Saint Helena, Ascension and Tristan da Cunha',
190
- KN: 'Saint Kitts and Nevis',
191
- LC: 'Saint Lucia',
192
- MF: 'Saint Martin (French part)',
193
- PM: 'Saint Pierre and Miquelon',
194
- VC: 'Saint Vincent and the Grenadines',
195
- WS: 'Samoa',
196
- SM: 'San Marino',
197
- ST: 'Sao Tome and Principe',
198
- SA: 'Saudi Arabia',
199
- SN: 'Senegal',
200
- RS: 'Serbia',
201
- SC: 'Seychelles',
202
- SL: 'Sierra Leone',
203
- SG: 'Singapore',
204
- SX: 'Sint Maarten (Dutch part)',
205
- SK: 'Slovakia',
206
- SI: 'Slovenia',
207
- SB: 'Solomon Islands',
208
- SO: 'Somalia',
209
- ZA: 'South Africa',
210
- GS: 'South Georgia and the South Sandwich Islands',
211
- SS: 'South Sudan',
212
- ES: 'Spain',
213
- LK: 'Sri Lanka',
214
- SD: 'Sudan',
215
- SR: 'Suriname',
216
- SJ: 'Svalbard and Jan Mayen',
217
- SZ: 'Swaziland',
218
- SE: 'Sweden',
219
- CH: 'Switzerland',
220
- SY: 'Syrian Arab Republic',
221
- TW: 'Taiwan, Province of China',
222
- TJ: 'Tajikistan',
223
- TZ: 'Tanzania, United Republic of',
224
- TH: 'Thailand',
225
- TL: 'Timor-Leste',
226
- TG: 'Togo',
227
- TK: 'Tokelau',
228
- TO: 'Tonga',
229
- TT: 'Trinidad and Tobago',
230
- TN: 'Tunisia',
231
- TR: 'Turkey',
232
- TM: 'Turkmenistan',
233
- TC: 'Turks and Caicos Islands',
234
- TV: 'Tuvalu',
235
- UG: 'Uganda',
236
- UA: 'Ukraine',
237
- AE: 'United Arab Emirates',
238
- GB: 'United Kingdom',
239
- US: 'United States',
240
- UM: 'United States Minor Outlying Islands',
241
- UY: 'Uruguay',
242
- UZ: 'Uzbekistan',
243
- VU: 'Vanuatu',
244
- VE: 'Venezuela, Bolivarian Republic of',
245
- VN: 'Viet Nam',
246
- VG: 'Virgin Islands, British',
247
- VI: 'Virgin Islands, U.S.',
248
- WF: 'Wallis and Futuna',
249
- EH: 'Western Sahara',
250
- YE: 'Yemen',
251
- ZM: 'Zambia',
252
- ZW: 'Zimbabwe'
253
- };
254
- export function isValidCountryCode(countryCode) {
255
- return (countryCode.toUpperCase() in countries);
256
- }
@@ -1,38 +0,0 @@
1
- import { MAX_NO_DATA_INTERVAL_MS, PING_INTERVAL_MS } from "../../config/index.js";
2
- /**
3
- * Adds a keep-alive mechanism to the WebSocket
4
- * client
5
- */
6
- export function addKeepAlive(ws, logger) {
7
- let sendTimeout;
8
- let killTimeout;
9
- ws.on('message', () => {
10
- logger.trace('data recv, resetting timer');
11
- resetTimer();
12
- });
13
- ws.on('pong', () => {
14
- logger.trace('pong received, resetting timer');
15
- resetTimer();
16
- });
17
- ws.on('error', cleanup);
18
- ws.on('close', cleanup);
19
- function resetTimer() {
20
- cleanup();
21
- resetSendTimeout();
22
- killTimeout = setTimeout(() => {
23
- logger.warn('no data received in a while, closing connection');
24
- ws.close();
25
- }, MAX_NO_DATA_INTERVAL_MS);
26
- }
27
- function resetSendTimeout() {
28
- // reset ping
29
- sendTimeout = setTimeout(() => {
30
- ws.ping();
31
- resetSendTimeout();
32
- }, PING_INTERVAL_MS);
33
- }
34
- function cleanup() {
35
- clearTimeout(killTimeout);
36
- clearTimeout(sendTimeout);
37
- }
38
- }