elvish_config/aliases/ips.elv

168 lines
4.2 KiB
Plaintext
Raw Normal View History

2024-06-16 10:18:51 +00:00
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
2021-08-24 18:45:25 +00:00
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
2021-08-24 18:45:25 +00:00
2021-12-11 21:09:02 +00:00
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
2021-08-24 18:45:25 +00:00
} elif (== $i1 1) {
ip:comp6 $e1 $e2
2021-08-24 18:45:25 +00:00
} else {
ip:comp4 $e1 $e2
2021-08-24 18:45:25 +00:00
}
}
2021-12-11 21:09:02 +00:00
fn dnssolve {|d|
var result = [
2021-08-24 18:45:25 +00:00
&ips=[&]
&resolvers=''
&txt=''
]
2021-12-11 21:09:02 +00:00
e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach {|l|
2021-09-01 13:55:25 +00:00
var ll = $l
var idx = (num:++ (str:index $ll :))
var value = (str:trim-space $ll[$idx..])
2021-09-01 13:55:25 +00:00
if (or (str:has-prefix $ll 'IPv4 addresses') (str:has-prefix $ll 'IPv6 addresses')) {
2021-12-11 21:09:02 +00:00
str:split ', ' $value | peach {|e|
2021-08-28 08:58:07 +00:00
var ee = $e
if (ip:is-ip $ee) {
2021-10-13 18:43:40 +00:00
set result[ips][$ee] = $nil
2021-08-24 18:45:25 +00:00
}
}
2021-09-01 13:55:25 +00:00
} elif (str:has-prefix $ll 'TXT records') {
2021-10-13 18:43:40 +00:00
set result[txt] = $value
2021-09-01 13:55:25 +00:00
} elif (str:has-prefix $ll 'Resolver') {
2021-10-13 18:43:40 +00:00
set result[resolvers] = $value
2021-09-01 13:55:25 +00:00
} elif (str:has-prefix $ll 'Name servers') {
2021-10-13 18:43:40 +00:00
set result[remote] = (not-eq $value 'name does not exist')
2021-08-24 18:45:25 +00:00
}
}
put $result
}
var hosts = [&]
2021-12-11 21:09:02 +00:00
cat /etc/hosts | peach {|l|
2021-08-28 08:58:07 +00:00
var ll = (str:trim-space $l)
if (and (not-eq $ll '') (not-eq $ll[0] '#')) {
put $ll
2021-08-24 18:45:25 +00:00
}
} | re:awk {|_ ip @domains|
2021-12-11 21:09:02 +00:00
peach {|d|
2021-08-28 08:58:07 +00:00
var dd = $d
if (has-key $hosts $dd) {
2021-10-13 18:43:40 +00:00
set hosts[$dd][$ip] = $nil
2021-08-24 18:45:25 +00:00
} else {
2021-10-13 18:43:40 +00:00
set hosts[$dd] = [&$ip=$nil]
2021-08-24 18:45:25 +00:00
}
} $domains
}
var remote = [&]
2021-12-11 21:09:02 +00:00
peach {|d|
2021-08-28 08:58:07 +00:00
var dd = $d
var solve = (dnssolve $dd)
if (> (count $solve[ips]) 0) {
2021-10-13 18:43:40 +00:00
set remote[$dd] = $solve
2021-08-24 18:45:25 +00:00
}
} $argv
2021-12-11 21:09:02 +00:00
each {|d|
var exists = $false
echo (styled (printf "Resolving '%s'…\n" $d) bright-green)
2021-08-24 18:45:25 +00:00
if (has-key $hosts $d) {
2021-10-13 18:43:40 +00:00
set exists = $true
2021-08-24 18:45:25 +00:00
echo (styled ' local:' bright-yellow)
keys $hosts[$d] | list:sort $sort-ip~ | each {|ip|
printf " - %s\n" $ip
2021-08-24 18:45:25 +00:00
}
}
if (and (has-key $remote $d) $remote[$d][remote]) {
2021-10-13 18:43:40 +00:00
set exists = $true
var info = $remote[$d]
2021-08-24 18:45:25 +00:00
echo (styled ' remote:' bright-yellow)
echo ' IPs:'
keys $info[ips] | list:sort $sort-ip~ | each {|ip|
printf " - %s\n" $ip
2021-08-24 18:45:25 +00:00
}
printf " Resolvers: %s\n" $info[resolvers]
printf " TXT records: %s\n" $info[txt]
2021-08-24 18:45:25 +00:00
}
if (not $exists) {
echo (styled 'No info found' bright-red)
}
echo
} $argv
}
2024-06-16 10:18:51 +00:00
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
}