IFACE=$(ip route show default 2>/dev/null | awk '/default/ {print $5}' | head -1) IFACE=${IFACE:-eth0}
check_root() { if [[ $EUID -ne 0 ]]; then echo -e "${RED}错误:此脚本需要 root 权限运行!${NC}" exit 1 fi }
check_deps() { local missing=() for cmd in tc iptables ip6tables ip; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [[ ${#missing[@]} -gt 0 ]]; then echo -e "${RED}缺少依赖命令: ${missing[*]}${NC}" exit 1 fi }
ensure_root_qdisc() { if ! tc qdisc show dev "$IFACE" 2>/dev/null | grep -q "htb"; then tc qdisc add dev "$IFACE" root handle 1: htb default 999 2>/dev/null tc class add dev "$IFACE" parent 1: classid 1:999 htb rate 10gbit 2>/dev/null fi }
add_limit() { local port=$1 rate_mbps=$2 local classid mark tc_rate classid=$(port_to_classid "$port") mark=$(port_to_mark "$port") tc_rate=$(mbps_to_tc "$rate_mbps")
# tc class if tc class show dev "$IFACE" classid "$classid" 2>/dev/null | grep -q "htb"; then echo -e "${YELLOW}端口 $port 已存在,正在更新...${NC}" tc class change dev "$IFACE" parent 1: classid "$classid" htb rate "$tc_rate" burst 15k else tc class add dev "$IFACE" parent 1: classid "$classid" htb rate "$tc_rate" burst 15k # IPv4 filter tc filter add dev "$IFACE" parent 1: protocol ip handle "$mark" fw flowid "$classid" # IPv6 filter tc filter add dev "$IFACE" parent 1: protocol ipv6 handle "$mark" fw flowid "$classid" fi
# iptables IPv4 for proto in tcp udp; do iptables -t mangle -C OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" 2>/dev/null \ || iptables -t mangle -A OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" done
# ip6tables IPv6 for proto in tcp udp; do ip6tables -t mangle -C OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" 2>/dev/null \ || ip6tables -t mangle -A OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" done
remove_limit() { local port=$1 local classid mark removed=0 classid=$(port_to_classid "$port") mark=$(port_to_mark "$port")
# 清 iptables IPv4 for proto in tcp udp; do while iptables -t mangle -C OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" 2>/dev/null; do iptables -t mangle -D OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" removed=1 done done
# 清 ip6tables IPv6 for proto in tcp udp; do while ip6tables -t mangle -C OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" 2>/dev/null; do ip6tables -t mangle -D OUTPUT -p "$proto" --sport "$port" \ -m comment --comment "LIMIT_PORT_${port}" -j MARK --set-mark "$mark" removed=1 done done
# 清 tc filter(IPv4 + IPv6) tc filter del dev "$IFACE" parent 1: protocol ip handle "$mark" fw 2>/dev/null && removed=1 tc filter del dev "$IFACE" parent 1: protocol ipv6 handle "$mark" fw 2>/dev/null && removed=1
# 清 tc class if tc class show dev "$IFACE" classid "$classid" 2>/dev/null | grep -q "htb"; then tc class del dev "$IFACE" classid "$classid" 2>/dev/null removed=1 fi
if [[ $removed -eq 1 ]]; then echo -e "${GREEN}✔ 端口 ${BOLD}$port${NC}${GREEN} 限速已解除!${NC}" else echo -e "${YELLOW}⚠ 未找到端口 $port 的限速规则${NC}" fi }