From 7f135c80b1c7edf790f0349e331d43f3d6ebdc54 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 22 Aug 2022 12:22:54 -0400 Subject: [PATCH] *Use promise-retry to make a best effort attempt at downloading the ssl cert and key in-case we have portal rate limits or downtime --- package.json | 1 + src/relay.ts | 23 +++++++++++++++++++---- yarn.lock | 23 ++++++++++++++++++----- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index fbce89d..086a076 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "node-cache": "^5.1.2", "node-cron": "^3.0.1", "node-fetch": "^3.2.6", + "promise-retry": "^2.0.1", "random-access-memory": "^4.1.0", "random-key": "^0.3.2", "sprintf-js": "^1.1.2", diff --git a/src/relay.ts b/src/relay.ts index 2eb38ac..08b49c3 100644 --- a/src/relay.ts +++ b/src/relay.ts @@ -26,6 +26,8 @@ import log from "loglevel"; import { AddressInfo } from "net"; import { sprintf } from "sprintf-js"; import { dynImport } from "./util.js"; +// @ts-ignore +import promiseRetry from "promise-retry"; let sslCtx: tls.SecureContext = tls.createSecureContext(); const sslParams: tls.SecureContextOptions = { cert: "", key: "" }; @@ -109,13 +111,26 @@ export async function start() { } async function setupSSl(bootup: boolean) { - let sslCert = await getSslCert(); - let sslKey = await getSslKey(); + let sslCert: IndependentFileSmall | boolean = false; + let sslKey: IndependentFileSmall | boolean = false; let certInfo; let exists = false; let domainValid = false; let dateValid = false; let configDomain = config.str("domain"); + await promiseRetry(async (retry: any) => { + sslCert = await getSslCert(); + if (!sslCert) { + retry(); + } + }); + + await promiseRetry(async (retry: any) => { + sslKey = await getSslCert(); + if (!sslKey) { + retry(); + } + }); if (sslCert && sslKey) { sslParams.cert = Buffer.from((sslCert as IndependentFileSmall).fileData); @@ -154,8 +169,8 @@ async function setupSSl(bootup: boolean) { } await createOrRenewSSl( - sslCert as IndependentFileSmall, - sslKey as IndependentFileSmall + sslCert as unknown as IndependentFileSmall, + sslKey as unknown as IndependentFileSmall ); } diff --git a/yarn.lock b/yarn.lock index f7e1e8d..2793d70 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3286,6 +3286,11 @@ engine.io-parser@~5.0.3: resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-5.0.4.tgz#0b13f704fa9271b3ec4f33112410d8f3f41d0fc0" integrity sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg== +err-code@^2.0.2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9" + integrity sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA== + err-code@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/err-code/-/err-code-3.0.1.tgz#a444c7b992705f2b120ee320b09972eef331c920" @@ -5804,7 +5809,7 @@ multicast-dns@^7.2.0: dns-packet "^5.2.2" thunky "^1.0.2" -multiformats@^9.0.0, multiformats@^9.0.2, multiformats@^9.0.4, multiformats@^9.1.0, multiformats@^9.4.2, multiformats@^9.4.5, multiformats@^9.4.7, multiformats@^9.5.1, multiformats@^9.5.4, multiformats@^9.6.3, multiformats@^9.6.4, multiformats@^9.7.1: +multiformats@^9.0.0, multiformats@^9.0.2, multiformats@^9.0.4, multiformats@^9.1.0, multiformats@^9.4.2, multiformats@^9.4.5, multiformats@^9.4.7, multiformats@^9.5.1, multiformats@^9.5.4, multiformats@^9.6.3, multiformats@^9.6.4: version "9.7.1" resolved "https://registry.yarnpkg.com/multiformats/-/multiformats-9.7.1.tgz#ab348e5fd6f8e7fb3fd56033211bda48854e2173" integrity sha512-TaVmGEBt0fhxiNJMGphBfB+oGvUxFs8KgGvgl8d3C+GWtrFcvXdJ2196eg+dYhmSFClmgFfSfJEklo+SZzdNuw== @@ -6365,6 +6370,14 @@ progress@^2.0.3: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== +promise-retry@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-2.0.1.tgz#ff747a13620ab57ba688f5fc67855410c370da22" + integrity sha512-y+WKFlBR8BGXnsNlIHFGPZmyDf3DFMoLhaflAnyZgV6rG6xu+JwesTo2Q9R6XwYmtmwAFCkAk3e35jEdoeh/3g== + dependencies: + err-code "^2.0.2" + retry "^0.12.0" + promise-timeout@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/promise-timeout/-/promise-timeout-1.3.0.tgz#d1c78dd50a607d5f0a5207410252a3a0914e1014" @@ -7608,10 +7621,10 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== -typescript@4.7.0-beta: - version "4.7.0-beta" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.0-beta.tgz#15952f24d4177479ca3d19f09436ad8c69a30563" - integrity sha512-m+CNL8lzHyHDxYYDTI+pm5hw5/bufYVGan2bokPyJY/y9C/4W/PCWMtYZ0vV9fLXbEL57elMeoaz9Evxs8UQ+A== +typescript@^4.7.4: + version "4.7.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235" + integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ== udx-native@^1.1.0: version "1.2.1"