mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 16:56:28 +00:00
108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
const WebSocket = require('ws');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const winston = require('../../../helpers/winston.logger');
|
|
const generate = require('../utils/utils');
|
|
const { requestHsm, HSM_TIMEOUT_MS } = require('./requestHsm');
|
|
require('dotenv').config();
|
|
|
|
const certDirectory = path.join(__dirname, '../../../assets/cert');
|
|
const ssl_ca_crt = path.join(certDirectory, 'CACerts.pem');
|
|
const ssl_client_pfx = path.join(certDirectory, 'FeliqClient0002-03.pfx');
|
|
const options = {
|
|
ca: fs.readFileSync(ssl_ca_crt),
|
|
agent: false,
|
|
passphrase: 'GPqs+Uia78',
|
|
pfx: fs.readFileSync(ssl_client_pfx),
|
|
};
|
|
|
|
const RECONNECT_DELAY_MS = 5900;
|
|
|
|
// Kelola satu koneksi, satu upaya autentikasi, dan satu jadwal pengganti per HSM.
|
|
function createHsmClient(serverNumber, authRetryDelayMs) {
|
|
const backup = process.env.CHANGE_TO_DRC?.toUpperCase() === 'YES' ? '_BACKUP' : '';
|
|
const suffix = `${backup}_${serverNumber}`;
|
|
const url = process.env[`URL_HSM_SERVER${suffix}`];
|
|
const apiKey = process.env[`APIKEY_HSM_SERVER${suffix}`];
|
|
const secret = process.env[`SECRETKEY_HSM_SERVER${suffix}`];
|
|
const systemCode = process.env.SYSTEM_CODE;
|
|
const client = {
|
|
ws: null,
|
|
clientAuthenticated: false,
|
|
sendData(data) {
|
|
this.ws.send(data);
|
|
},
|
|
};
|
|
|
|
if (!url || !apiKey || !secret || !systemCode) {
|
|
winston.logger.error(`HSM server ${serverNumber} configuration is incomplete`);
|
|
return client;
|
|
}
|
|
|
|
const authData = Buffer.concat([
|
|
Buffer.from(generate.hexToBytes(systemCode)),
|
|
Buffer.from(apiKey),
|
|
Buffer.from(secret),
|
|
]);
|
|
const authPacket = generate.makeRequestData(Buffer.alloc(2), Buffer.alloc(1), authData);
|
|
|
|
function scheduleReconnect() {
|
|
setTimeout(connect, RECONNECT_DELAY_MS).unref();
|
|
}
|
|
|
|
function connect() {
|
|
let socket;
|
|
try {
|
|
socket = new WebSocket(url, [], { ...options, handshakeTimeout: HSM_TIMEOUT_MS });
|
|
} catch {
|
|
winston.logger.warn(`HSM server ${serverNumber} could not open a WebSocket`);
|
|
scheduleReconnect();
|
|
return;
|
|
}
|
|
|
|
client.ws = socket;
|
|
let authRetry;
|
|
|
|
async function authenticate() {
|
|
try {
|
|
const response = await requestHsm(socket, authPacket);
|
|
if (client.ws !== socket || socket.readyState !== WebSocket.OPEN) return;
|
|
|
|
const code = generate.getResponseCode(response);
|
|
if (code === '0000' || code === '0502') {
|
|
client.clientAuthenticated = true;
|
|
return;
|
|
}
|
|
winston.logger.warn(`HSM server ${serverNumber} rejected authentication`);
|
|
} catch (error) {
|
|
if (client.ws !== socket || socket.readyState !== WebSocket.OPEN) return;
|
|
winston.logger.warn(`HSM server ${serverNumber} authentication failed`);
|
|
if (error.code === 'HSM_TIMEOUT') {
|
|
socket.terminate();
|
|
return;
|
|
}
|
|
}
|
|
authRetry = setTimeout(authenticate, authRetryDelayMs);
|
|
authRetry.unref();
|
|
}
|
|
|
|
socket.once('open', authenticate);
|
|
socket.on('error', () => {
|
|
client.clientAuthenticated = false;
|
|
winston.logger.warn(`HSM server ${serverNumber} WebSocket error`);
|
|
});
|
|
socket.once('close', () => {
|
|
clearTimeout(authRetry);
|
|
socket.off('open', authenticate);
|
|
client.clientAuthenticated = false;
|
|
client.ws = null;
|
|
scheduleReconnect();
|
|
});
|
|
}
|
|
|
|
connect();
|
|
return client;
|
|
}
|
|
|
|
module.exports = createHsmClient;
|