mirror of
https://dl.bcrjl.com/ghg/baichal/Socat.git
synced 2026-07-21 17:17:45 +08:00
feat: 分析脚本和转发加速完成度
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
This commit is contained in:
310
socat.sh
310
socat.sh
@@ -875,16 +875,26 @@ get_random_unused_port() {
|
||||
create_systemd_service() {
|
||||
local name=$1
|
||||
local command=$2
|
||||
|
||||
# 检查服务是否已存在且正在运行,若运行则先停止
|
||||
if systemctl is-active --quiet "$name" 2>/dev/null; then
|
||||
systemctl stop "$name" 2>/dev/null
|
||||
fi
|
||||
|
||||
cat > /etc/systemd/system/${name}.service <<EOF
|
||||
[Unit]
|
||||
Description=Socat Forwarding Service
|
||||
After=network.target
|
||||
After=network.target network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=$command
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
StartLimitInterval=60
|
||||
StartLimitBurst=5
|
||||
LimitNOFILE=65536
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -897,7 +907,7 @@ EOF
|
||||
# 创建单个Socat服务
|
||||
create_single_socat_service() {
|
||||
local protocol=$1 # tcp/udp
|
||||
local ip_version=$2 # 4/6
|
||||
local ip_version=$2 # 4/6/domain/domain6
|
||||
local listen_port=$3
|
||||
local target_ip=$4
|
||||
local target_port=$5
|
||||
@@ -906,26 +916,29 @@ create_single_socat_service() {
|
||||
local service_name="socat-${listen_port}-${target_port}-${protocol}"
|
||||
local socat_cmd=""
|
||||
|
||||
# TCP 通用选项:keepalive、地址复用、多进程、缓冲区优化
|
||||
local tcp_common_opts="reuseaddr,fork,so-keepalive,so-sndbuf=1048576,so-rcvbuf=1048576"
|
||||
|
||||
# 根据IP版本和协议构建socat命令
|
||||
if [ "$ip_version" == "4" ]; then
|
||||
socat_cmd="/usr/bin/socat TCP4-LISTEN:${listen_port},reuseaddr,fork TCP4:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat TCP4-LISTEN:${listen_port},${tcp_common_opts} TCP4:${target_ip}:${target_port},connect-timeout=10"
|
||||
if [ "$protocol" == "udp" ]; then
|
||||
socat_cmd="/usr/bin/socat UDP4-LISTEN:${listen_port},reuseaddr,fork UDP4:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat UDP4-LISTEN:${listen_port},reuseaddr,fork,so-sndbuf=1048576,so-rcvbuf=1048576 UDP4:${target_ip}:${target_port}"
|
||||
fi
|
||||
elif [ "$ip_version" == "6" ]; then
|
||||
socat_cmd="/usr/bin/socat TCP6-LISTEN:${listen_port},reuseaddr,fork TCP6:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat TCP6-LISTEN:${listen_port},${tcp_common_opts} TCP6:${target_ip}:${target_port},connect-timeout=10"
|
||||
if [ "$protocol" == "udp" ]; then
|
||||
socat_cmd="/usr/bin/socat UDP6-LISTEN:${listen_port},reuseaddr,fork UDP6:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat UDP6-LISTEN:${listen_port},reuseaddr,fork,so-sndbuf=1048576,so-rcvbuf=1048576 UDP6:${target_ip}:${target_port}"
|
||||
fi
|
||||
elif [ "$ip_version" == "domain" ]; then
|
||||
socat_cmd="/usr/bin/socat TCP4-LISTEN:${listen_port},reuseaddr,fork TCP:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat TCP4-LISTEN:${listen_port},${tcp_common_opts} TCP:${target_ip}:${target_port},connect-timeout=10"
|
||||
if [ "$protocol" == "udp" ]; then
|
||||
socat_cmd="/usr/bin/socat UDP4-LISTEN:${listen_port},reuseaddr,fork UDP:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat UDP4-LISTEN:${listen_port},reuseaddr,fork,so-sndbuf=1048576,so-rcvbuf=1048576 UDP:${target_ip}:${target_port}"
|
||||
fi
|
||||
elif [ "$ip_version" == "domain6" ]; then
|
||||
socat_cmd="/usr/bin/socat TCP6-LISTEN:${listen_port},reuseaddr,fork TCP6:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat TCP6-LISTEN:${listen_port},${tcp_common_opts} TCP6:${target_ip}:${target_port},connect-timeout=10"
|
||||
if [ "$protocol" == "udp" ]; then
|
||||
socat_cmd="/usr/bin/socat UDP6-LISTEN:${listen_port},reuseaddr,fork UDP6:${target_ip}:${target_port}"
|
||||
socat_cmd="/usr/bin/socat UDP6-LISTEN:${listen_port},reuseaddr,fork,so-sndbuf=1048576,so-rcvbuf=1048576 UDP6:${target_ip}:${target_port}"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -1545,93 +1558,11 @@ check_and_enable_bbr() {
|
||||
manage_network_acceleration() {
|
||||
local action="$1" # "enable" 或 "disable"
|
||||
|
||||
# 定义加速配置(键=值 格式)- 专为端口转发优化
|
||||
local -A acceleration_configs=(
|
||||
# 核心转发优化
|
||||
["net.ipv4.tcp_fastopen"]="3"
|
||||
["net.ipv4.tcp_slow_start_after_idle"]="0"
|
||||
["net.ipv4.tcp_mtu_probing"]="1"
|
||||
# 备份文件路径 - 保存用户原始配置
|
||||
local backup_file="$SOCATS_DIR/sysctl_backup.conf"
|
||||
|
||||
# 缓冲区优化 - 大幅提升转发性能
|
||||
["net.core.rmem_max"]="67108864"
|
||||
["net.core.wmem_max"]="67108864"
|
||||
["net.ipv4.tcp_rmem"]="4096 262144 67108864"
|
||||
["net.ipv4.tcp_wmem"]="4096 262144 67108864"
|
||||
["net.ipv4.tcp_mem"]="8388608 12582912 16777216"
|
||||
["net.core.netdev_max_backlog"]="8192"
|
||||
["net.core.netdev_budget"]="600"
|
||||
|
||||
# 连接管理优化
|
||||
["net.ipv4.tcp_max_syn_backlog"]="8192"
|
||||
["net.ipv4.tcp_tw_reuse"]="1"
|
||||
["net.ipv4.tcp_fin_timeout"]="10"
|
||||
["net.ipv4.tcp_keepalive_time"]="600"
|
||||
["net.ipv4.tcp_keepalive_intvl"]="30"
|
||||
["net.ipv4.tcp_keepalive_probes"]="3"
|
||||
["net.ipv4.tcp_max_tw_buckets"]="5000000"
|
||||
|
||||
# 高级TCP特性
|
||||
["net.ipv4.tcp_syncookies"]="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_no_metrics_save"]="1"
|
||||
["net.ipv4.tcp_rfc1337"]="1"
|
||||
["net.ipv4.tcp_timestamps"]="1"
|
||||
["net.ipv4.tcp_ecn"]="0" # 禁用ECN避免兼容性问题
|
||||
|
||||
# 性能调优
|
||||
["net.core.optmem_max"]="65536"
|
||||
["net.ipv4.tcp_notsent_lowat"]="32768"
|
||||
["net.ipv4.ip_local_port_range"]="1024 65535"
|
||||
["net.ipv4.tcp_max_orphans"]="8192"
|
||||
["net.ipv4.tcp_abort_on_overflow"]="0"
|
||||
["net.core.somaxconn"]="8192"
|
||||
)
|
||||
|
||||
# 定义默认配置(键=值 格式)
|
||||
local -A default_configs=(
|
||||
["net.ipv4.tcp_fastopen"]="0"
|
||||
["net.ipv4.tcp_congestion_control"]="cubic"
|
||||
["net.core.default_qdisc"]="pfifo_fast"
|
||||
["net.ipv4.tcp_slow_start_after_idle"]="1"
|
||||
["net.ipv4.tcp_mtu_probing"]="0"
|
||||
["net.core.rmem_max"]="212992"
|
||||
["net.core.wmem_max"]="212992"
|
||||
["net.ipv4.tcp_rmem"]="4096 87380 6291456"
|
||||
["net.ipv4.tcp_wmem"]="4096 16384 4194304"
|
||||
["net.ipv4.tcp_mem"]="378651 504868 757299"
|
||||
["net.core.netdev_max_backlog"]="1000"
|
||||
["net.core.netdev_budget"]="300"
|
||||
["net.ipv4.tcp_max_syn_backlog"]="128"
|
||||
["net.ipv4.tcp_tw_reuse"]="0"
|
||||
["net.ipv4.tcp_fin_timeout"]="60"
|
||||
["net.ipv4.tcp_keepalive_time"]="7200"
|
||||
["net.ipv4.tcp_keepalive_intvl"]="75"
|
||||
["net.ipv4.tcp_keepalive_probes"]="9"
|
||||
["net.ipv4.tcp_max_tw_buckets"]="180000"
|
||||
["net.ipv4.tcp_syncookies"]="1"
|
||||
["net.ipv4.tcp_rfc1337"]="0"
|
||||
["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_no_metrics_save"]="0"
|
||||
["net.ipv4.tcp_timestamps"]="1"
|
||||
["net.ipv4.tcp_ecn"]="2"
|
||||
["net.core.optmem_max"]="20480"
|
||||
["net.ipv4.tcp_notsent_lowat"]="4294967295"
|
||||
["net.ipv4.ip_local_port_range"]="32768 60999"
|
||||
["net.ipv4.tcp_max_orphans"]="8192"
|
||||
["net.ipv4.tcp_abort_on_overflow"]="0"
|
||||
["net.core.somaxconn"]="128"
|
||||
)
|
||||
|
||||
# 定义所有需要清理的配置键 - 包含新增和原有的所有网络参数
|
||||
local cleanup_keys=(
|
||||
# 定义所有需要管理的配置键
|
||||
local all_keys=(
|
||||
"net.ipv4.tcp_fastopen"
|
||||
"net.ipv4.tcp_congestion_control"
|
||||
"net.core.default_qdisc"
|
||||
@@ -1669,42 +1600,179 @@ manage_network_acceleration() {
|
||||
"net.core.somaxconn"
|
||||
)
|
||||
|
||||
# 清理配置文件中的相关配置
|
||||
for key in "${cleanup_keys[@]}"; do
|
||||
sed -i "/${key//\./\\.}/d" /etc/sysctl.conf
|
||||
done
|
||||
# 定义加速配置(键=值 格式)- 专为端口转发优化
|
||||
local -A acceleration_configs=(
|
||||
["net.ipv4.tcp_fastopen"]="3"
|
||||
["net.ipv4.tcp_slow_start_after_idle"]="0"
|
||||
["net.ipv4.tcp_mtu_probing"]="1"
|
||||
["net.core.rmem_max"]="67108864"
|
||||
["net.core.wmem_max"]="67108864"
|
||||
["net.ipv4.tcp_rmem"]="4096 262144 67108864"
|
||||
["net.ipv4.tcp_wmem"]="4096 262144 67108864"
|
||||
["net.ipv4.tcp_mem"]="8388608 12582912 16777216"
|
||||
["net.core.netdev_max_backlog"]="8192"
|
||||
["net.core.netdev_budget"]="600"
|
||||
["net.ipv4.tcp_max_syn_backlog"]="8192"
|
||||
["net.ipv4.tcp_tw_reuse"]="1"
|
||||
["net.ipv4.tcp_fin_timeout"]="10"
|
||||
["net.ipv4.tcp_keepalive_time"]="600"
|
||||
["net.ipv4.tcp_keepalive_intvl"]="30"
|
||||
["net.ipv4.tcp_keepalive_probes"]="3"
|
||||
["net.ipv4.tcp_max_tw_buckets"]="5000000"
|
||||
["net.ipv4.tcp_syncookies"]="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_no_metrics_save"]="1"
|
||||
["net.ipv4.tcp_rfc1337"]="1"
|
||||
["net.ipv4.tcp_timestamps"]="1"
|
||||
["net.ipv4.tcp_ecn"]="0"
|
||||
["net.core.optmem_max"]="65536"
|
||||
["net.ipv4.tcp_notsent_lowat"]="32768"
|
||||
["net.ipv4.ip_local_port_range"]="1024 65535"
|
||||
["net.ipv4.tcp_max_orphans"]="8192"
|
||||
["net.ipv4.tcp_abort_on_overflow"]="0"
|
||||
["net.core.somaxconn"]="8192"
|
||||
)
|
||||
|
||||
# 应用加速配置或默认配置
|
||||
local message=""
|
||||
# 备份当前系统配置(只在开启加速且没有备份时执行)
|
||||
backup_current_config() {
|
||||
if [ -f "$backup_file" ]; then
|
||||
return 0 # 已有备份,不覆盖
|
||||
fi
|
||||
echo "# Sysctl configuration backup - created $(date '+%Y-%m-%d %H:%M:%S')" > "$backup_file"
|
||||
for key in "${all_keys[@]}"; do
|
||||
local current_val=$(sysctl -n "$key" 2>/dev/null)
|
||||
if [[ -n "$current_val" ]]; then
|
||||
echo "$key = $current_val" >> "$backup_file"
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# 从 /etc/sysctl.conf 中清理所有相关配置
|
||||
cleanup_sysctl_conf() {
|
||||
for key in "${all_keys[@]}"; do
|
||||
sed -i "/${key//\./\\.}/d" /etc/sysctl.conf
|
||||
done
|
||||
}
|
||||
|
||||
if [[ "$action" == "enable" ]]; then
|
||||
message="端口转发加速已开启"
|
||||
# 先备份原始配置
|
||||
backup_current_config
|
||||
|
||||
# 额外处理BBR
|
||||
# 清理 sysctl.conf 中旧的相关配置
|
||||
cleanup_sysctl_conf
|
||||
|
||||
# 启用 BBR
|
||||
check_and_enable_bbr
|
||||
|
||||
# 应用加速配置
|
||||
# 应用加速参数
|
||||
local applied=0
|
||||
for key in "${!acceleration_configs[@]}"; do
|
||||
sysctl -w "${key}=${acceleration_configs[$key]}" >/dev/null 2>&1
|
||||
echo "${key} = ${acceleration_configs[$key]}" >> /etc/sysctl.conf
|
||||
if sysctl -w "${key}=${acceleration_configs[$key]}" >/dev/null 2>&1; then
|
||||
applied=$((applied + 1))
|
||||
fi
|
||||
done
|
||||
else
|
||||
message="端口转发加速已关闭"
|
||||
|
||||
# 应用默认配置
|
||||
for key in "${!default_configs[@]}"; do
|
||||
sysctl -w "${key}=${default_configs[$key]}" >/dev/null 2>&1
|
||||
echo "${key} = ${default_configs[$key]}" >> /etc/sysctl.conf
|
||||
# 写入 sysctl.conf 持久化
|
||||
for key in "${!acceleration_configs[@]}"; do
|
||||
echo "$key = ${acceleration_configs[$key]}" >> /etc/sysctl.conf
|
||||
done
|
||||
fi
|
||||
|
||||
sysctl -p >/dev/null 2>&1
|
||||
# 重新加载 sysctl.conf 确保生效
|
||||
sysctl -p >/dev/null 2>&1
|
||||
|
||||
echo -e "${Green}端口转发加速已开启${Font} (已优化 $applied 项参数)"
|
||||
|
||||
# 输出结果
|
||||
if [[ "$action" == "enable" ]]; then
|
||||
echo -e "${Green}${message}${Font}"
|
||||
else
|
||||
echo -e "${Yellow}${message}${Font}"
|
||||
# 关闭加速 - 优先从备份恢复
|
||||
local restored_ok=false
|
||||
|
||||
if [ -f "$backup_file" ]; then
|
||||
echo -e "${Yellow}正在从备份恢复原始网络配置...${Font}"
|
||||
|
||||
# 清理 sysctl.conf 中相关配置
|
||||
cleanup_sysctl_conf
|
||||
|
||||
# 应用备份值
|
||||
local restored_count=0
|
||||
while IFS='=' read -r key value; do
|
||||
key=$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
[[ -z "$key" || "$key" == \#* ]] && continue
|
||||
sysctl -w "$key=$value" >/dev/null 2>&1 && restored_count=$((restored_count + 1))
|
||||
echo "$key = $value" >> /etc/sysctl.conf
|
||||
done < "$backup_file"
|
||||
|
||||
sysctl -p >/dev/null 2>&1
|
||||
|
||||
if [[ $restored_count -gt 0 ]]; then
|
||||
restored_ok=true
|
||||
# 删除备份文件,表示已恢复
|
||||
rm -f "$backup_file"
|
||||
echo -e "${Green}端口转发加速已关闭${Font} (已恢复 $restored_count 项原始配置)"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 如果备份恢复失败,使用内置默认值作为兜底
|
||||
if [[ "$restored_ok" != true ]]; then
|
||||
echo -e "${Yellow}未找到原始配置备份,使用系统默认值恢复...${Font}"
|
||||
|
||||
cleanup_sysctl_conf
|
||||
|
||||
local -A fallback_defaults=(
|
||||
["net.ipv4.tcp_fastopen"]="0"
|
||||
["net.ipv4.tcp_congestion_control"]="cubic"
|
||||
["net.core.default_qdisc"]="pfifo_fast"
|
||||
["net.ipv4.tcp_slow_start_after_idle"]="1"
|
||||
["net.ipv4.tcp_mtu_probing"]="0"
|
||||
["net.core.rmem_max"]="212992"
|
||||
["net.core.wmem_max"]="212992"
|
||||
["net.ipv4.tcp_rmem"]="4096 87380 6291456"
|
||||
["net.ipv4.tcp_wmem"]="4096 16384 4194304"
|
||||
["net.ipv4.tcp_mem"]="378651 504868 757299"
|
||||
["net.core.netdev_max_backlog"]="1000"
|
||||
["net.core.netdev_budget"]="300"
|
||||
["net.ipv4.tcp_max_syn_backlog"]="128"
|
||||
["net.ipv4.tcp_tw_reuse"]="0"
|
||||
["net.ipv4.tcp_fin_timeout"]="60"
|
||||
["net.ipv4.tcp_keepalive_time"]="7200"
|
||||
["net.ipv4.tcp_keepalive_intvl"]="75"
|
||||
["net.ipv4.tcp_keepalive_probes"]="9"
|
||||
["net.ipv4.tcp_max_tw_buckets"]="180000"
|
||||
["net.ipv4.tcp_syncookies"]="1"
|
||||
["net.ipv4.tcp_rfc1337"]="0"
|
||||
["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_no_metrics_save"]="0"
|
||||
["net.ipv4.tcp_timestamps"]="1"
|
||||
["net.ipv4.tcp_ecn"]="2"
|
||||
["net.core.optmem_max"]="20480"
|
||||
["net.ipv4.tcp_notsent_lowat"]="4294967295"
|
||||
["net.ipv4.ip_local_port_range"]="32768 60999"
|
||||
["net.ipv4.tcp_max_orphans"]="8192"
|
||||
["net.ipv4.tcp_abort_on_overflow"]="0"
|
||||
["net.core.somaxconn"]="128"
|
||||
)
|
||||
|
||||
local restored=0
|
||||
for key in "${!fallback_defaults[@]}"; do
|
||||
if sysctl -w "${key}=${fallback_defaults[$key]}" >/dev/null 2>&1; then
|
||||
restored=$((restored + 1))
|
||||
fi
|
||||
echo "$key = ${fallback_defaults[$key]}" >> /etc/sysctl.conf
|
||||
done
|
||||
|
||||
sysctl -p >/dev/null 2>&1
|
||||
|
||||
echo -e "${Yellow}端口转发加速已关闭${Font} (已恢复 $restored 项默认配置)"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user