elvish_config/aliases/ip-of.elv

103 lines
2.4 KiB
Plaintext

#alias:new ip-of e:is-ip
fn ip-of [@_args]{
use str
use moi/util/condition
use moi/util/ip
use moi/util/list
fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 }
fn sort-ip [e1 e2]{
local:i1 = (condition:set (ip:is-ipv4 $e1) 0 (condition:set (ip:is-ipv6 $e1) 1 2))
local:i2 = (condition:set (ip:is-ipv4 $e2) 0 (condition:set (ip:is-ipv6 $e2) 1 2))
if (!= $i1 $i2) {
< $i1 $i2
} elif (== $i1 2) {
<s $e1 $e2
} elif (== $i1 1) {
less $ip:cmp6~ $e1 $e2
} else {
less $ip:cmp4~ $e1 $e2
}
}
fn dnssolve [d]{
local:result = [
&ips=[&]
&resolvers=''
&txt=''
]
e:dnscrypt-proxy --resolve $d | peach [l]{
if (has-prefix $l 'IP addresses:') {
local:ips = (str:trim-space (str:trim-prefix $l 'IP addresses:'))
splits ', ' $ips | peach [e]{
local:e = $e
if (ip:is-ip $e) {
result[ips][$e] = $nil
}
}
} elif (has-prefix $l 'TXT records:') {
result[txt] = (str:trim-space (str:trim-prefix $l 'TXT records:'))
} elif (has-prefix $l 'Resolver IP:') {
result[resolvers] = (str:trim-space (str:trim-prefix $l 'Resolver IP:'))
}
}
put $result
}
local:hosts = [&]
cat /etc/hosts | peach [l]{
local:l = $l
l = (str:trim-space $l)
if (and (not-eq $l '') (not-eq $l[0] '#')) {
put $l
}
} | eawk [_ ip @domains]{
peach [d]{
local:d = $d
if (has-key $hosts $d) {
hosts[$d][$ip] = $nil
} else {
hosts[$d] = [&$ip=$nil]
}
} $domains
}
local:remote = [&]
peach [d]{
local:d = $d
local:solve = (dnssolve $d)
if (> (keys $solve[ips] | count) 0) {
remote[$d] = $solve
}
} $_args
each [d]{
local:exists = $false
echo (styled 'Resolving '$d'…' bright-green)
echo
if (has-key $hosts $d) {
exists = $true
echo (styled ' local:' bright-yellow)
keys $hosts[$d] | list:sort $sort-ip~ | each [ip]{
echo ' - '$ip
}
}
if (has-key $remote $d) {
exists = $true
local:info = $remote[$d]
echo (styled ' remote:' bright-yellow)
echo ' IPs:'
keys $info[ips] | list:sort $sort-ip~ | each [ip]{
echo ' - '$ip
}
echo ' Resolvers: '$info[resolvers]
echo ' TXT records: '$info[txt]
}
if (not $exists) {
echo (styled 'No info found' bright-red)
}
echo
} $_args
}