From 2fc46db9a344aff2320fa07ab0281c4410ee96b8 Mon Sep 17 00:00:00 2001 From: KatoakDR <68095633+KatoakDR@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:10:43 -0500 Subject: [PATCH] feat: cache tls cert --- electron/main/sge/sge.login.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/electron/main/sge/sge.login.ts b/electron/main/sge/sge.login.ts index d297c114..18352e54 100644 --- a/electron/main/sge/sge.login.ts +++ b/electron/main/sge/sge.login.ts @@ -19,6 +19,10 @@ import { hashPassword, isProblemResponse } from './sge.utils'; const logger = createLogger('sge:login'); +// As of November 2023, the login server's self-signed certificate +// is valid until Nov 16, 3017. We'll cache it in memory for performance. +let cachedTlsCertificate: tls.PeerCertificate | undefined; + /** * SGE stands for Simutronics Game Entry * https://www.play.net/dr/play/sge-info.asp @@ -158,8 +162,7 @@ async function connect( const { host, port } = mergedOptions; - logger.info('downloading login server certificate', { host, port }); - const certToTrust = await downloadCertificate(mergedOptions); + const certToTrust = await getTrustedTlsCertificate(mergedOptions); mergedOptions = merge( mergedOptions, @@ -200,6 +203,26 @@ async function connect( return socket; } +/** + * Gets the play.net login server's self-signed certificate. + * Use this anytime we connect to the SGE server to get or send customer data. + */ +async function getTrustedTlsCertificate( + connectOptions: tls.ConnectionOptions +): Promise { + const { host, port } = connectOptions; + + if (cachedTlsCertificate) { + logger.info('using cached login server certificate', { host, port }); + return cachedTlsCertificate; + } + + logger.info('downloading login server certificate', { host, port }); + cachedTlsCertificate = await downloadCertificate(connectOptions); + + return cachedTlsCertificate; +} + /** * Authenticate to login server. */