#!/bin/bash
get_pppd_config_name() {
    local pppd_process_line
    pppd_process_line=$(ps -eo args | grep -E 'pppd.*call' | grep -v 'grep' | head -n 1)
    if [ -z "$pppd_process_line" ]; then
        echo ""
        return
    fi
    local config_name
    config_name=$(echo "$pppd_process_line" | awk '{print $3}')
    echo "$config_name"
}
resolve_to_ipv4() {
    local addr="$1"
    if [[ "$addr" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        echo "$addr"
        return
    fi
    local resolved_ip
    resolved_ip=$(getent ahosts "$addr" 2>/dev/null | awk '/STREAM/ {print $1; exit}')
    if [[ -z "$resolved_ip" ]]; then
        resolved_ip=$(nslookup "$addr" 2>/dev/null | awk '/^Address: / {print $2; exit}')
    fi
    if [[ -n "$resolved_ip" ]] && [[ "$resolved_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
        echo "$resolved_ip"
    else
        echo "$addr"
    fi
}
check_redirect_gateway() {
    local config_name="$1"
    r=$(iniparse -f /etc/pptp/pptpclient.conf -s "$config_name" -k redirect-gateway -c)
    r=${r:-"0"}
    if [ "$r" -eq "1" ]; then
        echo "1"
        return
    fi
    r=$(iniparse -f /etc/l2tp/l2tpclient.conf -s "$config_name" -k def_gateway -c)
    r=${r:-"0"}
    if [ "$r" -eq "1" ]; then
        echo "1"
        return
    fi
}
get_xl2tpd_config_name() {
    local xl2tpd_process_line
    xl2tpd_process_line=$(ps -eo args | grep -E 'xl2tpd.*-c' | grep -v 'grep' | head -n 1)
    if [[ -z "$xl2tpd_process_line" ]]; then
        echo ""
        return
    fi
    local conf_path
    conf_path=$(echo "$xl2tpd_process_line" | grep -oP '/etc/l2tp/connect_\K[^ ]+')
    if [[ -z "$conf_path" ]]; then
        echo ""
        return
    fi
    echo "$conf_path"
}
REMOTE_IP=$5
DEVICE=$1
config_name=$(get_xl2tpd_config_name)
if [[ -z "$config_name" ]]; then
    config_name=$(get_pppd_config_name)
fi
echo "$config_name:$DEVICE" > /tmp/ppp_"$config_name"
vpn_server_ip=""
if [[ -n $(get_xl2tpd_config_name) ]]; then
    vpn_server_ip=$(iniparse -f /etc/l2tp/l2tpclient.conf -s "$config_name" -k server_address -c)
else
    vpn_server_ip=$(iniparse -f /etc/pptp/pptpclient.conf -s "$config_name" -k server_address -c)
fi
if [ -n "$vpn_server_ip" ]; then
    vpn_server_ip=$(resolve_to_ipv4 "$vpn_server_ip")
    route_info=$(ip route get 8.8.8.8 2>/dev/null)
    physical_gw_ip=$(echo "$route_info" | awk '/via/ {print $3}')
    physical_gw_dev=$(echo "$route_info" | awk '/dev/ {print $5}')
    if [ -n "$physical_gw_ip" ] && [ -n "$physical_gw_dev" ]; then
        echo "$vpn_server_ip $physical_gw_ip $physical_gw_dev" > "/tmp/ppp_route_info_$config_name"
    fi
fi
redirect_gateway=$(check_redirect_gateway "$config_name")
if [ "$redirect_gateway" -eq "1" ]; then
    if [ -n "$vpn_server_ip" ] && [ -n "$physical_gw_ip" ] && [ -n "$physical_gw_dev" ]; then
        ip route add "$vpn_server_ip" via "$physical_gw_ip" dev "$physical_gw_dev" 2>/dev/null || true
    fi
    ip route add default via "$REMOTE_IP" dev "$DEVICE"
    /etc/ppp/route-monitor.sh start 2>/dev/null || true
fi
