|
|
|
|
@@ -149,28 +149,6 @@ _resolve_hostname() {
|
|
|
|
|
echo "${ips[@]}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_endpoint_route_add() {
|
|
|
|
|
local ip="$1"
|
|
|
|
|
local rt via dev
|
|
|
|
|
rt="$(ip route get "${ip}" 2>/dev/null | head -n1)"
|
|
|
|
|
via="$(awk '{for(i=1;i<=NF;i++) if($i=="via"){print $(i+1);exit}}' <<< "${rt}")"
|
|
|
|
|
dev="$(awk '{for(i=1;i<=NF;i++) if($i=="dev"){print $(i+1);exit}}' <<< "${rt}")"
|
|
|
|
|
[ -z "${dev}" ] && { bashio::log.error "No route to VPN endpoint ${ip}."; return 1; }
|
|
|
|
|
if [ -n "${via}" ]; then
|
|
|
|
|
ip route replace "${ip}" via "${via}" dev "${dev}" || return 1
|
|
|
|
|
else
|
|
|
|
|
ip route replace "${ip}" dev "${dev}" || return 1
|
|
|
|
|
fi
|
|
|
|
|
bashio::log.info "Pinned VPN endpoint ${ip} to pre-VPN route (dev ${dev})."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_endpoint_route_del() {
|
|
|
|
|
local ip
|
|
|
|
|
ip="$(cat "${config["EndpointIPFile"]}" 2>/dev/null)"
|
|
|
|
|
[ -n "${ip}" ] && ip route del "${ip}" 2>/dev/null || true
|
|
|
|
|
rm -f "${config["EndpointIPFile"]}" || true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_routing_add() {
|
|
|
|
|
bashio::log.info "Adding routing rules for VPN interface ${config["Interface"]}..."
|
|
|
|
|
|
|
|
|
|
@@ -366,8 +344,6 @@ _wireguard_up() {
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
_cmd "ip link set ${config["Interface"]} up" || return 1
|
|
|
|
|
_endpoint_route_add "${config["EndpointIP"]}" || return 1
|
|
|
|
|
echo "${config["EndpointIP"]}" > "${config["EndpointIPFile"]}"
|
|
|
|
|
|
|
|
|
|
# Add routing rules for VPN interface and DNS servers
|
|
|
|
|
_routing_add || return 1
|
|
|
|
|
@@ -382,7 +358,6 @@ _wireguard_up() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_wireguard_down() {
|
|
|
|
|
_endpoint_route_del || true
|
|
|
|
|
# Update resolv.conf to remove VPN DNS servers
|
|
|
|
|
_resolvconf "reset" || true
|
|
|
|
|
# Remove routing rules for VPN interface and DNS servers
|
|
|
|
|
@@ -424,7 +399,6 @@ wireguard() {
|
|
|
|
|
config["Interface"]="${interface}"
|
|
|
|
|
config["ConfigFile"]="${config_file}"
|
|
|
|
|
config["Table"]="${config["Table"]:-1000}"
|
|
|
|
|
config["EndpointIPFile"]="${WIREGUARD_STATE_DIR}/endpoint-ip"
|
|
|
|
|
config["ListenPort"]="${config["ListenPort"]:-51820}"
|
|
|
|
|
config["EndpointHost"]="${config["Endpoint"]%:*}"
|
|
|
|
|
config["EndpointPort"]="${config["Endpoint"]##*:}"
|
|
|
|
|
@@ -511,8 +485,6 @@ _openvpn_check() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_openvpn_up() {
|
|
|
|
|
local endpoint_ip result=0
|
|
|
|
|
|
|
|
|
|
bashio::log.warning "This script force OpenvPN to ignore any routes and DNS settings pushed by the server."
|
|
|
|
|
bashio::log.warning "Default route will be inserted into custom routing table: ${config["Table"]}"
|
|
|
|
|
bashio::log.warning "This routing table will be used for traffic from the VPN interface and to the configured DNS servers."
|
|
|
|
|
@@ -526,15 +498,6 @@ _openvpn_up() {
|
|
|
|
|
echo "${config["MySelf"]} openvpn postdown" >> ${config["PostDownScript"]}
|
|
|
|
|
chmod 755 ${config["PostDownScript"]}
|
|
|
|
|
|
|
|
|
|
endpoint_ip="$(awk '/^[[:space:]]*remote[[:space:]]/ {print $2; exit}' "${config["ConfigFile"]}")"
|
|
|
|
|
if [ -n "${endpoint_ip}" ]; then
|
|
|
|
|
_check_host "${endpoint_ip}" || result=$?
|
|
|
|
|
[ "${result}" -eq 3 ] && endpoint_ip="$(_resolve_hostname "${endpoint_ip}" | awk '{print $1}')"
|
|
|
|
|
if [ -n "${endpoint_ip}" ]; then
|
|
|
|
|
_endpoint_route_add "${endpoint_ip}" && echo "${endpoint_ip}" > "${config["EndpointIPFile"]}"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Define logging
|
|
|
|
|
declare -A verbosity=(
|
|
|
|
|
["fatal"]=1
|
|
|
|
|
@@ -570,12 +533,34 @@ _openvpn_up() {
|
|
|
|
|
_openvpn_down() {
|
|
|
|
|
# Terminate OpenVPN process
|
|
|
|
|
pkill -f "openvpn --config ${config["ConfigFile"]}" || true
|
|
|
|
|
_endpoint_route_del || true
|
|
|
|
|
# Safety-net cleanup in case the --down callback was never invoked
|
|
|
|
|
_routing_del || true
|
|
|
|
|
# Safety-net: remove blackhole route for VPN server if postdown was never invoked
|
|
|
|
|
if [ -f "${OPENVPN_STATE_DIR}/server_ip" ]; then
|
|
|
|
|
local saved_ip
|
|
|
|
|
saved_ip=$(cat "${OPENVPN_STATE_DIR}/server_ip" 2>/dev/null || true)
|
|
|
|
|
if [ -n "${saved_ip}" ]; then
|
|
|
|
|
ip -4 route del blackhole "${saved_ip}/32" table "${config["Table"]}" 2>/dev/null || true
|
|
|
|
|
fi
|
|
|
|
|
rm -f "${OPENVPN_STATE_DIR}/server_ip"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_openpvn_postup() {
|
|
|
|
|
_openvpn_postup() {
|
|
|
|
|
# Prevent recursive routing: add a blackhole route for the VPN server IP in
|
|
|
|
|
# table 1000. qBittorrent is bound to tun0, so its traffic is policy-routed
|
|
|
|
|
# into table 1000 (rule: from <tun_ip> -> table 1000). Without this, packets
|
|
|
|
|
# destined for the VPN server go through tun0, OpenVPN detects the loop and
|
|
|
|
|
# drops them. The blackhole makes qBittorrent's traffic to the server IP fail
|
|
|
|
|
# immediately (EHOSTUNREACH) instead of looping. OpenVPN itself is not bound
|
|
|
|
|
# to tun0, so its traffic uses the main table and reaches the server normally.
|
|
|
|
|
if [ -n "${trusted_ip:-}" ]; then
|
|
|
|
|
bashio::log.info "Adding blackhole route for VPN server ${trusted_ip} in table ${config["Table"]} to prevent recursive routing."
|
|
|
|
|
ip -4 route add blackhole "${trusted_ip}/32" table "${config["Table"]}" 2>/dev/null \
|
|
|
|
|
&& echo "${trusted_ip}" > "${OPENVPN_STATE_DIR}/server_ip" \
|
|
|
|
|
|| bashio::log.warning "Could not add blackhole route for VPN server ${trusted_ip}."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Add routing rules for VPN interface and DNS servers
|
|
|
|
|
_routing_add || return 1
|
|
|
|
|
# Add firewall rules for VPN interface (only when UPnP port mapping is enabled)
|
|
|
|
|
@@ -586,7 +571,19 @@ _openpvn_postup() {
|
|
|
|
|
_resolvconf "update" || return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_openpvn_postdown() {
|
|
|
|
|
_openvpn_postdown() {
|
|
|
|
|
# Remove blackhole route for VPN server (added in postup to prevent recursive routing)
|
|
|
|
|
local server_ip=""
|
|
|
|
|
if [ -n "${trusted_ip:-}" ]; then
|
|
|
|
|
server_ip="${trusted_ip}"
|
|
|
|
|
elif [ -f "${OPENVPN_STATE_DIR}/server_ip" ]; then
|
|
|
|
|
server_ip=$(cat "${OPENVPN_STATE_DIR}/server_ip" 2>/dev/null || true)
|
|
|
|
|
fi
|
|
|
|
|
if [ -n "${server_ip}" ]; then
|
|
|
|
|
ip -4 route del blackhole "${server_ip}/32" table "${config["Table"]}" 2>/dev/null || true
|
|
|
|
|
rm -f "${OPENVPN_STATE_DIR}/server_ip"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Update resolv.conf to remove VPN DNS servers
|
|
|
|
|
_resolvconf "reset" || true
|
|
|
|
|
# Remove routing rules for VPN interface and DNS servers
|
|
|
|
|
@@ -595,7 +592,6 @@ _openpvn_postdown() {
|
|
|
|
|
if bashio::config.true 'vpn_upnp_enabled'; then
|
|
|
|
|
_firewall_del || true
|
|
|
|
|
fi
|
|
|
|
|
_endpoint_route_del || true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openvpn() {
|
|
|
|
|
@@ -625,7 +621,6 @@ openvpn() {
|
|
|
|
|
config["Interface"]="${interface}"
|
|
|
|
|
config["ConfigFile"]="${config_file}"
|
|
|
|
|
config["Table"]="${config["Table"]:-1000}"
|
|
|
|
|
config["EndpointIPFile"]="${OPENVPN_STATE_DIR}/endpoint-ip"
|
|
|
|
|
config["PostUpScript"]="${OPENVPN_STATE_DIR}/up.sh"
|
|
|
|
|
config["PostDownScript"]="${OPENVPN_STATE_DIR}/down.sh"
|
|
|
|
|
|
|
|
|
|
@@ -644,10 +639,10 @@ openvpn() {
|
|
|
|
|
bashio::log.info "OpenVPN on interface ${config["Interface"]} is down."
|
|
|
|
|
bashio::exit.ok 'OpenVPN stopped.'
|
|
|
|
|
elif [ "${mode}" = "postup" ]; then
|
|
|
|
|
_openpvn_postup
|
|
|
|
|
_openvpn_postup
|
|
|
|
|
bashio::exit.ok 'OpenVPN routes added.'
|
|
|
|
|
elif [ "${mode}" = "postdown" ]; then
|
|
|
|
|
_openpvn_postdown
|
|
|
|
|
_openvpn_postdown
|
|
|
|
|
bashio::exit.ok 'OpenVPN routes deleted.'
|
|
|
|
|
else
|
|
|
|
|
bashio::log.error "Invalid OpenVPN mode specified. Use 'up', 'down', 'postup', or 'postdown'."
|
|
|
|
|
|