Main does now wait till provider finished loading
- Provider disable method now takes complete provider instance instead - Handle when no provider is available - Remove debug messages
This commit is contained in:
@@ -108,7 +108,10 @@ async function run() {
|
||||
}
|
||||
|
||||
providerManager = new ProviderManager(resolve('./src/helper/providers'));
|
||||
providerManager.scan();
|
||||
await providerManager.scan().catch(err => {
|
||||
logger.error(err);
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
invoiceManager = new InvoiceManager();
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ export async function loginUser(req: Request, res: Response) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if 2FA is turned on (the attack doesn't know yet if the password is wrong)
|
||||
// Check if 2FA is turned on (the attacker doesn't know if the password is wrong yet)
|
||||
if (user.twoFASecret != undefined) {
|
||||
if (twoFA == undefined) {
|
||||
res.status(401).send({ message: "2FA code is required." });
|
||||
@@ -135,6 +135,10 @@ export async function MW_User(req: LibrePayRequest, res: Response, next: () => v
|
||||
}
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
if (err === "jwt expired") {
|
||||
res.status(401).send({ message: "Your token expired" });
|
||||
return;
|
||||
}
|
||||
res.status(500).send({ message: "We failed validating your token for some reason." });
|
||||
logger.error(err);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,12 @@ export class InvoiceManager {
|
||||
if (invoice.status === PaymentStatus.PENDING) { this.pendingInvoices.push(invoice); }
|
||||
if (invoice.status === PaymentStatus.UNCONFIRMED) { this.unconfirmedTranscations.push(invoice); }
|
||||
|
||||
try {
|
||||
providerManager.getProvider(invoice.paymentMethod).validateInvoice(invoice);
|
||||
} catch (err) {
|
||||
logger.debug(`Cannot validate invoice ${invoice.id} because there is no provider for ${invoice.paymentMethod}. Remove ...`);
|
||||
this.removeInvoice(invoice);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -162,6 +167,13 @@ export class InvoiceManager {
|
||||
this.unconfirmedTranscations.forEach(async invoice => {
|
||||
const transcation = invoice.transcationHash;
|
||||
|
||||
if (providerManager.getProvider(invoice.paymentMethod) === undefined) {
|
||||
logger.debug(`Cannot get confirmations of invoice ${invoice.id} because there is no provider for ${invoice.paymentMethod}. Remove ...`);
|
||||
this.removeInvoice(invoice);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
const provider = providerManager.getProvider(invoice.paymentMethod);
|
||||
const tx = await provider.getTransaction(transcation);
|
||||
this.setConfirmationCount(invoice, tx.confirmations);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { readdirSync } from 'fs';
|
||||
import { readdir } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { config } from '../../config';
|
||||
import { invoiceManager, logger, providerManager } from '../app';
|
||||
@@ -22,25 +22,24 @@ export class ProviderManager {
|
||||
/**
|
||||
* Scan & load all found providers
|
||||
*/
|
||||
scan() {
|
||||
const getDirectories = () =>
|
||||
readdirSync(this.providerFilePath, { withFileTypes: true })
|
||||
.filter(dirent => dirent.name.endsWith('.ts'))
|
||||
.map(dirent => dirent.name)
|
||||
|
||||
getDirectories().forEach(async file => {
|
||||
async scan() {
|
||||
return new Promise<void>(async (resolve, reject) => {
|
||||
await readdir(this.providerFilePath, { withFileTypes: true }, async (err, files) => {
|
||||
const directories = files.filter(dirent => dirent.name.endsWith('.ts'))
|
||||
.map(dirent => dirent.name);
|
||||
for (let i = 0; i < directories.length; i++) {
|
||||
const file = directories[i];
|
||||
const absolutePath = join(this.providerFilePath, file);
|
||||
const providerModule = require(absolutePath);
|
||||
const provider = new providerModule.Provider() as BackendProvider;
|
||||
|
||||
provider.CRYPTO.forEach(crypto => {
|
||||
if (this.cryptoProvider.has(crypto)) {
|
||||
logger.warn(`Provider ${provider.NAME} will be ignored since there is already another provider active for ${provider.CRYPTO}!`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.warn(`Provider ${provider.NAME} will not be activated for ${provider.CRYPTO} since there is already another provider in place!`);
|
||||
} else {
|
||||
this.cryptoProvider.set(crypto, provider);
|
||||
config.payment.methods.push(crypto);
|
||||
}
|
||||
});
|
||||
|
||||
// Execute onEnable() function of this provider
|
||||
@@ -49,21 +48,30 @@ export class ProviderManager {
|
||||
logger.info(`Loaded provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) for ${provider.CRYPTO.join(', ')}`);
|
||||
} catch (err) {
|
||||
logger.error(`Provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) failed to start: ${err}`);
|
||||
this.disable(provider.NAME);
|
||||
this.disable(provider);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.cryptoProvider.size === 0) {
|
||||
reject('No providers were initialized!');
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This provider will be no longer be used.
|
||||
*/
|
||||
disable(name: string) {
|
||||
this.cryptoProvider.forEach(provider => {
|
||||
if (provider.NAME === name) {
|
||||
disable(provider: BackendProvider) {
|
||||
this.cryptoProvider.forEach(cryptoProvider => {
|
||||
if (provider === cryptoProvider) {
|
||||
// Disable all coins that are supported by this provider.
|
||||
provider.CRYPTO.forEach(crypto => {
|
||||
this.cryptoProvider.delete(crypto);
|
||||
config.payment.methods.splice(config.payment.methods.indexOf(crypto), 1);
|
||||
});
|
||||
logger.warning(`Provider "${provider.NAME}" is now disabled.`);
|
||||
}
|
||||
|
||||
@@ -101,7 +101,6 @@ export class Provider implements BackendProvider {
|
||||
|
||||
const paymentTransaction = message.result.transfer;
|
||||
if (paymentTransaction === undefined) {
|
||||
console.log(message)
|
||||
logger.warning(`Tried to get transfer by txid but failed: ${message}`);
|
||||
resolve(null);
|
||||
return;
|
||||
@@ -175,8 +174,6 @@ export class Provider implements BackendProvider {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(payment_id, message);
|
||||
|
||||
// The payment has not been made yet
|
||||
if (message.result.payments === undefined) {
|
||||
resolve(null);
|
||||
|
||||
Reference in New Issue
Block a user