-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a21dba
commit 91d14f1
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
let data; | ||
let githubNetworks = []; | ||
|
||
class IPSubnet { | ||
constructor(cidr, service) { | ||
this.cidr = cidr; | ||
this.service = service; | ||
const [network, prefixLength] = cidr.split('/'); | ||
this.network = network; | ||
this.prefixLength = parseInt(prefixLength, 10); | ||
this.isIPv6 = network.includes(':'); | ||
} | ||
|
||
parseIPv4(ip) { | ||
return ip.split('.').map(Number); | ||
} | ||
|
||
parseIPv6(ip) { | ||
const parts = ip.split(':'); | ||
const fullParts = []; | ||
for (let part of parts) { | ||
if (part === '') { | ||
fullParts.push(...Array(8 - parts.length + 1).fill('0000')); | ||
} else { | ||
fullParts.push(part.padStart(4, '0')); | ||
} | ||
} | ||
return fullParts.join(''); | ||
} | ||
|
||
isInSubnet(ip) { | ||
if (this.isIPv6) { | ||
const subnetBinary = this.parseIPv6(this.network); | ||
const ipBinary = this.parseIPv6(ip); | ||
return subnetBinary.slice(0, this.prefixLength) === ipBinary.slice(0, this.prefixLength); | ||
} else { | ||
const subnetParts = this.parseIPv4(this.network); | ||
const ipParts = this.parseIPv4(ip); | ||
const mask = -1 << (32 - this.prefixLength); | ||
const subnetInt = subnetParts.reduce((acc, part) => (acc << 8) + part, 0); | ||
const ipInt = ipParts.reduce((acc, part) => (acc << 8) + part, 0); | ||
return (subnetInt & mask) === (ipInt & mask); | ||
} | ||
} | ||
} | ||
|
||
async function fetchJSON(url) { | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
data = await response.json(); // Assign the fetched JSON to the global data variable | ||
} catch (error) { | ||
console.error('Error fetching JSON:', error); | ||
} | ||
} | ||
|
||
const url = 'https://api.github.com/meta'; | ||
fetchJSON(url).then(() => { | ||
if (data) { | ||
console.log('Fetched JSON'); | ||
} | ||
}); | ||
|
||
ignored_keys = ["verifiable_password_authentication", "ssh_key_fingerprints", "ssh_keys", "domains"]; | ||
for (const key in data){ | ||
if(!ignored_keys.includes(key)){ | ||
networks = data[key] | ||
console.log(`${key}`) | ||
for(network in networks){ | ||
//console.log(networks[network]) | ||
networkObject = new IPSubnet(networks[network],key) | ||
githubNetworks.push(networkObject) | ||
} | ||
} | ||
} |