Browse Source

get store by id or name

pull/468/head
tm-sanjay 4 years ago
parent
commit
707cd58656
  1. 29
      src/lib/Server.js
  2. 26
      src/lib/WireGuard.js

29
src/lib/Server.js

@ -108,6 +108,35 @@ module.exports = class Server {
res.header('Content-Type', 'text/plain'); res.header('Content-Type', 'text/plain');
res.send(config); res.send(config);
})) }))
//get client by storeId
.get('/api/wireguard/client/storeId/:storeId', Util.promisify(async (req, res) => {
const { storeId } = req.params;
debug(storeId);
const clientId = await WireGuard.getClientIdByStoreId({ storeId });
const config = await WireGuard.getClientConfiguration({ clientId: clientId });
const configName = storeId
.replace(/[^a-zA-Z0-9_=+.-]/g, '-')
.replace(/(-{2,}|-$)/g, '-')
.replace(/-$/, '')
.substring(0, 32);
res.header('Content-Disposition', `attachment; filename="${configName || clientId}.conf"`);
res.header('Content-Type', 'text/plain');
res.send(config);
}))
//get client by name //!!! only for old windows API
.get('/api/wireguard/client/:name', Util.promisify(async (req, res) => {
const { name } = req.params;
const clientId = await WireGuard.getClientIdByName({ name });
const config = await WireGuard.getClientConfiguration({ clientId: clientId });
const configName = name
.replace(/[^a-zA-Z0-9_=+.-]/g, '-')
.replace(/(-{2,}|-$)/g, '-')
.replace(/-$/, '')
.substring(0, 32);
res.header('Content-Disposition', `attachment; filename="${configName || clientId}.conf"`);
res.header('Content-Type', 'text/plain');
res.send(config);
}))
.post('/api/wireguard/client', Util.promisify(async req => { .post('/api/wireguard/client', Util.promisify(async req => {
const { name , storeId } = req.body; const { name , storeId } = req.body;
if (name === undefined) { if (name === undefined) {

26
src/lib/WireGuard.js

@ -186,12 +186,36 @@ AllowedIPs = ${client.address}/32`;
const config = await this.getConfig(); const config = await this.getConfig();
const client = config.clients[clientId]; const client = config.clients[clientId];
if (!client) { if (!client) {
throw new ServerError(`Client Not Found: ${clientId}`, 404); throw new ServerError(`Client Not Found2: ${clientId}`, 404);
} }
return client; return client;
} }
//get client by storeId
async getClientIdByStoreId({ storeId }) {
//get all clients
const clients = await this.getClients();
//find client by storeId
const client = clients.find(client => client.storeId === storeId);
if (!client) {
throw new ServerError(`Client Not Found3: ${storeId}`, 404);
}
return client.id;
}
//get client by name
async getClientIdByName({ name }) {
//get all clients
const clients = await this.getClients();
//find client by name
const client = clients.find(client => client.name === name);
if (!client) {
throw new ServerError(`Client Not Found: ${name}`, 404);
}
return client.id;
}
//get local ip //get local ip
static async getLocalIP() { static async getLocalIP() {
var ip = await Util.exec('hostname -I'); var ip = await Util.exec('hostname -I');

Loading…
Cancel
Save