#!/bin/bash

#
# Copyright (C) 2024 Nosignal <https://github.com/nosignals>
# 
# Contributors:
# - bobbyunknown <https://github.com/bobbyunknown>
#
# https://opensource.org/license/mit
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Variables
neko_dir="/etc/neko"
neko_tmp_dir="$neko_dir/tmp"
log="$neko_dir/tmp/log.txt"

# Get router IP
ROUTER_IP=$(uci get network.lan.ipaddr)

logs() {
    echo "[ `date +%T` ] $1"
    if [ -z $2 ]; then
        echo "[ `date +%T` ] $1" >> $log
    elif [ $2 -eq 2 ]; then
        echo "[ `date +%T` ] $1" > $log
    fi
}

start_tproxy() {
    logs "Setting up TPROXY rules" 2
    
    # Enable IP Forward
    echo 1 > /proc/sys/net/ipv4/ip_forward
    
    stop_tproxy > /dev/null 2>&1
    
    # Setup TPROXY rules
    ip rule add fwmark 1 lookup 100
    ip route add local default dev lo table 100
    
    # Setup iptables
    iptables -t mangle -N NEKO
    iptables -t mangle -F NEKO
    
    # Bypass router & local addresses
    iptables -t mangle -A NEKO -d $ROUTER_IP -j RETURN
    iptables -t mangle -A NEKO -s $ROUTER_IP -j RETURN
    iptables -t mangle -A NEKO -d 127.0.0.0/8 -j RETURN
    iptables -t mangle -A NEKO -d 224.0.0.0/4 -j RETURN
    iptables -t mangle -A NEKO -d 192.168.0.0/16 -j RETURN
    iptables -t mangle -A NEKO -d 10.0.0.0/8 -j RETURN
    iptables -t mangle -A NEKO -d 172.16.0.0/12 -j RETURN
    
    # Bypass LAN access
    iptables -t mangle -A NEKO -i br-lan -d 192.168.0.0/16 -j RETURN
    iptables -t mangle -A NEKO -i br-lan -d $ROUTER_IP -j RETURN
    
    # Bypass DNS traffic
    iptables -t mangle -A NEKO -p udp --dport 53 -j RETURN
    iptables -t mangle -A NEKO -p tcp --dport 53 -j RETURN
    
    # Mark the packets for TPROXY
    iptables -t mangle -A NEKO -p tcp -j TPROXY --on-port 9888 --tproxy-mark 1
    iptables -t mangle -A NEKO -p udp -j TPROXY --on-port 9888 --tproxy-mark 1
    
    # Apply rules to PREROUTING
    iptables -t mangle -A PREROUTING -j NEKO
    
    # Add OUTPUT rules untuk local traffic
    iptables -t mangle -N NEKO_MARK
    iptables -t mangle -F NEKO_MARK
    
    # Bypass router
    iptables -t mangle -A NEKO_MARK -d $ROUTER_IP -j RETURN
    iptables -t mangle -A NEKO_MARK -s $ROUTER_IP -j RETURN
    
    # Bypass local networks
    iptables -t mangle -A NEKO_MARK -d 127.0.0.0/8 -j RETURN
    iptables -t mangle -A NEKO_MARK -d 224.0.0.0/4 -j RETURN
    iptables -t mangle -A NEKO_MARK -d 192.168.0.0/16 -j RETURN
    iptables -t mangle -A NEKO_MARK -d 10.0.0.0/8 -j RETURN
    iptables -t mangle -A NEKO_MARK -d 172.16.0.0/12 -j RETURN
    
    # Mark remaining traffic
    iptables -t mangle -A NEKO_MARK -j MARK --set-mark 1
    iptables -t mangle -A OUTPUT -j NEKO_MARK
    
    logs "TPROXY rules applied successfully"
}

stop_tproxy() {
    logs "Cleaning up TPROXY rules" 2
    
    # Clean up ip rules
    ip rule del fwmark 1 lookup 100 2>/dev/null
    ip route del local default dev lo table 100 2>/dev/null
    
    # Clean up iptables PREROUTING
    iptables -t mangle -D PREROUTING -j NEKO 2>/dev/null
    iptables -t mangle -F NEKO 2>/dev/null
    iptables -t mangle -X NEKO 2>/dev/null
    
    # Clean up iptables OUTPUT
    iptables -t mangle -D OUTPUT -j NEKO_MARK 2>/dev/null
    iptables -t mangle -F NEKO_MARK 2>/dev/null
    iptables -t mangle -X NEKO_MARK 2>/dev/null
    
    logs "TPROXY rules cleaned up"
}

case "$1" in
    -s|--start)
        start_tproxy
        ;;
    -k|--stop)
        stop_tproxy
        ;;
    *)
        echo "Usage: $0 {-s|--start|-k|--stop}"
        exit 1
        ;;
esac