echo "
# ===========================
# 系统资源限制
# ===========================
fs.file-max = 655360
fs.inotify.max_user_instances = 8192
net.core.somaxconn = 3276800
net.core.netdev_max_backlog = 32768
net.core.netdev_budget = 5000
net.core.rmem_max = 33554432
net.core.wmem_max = 33554432
net.core.default_qdisc = fq

# ===========================
# IP 包转发
# ===========================
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding=1

# ===========================
# TCP 连接管理
# ===========================
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
# ===========================
# 缓冲区与窗口设置
# ===========================
net.ipv4.tcp_rmem = 8192 436600 33554432
net.ipv4.tcp_wmem = 8192 436600 33554432
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192
# ===========================
# TCP 优化特性
# ===========================
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_reordering = 300
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_autocorking = 1
net.ipv4.tcp_low_latency = 0
net.ipv4.tcp_no_metrics_save = 0
net.ipv4.tcp_tso_win_divisor = 3
net.ipv4.tcp_frto = 2
net.ipv4.tcp_ecn = 1
net.ipv4.tcp_ecn_fallback = 1
net.ipv4.tcp_mtu_probing = 0
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_adv_win_scale = 1
net.ipv4.tcp_moderate_rcvbuf = 1
# ===========================
# 拥塞控制
# ===========================
net.ipv4.tcp_congestion_control = bbr
" > /etc/sysctl.d/999-sysctl.conf

# 应用配置并清理历史记录
sysctl -p /etc/sysctl.d/999-sysctl.conf && rm ./optimize.sh && rm -rf /root/.bash_history && history -c && clear && echo -e "Optimize Complete!" && echo -e "Please reboot for all configurations to take effect."