elvish_config/aliases/ips.elv

168 lines
4.2 KiB
Plaintext

edit:add-var add-badips~ {|@argv|
use str
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/map
fn add {|f cmp ips t|
var @cips = (cat $f)
var m c = (map:to-set $cips) 0
each {|e|
if (not (has-key $m $e)) {
set m[$e] = $nil
set @cips = $@cips $e
set c = (+ $c 1)
echo $e ajoutée
}
} $ips
if (> $c 0) {
set @cips = (list:sort $cmp $cips)
echo (str:join "\n" $cips) > $f
}
printf "%d %s ajoutées (%d au total)\n" $c $t (count $cips)
}
var ip4 ip6 = (list:filter $ip:is-ip~ $argv | list:unzip {|_ ip| ip:is-ipv4 $ip})
if (list:is-not-empty $ip4) {
add $E:HOME/ips/bad_ips.txt $ip:comp4~ $ip4 IPv4s
}
if (list:is-not-empty $ip6) {
add $E:HOME/ips/bad_ipsv6.txt $ip:comp6~ $ip6 IPv6s
}
}
edit:add-var ip-info~ {|@argv|
use framagit.org/benjamin.vaudour/elv-lib/mods/ip
var token = (cat $E:HOME/.config/elvish/private/ipinfo_token)
fn url-of {|ip|
printf 'ipinfo.io/%s?token=%s' $ip $token
}
fn info {|ip|
if (ip:is-ip $ip) {
var url = (url-of $ip)
printf "URL: %s\n" $url
e:curlie $url
}
}
each $info~ $argv
}
edit:add-var ip-of~ {|@argv|
use re
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
}
} | re:awk {|_ 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
}
edit:add-var ip-public~ {|@argv|
use framagit.org/benjamin.vaudour/elv-lib/mods/common
var f = {|t|
var l = (common:cond (eq $t -6) 'IPv6: ' 'IPv4: ')
e:curl $t ifconfig.co/ip 2> /dev/null | each {|ip| echo (styled $l bold yellow) $ip}
}
$f -4
$f -6
}