feat: hotspot PIX MikroTik + backend Docker + CI Gitea Actions
build-image / build-push (push) Failing after 4m36s
build-image / build-push (push) Failing after 4m36s
This commit is contained in:
commit
de9bd4ae0b
15 files changed
+1720
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import express from 'express';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import config from './config.js';
|
||||
import { initDb, getDb, saveDb, genCode } from './db.js';
|
||||
import { initMp, createPixPayment, getPaymentStatus } from './mp.js';
|
||||
import { createHotspotUser } from './ros.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, '../public')));
|
||||
|
||||
initDb();
|
||||
initMp();
|
||||
|
||||
async function fulfill(payment) {
|
||||
if (payment.voucher) return payment;
|
||||
const user = 'G' + genCode(6);
|
||||
const pass = genCode(8);
|
||||
const plan = getDb().plans.find((p) => p.id === payment.planId);
|
||||
await createHotspotUser(user, pass, plan?.profile || payment.profile, plan?.uptime);
|
||||
payment.voucher = { user, pass };
|
||||
payment.status = 'paid';
|
||||
payment.paidAt = new Date().toISOString();
|
||||
saveDb();
|
||||
console.log(`[PAGA] ${payment.planId} -> voucher ${user}`);
|
||||
return payment;
|
||||
}
|
||||
|
||||
async function checkPayment(payment) {
|
||||
if (!payment.mpId || payment.status !== 'pending') return payment;
|
||||
try {
|
||||
const st = await getPaymentStatus(payment.mpId);
|
||||
if (st === 'approved') return await fulfill(payment);
|
||||
if (st === 'rejected' || st === 'cancelled' || st === 'expired') {
|
||||
payment.status = st;
|
||||
saveDb();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[MP] erro ao consultar:', e.message);
|
||||
}
|
||||
return payment;
|
||||
}
|
||||
|
||||
// ===== Rotas =====
|
||||
|
||||
app.get('/api/plans', (req, res) => {
|
||||
res.json(getDb().plans.filter((p) => p.active));
|
||||
});
|
||||
|
||||
app.post('/api/checkout', async (req, res) => {
|
||||
try {
|
||||
const { planId, mac } = req.body || {};
|
||||
const plan = getDb().plans.find((p) => p.id === planId && p.active);
|
||||
if (!plan) return res.status(400).json({ error: 'plano invalido' });
|
||||
|
||||
const payment = {
|
||||
id: 'P' + Date.now() + genCode(4),
|
||||
planId: plan.id,
|
||||
profile: plan.profile,
|
||||
mac: mac || null,
|
||||
price: plan.price,
|
||||
status: 'pending',
|
||||
mpId: null,
|
||||
voucher: null,
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const pix = await createPixPayment(plan.price, payment.id, `WiFi ${plan.name}`);
|
||||
payment.mpId = pix.mpId;
|
||||
payment.qrCodeBase64 = pix.qrCodeBase64;
|
||||
payment.copiaCola = pix.copiaCola;
|
||||
getDb().payments.push(payment);
|
||||
saveDb();
|
||||
|
||||
res.json({
|
||||
paymentId: payment.id,
|
||||
qrCodeBase64: pix.qrCodeBase64,
|
||||
copiaCola: pix.copiaCola,
|
||||
price: plan.price,
|
||||
planName: plan.name,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('[CHECKOUT]', e.message);
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/status/:id', async (req, res) => {
|
||||
const payment = getDb().payments.find((p) => p.id === req.params.id);
|
||||
if (!payment) return res.status(404).json({ error: 'nao encontrado' });
|
||||
await checkPayment(payment);
|
||||
res.json({
|
||||
status: payment.status,
|
||||
voucher: payment.voucher,
|
||||
planName: getDb().plans.find((p) => p.id === payment.planId)?.name,
|
||||
});
|
||||
});
|
||||
|
||||
function adminAuth(req, res, next) {
|
||||
const token = req.headers['x-admin-token'] || req.query.token;
|
||||
if (token !== config.adminToken) return res.status(401).json({ error: 'token invalido' });
|
||||
next();
|
||||
}
|
||||
|
||||
app.get('/api/admin/payments', adminAuth, (req, res) => {
|
||||
res.json(getDb().payments.slice(-100).reverse());
|
||||
});
|
||||
|
||||
app.post('/api/admin/plans', adminAuth, (req, res) => {
|
||||
const { plans } = req.body || {};
|
||||
if (!Array.isArray(plans)) return res.status(400).json({ error: 'plans[] obrigatorio' });
|
||||
getDb().plans = plans;
|
||||
saveDb();
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.get('/api/health', async (req, res) => {
|
||||
res.json({ ok: true, mp: !!config.mpAccessToken && !config.mpAccessToken.includes('COLE_SEU'), time: new Date().toISOString() });
|
||||
});
|
||||
|
||||
// ===== Polling: confirma pagamentos pendentes sozinho =====
|
||||
setInterval(async () => {
|
||||
const pendings = getDb().payments.filter((p) => p.status === 'pending' && p.mpId);
|
||||
for (const p of pendings) await checkPayment(p);
|
||||
}, 15000);
|
||||
|
||||
app.listen(config.port, '0.0.0.0', () => {
|
||||
console.log(`pix-hotspot rodando em ${config.baseUrl} (porta ${config.port})`);
|
||||
console.log(`MikroTik: ${config.mikrotik.host}:${config.mikrotik.port}`);
|
||||
});
|
||||
Reference in new issue
Block a user