1
1
#! /bin/bash
2
2
3
+ # TODO: showing version or git commit on running
3
4
VERSION=0.6.0
4
5
PROGNAME=" $( basename $0 ) "
5
6
@@ -100,10 +101,11 @@ Options:
100
101
101
102
Instance managing:
102
103
--daemon Run in background
103
- -- list-running Show running instances
104
- --lc, --list-clients <id>
104
+ -l, -- list-running Show running instances
105
+ --lc, --list-clients <id|interface >
105
106
List clients of an instance. Or list neighbors of
106
- any interface, even if it isn't handled by us
107
+ an interface, even if it isn't handled by us.
108
+ (passive mode)
107
109
--stop <id> Stop a running instance
108
110
For <id> you can use PID or subnet interface name.
109
111
You can get them with '--list-running'
@@ -118,7 +120,6 @@ Examples:
118
120
$PROGNAME -i eth1
119
121
$PROGNAME --ap wlan0 MyAccessPoint
120
122
$PROGNAME --ap wlan0 MyAccessPoint -p MyPassPhrase
121
- $PROGNAME -n --ap wlan0 MyAccessPoint -p MyPassPhrase
122
123
$PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy>
123
124
EOF
124
125
}
@@ -440,7 +441,7 @@ parse_user_options(){
440
441
STOP_ID=" $1 "
441
442
shift
442
443
;;
443
- --list-running)
444
+ -l|- -list-running)
444
445
shift
445
446
LIST_RUNNING=1
446
447
;;
@@ -960,6 +961,7 @@ unallow_dhcp() {
960
961
fi
961
962
}
962
963
964
+ # TODO: use 'DNAT' instead of '--to-ports' to support other IP
963
965
start_redsocks () {
964
966
echo
965
967
echo " iptables: transparent proxy non-LAN TCP/UDP traffic to port ${TP_PORT} "
@@ -1026,7 +1028,7 @@ backup_ipv6_bits() {
1026
1028
" /proc/sys/net/ipv6/conf/$SUBNET_IFACE /accept_ra" \
1027
1029
" /proc/sys/net/ipv6/conf/$SUBNET_IFACE /use_tempaddr" \
1028
1030
" /proc/sys/net/ipv6/conf/$SUBNET_IFACE /addr_gen_mode" \
1029
- " $CONFDIR /sys_6_conf_iface/" || die " Failed backking up interface ipv6 bits"
1031
+ " $CONFDIR /sys_6_conf_iface/" || die " Failed backing up interface ipv6 bits"
1030
1032
1031
1033
if [[ " $SHARE_METHOD " == ' redsocks' ]] ; then
1032
1034
cp " /proc/sys/net/ipv6/conf/$SUBNET_IFACE /forwarding" \
@@ -1066,10 +1068,10 @@ backup_interface_status() {
1066
1068
(ip link show ${SUBNET_IFACE} | grep -q " state UP" ) && SUBNET_IFACE_ORIGINAL_UP_STATUS=1
1067
1069
1068
1070
# save interface old mac
1069
- if [[ -n " $NEW_MACADDR " ]]; then
1071
+ # if [[ -n "$NEW_MACADDR" ]]; then
1070
1072
OLD_MACADDR=$( get_interface_mac $SUBNET_IFACE )
1071
- echo " Saved ${SUBNET_IFACE} old MAC address ${OLD_MACADDR} into RAM"
1072
- fi
1073
+ # echo "Saved ${SUBNET_IFACE} old MAC address ${OLD_MACADDR} into RAM"
1074
+ # fi
1073
1075
1074
1076
backup_ipv6_bits
1075
1077
@@ -1084,9 +1086,9 @@ restore_interface_status() {
1084
1086
1085
1087
restore_ipv6_bits
1086
1088
1087
- if [[ -n " $OLD_MACADDR " ]] ; then
1089
+ if [[ -n " $OLD_MACADDR " && " $( get_interface_mac $SUBNET_IFACE ) " != " $OLD_MACADDR " ]] ; then
1088
1090
echo " Restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} ..."
1089
- set_interface_mac ${SUBNET_IFACE} ${OLD_MACADDR} && echo " Successfully restored ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} "
1091
+ set_interface_mac ${SUBNET_IFACE} ${OLD_MACADDR} || echo " Failed restoring ${SUBNET_IFACE} to old MAC address ${OLD_MACADDR} " >&2
1090
1092
fi
1091
1093
1092
1094
nm_restore_manage
@@ -1180,6 +1182,7 @@ cleanup() {
1180
1182
# kill -9 -$pgid
1181
1183
}
1182
1184
1185
+ # NOTE function die() is designed not to be used before init_trap() executed
1183
1186
die () { # SIGUSR2
1184
1187
echo " Error occured"
1185
1188
[[ -n " $1 " ]] && echo -e " \nERROR: $1 \n" >&2
@@ -1267,18 +1270,19 @@ print_clients_from_leases() { # MAC|IP|HOST|lease
1267
1270
1268
1271
FILEC=" $( cat " $LEASE_FILE " | grep -v -E " ^duid\b" | sed -r ' /^\s*$/d' ) "
1269
1272
1273
+ # TODO: duid is somewhat related to ipv6. I don't know about it. Not sure excluding it miss some info or not
1270
1274
echo " $FILEC " | while read line
1271
1275
do
1272
1276
# echo aa$line
1273
1277
LEASEstamp=" $( echo " $line " | awk ' {print $1}' ) "
1274
1278
MAC=" $( echo " $line " | awk ' {print $2}' ) "
1275
1279
IP=" $( echo " $line " | awk ' {print $3}' | sed ' s/\[//g' | sed ' s/\]//g' ) "
1276
- HOST=" $( echo " $line " | awk ' {print $4}' | sed ' s/*/?/g' ) "
1280
+ HOST=" $( echo " $line " | awk ' {print $4}' | sed ' s/*/?/g' | sed ' s/|/_/g ' | sed ' s/ /_/g ' ) "
1277
1281
1278
1282
if [[ -n " $MAC " ]]; then
1279
1283
LEASEstr=" $( date -d @${LEASEstamp} +%m-%d_%X) "
1280
1284
1281
- echo " $MAC |$IP |$HOST |$LEASEstr "
1285
+ echo " $MAC |$IP |$HOST |lease_ $LEASEstr "
1282
1286
fi
1283
1287
done
1284
1288
@@ -1303,7 +1307,7 @@ print_interface_neighbors_via_iproute() { # MAC|IP|_|STATUS
1303
1307
MAC=" ?"
1304
1308
STATUS=" $( echo $line | awk -F' |' ' $1="";$2="";$3="";$4="";{print}' | awk ' {$1=$1;print}' | sed ' s/ /,/g' ) "
1305
1309
fi
1306
- if [[ -n " $IP " ]]; then
1310
+ if [[ -n " $IP " && ( " $MAC " ! = " ? " || " $STATUS " ! = " FAILED " ) ]]; then
1307
1311
echo " $MAC |$IP |?|$STATUS "
1308
1312
fi
1309
1313
done
@@ -1315,12 +1319,12 @@ print_interface_neighbors_via_iw() { # MAC|_|_|signal
1315
1319
do
1316
1320
if [[ -n " $MAC " ]]; then
1317
1321
SIGNAL=" $( iw dev $IFACE station get $MAC | grep " signal:" | awk ' {print $2}' ) "
1318
- echo " ${MAC} |?|?|${SIGNAL} dBm "
1322
+ echo " ${MAC} |?|?|${SIGNAL} _dBm "
1319
1323
fi
1320
1324
done
1321
1325
}
1322
1326
1323
- list_clients () {
1327
+ list_clients () { # passive mode. (use 'arp-scan' or 'netdiscover' if want active mode)
1324
1328
local IFACE pid
1325
1329
local CONFDIR
1326
1330
@@ -1336,7 +1340,7 @@ list_clients() {
1336
1340
else # non-number given
1337
1341
IFACE=" $1 "
1338
1342
if ( ! is_interface $IFACE ) ; then
1339
- echo " '$IFACE ' is not an interface" >&2
1343
+ echo " '$IFACE ' is not an interface or PID " >&2
1340
1344
exit 1
1341
1345
fi
1342
1346
pid=$( get_pid_from_subn_iface " $IFACE " )
@@ -1459,6 +1463,18 @@ daemonizing_check(){
1459
1463
1460
1464
# ============================
1461
1465
check_wifi_settings () {
1466
+
1467
+ if ! ( which iw > /dev/null 2>&1 && iw dev $WIFI_IFACE info > /dev/null 2>&1 ); then
1468
+ echo " WARN: Can't use 'iw' to operation this WiFi interface, trying 'iwconfig' ..." >&2
1469
+ if which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1 ; then
1470
+ USE_IWCONFIG=1
1471
+ echo " WARN: Using 'iwconfig', not as good as 'iw'" >&2
1472
+ else
1473
+ echo " ERROR: Can't use 'iwconfig' to operation this WiFi interface neither" >&2
1474
+ exit 1
1475
+ fi
1476
+ fi
1477
+
1462
1478
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
1463
1479
echo " ERROR: Invalid frequency band" >&2
1464
1480
exit 1
@@ -1547,8 +1563,8 @@ decide_target_interface() {
1547
1563
elif [[ " $WIFI_IFACE " ]]; then
1548
1564
echo " $WIFI_IFACE "
1549
1565
else
1550
- die " No target interface specified"
1551
- exit 1
1566
+ echo " No target interface specified" >&2
1567
+ return 1
1552
1568
fi
1553
1569
}
1554
1570
@@ -1595,8 +1611,9 @@ prepare_wifi_interface() {
1595
1611
VWIFI_IFACE=$( alloc_new_vface_name)
1596
1612
if iw dev ${WIFI_IFACE} interface add ${VWIFI_IFACE} type __ap; then
1597
1613
# Successfully created virtual wifi interface
1598
- sleep 2 # wait for virtual interface MAC may change by system (but could be changed back by other programs)
1599
- echo " ${VWIFI_IFACE} created)"
1614
+ # if NM running, it will give the new virtual interface a random MAC. MAC will go back after setting NM unmanaged
1615
+ sleep 2
1616
+ echo " ${VWIFI_IFACE} created"
1600
1617
else
1601
1618
VWIFI_IFACE=
1602
1619
die " Failed creating virtual WiFi interface. Maybe your WiFi adapter does not fully support virtual interfaces. Try again with '--no-virt'"
@@ -1890,8 +1907,9 @@ daemonizing_check
1890
1907
# # ========================================================
1891
1908
echo " PID: $$ "
1892
1909
1893
- TARGET_IFACE=" $( decide_target_interface) " # judge wired (-i CONN_IFACE) or wireless hotspot (--ap $WIFI_IFACE)
1894
- echo " Target interface is ${TARGET_IFACE} "
1910
+ TARGET_IFACE=" $( decide_target_interface) " || exit 1 # judge wired (-i CONN_IFACE) or wireless hotspot (--ap $WIFI_IFACE)
1911
+ echo " Target interface is ${TARGET_IFACE} ($( get_interface_mac $TARGET_IFACE ) )"
1912
+ # TODO: show interface type, device model and pci/usb id (hwdata pci.ids), current driver
1895
1913
1896
1914
if [[ " $MAC_USE_RANDOM " -eq 1 ]] ; then
1897
1915
NEW_MACADDR=" $( generate_random_mac) "
@@ -1907,6 +1925,7 @@ decide_ip_addresses # ip 4 & 6 lan addresses
1907
1925
# ==== begin to do some change on config files and system===
1908
1926
1909
1927
init_trap
1928
+ # NOTE function die() is designed not to be used before init_trap() executed
1910
1929
1911
1930
init_conf_dirs # CONFDIR , COMMON_CONFDIR . make dir
1912
1931
@@ -1947,7 +1966,7 @@ ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down
1947
1966
# flush old IPs of subnet interface
1948
1967
ip addr flush ${SUBNET_IFACE} || die " Failed flush ${SUBNET_IFACE} IP"
1949
1968
1950
- dealwith_mac
1969
+ dealwith_mac # setting MAC should be after setting NM unmanaged
1951
1970
1952
1971
[[ $WIFI_IFACE ]] && check_rfkill_unblock_wifi
1953
1972
0 commit comments