tidy code

This commit is contained in:
garywill 2021-01-20 13:10:02 +08:00
parent 51b69cb5e5
commit 6f78433881
1 changed files with 348 additions and 359 deletions

View File

@ -192,6 +192,8 @@ define_global_variables(){
# -- variables for running # -- variables for running
CONFDIR= CONFDIR=
NM_RUNNING=0
NM_UNM_LIST= # it's called "list" but for now one interface
} }
@ -737,8 +739,6 @@ haveged_watchdog() {
# only support NetworkManager >= 0.9.9 # only support NetworkManager >= 0.9.9
nm_initcheck() { nm_initcheck() {
NM_RUNNING=0
NM_UNM_LIST=
if (which nmcli >/dev/null 2>&1 ) && (nmcli -t -f RUNNING g 2>&1 | grep -E '^running$' >/dev/null 2>&1 ) ; then if (which nmcli >/dev/null 2>&1 ) && (nmcli -t -f RUNNING g 2>&1 | grep -E '^running$' >/dev/null 2>&1 ) ; then
NM_RUNNING=1 NM_RUNNING=1
fi fi
@ -1269,6 +1269,8 @@ send_stop() {
## ======================================================== ## ========================================================
## ======================================================== ## ========================================================
# decide linux-router's global temporary path for all instances
# this is different and should be before config-saving dir. The latter is for one instance
init_tmpdir(){ init_tmpdir(){
if [[ -d /dev/shm ]]; then if [[ -d /dev/shm ]]; then
TMPD=/dev/shm TMPD=/dev/shm
@ -1318,8 +1320,6 @@ daemonizing_check(){
#============================ #============================
prepare_wifi() { prepare_wifi() {
if [[ $WIFI_IFACE ]]; then
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
echo "ERROR: Invalid frequency band" >&2 echo "ERROR: Invalid frequency band" >&2
exit 1 exit 1
@ -1388,12 +1388,9 @@ prepare_wifi() {
fi fi
echo "WARN: If AP doesn't work, read https://github.com/oblique/create_ap/blob/master/howto/realtek.md" >&2 echo "WARN: If AP doesn't work, read https://github.com/oblique/create_ap/blob/master/howto/realtek.md" >&2
fi fi
fi
} }
check_if_new_mac_valid() { check_if_new_mac_valid() {
if [[ -n "$NEW_MACADDR" ]]; then
if ! is_unicast_macaddr "$NEW_MACADDR"; then if ! is_unicast_macaddr "$NEW_MACADDR"; then
echo "ERROR: The first byte of MAC address (${NEW_MACADDR}) must be even" >&2 echo "ERROR: The first byte of MAC address (${NEW_MACADDR}) must be even" >&2
exit 1 exit 1
@ -1402,7 +1399,6 @@ check_if_new_mac_valid() {
if [[ $(get_all_macaddrs | grep -c ${NEW_MACADDR}) -ne 0 ]]; then if [[ $(get_all_macaddrs | grep -c ${NEW_MACADDR}) -ne 0 ]]; then
echo "WARN: MAC address '${NEW_MACADDR}' already exists" >&2 echo "WARN: MAC address '${NEW_MACADDR}' already exists" >&2
fi fi
fi
} }
decide_target_interface() { decide_target_interface() {
@ -1434,8 +1430,6 @@ decide_ip_addresses() {
} }
prepare_wifi_interface() { prepare_wifi_interface() {
if [[ $WIFI_IFACE ]]; then
if [[ $USE_IWCONFIG -eq 0 ]]; then if [[ $USE_IWCONFIG -eq 0 ]]; then
iw dev ${WIFI_IFACE} set power_save off iw dev ${WIFI_IFACE} set power_save off
fi fi
@ -1482,8 +1476,6 @@ prepare_wifi_interface() {
OLD_MACADDR=$(get_macaddr ${WIFI_IFACE}) OLD_MACADDR=$(get_macaddr ${WIFI_IFACE})
AP_IFACE=${WIFI_IFACE} AP_IFACE=${WIFI_IFACE}
fi fi
fi
} }
decide_subnet_interface() { decide_subnet_interface() {
@ -1495,20 +1487,18 @@ decide_subnet_interface() {
} }
write_hostapd_conf() { write_hostapd_conf() {
if [[ $WIFI_IFACE ]]; then
if [[ -n "$COUNTRY" && $USE_IWCONFIG -eq 0 ]]; then if [[ -n "$COUNTRY" && $USE_IWCONFIG -eq 0 ]]; then
iw reg set "$COUNTRY" iw reg set "$COUNTRY"
fi fi
can_transmit_to_channel ${AP_IFACE} ${CHANNEL} || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz." can_transmit_to_channel ${AP_IFACE} ${CHANNEL} || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz."
[[ $HIDDEN -eq 1 ]] && echo "Access Point's SSID is hidden!" [[ $HIDDEN -eq 1 ]] && echo "Access Point's SSID is hidden!"
[[ $MAC_FILTER -eq 1 ]] && echo "MAC address filtering is enabled!" [[ $MAC_FILTER -eq 1 ]] && echo "MAC address filtering is enabled!"
[[ $ISOLATE_CLIENTS -eq 1 ]] && echo "Access Point's clients will be isolated!" [[ $ISOLATE_CLIENTS -eq 1 ]] && echo "Access Point's clients will be isolated!"
# TODO: move above code
# hostapd config # hostapd config
cat <<- EOF > "$CONFDIR/hostapd.conf" cat <<- EOF > "$CONFDIR/hostapd.conf"
@ -1580,11 +1570,9 @@ write_hostapd_conf() {
echo "WARN: Wifi is not protected by password" >&2 echo "WARN: Wifi is not protected by password" >&2
fi fi
chmod 600 "$CONFDIR/hostapd.conf" chmod 600 "$CONFDIR/hostapd.conf"
fi
} }
write_dnsmasq_conf() { write_dnsmasq_conf() {
if [[ $NO_DNSMASQ -eq 0 ]]; then
if grep "^nobody:" /etc/group >/dev/null 2>&1 ; then if grep "^nobody:" /etc/group >/dev/null 2>&1 ; then
NOBODY_GROUP="nobody" NOBODY_GROUP="nobody"
else else
@ -1664,12 +1652,9 @@ write_dnsmasq_conf() {
echo "dhcp-option=option6:dns-server,${dns_offer6}" >> "$CONFDIR/dnsmasq.conf" echo "dhcp-option=option6:dns-server,${dns_offer6}" >> "$CONFDIR/dnsmasq.conf"
fi fi
fi fi
fi
} }
run_wifi_ap_processes() { run_wifi_ap_processes() {
if [[ $WIFI_IFACE ]]; then
if [[ $NO_HAVEGED -eq 0 ]]; then if [[ $NO_HAVEGED -eq 0 ]]; then
haveged_watchdog & haveged_watchdog &
HAVEGED_WATCHDOG_PID=$! HAVEGED_WATCHDOG_PID=$!
@ -1698,7 +1683,6 @@ run_wifi_ap_processes() {
( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die "hostapd exited" ) & ( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die "hostapd exited" ) &
sleep 3 sleep 3
fi
} }
backup_interface_ipv6_status() { backup_interface_ipv6_status() {
@ -1713,8 +1697,10 @@ backup_interface_ipv6_status() {
echo 0 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/use_tempaddr" echo 0 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/use_tempaddr"
echo 0 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/addr_gen_mode" echo 0 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/addr_gen_mode"
ip -6 addr add ${GATEWAY6}/64 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv6" # TODO: move this code
ip -6 addr add ${GATEWAY6}/64 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv6 address"
else else
# TODO: also need to deal with "disable_ipv6" if ipv6 is enabled using this script
cp "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/disable_ipv6" "$CONFDIR/sys_6_conf_iface/" cp "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/disable_ipv6" "$CONFDIR/sys_6_conf_iface/"
echo 1 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/disable_ipv6" echo 1 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/disable_ipv6"
fi fi
@ -1722,7 +1708,6 @@ backup_interface_ipv6_status() {
start_dnsmasq() { start_dnsmasq() {
if [[ $NO_DNSMASQ -eq 0 ]]; then
start_dhcp start_dhcp
if which complain > /dev/null 2>&1; then if which complain > /dev/null 2>&1; then
@ -1747,47 +1732,45 @@ start_dnsmasq() {
######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child ######(wait $DNSMASQ_PID ; die "dnsmasq failed") & # wait can't deal with non-child
( while [ -e "/proc/$DNSMASQ_PID" ]; do sleep 10; done ; die "dnsmasq exited" ) & ( while [ -e "/proc/$DNSMASQ_PID" ]; do sleep 10; done ; die "dnsmasq exited" ) &
sleep 2 sleep 2
fi
} }
check_if_need_rfkill_unblock_wifi() { check_if_need_rfkill_unblock_wifi() {
if [[ $WIFI_IFACE ]]; then
if which rfkill > /dev/null 2>&1 ; then if which rfkill > /dev/null 2>&1 ; then
PHY=$(get_phy_device ${SUBNET_IFACE}) PHY=$(get_phy_device ${SUBNET_IFACE})
[[ -n $PHY ]] && rfkill unblock $(rfkill | grep $PHY | awk '{print $1}') >/dev/null 2>&1 [[ -n $PHY ]] && rfkill unblock $(rfkill | grep $PHY | awk '{print $1}') >/dev/null 2>&1
fi fi
fi
} }
#=========== Above are functions ====================== #=========== Above are functions ======================
#=========== Executing begin ============================== #=========== Executing begin ==============================
# show usage and exit if empty option # if empty option, show usage and exit
check_empty_option "$@" check_empty_option "$@"
# TODO: some global variables are still defined in those following code # TODO: are some global variables are still defined in those following code?
define_global_variables define_global_variables
ARGS=( "$@" ) ARGS=( "$@" )
parse_user_options "$@" parse_user_options "$@"
# check if networkManager running
nm_initcheck nm_initcheck
init_tmpdir init_tmpdir
# will exit after this if user choose to deal with running instances # if user choose to deal with running instances, will exit after this
check_other_functions check_other_functions
# will start new background process and exit this if user choose to daemonize # if user choose to daemonize, will start new background process and exit this
daemonizing_check daemonizing_check
prepare_wifi # check if wifi will work on this system and user settings
[[ $WIFI_IFACE ]] && prepare_wifi
[[ "$USE_RANDOM_MAC" -eq 1 ]] && generate_random_mac [[ "$USE_RANDOM_MAC" -eq 1 ]] && generate_random_mac
check_if_new_mac_valid [[ -n "$NEW_MACADDR" ]] && check_if_new_mac_valid
# checks finished # checks finished
@ -1801,16 +1784,14 @@ decide_target_interface
decide_ip_addresses decide_ip_addresses
if [[ $TP_PORT ]]; then # TODO: should this be some other place?
SHARE_METHOD=redsocks [[ $TP_PORT ]] && SHARE_METHOD=redsocks
fi
if [[ $DHCP_DNS != 'gateway' && $DHCP_DNS6 != 'gateway' ]]; then # if user choose to make DHCP to tell clients to use other DNS, we don't have to serve DNS
dnsmasq_NO_DNS=1 [[ $DHCP_DNS != 'gateway' && $DHCP_DNS6 != 'gateway' ]] && dnsmasq_NO_DNS=1
fi
#================= #===========================================================
# begin to do some change on config files and system #==== begin to do some change on config files and system===
trap "cleanup" EXIT trap "cleanup" EXIT
trap "clean_exit" SIGINT SIGUSR1 SIGTERM trap "clean_exit" SIGINT SIGUSR1 SIGTERM
@ -1829,38 +1810,46 @@ echo $$ > "$CONFDIR/pid"
COMMON_CONFDIR="$TMPDIR/lnxrouter_common.conf" COMMON_CONFDIR="$TMPDIR/lnxrouter_common.conf"
mkdir -p "$COMMON_CONFDIR" mkdir -p "$COMMON_CONFDIR"
prepare_wifi_interface [[ $WIFI_IFACE ]] && prepare_wifi_interface
decide_subnet_interface decide_subnet_interface
echo "$SUBNET_IFACE" > "$CONFDIR/subn_iface" echo "$SUBNET_IFACE" > "$CONFDIR/subn_iface"
write_hostapd_conf [[ $WIFI_IFACE ]] && write_hostapd_conf
#=================================================== #===================================================
#=================================================== #===================================================
# set interface unmanaged by networkManager
if [[ $NM_RUNNING -eq 1 ]] && nm_knows $TARGET_IFACE ; then if [[ $NM_RUNNING -eq 1 ]] && nm_knows $TARGET_IFACE ; then
nm_set_unmanaged ${SUBNET_IFACE} nm_set_unmanaged ${SUBNET_IFACE}
fi fi
write_dnsmasq_conf [[ $NO_DNSMASQ -eq 0 ]] && write_dnsmasq_conf
#=========================== #===========================
# initialize subnet interface # initialize subnet interface
# put subnet interface down first
ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down" ip link set down dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} down"
# flush IPs of subnet interface
ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP" ip addr flush ${SUBNET_IFACE} || die "Failed flush ${SUBNET_IFACE} IP"
if [[ -n "$NEW_MACADDR" ]]; then # set subnet mac if needed
if [[ -n "$NEW_MACADDR" ]] ; then
ip link set dev ${SUBNET_IFACE} address ${NEW_MACADDR} || die "Failed setting new MAC address" ip link set dev ${SUBNET_IFACE} address ${NEW_MACADDR} || die "Failed setting new MAC address"
fi fi
check_if_need_rfkill_unblock_wifi [[ $WIFI_IFACE ]] && check_if_need_rfkill_unblock_wifi
# bring subnet interface up
ip link set up dev ${SUBNET_IFACE} || die "Failed bringing ${SUBNET_IFACE} up" ip link set up dev ${SUBNET_IFACE} || die "Failed bringing ${SUBNET_IFACE} up"
run_wifi_ap_processes # hostapd , haveged
[[ $WIFI_IFACE ]] && run_wifi_ap_processes
ip addr add ${GATEWAY}/24 broadcast ${GATEWAY%.*}.255 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IP" # add ipv4 address to subnet interface
# TODO: move ipv6 code here
ip addr add ${GATEWAY}/24 broadcast ${GATEWAY%.*}.255 dev ${SUBNET_IFACE} || die "Failed setting ${SUBNET_IFACE} IPv4 address"
backup_interface_ipv6_status backup_interface_ipv6_status
@ -1891,13 +1880,13 @@ fi
# start dhcp + dns (optional) # start dhcp + dns (optional)
if [[ "$DHCP_DNS" == "gateway" || "$DHCP_DNS6" == "gateway" ]]; then # allow dns port input even if we don't run dnsmasq
allow_dns_port # user can serve their own dns server
fi [[ "$DHCP_DNS" == "gateway" || "$DHCP_DNS6" == "gateway" ]] && allow_dns_port
[[ "$CATCH_DNS" -eq 1 ]] && start_catch_dns [[ "$CATCH_DNS" -eq 1 ]] && start_catch_dns
start_dnsmasq [[ $NO_DNSMASQ -eq 0 ]] && start_dnsmasq
echo echo
echo "== Setting up completed, now linux-router is working ==" echo "== Setting up completed, now linux-router is working =="