-
Notifications
You must be signed in to change notification settings - Fork 4
/
geoip-shell-detect-lan.sh
320 lines (259 loc) · 8.38 KB
/
geoip-shell-detect-lan.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/sh
# shellcheck disable=SC2154,SC2086,SC1090
# geoip-shell-detect-lan.sh
# Copyright: antonk (antonk.d3v@gmail.com)
# github.com/friendly-bits
# Detects local area ipv4 and ipv6 subnets
# Only use on a machine which has no dedicated WAN interfaces (physical or logical)
# otherwise WAN subnet may be wrongly detected as LAN subnet
# outputs aggregated subnets
# to output only aggregated subnets (and no other text), run with the '-s' argument
# to only check a specific family (inet or inet6), run with the '-f <family>' argument
# This is a customized version of detect-local-subnets-AIO.sh script found here:
# github.com/friendly-bits/subnet-tools
#### Initial setup
p_name="geoip-shell"
script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)
geoinit="${p_name}-geoinit.sh"
for geoinit_path in "$script_dir/$geoinit" "/usr/bin/$geoinit"; do
[ -f "$geoinit_path" ] && break
done
. "$geoinit_path" || exit 1
## Simple args parsing
for arg in "$@"; do
case "$arg" in
-s ) subnets_only=1 ;;
-f ) families_arg=check ;;
* ) case "$families_arg" in check) families_arg="$arg"; esac
esac
done
[ "$families_arg" = check ] && die "Specify family with '-f'."
setdebug
debugentermsg
## Functions
# 1 - mask bits
# 2 - ip length in bytes
generate_mask() {
maskbits="$1"
ip_len_bytes="$2"
bytes_done='' i='' sum=0 cur=128
octets=$((maskbits / 8))
frac=$((maskbits % 8))
while true; do
case ${#bytes_done} in "$octets") break; esac
case $((${#bytes_done}%chunk_len_bytes==0)) in 1) printf ' 0x'; esac
printf %s "ff"
bytes_done="${bytes_done}1"
done
[ ${#bytes_done} != $ip_len_bytes ] && {
while true; do
case ${#i} in "$frac") break; esac
sum=$((sum + cur))
cur=$((cur / 2))
i="${i}1"
done
case "$((${#bytes_done}%chunk_len_bytes))" in 0) printf ' 0x'; esac
printf "%02x" "$sum" || { printf '%s\n' "generate_mask: Error: failed to convert byte '$sum' to hex." >&2; return 1; }
bytes_done="${bytes_done}1"
while true; do
case ${#bytes_done} in "$ip_len_bytes") break; esac
case "$((${#bytes_done}%chunk_len_bytes))" in 0) printf ' 0x'; esac
printf %s "00"
bytes_done="${bytes_done}1"
done
}
}
# 1 - ip
# 2 - family
ip_to_hex() {
ip="$1"; family="$2"
case "$family" in
inet ) chunk_delim='.'; hex_flag='' ;;
inet6 )
chunk_delim=':'; hex_flag='0x'
# expand ::
case "$ip" in *::*)
zeroes=":0:0:0:0:0:0:0:0:0"
ip_tmp="$ip"
while true; do
case "$ip_tmp" in *:*) ip_tmp="${ip_tmp#*:}";; *) break; esac
zeroes="${zeroes#??}"
done
# replace '::'
ip="${ip%::*}$zeroes${ip##*::}"
# prepend 0 if we start with :
case "$ip" in :*) ip="0${ip}"; esac
esac
esac
IFS="$chunk_delim"
for chunk in $ip; do
printf " 0x%0${chunk_len_chars}x" "$hex_flag$chunk"
done
}
# 1 - input hex chunks
# 2 - family
# 3 - var name for output
hex_to_ip() {
family="$2"; out_var="$3"
ip="$(IFS=' ' printf "%$_fmt_id$_fmt_delim" $1)" || { echo "hex_to_ip: Error: failed to convert hex to ip." >&2; return 1; }
case "$family" in inet6 )
## compress ipv6
case "$ip" in :* ) ;; *) ip=":$ip"; esac
# compress 0's across neighbor chunks
for zeroes in ":0:0:0:0:0:0:0:0" ":0:0:0:0:0:0:0" ":0:0:0:0:0:0" ":0:0:0:0:0" ":0:0:0:0" ":0:0:0" ":0:0"; do
case "$ip" in *$zeroes* )
ip="${ip%%"$zeroes"*}::${ip#*"$zeroes"}"
break
esac
done
# trim leading colon if it's not a double colon
case "$ip" in
::::*) ip="${ip#::}" ;;
:::*) ip="${ip#:}" ;;
::*) ;;
:*) ip="${ip#:}"
esac
esac
eval "$out_var"='${ip%$_fmt_delim}'
}
# 1- family
get_local_subnets() {
family="$1"
unset res_subnets res_ips
case "$family" in
inet) ip_len_bits=32; chunk_len_bits=8; _fmt_id='d'; _fmt_delim='.' ;;
inet6) ip_len_bits=128; chunk_len_bits=16; _fmt_id='x'; _fmt_delim=':' ;;
*) printf '%s\n' "get_local_subnets: invalid family '$family'." >&2; return 1
esac
ip_len_bytes=$((ip_len_bits/8))
chunk_len_bytes=$((chunk_len_bits/8))
chunk_len_chars=$((chunk_len_bytes*2))
subnets_hex="$(
if [ "$family" = inet ]; then
ip -f inet route show table local scope link |
grep -v "[[:space:]]lo[[:space:]]" | grep -oE "dev[[:space:]]+[^[:space:]]+" | sed 's/^dev[[:space:]]*//g' | sort -u |
while read -r iface; do
ip -o -f inet addr show "$iface" | grep -oE "$subnet_regex_ipv4"
done
else
ip -o -f inet6 addr show | grep -oE "inet6[[:space:]]+(fd[0-9a-f]{0,2}:|fe80:)(([[:alnum:]:/])+)" | grep -oE "$subnet_regex_ipv6$"
fi |
while read -r subnet; do
printf %s "${subnet#*/}/"
ip_to_hex "${subnet%%/*}" "$family"
printf '\n'
done | sort -n
)"
[ -z "$subnets_hex" ] &&
{ printf '%s\n' "get_local_subnets(): Failed to detect local subnets for family $family." >&2; return 1; }
subnets_hex="$subnets_hex$_nl"
while true; do
case "$subnets_hex" in ''|"$_nl") break; esac
## trim the 1st (largest) subnet on the list to its mask bits
# get the first subnet from the list
IFS_OLD="$IFS"; IFS="$_nl"
set -- $subnets_hex
subnet1_hex="$1"
# remove current subnet from the list
shift 1
subnets_hex="$*$_nl"
IFS="$IFS_OLD"
# debugprint "processing subnet: $subnet1_hex"
# get mask bits
maskbits="${subnet1_hex%/*}"
# chop off mask bits
ip_hex="${subnet1_hex#*/}"
# generate mask if it's not been generated yet
eval "mask=\"\$mask_${family}_${maskbits}\""
[ ! "$mask" ] && {
mask="$(generate_mask "$maskbits" "$ip_len_bytes")" || return 1
eval "mask_${family}_${maskbits}=\"$mask\""
}
# calculate ip & mask
ip1_hex="$(
# copy ~ $maskbits bits
IFS=' '; chunks_done=''; bits_done=0
for ip_chunk in $ip_hex; do
[ $((bits_done + chunk_len_bits < maskbits)) = 0 ] && break
printf ' %s' "$ip_chunk"
bits_done=$((bits_done + chunk_len_bits))
chunks_done="${chunks_done}1"
done
# calculate the next chunk if needed
[ $bits_done != $maskbits ] && {
set -- $mask
chunks_done="${chunks_done}1"
eval "mask_chunk=\"\${${#chunks_done}}\""
printf " 0x%0${chunk_len_chars}x" $(( ip_chunk & mask_chunk ))
bits_done=$((bits_done + chunk_len_bits))
}
# repeat 00 for every missing byte
while [ $bits_done != $ip_len_bits ]; do
[ $((bits_done%chunk_len_bits)) = 0 ] && printf ' 0x'
printf %s "00"
bits_done=$((bits_done + 8))
done
)"
# debugprint "calculated '$ip_hex' & '$mask' = '$ip1_hex'"
# format from hex number back to ip
hex_to_ip "$ip1_hex" "$family" "res_ip"
# append mask bits and add current subnet to resulting list
res_subnets="${res_subnets}${res_ip}/${maskbits}${_nl}"
res_ips="${res_ips}${res_ip}${_nl}"
IFS="$_nl"
# iterate over all remaining subnets
for subnet2_hex in $subnets_hex; do
# chop off mask bits
ip2_hex="${subnet2_hex#*/}"
bytes_diff=0; bits_done=0; chunks_done=
# compare ~ $maskbits bits of ip1 and ip2
IFS=' '
for ip1_chunk in $ip1_hex; do
[ $((bits_done + chunk_len_bits < maskbits)) = 0 ] && break
bits_done=$((bits_done + chunk_len_bits))
chunks_done="${chunks_done}1"
set -- $ip2_hex
eval "ip2_chunk=\"\${${#chunks_done}}\""
bytes_diff=$((ip1_chunk - ip2_chunk))
# if there is any difference, no need to calculate further
[ "$bytes_diff" != 0 ] && break
done
# if needed, calculate the next ip2 chunk and compare to ip1 chunk
[ "$bits_done" = "$maskbits" ] || [ "$bytes_diff" != 0 ] && continue
chunks_done="${chunks_done}1"
set -- $ip2_hex
eval "ip2_chunk=\"\${${#chunks_done}}\""
set -- $mask
eval "mask_chunk=\"\${${#chunks_done}}\""
bytes_diff=$((ip1_chunk - (ip2_chunk & mask_chunk) ))
# if no differences found, subnet2 is encapsulated in subnet1 - remove subnet2 from the list
[ "$bytes_diff" = 0 ] && subnets_hex="${subnets_hex%%"$subnet2_hex$_nl"*}${subnets_hex#*"$subnet2_hex$_nl"}"
done
IFS="$IFS_OLD"
done
validate_ip "${res_ips%"$_nl"}" "$family" ||
{ echo "get_local_subnets: Error: failed to validate one or more of output addresses." >&2; return 1; }
[ ! "$subnets_only" ] && printf '%s\n' "Local $family subnets (aggregated):"
case "$res_subnets" in
'') [ ! "$subnets_only" ] && echo "None found." ;;
*) printf %s "$res_subnets"
esac
[ ! "$subnets_only" ] && echo
:
}
## Main
families=
tolower families_arg
for f in $families_arg; do
case "$f" in
inet|ipv4) add2list families inet ;;
inet6|ipv6) add2list families inet6 ;;
*) die "Invalid family '$f'."
esac
done
: "${families:="inet inet6"}"
rv_gl=0
for family in $families; do
get_local_subnets "$family" || rv_gl=1
done
exit $rv_gl