Fixed found_ipvx filters

master
wgapi Cloud9 2021-10-15 14:10:39 -06:00
parent a19b239969
commit 9cce98e737
1 changed files with 13 additions and 12 deletions

View File

@ -38,7 +38,7 @@ app.get('/', async (req,res) => {
const privkey = req.query['del'] const privkey = req.query['del']
const hostname = req.query['add'] const hostname = req.query['add']
//const requester = req.ip.replace('::ffff:','') //const requester = req.ip.replace('::ffff:','')
const requester = '10.4.9.1' const requester = '10.4.1.1'
// Delete a peer // Delete a peer
if (privkey) { if (privkey) {
@ -79,8 +79,8 @@ app.get('/', async (req,res) => {
} }
else if (line.includes('AllowedIPs')) { else if (line.includes('AllowedIPs')) {
const ips = line.split('=')[1].split(',') const ips = line.split('=')[1].split(',')
found_ipv4s.push(ips.filter( (ip) => ip.includes(IPV4_NET) )) found_ipv4s.push(ips.filter( (ip) => ip.includes(IPV4_NET) )[0].trim())
found_ipv6s.push(ips.filter( (ip) => ip.includes(IPV6_NET) )) found_ipv6s.push(ips.filter( (ip) => ip.includes(IPV6_NET) )[0].trim())
} }
} }
} }
@ -90,25 +90,26 @@ app.get('/', async (req,res) => {
console.log(`Found unmatching IPv4 address subnets for ${requester}: ${found_ipv4s}`) console.log(`Found unmatching IPv4 address subnets for ${requester}: ${found_ipv4s}`)
res.sendStatus(500); return res.sendStatus(500); return
} else if (!found_ipv6s.every((found_ipv6) => } else if (!found_ipv6s.every((found_ipv6) =>
found_ipv6.toString().includes(`${IPV6_NET}:${subnet}:`))) { found_ipv6.toString().includes(`${IPV6_NET}:${subnet}:`))) {
console.log(`Found unmatching IPv6 address subnets for ${requester}: ${found_ipv6s}`) console.log(`Found unmatching IPv6 address subnets for ${requester}: ${found_ipv6s}`)
res.sendStatus(500); return res.sendStatus(500); return
// Check that the requester is "on the list" // Check that the requester is "on the list"
} else if (found_usernames.length ===0) { // Should never get here because this requester can't access this IP!
console.log(`Requester ${requester} is not in the network yet`) } else if (found_usernames.length ===0) {
console.log(`Received request from ${requester} not in wg.conf!`)
res.sendStatus(500); return res.sendStatus(500); return
// Check that all usernames are correct or error out // Check that all usernames are correct or error out
// https://stackoverflow.com/a/35568895 // https://stackoverflow.com/a/35568895
} else if (!found_usernames.every( (v,i,r) => v === r[0] )) { } else if (!found_usernames.every( (v,i,r) => v === r[0] )) {
console.log(`Found unmatching usernames for ${requester}: ${found_usernames.toString()}`) console.log(`Found unmatching usernames for ${requester}: ${found_usernames.toString()}`)
res.sendStatus(500); return res.sendStatus(500); return
// Everything looks good! Proceed // Everything looks good! Proceed
} else { } else {
const username = found_usernames[0] const username = found_usernames[0]
console.log(`${requester} must be ${username}`) console.log(`${requester} must be ${username}`)
// Find next available host part in config // Find next available host part in config
const used_ipv4_hosts = found_ipv4s.map((found_ipv4) => const used_ipv4_hosts = found_ipv4s.map((found_ipv4) =>
found_ipv4.toString().split('.')[3].split('/')[0]) found_ipv4.toString().split('.')[3].split('/')[0])