Compare commits
No commits in common. "ed76e609beb9fe4b6b78a5eb1434201cf1c3ded7" and "2fe43cda9374d152d791b96d1434615f8f5284ab" have entirely different histories.
ed76e609be
...
2fe43cda93
118
socat.sh
118
socat.sh
|
|
@ -5,7 +5,7 @@ export PATH
|
||||||
# ====================================================
|
# ====================================================
|
||||||
# 系统要求: CentOS 6+、Debian 7+、Ubuntu 14+
|
# 系统要求: CentOS 6+、Debian 7+、Ubuntu 14+
|
||||||
# 描述: Socat 一键安装管理脚本
|
# 描述: Socat 一键安装管理脚本
|
||||||
# 版本: 3.4
|
# 版本: 3.3
|
||||||
# ====================================================
|
# ====================================================
|
||||||
|
|
||||||
Green="\033[32m"
|
Green="\033[32m"
|
||||||
|
|
@ -14,14 +14,8 @@ Blue="\033[34m"
|
||||||
Red="\033[31m"
|
Red="\033[31m"
|
||||||
Yellow="\033[33m"
|
Yellow="\033[33m"
|
||||||
|
|
||||||
# 创建 socats 目录并定义相关路径
|
|
||||||
SOCATS_DIR="$HOME/socats"
|
|
||||||
mkdir -p "$SOCATS_DIR"
|
|
||||||
|
|
||||||
# 配置文件路径
|
# 配置文件路径
|
||||||
CONFIG_FILE="$SOCATS_DIR/socat_forwards.conf"
|
CONFIG_FILE="./socat_forwards.conf"
|
||||||
TCP_LOG="$SOCATS_DIR/socat_tcp.log"
|
|
||||||
UDP_LOG="$SOCATS_DIR/socat_udp.log"
|
|
||||||
|
|
||||||
# 清屏函数
|
# 清屏函数
|
||||||
clear_screen() {
|
clear_screen() {
|
||||||
|
|
@ -212,6 +206,7 @@ check_ipv6_support() {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# 配置Socat
|
# 配置Socat
|
||||||
config_socat(){
|
config_socat(){
|
||||||
echo -e "${Green}请选择转发类型:${Font}"
|
echo -e "${Green}请选择转发类型:${Font}"
|
||||||
|
|
@ -347,30 +342,25 @@ configure_firewall() {
|
||||||
"firewalld")
|
"firewalld")
|
||||||
if [ "$ip_version" == "ipv4" ]; then
|
if [ "$ip_version" == "ipv4" ]; then
|
||||||
firewall-cmd --zone=public --add-port=${port}/tcp --permanent >/dev/null 2>&1
|
firewall-cmd --zone=public --add-port=${port}/tcp --permanent >/dev/null 2>&1
|
||||||
firewall-cmd --zone=public --add-port=${port}/udp --permanent >/dev/null 2>&1
|
|
||||||
else
|
else
|
||||||
firewall-cmd --zone=public --add-port=${port}/tcp --permanent --ipv6 >/dev/null 2>&1
|
firewall-cmd --zone=public --add-port=${port}/tcp --permanent --ipv6 >/dev/null 2>&1
|
||||||
firewall-cmd --zone=public --add-port=${port}/udp --permanent --ipv6 >/dev/null 2>&1
|
|
||||||
fi
|
fi
|
||||||
firewall-cmd --reload >/dev/null 2>&1
|
firewall-cmd --reload >/dev/null 2>&1
|
||||||
;;
|
;;
|
||||||
"ufw")
|
"ufw")
|
||||||
ufw allow ${port}/tcp >/dev/null 2>&1
|
ufw allow ${port}/tcp >/dev/null 2>&1
|
||||||
ufw allow ${port}/udp >/dev/null 2>&1
|
|
||||||
;;
|
;;
|
||||||
"iptables")
|
"iptables")
|
||||||
if [ "$ip_version" == "ipv4" ]; then
|
if [ "$ip_version" == "ipv4" ]; then
|
||||||
iptables -I INPUT -p tcp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
iptables -I INPUT -p tcp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
||||||
iptables -I INPUT -p udp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
|
||||||
else
|
else
|
||||||
ip6tables -I INPUT -p tcp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
ip6tables -I INPUT -p tcp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
||||||
ip6tables -I INPUT -p udp --dport ${port} -j ACCEPT >/dev/null 2>&1
|
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
echo -e "${Green}已成功为 ${ip_version} 端口 ${port} 配置防火墙规则 (TCP/UDP)。${Font}"
|
echo -e "${Green}已成功为 ${ip_version} 端口 ${port} 配置防火墙规则。${Font}"
|
||||||
else
|
else
|
||||||
echo -e "${Yellow}检测到 ${firewall_tool},但无权限修改。请手动配置 ${ip_version} 端口 ${port} 的防火墙规则 (TCP/UDP)。${Font}"
|
echo -e "${Yellow}检测到 ${firewall_tool},但无权限修改。请手动配置 ${ip_version} 端口 ${port} 的防火墙规则。${Font}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -428,14 +418,14 @@ start_socat(){
|
||||||
|
|
||||||
if [ "$ip_version" == "1" ]; then
|
if [ "$ip_version" == "1" ]; then
|
||||||
# TCP转发
|
# TCP转发
|
||||||
nohup socat TCP4-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP4:${socatip}:${port2},keepalive,nodelay >> "$TCP_LOG" 2>&1 &
|
nohup socat TCP4-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP4:${socatip}:${port2},keepalive,nodelay >> ./socat_tcp.log 2>&1 &
|
||||||
# UDP转发
|
# UDP转发
|
||||||
nohup socat UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> "$UDP_LOG" 2>&1 &
|
nohup socat UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> ./socat_udp.log 2>&1 &
|
||||||
elif [ "$ip_version" == "2" ]; then
|
elif [ "$ip_version" == "2" ]; then
|
||||||
# TCP转发
|
# TCP转发
|
||||||
nohup socat TCP6-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP6:${socatip}:${port2},keepalive,nodelay >> "$TCP_LOG" 2>&1 &
|
nohup socat TCP6-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP6:${socatip}:${port2},keepalive,nodelay >> ./socat_tcp.log 2>&1 &
|
||||||
# UDP转发
|
# UDP转发
|
||||||
nohup socat UDP6-LISTEN:${port1},reuseaddr,fork UDP6:${socatip}:${port2} >> "$UDP_LOG" 2>&1 &
|
nohup socat UDP6-LISTEN:${port1},reuseaddr,fork UDP6:${socatip}:${port2} >> ./socat_udp.log 2>&1 &
|
||||||
else
|
else
|
||||||
echo -e "${Red}无效的选项,退出配置。${Font}"
|
echo -e "${Red}无效的选项,退出配置。${Font}"
|
||||||
return
|
return
|
||||||
|
|
@ -469,9 +459,9 @@ start_socat(){
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo -e "${Red}Socat启动失败,请检查配置和系统设置。${Font}"
|
echo -e "${Red}Socat启动失败,请检查配置和系统设置。${Font}"
|
||||||
echo "检查 $TCP_LOG 和 $UDP_LOG 文件以获取更多信息。"
|
echo "检查 socat_tcp.log 和 socat_udp.log 文件以获取更多信息。"
|
||||||
tail -n 10 "$TCP_LOG"
|
tail -n 10 ./socat_tcp.log
|
||||||
tail -n 10 "$UDP_LOG"
|
tail -n 10 ./socat_udp.log
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -483,11 +473,11 @@ add_to_startup() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$ip_version" == "1" ]; then
|
if [ "$ip_version" == "1" ]; then
|
||||||
tcp_startup_cmd="nohup socat TCP4-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP4:${socatip}:${port2},keepalive,nodelay >> $TCP_LOG 2>&1 &"
|
tcp_startup_cmd="nohup socat TCP4-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP4:${socatip}:${port2},keepalive,nodelay >> $(pwd)/socat_tcp.log 2>&1 &"
|
||||||
udp_startup_cmd="nohup socat UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> $UDP_LOG 2>&1 &"
|
udp_startup_cmd="nohup socat UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> $(pwd)/socat_udp.log 2>&1 &"
|
||||||
else
|
else
|
||||||
tcp_startup_cmd="nohup socat TCP6-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP6:${socatip}:${port2},keepalive,nodelay >> $TCP_LOG 2>&1 &"
|
tcp_startup_cmd="nohup socat TCP6-LISTEN:${port1},reuseaddr,fork,keepalive,nodelay TCP6:${socatip}:${port2},keepalive,nodelay >> $(pwd)/socat_tcp.log 2>&1 &"
|
||||||
udp_startup_cmd="nohup socat UDP6-LISTEN:${port1},reuseaddr,fork UDP6:${socatip}:${port2} >> $UDP_LOG 2>&1 &"
|
udp_startup_cmd="nohup socat UDP6-LISTEN:${port1},reuseaddr,fork UDP6:${socatip}:${port2} >> $(pwd)/socat_udp.log 2>&1 &"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! grep -q "$tcp_startup_cmd" "$rc_local"; then
|
if ! grep -q "$tcp_startup_cmd" "$rc_local"; then
|
||||||
|
|
@ -500,6 +490,48 @@ add_to_startup() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 显示和删除转发
|
||||||
|
view_delete_forward() {
|
||||||
|
if [ ! -s "$CONFIG_FILE" ]; then
|
||||||
|
echo -e "${Red}当前没有活动的转发。${Font}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${Green}当前转发列表:${Font}"
|
||||||
|
local i=1
|
||||||
|
local entries=()
|
||||||
|
while IFS=' ' read -r ip_type listen_port remote_ip remote_port; do
|
||||||
|
entries+=("$ip_type $listen_port $remote_ip $remote_port")
|
||||||
|
if [ "$ip_type" == "ipv4" ]; then
|
||||||
|
echo "$i. IPv4: $ip:$listen_port --> $remote_ip:$remote_port"
|
||||||
|
else
|
||||||
|
echo "$i. IPv6: [$ipv6]:$listen_port --> [$remote_ip]:$remote_port"
|
||||||
|
fi
|
||||||
|
((i++))
|
||||||
|
done < "$CONFIG_FILE"
|
||||||
|
|
||||||
|
read -p "请输入要删除的转发编号(多个编号用空格分隔,直接回车取消): " numbers
|
||||||
|
if [ -n "$numbers" ]; then
|
||||||
|
local nums_to_delete=($(echo "$numbers" | tr ' ' '\n' | sort -rn))
|
||||||
|
for num in "${nums_to_delete[@]}"; do
|
||||||
|
if [ $num -ge 1 ] && [ $num -lt $i ]; then
|
||||||
|
local index=$((num-1))
|
||||||
|
IFS=' ' read -r ip_type listen_port remote_ip remote_port <<< "${entries[$index]}"
|
||||||
|
pkill -f "socat.*LISTEN:${listen_port}"
|
||||||
|
sed -i "${num}d" "$CONFIG_FILE"
|
||||||
|
remove_from_startup "$listen_port" "$ip_type"
|
||||||
|
if [ "$ip_type" == "ipv4" ]; then
|
||||||
|
echo -e "${Green}已删除IPv4转发: $ip:$listen_port${Font}"
|
||||||
|
else
|
||||||
|
echo -e "${Green}已删除IPv6转发: [$ipv6]:$listen_port${Font}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e "${Red}无效的编号: $num${Font}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# 从开机自启动中移除
|
# 从开机自启动中移除
|
||||||
remove_from_startup() {
|
remove_from_startup() {
|
||||||
local listen_port=$1
|
local listen_port=$1
|
||||||
|
|
@ -534,8 +566,6 @@ kill_all_socat() {
|
||||||
# 清理开机自启动脚本
|
# 清理开机自启动脚本
|
||||||
sed -i '/socat TCP4-LISTEN/d' /etc/rc.local
|
sed -i '/socat TCP4-LISTEN/d' /etc/rc.local
|
||||||
sed -i '/socat TCP6-LISTEN/d' /etc/rc.local
|
sed -i '/socat TCP6-LISTEN/d' /etc/rc.local
|
||||||
sed -i '/socat UDP4-LISTEN/d' /etc/rc.local
|
|
||||||
sed -i '/socat UDP6-LISTEN/d' /etc/rc.local
|
|
||||||
echo -e "${Green}已从配置和开机自启动中移除所有 Socat 转发${Font}"
|
echo -e "${Green}已从配置和开机自启动中移除所有 Socat 转发${Font}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -582,7 +612,7 @@ check_and_enable_bbr() {
|
||||||
enable_acceleration() {
|
enable_acceleration() {
|
||||||
echo -e "${Green}正在开启端口转发加速...${Font}"
|
echo -e "${Green}正在开启端口转发加速...${Font}"
|
||||||
|
|
||||||
# 清理旧设置
|
# 清理旧设置
|
||||||
sed -i '/net.ipv4.tcp_fastopen/d' /etc/sysctl.conf
|
sed -i '/net.ipv4.tcp_fastopen/d' /etc/sysctl.conf
|
||||||
sed -i '/net.ipv4.tcp_slow_start_after_idle/d' /etc/sysctl.conf
|
sed -i '/net.ipv4.tcp_slow_start_after_idle/d' /etc/sysctl.conf
|
||||||
sed -i '/net.ipv4.tcp_mtu_probing/d' /etc/sysctl.conf
|
sed -i '/net.ipv4.tcp_mtu_probing/d' /etc/sysctl.conf
|
||||||
|
|
@ -644,6 +674,8 @@ enable_acceleration() {
|
||||||
|
|
||||||
# 持久化设置
|
# 持久化设置
|
||||||
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
|
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
|
||||||
|
echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
|
||||||
|
echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
|
||||||
echo "net.ipv4.tcp_slow_start_after_idle = 0" >> /etc/sysctl.conf
|
echo "net.ipv4.tcp_slow_start_after_idle = 0" >> /etc/sysctl.conf
|
||||||
echo "net.ipv4.tcp_mtu_probing = 1" >> /etc/sysctl.conf
|
echo "net.ipv4.tcp_mtu_probing = 1" >> /etc/sysctl.conf
|
||||||
# 添加新增的优化参数到sysctl.conf
|
# 添加新增的优化参数到sysctl.conf
|
||||||
|
|
@ -687,7 +719,6 @@ disable_acceleration() {
|
||||||
sysctl -w net.ipv4.tcp_mtu_probing=0
|
sysctl -w net.ipv4.tcp_mtu_probing=0
|
||||||
|
|
||||||
# 恢复其他参数到默认值
|
# 恢复其他参数到默认值
|
||||||
sysctl -w net.core.rmem_max=212992
|
|
||||||
sysctl -w net.core.wmem_max=212992
|
sysctl -w net.core.wmem_max=212992
|
||||||
sysctl -w net.ipv4.tcp_rmem='4096 87380 6291456'
|
sysctl -w net.ipv4.tcp_rmem='4096 87380 6291456'
|
||||||
sysctl -w net.ipv4.tcp_wmem='4096 16384 4194304'
|
sysctl -w net.ipv4.tcp_wmem='4096 16384 4194304'
|
||||||
|
|
@ -743,23 +774,14 @@ disable_acceleration() {
|
||||||
|
|
||||||
# 显示菜单
|
# 显示菜单
|
||||||
show_menu() {
|
show_menu() {
|
||||||
echo -e "${Green}
|
echo -e "${Green}========= Socat 管理脚本 ==========${Font}"
|
||||||
_____ __
|
echo "1. 添加新转发"
|
||||||
/ ___/____ _________ _/ /_
|
echo "2. 查看或删除转发"
|
||||||
\__ \/ __ \/ ___/ __ \`/ __/
|
echo "3. 强制终止所有 Socat 进程"
|
||||||
___/ / /_/ / /__/ /_/ / /_
|
echo "4. 开启端口转发加速"
|
||||||
/____/\____/\___/\__,_/\__/ ${Yellow}Management Script${Font}"
|
echo "5. 关闭端口转发加速"
|
||||||
echo -e "${Blue}==========================================${Font}"
|
echo "6. 退出脚本"
|
||||||
echo -e "${Yellow}1.${Font} 添加新转发"
|
echo -e "${Green}=====================================${Font}"
|
||||||
echo -e "${Yellow}2.${Font} 查看或删除转发"
|
|
||||||
echo -e "${Yellow}3.${Font} 强制终止所有 Socat 进程"
|
|
||||||
echo -e "${Yellow}4.${Font} 开启端口转发加速"
|
|
||||||
echo -e "${Yellow}5.${Font} 关闭端口转发加速"
|
|
||||||
echo -e "${Yellow}6.${Font} 退出脚本"
|
|
||||||
echo -e "${Blue}==========================================${Font}"
|
|
||||||
echo -e "${Green}当前 IPv4: ${ip:-未知}${Font}"
|
|
||||||
echo -e "${Green}当前 IPv6: ${ipv6:-未知}${Font}"
|
|
||||||
echo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 主程序
|
# 主程序
|
||||||
|
|
@ -778,8 +800,6 @@ main() {
|
||||||
init_config
|
init_config
|
||||||
clear_screen
|
clear_screen
|
||||||
|
|
||||||
echo -e "${Green}所有配置和日志文件将保存在: $SOCATS_DIR${Font}"
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
show_menu
|
show_menu
|
||||||
read -p "请输入选项 [1-6]: " choice
|
read -p "请输入选项 [1-6]: " choice
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue