forked from mirrors/amnezia-wg-easy
wip
This commit is contained in:
parent
18b6ce7c78
commit
9d355f67d8
12 changed files with 60 additions and 37 deletions
|
@ -31,8 +31,14 @@ module.exports = class Server {
|
|||
|
||||
// Authentication
|
||||
.get('/api/session', Util.promisify(async req => {
|
||||
const requiresPassword = !!process.env.PASSWORD;
|
||||
const authenticated = requiresPassword
|
||||
? !!(req.session && req.session.authenticated)
|
||||
: true;
|
||||
|
||||
return {
|
||||
authenticated: !!(req.session && req.session.authenticated),
|
||||
requiresPassword,
|
||||
authenticated,
|
||||
};
|
||||
}))
|
||||
.post('/api/session', Util.promisify(async req => {
|
||||
|
@ -55,7 +61,19 @@ module.exports = class Server {
|
|||
}))
|
||||
|
||||
// WireGuard
|
||||
.use(Util.requireSession)
|
||||
.use((req, res, next) => {
|
||||
if (!PASSWORD) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (req.session && req.session.authenticated) {
|
||||
return next();
|
||||
}
|
||||
|
||||
return res.status(401).json({
|
||||
error: 'Not Logged In',
|
||||
});
|
||||
})
|
||||
.delete('/api/session', Util.promisify(async req => {
|
||||
const sessionId = req.session.id;
|
||||
|
||||
|
|
|
@ -4,16 +4,6 @@ const childProcess = require('child_process');
|
|||
|
||||
module.exports = class Util {
|
||||
|
||||
static requireSession(req, res, next) {
|
||||
if (req.session && req.session.authenticated) {
|
||||
return next();
|
||||
}
|
||||
|
||||
return res.status(401).json({
|
||||
error: 'Not Logged In',
|
||||
});
|
||||
}
|
||||
|
||||
static promisify(fn) {
|
||||
return function(req, res) {
|
||||
Promise.resolve().then(async () => fn(req, res))
|
||||
|
@ -53,7 +43,7 @@ module.exports = class Util {
|
|||
return new Promise((resolve, reject) => {
|
||||
childProcess.exec(cmd, (err, stdout) => {
|
||||
if (err) return reject(err);
|
||||
return resolve(stdout);
|
||||
return resolve(String(stdout).trim());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -34,8 +34,11 @@ module.exports = class WireGuard {
|
|||
},
|
||||
clients: {},
|
||||
};
|
||||
await this.saveConfig();
|
||||
}
|
||||
|
||||
await Util.exec('wg-quick up wg0');
|
||||
|
||||
return config;
|
||||
});
|
||||
}
|
||||
|
@ -99,7 +102,7 @@ AllowedIPs = ${client.address}/32`;
|
|||
const [
|
||||
publicKey,
|
||||
preSharedKey, // eslint-disable-line no-unused-vars
|
||||
endpoint,
|
||||
endpoint, // eslint-disable-line no-unused-vars
|
||||
allowedIps, // eslint-disable-line no-unused-vars
|
||||
latestHandshakeAt,
|
||||
transferRx,
|
||||
|
@ -110,9 +113,6 @@ AllowedIPs = ${client.address}/32`;
|
|||
const client = clients.find(client => client.publicKey === publicKey);
|
||||
if (!client) return;
|
||||
|
||||
client.endpoint = endpoint === '(none)'
|
||||
? null
|
||||
: endpoint;
|
||||
client.latestHandshakeAt = latestHandshakeAt === '0'
|
||||
? null
|
||||
: new Date(Number(`${latestHandshakeAt}000`));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue