elvish_config/aliases/ipof.elv

102 lines
2.7 KiB
Plaintext

edit:add-var ipof~ {|@argv|
use str
use framagit.org/benjamin.vaudour/elv-lib/mods/common
use framagit.org/benjamin.vaudour/elv-lib/mods/ip
use framagit.org/benjamin.vaudour/elv-lib/mods/list
use framagit.org/benjamin.vaudour/elv-lib/mods/num
fn sort-ip {|e1 e2|
var check = {|e| common:cexec (ip:is-ipv4 $e) 0 { common:cond (ip:is-ipv6 $e) 1 2 }}
var i1 = ($check $e1)
var i2 = ($check $e2)
if (or (!= $i1 $i2) (== $i1 2)) {
compare $i1 $i2
} elif (== $i1 1) {
ip:comp6 $e1 $e2
} else {
ip:comp4 $e1 $e2
}
}
fn dnssolve {|d|
var result = [
&ips=[&]
&resolvers=''
&txt=''
]
e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach {|l|
var ll = $l
var idx = (num:++ (str:index $ll :))
var value = (str:trim-space $ll[$idx..])
if (or (str:has-prefix $ll 'IPv4 addresses') (str:has-prefix $ll 'IPv6 addresses')) {
str:split ', ' $value | peach {|e|
var ee = $e
if (ip:is-ip $ee) {
set result[ips][$ee] = $nil
}
}
} elif (str:has-prefix $ll 'TXT records') {
set result[txt] = $value
} elif (str:has-prefix $ll 'Resolver') {
set result[resolvers] = $value
} elif (str:has-prefix $ll 'Name servers') {
set result[remote] = (not-eq $value 'name does not exist')
}
}
put $result
}
var hosts = [&]
cat /etc/hosts | peach {|l|
var ll = (str:trim-space $l)
if (and (not-eq $ll '') (not-eq $ll[0] '#')) {
put $ll
}
} | eawk {|_ ip @domains|
peach {|d|
var dd = $d
if (has-key $hosts $dd) {
set hosts[$dd][$ip] = $nil
} else {
set hosts[$dd] = [&$ip=$nil]
}
} $domains
}
var remote = [&]
peach {|d|
var dd = $d
var solve = (dnssolve $dd)
if (> (count $solve[ips]) 0) {
set remote[$dd] = $solve
}
} $argv
each {|d|
var exists = $false
echo (styled (printf "Resolving '%s'…\n" $d) bright-green)
if (has-key $hosts $d) {
set exists = $true
echo (styled ' local:' bright-yellow)
keys $hosts[$d] | list:sort $sort-ip~ | each {|ip|
printf " - %s\n" $ip
}
}
if (and (has-key $remote $d) $remote[$d][remote]) {
set exists = $true
var info = $remote[$d]
echo (styled ' remote:' bright-yellow)
echo ' IPs:'
keys $info[ips] | list:sort $sort-ip~ | each {|ip|
printf " - %s\n" $ip
}
printf " Resolvers: %s\n" $info[resolvers]
printf " TXT records: %s\n" $info[txt]
}
if (not $exists) {
echo (styled 'No info found' bright-red)
}
echo
} $argv
}