You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.7 KiB

const fetch = require('cross-fetch'),
// relays = require('../relays.yaml'),
fs = require('fs'),
YAML = require('yaml')
let object,
yaml,
result,
file = fs.readFileSync('./relays.yaml', 'utf8')
2 years ago
const getDns = async function(relay){
let dns
await fetch(`https://1.1.1.1/dns-query?name=${relay.replace('wss://', '')}`, { headers: { 'accept': 'application/dns-json' } })
.then(response => response.json())
2 years ago
.then((data) => { dns = data.Answer ? data.Answer : false })
.catch(err => console.log('./scripts/geo.js', err))
2 years ago
return dns
}
const getIp = async function(dns){
let ip;
if(dns)
ip = dns[dns.length-1].data
return ip
}
const getGeo = async function(ip) {
let geo
await fetch(`http://ip-api.com/json/${ip}`, { headers: { 'accept': 'application/dns-json' } })
2 years ago
.then(response => response.json())
.then((data) => { geo = data })
.catch(err => console.error('./scripts/geo.js', err))
2 years ago
return geo;
}
const query = async function(){
2 years ago
const relays = YAML.parse(file).relays,
result = {}
for (const relay of relays) {
2 years ago
let dns, ip, geo
dns = await getDns(relay)
ip = await getIp(dns)
// console.log(dns, ip)
geo = await getGeo(ip)
2 years ago
// console.log(geo, ip, dns)
2 years ago
if(geo && dns)
2 years ago
geo.dns = dns[dns.length-1]
if(geo && geo.status == 'success')
result[relay] = geo
}
return result
}
const run = async function(){
result = await query()
object = { geo: result }
yaml = new YAML.Document()
yaml.contents = object
// console.log(object)
fs.writeFile('./geo.yaml', yaml.toString(), (err) => {
if (err) return console.error('./scripts/geo.js', err);
});
}
run()