Skip to content

Commit 2fc46db

Browse files
committed
feat: cache tls cert
1 parent 07e6573 commit 2fc46db

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

electron/main/sge/sge.login.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { hashPassword, isProblemResponse } from './sge.utils';
1919

2020
const logger = createLogger('sge:login');
2121

22+
// As of November 2023, the login server's self-signed certificate
23+
// is valid until Nov 16, 3017. We'll cache it in memory for performance.
24+
let cachedTlsCertificate: tls.PeerCertificate | undefined;
25+
2226
/**
2327
* SGE stands for Simutronics Game Entry
2428
* https://www.play.net/dr/play/sge-info.asp
@@ -158,8 +162,7 @@ async function connect(
158162

159163
const { host, port } = mergedOptions;
160164

161-
logger.info('downloading login server certificate', { host, port });
162-
const certToTrust = await downloadCertificate(mergedOptions);
165+
const certToTrust = await getTrustedTlsCertificate(mergedOptions);
163166

164167
mergedOptions = merge(
165168
mergedOptions,
@@ -200,6 +203,26 @@ async function connect(
200203
return socket;
201204
}
202205

206+
/**
207+
* Gets the play.net login server's self-signed certificate.
208+
* Use this anytime we connect to the SGE server to get or send customer data.
209+
*/
210+
async function getTrustedTlsCertificate(
211+
connectOptions: tls.ConnectionOptions
212+
): Promise<tls.PeerCertificate> {
213+
const { host, port } = connectOptions;
214+
215+
if (cachedTlsCertificate) {
216+
logger.info('using cached login server certificate', { host, port });
217+
return cachedTlsCertificate;
218+
}
219+
220+
logger.info('downloading login server certificate', { host, port });
221+
cachedTlsCertificate = await downloadCertificate(connectOptions);
222+
223+
return cachedTlsCertificate;
224+
}
225+
203226
/**
204227
* Authenticate to login server.
205228
*/

0 commit comments

Comments
 (0)