tidy code
This commit is contained in:
parent
51b69cb5e5
commit
6f78433881
91
lnxrouter
91
lnxrouter
|
@ -192,6 +192,8 @@ define_global_variables(){
|
|||
|
||||
# -- variables for running
|
||||
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
|
||||
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
|
||||
NM_RUNNING=1
|
||||
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(){
|
||||
if [[ -d /dev/shm ]]; then
|
||||
TMPD=/dev/shm
|
||||
|
@ -1318,8 +1320,6 @@ daemonizing_check(){
|
|||
|
||||
#============================
|
||||
prepare_wifi() {
|
||||
if [[ $WIFI_IFACE ]]; then
|
||||
|
||||
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
|
||||
echo "ERROR: Invalid frequency band" >&2
|
||||
exit 1
|
||||
|
@ -1388,12 +1388,9 @@ prepare_wifi() {
|
|||
fi
|
||||
echo "WARN: If AP doesn't work, read https://github.com/oblique/create_ap/blob/master/howto/realtek.md" >&2
|
||||
fi
|
||||
|
||||
fi
|
||||
}
|
||||
|
||||
check_if_new_mac_valid() {
|
||||
if [[ -n "$NEW_MACADDR" ]]; then
|
||||
if ! is_unicast_macaddr "$NEW_MACADDR"; then
|
||||
echo "ERROR: The first byte of MAC address (${NEW_MACADDR}) must be even" >&2
|
||||
exit 1
|
||||
|
@ -1402,7 +1399,6 @@ check_if_new_mac_valid() {
|
|||
if [[ $(get_all_macaddrs | grep -c ${NEW_MACADDR}) -ne 0 ]]; then
|
||||
echo "WARN: MAC address '${NEW_MACADDR}' already exists" >&2
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
decide_target_interface() {
|
||||
|
@ -1434,8 +1430,6 @@ decide_ip_addresses() {
|
|||
}
|
||||
|
||||
prepare_wifi_interface() {
|
||||
if [[ $WIFI_IFACE ]]; then
|
||||
|
||||
if [[ $USE_IWCONFIG -eq 0 ]]; then
|
||||
iw dev ${WIFI_IFACE} set power_save off
|
||||
fi
|
||||
|
@ -1482,8 +1476,6 @@ prepare_wifi_interface() {
|
|||
OLD_MACADDR=$(get_macaddr ${WIFI_IFACE})
|
||||
AP_IFACE=${WIFI_IFACE}
|
||||
fi
|
||||
|
||||
fi
|
||||
}
|
||||
|
||||
decide_subnet_interface() {
|
||||
|
@ -1495,20 +1487,18 @@ decide_subnet_interface() {
|
|||
}
|
||||
|
||||
write_hostapd_conf() {
|
||||
if [[ $WIFI_IFACE ]]; then
|
||||
|
||||
if [[ -n "$COUNTRY" && $USE_IWCONFIG -eq 0 ]]; then
|
||||
iw reg set "$COUNTRY"
|
||||
fi
|
||||
|
||||
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!"
|
||||
|
||||
[[ $MAC_FILTER -eq 1 ]] && echo "MAC address filtering is enabled!"
|
||||
|
||||
[[ $ISOLATE_CLIENTS -eq 1 ]] && echo "Access Point's clients will be isolated!"
|
||||
# TODO: move above code
|
||||
|
||||
# hostapd config
|
||||
cat <<- EOF > "$CONFDIR/hostapd.conf"
|
||||
|
@ -1580,11 +1570,9 @@ write_hostapd_conf() {
|
|||
echo "WARN: Wifi is not protected by password" >&2
|
||||
fi
|
||||
chmod 600 "$CONFDIR/hostapd.conf"
|
||||
fi
|
||||
}
|
||||
|
||||
write_dnsmasq_conf() {
|
||||
if [[ $NO_DNSMASQ -eq 0 ]]; then
|
||||
if grep "^nobody:" /etc/group >/dev/null 2>&1 ; then
|
||||
NOBODY_GROUP="nobody"
|
||||
else
|
||||
|
@ -1664,12 +1652,9 @@ write_dnsmasq_conf() {
|
|||
echo "dhcp-option=option6:dns-server,${dns_offer6}" >> "$CONFDIR/dnsmasq.conf"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
run_wifi_ap_processes() {
|
||||
if [[ $WIFI_IFACE ]]; then
|
||||
|
||||
if [[ $NO_HAVEGED -eq 0 ]]; then
|
||||
haveged_watchdog &
|
||||
HAVEGED_WATCHDOG_PID=$!
|
||||
|
@ -1698,7 +1683,6 @@ run_wifi_ap_processes() {
|
|||
( while [ -e /proc/$HOSTAPD_PID ]; do sleep 10; done ; die "hostapd exited" ) &
|
||||
|
||||
sleep 3
|
||||
fi
|
||||
}
|
||||
|
||||
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/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
|
||||
# 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/"
|
||||
echo 1 > "/proc/sys/net/ipv6/conf/$SUBNET_IFACE/disable_ipv6"
|
||||
fi
|
||||
|
@ -1722,7 +1708,6 @@ backup_interface_ipv6_status() {
|
|||
|
||||
|
||||
start_dnsmasq() {
|
||||
if [[ $NO_DNSMASQ -eq 0 ]]; then
|
||||
start_dhcp
|
||||
|
||||
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
|
||||
( while [ -e "/proc/$DNSMASQ_PID" ]; do sleep 10; done ; die "dnsmasq exited" ) &
|
||||
sleep 2
|
||||
|
||||
fi
|
||||
}
|
||||
|
||||
check_if_need_rfkill_unblock_wifi() {
|
||||
if [[ $WIFI_IFACE ]]; then
|
||||
if which rfkill > /dev/null 2>&1 ; then
|
||||
PHY=$(get_phy_device ${SUBNET_IFACE})
|
||||
[[ -n $PHY ]] && rfkill unblock $(rfkill | grep $PHY | awk '{print $1}') >/dev/null 2>&1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
#=========== Above are functions ======================
|
||||
#=========== Executing begin ==============================
|
||||
|
||||
# show usage and exit if empty option
|
||||
# if empty option, show usage and exit
|
||||
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
|
||||
|
||||
ARGS=( "$@" )
|
||||
|
||||
parse_user_options "$@"
|
||||
|
||||
# check if networkManager running
|
||||
nm_initcheck
|
||||
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
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
|
||||
|
||||
check_if_new_mac_valid
|
||||
[[ -n "$NEW_MACADDR" ]] && check_if_new_mac_valid
|
||||
|
||||
# checks finished
|
||||
|
||||
|
@ -1801,16 +1784,14 @@ decide_target_interface
|
|||
|
||||
decide_ip_addresses
|
||||
|
||||
if [[ $TP_PORT ]]; then
|
||||
SHARE_METHOD=redsocks
|
||||
fi
|
||||
# TODO: should this be some other place?
|
||||
[[ $TP_PORT ]] && SHARE_METHOD=redsocks
|
||||
|
||||
if [[ $DHCP_DNS != 'gateway' && $DHCP_DNS6 != 'gateway' ]]; then
|
||||
dnsmasq_NO_DNS=1
|
||||
fi
|
||||
# if user choose to make DHCP to tell clients to use other DNS, we don't have to serve DNS
|
||||
[[ $DHCP_DNS != 'gateway' && $DHCP_DNS6 != 'gateway' ]] && dnsmasq_NO_DNS=1
|
||||
|
||||
#=================
|
||||
# begin to do some change on config files and system
|
||||
#===========================================================
|
||||
#==== begin to do some change on config files and system===
|
||||
|
||||
trap "cleanup" EXIT
|
||||
trap "clean_exit" SIGINT SIGUSR1 SIGTERM
|
||||
|
@ -1829,38 +1810,46 @@ echo $$ > "$CONFDIR/pid"
|
|||
COMMON_CONFDIR="$TMPDIR/lnxrouter_common.conf"
|
||||
mkdir -p "$COMMON_CONFDIR"
|
||||
|
||||
prepare_wifi_interface
|
||||
[[ $WIFI_IFACE ]] && prepare_wifi_interface
|
||||
|
||||
decide_subnet_interface
|
||||
|
||||
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
|
||||
nm_set_unmanaged ${SUBNET_IFACE}
|
||||
fi
|
||||
|
||||
write_dnsmasq_conf
|
||||
[[ $NO_DNSMASQ -eq 0 ]] && write_dnsmasq_conf
|
||||
#===========================
|
||||
|
||||
# initialize subnet interface
|
||||
# put subnet interface down first
|
||||
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"
|
||||
|
||||
# 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"
|
||||
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"
|
||||
|
||||
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
|
||||
|
||||
|
@ -1891,13 +1880,13 @@ fi
|
|||
|
||||
# start dhcp + dns (optional)
|
||||
|
||||
if [[ "$DHCP_DNS" == "gateway" || "$DHCP_DNS6" == "gateway" ]]; then
|
||||
allow_dns_port
|
||||
fi
|
||||
# allow dns port input even if we don't run dnsmasq
|
||||
# user can serve their own dns server
|
||||
[[ "$DHCP_DNS" == "gateway" || "$DHCP_DNS6" == "gateway" ]] && allow_dns_port
|
||||
|
||||
[[ "$CATCH_DNS" -eq 1 ]] && start_catch_dns
|
||||
|
||||
start_dnsmasq
|
||||
[[ $NO_DNSMASQ -eq 0 ]] && start_dnsmasq
|
||||
|
||||
echo
|
||||
echo "== Setting up completed, now linux-router is working =="
|
||||
|
|
Loading…
Reference in New Issue