Skip to content

Commit 1e3c500

Browse files
committed
some improvements
add '-l' change MAC addr backking up and restoring judgement to OLD_MACADDR == now filter --lc STATUS==FAILED line fix iw and iwconfig availability not checked not use die() before trap
1 parent 2341938 commit 1e3c500

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ Options:
303303
304304
Instance managing:
305305
--daemon Run in background
306-
--list-running Show running instances
307-
--lc, --list-clients <id>
306+
-l, --list-running Show running instances
307+
--lc, --list-clients <id|interface>
308308
List clients of an instance. Or list neighbors of
309-
any interface, even if it isn't handled by us
309+
an interface, even if it isn't handled by us.
310+
(passive mode)
310311
--stop <id> Stop a running instance
311312
For <id> you can use PID or subnet interface name.
312313
You can get them with '--list-running'

lnxrouter

100755100644
Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22

3+
# TODO: showing version or git commit on running
34
VERSION=0.6.0
45
PROGNAME="$(basename $0)"
56

@@ -100,10 +101,11 @@ Options:
100101
101102
Instance managing:
102103
--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>
105106
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)
107109
--stop <id> Stop a running instance
108110
For <id> you can use PID or subnet interface name.
109111
You can get them with '--list-running'
@@ -118,7 +120,6 @@ Examples:
118120
$PROGNAME -i eth1
119121
$PROGNAME --ap wlan0 MyAccessPoint
120122
$PROGNAME --ap wlan0 MyAccessPoint -p MyPassPhrase
121-
$PROGNAME -n --ap wlan0 MyAccessPoint -p MyPassPhrase
122123
$PROGNAME -i eth1 --tp <transparent-proxy> --dns <dns-proxy>
123124
EOF
124125
}
@@ -440,7 +441,7 @@ parse_user_options(){
440441
STOP_ID="$1"
441442
shift
442443
;;
443-
--list-running)
444+
-l|--list-running)
444445
shift
445446
LIST_RUNNING=1
446447
;;
@@ -960,6 +961,7 @@ unallow_dhcp() {
960961
fi
961962
}
962963

964+
# TODO: use 'DNAT' instead of '--to-ports' to support other IP
963965
start_redsocks() {
964966
echo
965967
echo "iptables: transparent proxy non-LAN TCP/UDP traffic to port ${TP_PORT}"
@@ -1026,7 +1028,7 @@ backup_ipv6_bits() {
10261028
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/accept_ra" \
10271029
"/proc/sys/net/ipv6/conf/$SUBNET_IFACE/use_tempaddr" \
10281030
"/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"
10301032

10311033
if [[ "$SHARE_METHOD" == 'redsocks' ]] ; then
10321034
cp "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/forwarding" \
@@ -1066,10 +1068,10 @@ backup_interface_status() {
10661068
(ip link show ${SUBNET_IFACE} |grep -q "state UP") && SUBNET_IFACE_ORIGINAL_UP_STATUS=1
10671069

10681070
# save interface old mac
1069-
if [[ -n "$NEW_MACADDR" ]]; then
1071+
#if [[ -n "$NEW_MACADDR" ]]; then
10701072
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
10731075

10741076
backup_ipv6_bits
10751077

@@ -1084,9 +1086,9 @@ restore_interface_status() {
10841086

10851087
restore_ipv6_bits
10861088

1087-
if [[ -n "$OLD_MACADDR" ]] ; then
1089+
if [[ -n "$OLD_MACADDR" && "$(get_interface_mac $SUBNET_IFACE)" != "$OLD_MACADDR" ]] ; then
10881090
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
10901092
fi
10911093

10921094
nm_restore_manage
@@ -1180,6 +1182,7 @@ cleanup() {
11801182
#kill -9 -$pgid
11811183
}
11821184

1185+
# NOTE function die() is designed not to be used before init_trap() executed
11831186
die() { # SIGUSR2
11841187
echo "Error occured"
11851188
[[ -n "$1" ]] && echo -e "\nERROR: $1\n" >&2
@@ -1267,18 +1270,19 @@ print_clients_from_leases() { # MAC|IP|HOST|lease
12671270

12681271
FILEC="$(cat "$LEASE_FILE" | grep -v -E "^duid\b" | sed -r '/^\s*$/d' )"
12691272

1273+
# TODO: duid is somewhat related to ipv6. I don't know about it. Not sure excluding it miss some info or not
12701274
echo "$FILEC" | while read line
12711275
do
12721276
#echo aa$line
12731277
LEASEstamp="$(echo "$line" | awk '{print $1}')"
12741278
MAC="$(echo "$line" | awk '{print $2}')"
12751279
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' )"
12771281

12781282
if [[ -n "$MAC" ]]; then
12791283
LEASEstr="$(date -d @${LEASEstamp} +%m-%d_%X)"
12801284

1281-
echo "$MAC|$IP|$HOST|$LEASEstr"
1285+
echo "$MAC|$IP|$HOST|lease_$LEASEstr"
12821286
fi
12831287
done
12841288

@@ -1303,7 +1307,7 @@ print_interface_neighbors_via_iproute() { # MAC|IP|_|STATUS
13031307
MAC="?"
13041308
STATUS="$(echo $line | awk -F'|' '$1="";$2="";$3="";$4="";{print}' | awk '{$1=$1;print}' | sed 's/ /,/g')"
13051309
fi
1306-
if [[ -n "$IP" ]]; then
1310+
if [[ -n "$IP" && ( "$MAC" != "?" || "$STATUS" != "FAILED" ) ]]; then
13071311
echo "$MAC|$IP|?|$STATUS"
13081312
fi
13091313
done
@@ -1315,12 +1319,12 @@ print_interface_neighbors_via_iw() { # MAC|_|_|signal
13151319
do
13161320
if [[ -n "$MAC" ]]; then
13171321
SIGNAL="$(iw dev $IFACE station get $MAC | grep "signal:" | awk '{print $2}')"
1318-
echo "${MAC}|?|?|${SIGNAL} dBm"
1322+
echo "${MAC}|?|?|${SIGNAL}_dBm"
13191323
fi
13201324
done
13211325
}
13221326

1323-
list_clients() {
1327+
list_clients() { # passive mode. (use 'arp-scan' or 'netdiscover' if want active mode)
13241328
local IFACE pid
13251329
local CONFDIR
13261330

@@ -1336,7 +1340,7 @@ list_clients() {
13361340
else # non-number given
13371341
IFACE="$1"
13381342
if ( ! is_interface $IFACE ) ; then
1339-
echo "'$IFACE' is not an interface" >&2
1343+
echo "'$IFACE' is not an interface or PID" >&2
13401344
exit 1
13411345
fi
13421346
pid=$(get_pid_from_subn_iface "$IFACE")
@@ -1459,6 +1463,18 @@ daemonizing_check(){
14591463

14601464
#============================
14611465
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+
14621478
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
14631479
echo "ERROR: Invalid frequency band" >&2
14641480
exit 1
@@ -1547,8 +1563,8 @@ decide_target_interface() {
15471563
elif [[ "$WIFI_IFACE" ]]; then
15481564
echo "$WIFI_IFACE"
15491565
else
1550-
die "No target interface specified"
1551-
exit 1
1566+
echo "No target interface specified" >&2
1567+
return 1
15521568
fi
15531569
}
15541570

@@ -1595,8 +1611,9 @@ prepare_wifi_interface() {
15951611
VWIFI_IFACE=$(alloc_new_vface_name)
15961612
if iw dev ${WIFI_IFACE} interface add ${VWIFI_IFACE} type __ap; then
15971613
# 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"
16001617
else
16011618
VWIFI_IFACE=
16021619
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
18901907
## ========================================================
18911908
echo "PID: $$"
18921909

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
18951913

18961914
if [[ "$MAC_USE_RANDOM" -eq 1 ]] ; then
18971915
NEW_MACADDR="$(generate_random_mac)"
@@ -1907,6 +1925,7 @@ decide_ip_addresses # ip 4 & 6 lan addresses
19071925
#==== begin to do some change on config files and system===
19081926

19091927
init_trap
1928+
# NOTE function die() is designed not to be used before init_trap() executed
19101929

19111930
init_conf_dirs # CONFDIR , COMMON_CONFDIR . make dir
19121931

@@ -1947,7 +1966,7 @@ ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down
19471966
# flush old IPs of subnet interface
19481967
ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP"
19491968

1950-
dealwith_mac
1969+
dealwith_mac # setting MAC should be after setting NM unmanaged
19511970

19521971
[[ $WIFI_IFACE ]] && check_rfkill_unblock_wifi
19531972

0 commit comments

Comments
 (0)