Skip to content

Commit e5fc9ef

Browse files
committed
support short gateway ip expression
fix dnsmasq pid not get watchdog zombie judgement iptables nft and comment judgement use fifo for dnsmasq log
1 parent 1e3c500 commit e5fc9ef

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Set Linux as router in one command. Able to Provide Internet, or create Wifi hot
44

55
It wraps `iptables`, `dnsmasq` etc. stuff. Use in one command, restore in one command or by `control-c` (or even by closing terminal window).
66

7-
[Buy me a coffee](https://github.com/garywill/receiving/blob/master/receiving_methods.md) :)
7+
[Buy me a coffee](https://github.com/garywill/receiving/blob/master/receiving_methods.md)
8+
9+
( ^\_^)o自自o(^_^ )
810

911
## Features
1012

@@ -110,7 +112,9 @@ lxc.network.hwaddr = xx:xx:xx:xx:xx:xx
110112
sudo lnxrouter -i lxcbr5
111113
```
112114

113-
### Transparent proxy with Tor
115+
### Transparent proxy
116+
117+
For example through Tor
114118

115119
```
116120
sudo lnxrouter -i eth1 --tp 9040 --dns 9053 -g 192.168.55.1 --p6 fd00:5:6:7::
@@ -127,7 +131,7 @@ DNSPort [fd00:5:6:7::1]:9053
127131

128132
### Clients-in-sandbox network
129133

130-
To not give our infomation to clients:
134+
To not give our infomation to clients. Clients can still access Internet.
131135

132136
```
133137
sudo lnxrouter -i eth1 \
@@ -232,12 +236,14 @@ Options:
232236
--ban-priv Disallow clients to access my private network
233237
234238
-g <ip> This host's IPv4 address in subnet (mask is /24)
239+
(example: '192.168.5.1' or '5' shortly)
235240
-6 Enable IPv6 (NAT)
236241
--no4 Disable IPv4 Internet (not forwarding IPv4)
237242
(See Notice 1). Usually used with '-6'
238243
239244
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
240-
(example: fd00:1:2:3::) Using this enables '-6'
245+
(example: 'fd00:0:0:5::' or '5' shortly)
246+
Using this enables '-6'
241247
242248
--dns <ip>|<port>|<ip:port>
243249
DNS server's upstream DNS.
@@ -333,13 +339,13 @@ Options:
333339
- procps or procps-ng
334340
- iproute2
335341
- dnsmasq
336-
- iptables
342+
- iptables (legacy. nft not tested)
337343
- WiFi hotspot dependencies
338344
- hostapd
339345
- iw
340346
- iwconfig (you only need this if 'iw' can not recognize your adapter)
341347
- haveged (optional)
342-
- qrencode (opional)
348+
- qrencode (optional)
343349

344350
## TODO
345351

@@ -354,10 +360,12 @@ Options:
354360

355361
( ^\_^)o自自o(^_^ )
356362

357-
No? Okay, or just give me a star!
363+
[打赏一个](https://github.com/garywill/receiving/blob/master/receiving_methods.md)
358364

359365
## For developers
360366

361367
**Many thanks to project [create_ap](https://github.com/oblique/create_ap)**.
362368

363369
This script was forked from create\_ap. Now it's quite different from it. (See `history` branch for how I modified create_ap)
370+
371+
There're some TO-DOs listed, at both above and in the code file. We'll appreciate your help.

lnxrouter

100644100755
Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# TODO: showing version or git commit on running
3+
# TODO: showing version (or git commit) on running
44
VERSION=0.6.0
55
PROGNAME="$(basename $0)"
66

@@ -30,12 +30,14 @@ Options:
3030
--ban-priv Disallow clients to access my private network
3131
3232
-g <ip> This host's IPv4 address in subnet (mask is /24)
33+
(example: '192.168.5.1' or '5' shortly)
3334
-6 Enable IPv6 (NAT)
3435
--no4 Disable IPv4 Internet (not forwarding IPv4)
3536
(See Notice 1). Usually used with '-6'
3637
3738
--p6 <prefix> Set IPv6 LAN address prefix (length 64)
38-
(example: fd00:1:2:3::) Using this enables '-6'
39+
(example: 'fd00:0:0:5::' or '5' shortly)
40+
Using this enables '-6'
3941
4042
--dns <ip>|<port>|<ip:port>
4143
DNS server's upstream DNS.
@@ -201,6 +203,7 @@ define_global_variables(){
201203
CONFDIR=
202204
NM_RUNNING=0
203205
NM_UNM_LIST= # it's called "list" but for now one interface
206+
XT_COMMENT=1
204207
}
205208

206209
parse_user_options(){
@@ -736,7 +739,7 @@ haveged_watchdog() {
736739
echo "WARN: Low entropy detected. We recommend you to install \`haveged'" 1>&2
737740
show_warn=0
738741
fi
739-
elif ! pidof haveged > /dev/null 2>&1; then
742+
elif ! pidof haveged > /dev/null 2>&1; then # TODO judge zombie ?
740743
echo "Low entropy detected, starting haveged" 1>&2
741744
# boost low-entropy
742745
haveged -w 1024 -p $COMMON_CONFDIR/haveged.pid
@@ -745,7 +748,24 @@ haveged_watchdog() {
745748
sleep 2
746749
done
747750
}
748-
751+
pid_watchdog() {
752+
local PID="$1"
753+
local SLEEP="$2"
754+
local ERR_MSG="$3"
755+
local ST
756+
while true
757+
do
758+
if [[ -e "/proc/$PID" ]]; then
759+
ST="$(cat "/proc/$PID/status" | grep "^State:" | awk '{print $2}')"
760+
if [[ "$ST" != 'Z' ]]; then
761+
sleep $SLEEP
762+
continue
763+
fi
764+
fi
765+
die "$ERR_MSG"
766+
done
767+
768+
}
749769
#========
750770

751771

@@ -792,15 +812,22 @@ nm_restore_manage() {
792812
fi
793813
}
794814
#=========
795-
796815
iptables_()
797816
{
798-
iptables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
817+
if [[ $XT_COMMENT -eq 1 ]]; then
818+
iptables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
819+
else
820+
iptables -w $@
821+
fi
799822
return $?
800823
}
801824
ip6tables_()
802825
{
803-
ip6tables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
826+
if [[ $XT_COMMENT -eq 1 ]]; then
827+
ip6tables -w $@ -m comment --comment "lnxrouter-$$-$SUBNET_IFACE"
828+
else
829+
ip6tables -w $@
830+
fi
804831
return $?
805832
}
806833

@@ -843,7 +870,7 @@ start_ban_lan() {
843870
echo
844871
echo "iptables: Disallow clients to access LAN"
845872
iptables_ -N BANLAN-f-${SUBNET_IFACE} || die
846-
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 0.0.0.0/8 -j REJECT || die
873+
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 0.0.0.0/8 -j REJECT || die # TODO: use array
847874
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 10.0.0.0/8 -j REJECT || die
848875
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 100.64.0.0/10 -j REJECT || die
849876
iptables_ -v -I BANLAN-f-${SUBNET_IFACE} -d 127.0.0.0/8 -j REJECT || die
@@ -858,6 +885,7 @@ start_ban_lan() {
858885
iptables_ -N BANLAN-i-${SUBNET_IFACE}
859886
#iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} -j REJECT || die
860887
iptables_ -v -I BANLAN-i-${SUBNET_IFACE} -i ${SUBNET_IFACE} ! -p icmp -j REJECT || die
888+
# TODO: ipv6 need icmp to function. maybe we can block some unneeded icmp to improve security
861889

862890
iptables_ -I INPUT -i ${SUBNET_IFACE} -j BANLAN-i-${SUBNET_IFACE} || die
863891

@@ -1465,12 +1493,13 @@ daemonizing_check(){
14651493
check_wifi_settings() {
14661494

14671495
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
1496+
echo "WARN: Can't use 'iw' to operate interfce '$WIFI_IFACE', trying 'iwconfig' (not as good as 'iw') ..." >&2
1497+
USE_IWCONFIG=1
1498+
fi
1499+
1500+
if [[ $USE_IWCONFIG -eq 1 ]]; then
1501+
if ! (which iwconfig > /dev/null 2>&1 && iwconfig $WIFI_IFACE > /dev/null 2>&1); then
1502+
echo "ERROR: Can't use 'iwconfig' to operate interfce '$WIFI_IFACE'" >&2
14741503
exit 1
14751504
fi
14761505
fi
@@ -1572,11 +1601,15 @@ decide_ip_addresses() {
15721601
if [[ ! -n $GATEWAY ]]; then
15731602
GATEWAY="$(generate_random_ip4)"
15741603
echo "Use random LAN IPv4 address $GATEWAY"
1604+
elif [[ ! "$GATEWAY" =~ "." ]]; then
1605+
GATEWAY="192.168.${GATEWAY}.1"
15751606
fi
15761607

15771608
if [[ $IPV6 -eq 1 && ! -n $PREFIX6 ]]; then
15781609
PREFIX6="$(generate_random_lan_ip6_prefix)"
15791610
echo "Use random LAN IPv6 address ${PREFIX6}${IID6}"
1611+
elif [[ ! "$PREFIX6" =~ ":" ]]; then
1612+
PREFIX6="fd00:0:0:${PREFIX6}::"
15801613
fi
15811614
if [[ $IPV6 -eq 1 ]]; then
15821615
GATEWAY6="${PREFIX6}${IID6}"
@@ -1729,6 +1762,11 @@ write_dnsmasq_conf() {
17291762
else
17301763
NOBODY_GROUP="nogroup"
17311764
fi
1765+
1766+
mkfifo "$CONFDIR/dnsmasq.log" || die "Failed creating pipe file for dnsmasq"
1767+
chown nobody "$CONFDIR/dnsmasq.log" || die "Failed changing dnsmasq log file owner"
1768+
cat "$CONFDIR/dnsmasq.log" &
1769+
17321770
cat <<- EOF > "$CONFDIR/dnsmasq.conf"
17331771
user=nobody
17341772
group=$NOBODY_GROUP
@@ -1740,7 +1778,7 @@ write_dnsmasq_conf() {
17401778
dhcp-range=${GATEWAY%.*}.10,${GATEWAY%.*}.250,255.255.255.0
17411779
dhcp-option-force=option:router,${GATEWAY}
17421780
#log-dhcp
1743-
log-facility=/dev/stdout
1781+
log-facility=$CONFDIR/dnsmasq.log
17441782
bogus-priv
17451783
domain-needed
17461784
EOF
@@ -1831,8 +1869,7 @@ run_wifi_ap_processes() {
18311869
# sleep 1
18321870
#done
18331871
#echo -n "hostapd PID: " ; cat $CONFDIR/hostapd.pid
1834-
( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die "hostapd exited" ) &
1835-
1872+
pid_watchdog $HOSTAPD_PID 10 "hostapd failed" &
18361873
sleep 3
18371874
}
18381875

@@ -1855,9 +1892,10 @@ start_dnsmasq() {
18551892
i=$((i + 1))
18561893
if [[ $i -gt 10 ]]; then die "Couldn't get dnsmasq PID" ; fi
18571894
done
1858-
echo -n "dnsmasq PID: " ; cat "$CONFDIR/dnsmasq.pid"
1895+
DNSMASQ_PID="$(cat "$CONFDIR/dnsmasq.pid" )"
1896+
echo "dnsmasq PID: $DNSMASQ_PID"
18591897
######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child
1860-
( while [ -e "/proc/$DNSMASQ_PID" ]; do sleep 10; done ; die "dnsmasq exited" ) &
1898+
pid_watchdog $DNSMASQ_PID 9 "dnsmasq failed" &
18611899
sleep 2
18621900
}
18631901

@@ -1986,6 +2024,18 @@ if [[ $IPV6 -eq 1 ]] ; then
19862024
ip -6 addr add ${GATEWAY6}/64 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv6 address"
19872025
fi
19882026

2027+
function check_iptables() {
2028+
if iptables --version | grep "nf_tables" >/dev/null 2>&1 ; then
2029+
echo -e "\nWARN: Your system is using nftables. This script is tested with iptables legacy only. If you encounter problems, visit following URL for infomation:\n https://github.com/oblique/create_ap/issues/373\n https://github.com/oblique/create_ap/issues/433\n https://github.com/garywill/linux-router/issues/18\n" >&2
2030+
fi
2031+
2032+
if ! lsmod | grep -E "\bxt_comment\b" >/dev/null 2>&1 ; then
2033+
XT_COMMENT=0
2034+
fi
2035+
}
2036+
2037+
check_iptables
2038+
19892039
# enable Internet sharing
19902040
if [[ "$SHARE_METHOD" == "none" ]]; then
19912041

0 commit comments

Comments
 (0)