Skip to content

Instantly share code, notes, and snippets.

@meanother
Created July 26, 2024 07:22
Show Gist options
  • Save meanother/452f03770c7dfd3cb745a664c15bc850 to your computer and use it in GitHub Desktop.
Save meanother/452f03770c7dfd3cb745a664c15bc850 to your computer and use it in GitHub Desktop.
New index.js with fix create new users
const wgService = require("./wgService");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require('cors');
const config = require("./config");
const asyncHandler = require('express-async-handler');
const fs = require('fs');
const path = require("path");
const archiver = require('archiver');
const app = express();
const filesDir = path.join(__dirname, "files");
app.set("trust proxy", true);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
console.log(req.headers);
console.log(req.body);
next();
});
app.post('/api/v1/addUser', asyncHandler(async (req, res) => {
const apiKey = req.headers["x-api-key"];
if (apiKey !== config.apiKey) {
return res.json({ success: false, message: "Invalid API key" });
}
const name = req.headers["name"];
await wgService.auth();
await wgService.addClient(name);
const allClients = await wgService.clientList();
let myClient;
for (const client of allClients) {
if (client.name === name) {
myClient = client;
break;
}
}
const configuration = await wgService.configuration(myClient.id);
const zipFile = path.join(filesDir, `${name}.zip`);
try {
fs.mkdirSync(filesDir);
} catch (e) {
}
const output = fs.createWriteStream(zipFile);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
output.on('close', function () {
return res.sendFile(zipFile);
});
archive.on('error', function (err) {
return res.status(500).json({ success: false });
});
archive.pipe(output);
archive.append(configuration, { name: `${name}.conf` });
archive.finalize();
}));
app.get('/api/v1/getArchive', asyncHandler(async (req, res) => {
const name = req.query["id"];
await wgService.auth();
const allClients = await wgService.clientList();
let myClient = null;
for (let client of allClients) {
if (client.name === name) {
myClient = client;
break;
}
}
if (myClient == null) {
return res.json({ status: 'failed', info: `cant find user with name: ${name}` });
}
const configuration = await wgService.configuration(myClient.id);
const zipFile = path.join(filesDir, `${name}.zip`);
try {
fs.mkdirSync(filesDir);
} catch (e) {
}
fs.access(zipFile, (err) => {
if (!err) {
const output = fs.createWriteStream(zipFile);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
output.on('close', function () {
return res.sendFile(zipFile);
});
archive.on('error', function (err) {
return res.status(500).json({ success: false });
});
archive.pipe(output);
archive.append(configuration, { name: `${name}.conf` });
archive.finalize();
} else {
return res.json({ status: 'failed', info: `cant send configuration via name: ${name} , user not found`, error: err });
}
})
}));
app.post('/api/v1/deleteUser', asyncHandler(async (req, res) => {
const apiKey = req.headers["x-api-key"];
if (apiKey !== config.apiKey) {
return res.json({ success: false, message: "Invalid API key" });
}
const name = req.headers["name"];
await wgService.auth();
const allClients = await wgService.clientList();
let myClient = null;
for (const client of allClients) {
if (client.name === name) {
myClient = client;
break;
}
}
if (!myClient) {
return res.json({ success: true });
}
await wgService.deleteClient(myClient.id);
return res.json({ success: true });
}));
app.listen(config.port, () => {
console.log(`Running a server at http://localhost:${config.port}`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment