#!/bin/bash
export LANG=en_US.UTF-8
# 定义颜色
re='\e[0m'
red='\e[1;91m'
white='\e[1;97m'
green='\e[1;32m'
yellow='\e[1;33m'
purple='\e[1;35m'
skyblue='\e[1;96m'
# 配置文件路径
cfnat_file=$HOME/cfnat
config_file=$cfnat_file/cfnat.conf
ps=""
Androidps=""
cfnatcolo="SJC,LAX,HKG"
cfnatport="1234"
cfnatdelay="300"
################################################################
# 安装依赖包
install() {
if [ $# -eq 0 ]; then
echo -e "${red}未提供软件包参数!${re}"
return 1
fi
for package in "$@"; do
if command -v "$package" &>/dev/null; then
echo -e "${green}${package}已经安装了!${re}"
continue
fi
echo -e "${yellow}正在安装 ${package}...${re}"
if [ -n "$PREFIX" ] && [ -d "$PREFIX" ] && echo "$PREFIX" | grep -q "/data/data/com.termux/files"; then
pkg install -y "$package"
elif command -v apt &>/dev/null; then
apt install -y "$package"
elif command -v dnf &>/dev/null; then
dnf install -y "$package"
elif command -v yum &>/dev/null; then
yum install -y "$package"
elif command -v apk &>/dev/null; then
apk add "$package"
elif [ -f /etc/openwrt_release ]; then
opkg update
opkg install coreutils coreutils-nohup crontab
else
echo -e"${red}暂不支持你的系统!${re}"
return 1
fi
done
return 0
}
# 选择客户端 CPU 架构
archAffix(){
if [ -n "$PREFIX" ] && [ -d "$PREFIX" ] && echo "$PREFIX" | grep -q "/data/data/com.termux/files"; then
echo 'termux'
else
case "$(uname -m)" in
i386 | i686 ) echo '386' ;;
x86_64 | amd64 ) echo 'amd64' ;;
armv7 ) echo 'arm' ;;
armv8 | arm64 | aarch64 ) echo 'arm64' ;;
s390x ) echo 's390x' ;;
* ) echo '未知' ;;
esac
fi
}
# 等待用户返回
break_end() {
echo -e "${green}执行完成${re}"
echo -e "${yellow}按任意键返回...${re}"
read -n 1 -s -r -p ""
echo ""
clear
}
# 安装cfnat
install_cfnat(){
if [ -n "$1" ]; then
install curl nohup crontab
else
echo -e "脚本所需依赖包 ${yellow}curl nohup crontab${re}"
read -p "是否允许脚本自动安装以上所需的依赖包(Y): " install_apps
install_apps=${install_apps^^} # 转换为大写
if [ "$install_apps" == "Y" ]; then
install curl nohup crontab
fi
fi
# 检测 $cfnat_file 文件夹是否存在
if [ ! -d $cfnat_file ]; then
# 如果不存在,则创建该文件夹
mkdir $cfnat_file
echo "目录 $cfnat_file 已创建。"
fi
# 检测 $cfnat_file/locations.json 是否存在
if [ ! -f $cfnat_file/locations.json ]; then
# 如果不存在,则使用 curl 下载文件
curl -SL https://cf.090227.xyz/locations -o $cfnat_file/locations.json
if [ ! -f $cfnat_file/locations.json ]; then
curl -SL https://raw.cmliussss.com/cfnat/locations.json -o $cfnat_file/locations.json
fi
if [ ! -f $cfnat_file/locations.json ]; then
echo "locations.json 下载失败。"
else
echo "locations.json 下载完成。"
fi
else
echo "locations.json 准备就绪。"
fi
# 检测 $cfnat_file/cfnat 是否存在
if [ ! -f $cfnat_file/cfnat ]; then
# 如果不存在,则使用 curl 下载文件
if [ ${Architecture} = "termux" ]; then
curl -SL https://raw.cmliussss.com/cfnat/cfnat-termux -o $cfnat_file/cfnat
else
curl -SL https://raw.cmliussss.com/cfnat/cfnat-linux-${Architecture} -o $cfnat_file/cfnat
fi
chmod +x $cfnat_file/cfnat
echo "cfnat主程序 下载完成。"
else
echo "cfnat主程序 准备就绪。"
fi
}
# 卸载cfnat
uninstall_cfnat(){
kill_cfnat
delete_cron
rm -rf $cfnat_file
echo "cfnat 已清除。"
}
check_cfnat(){
if grep -q "^release=" "$config_file"; then
release=$(grep "^release=" "$config_file" | cut -d'=' -f2)
else
if [[ -f /etc/redhat-release ]]; then
release="Centos"
elif grep -q -E -i "alpine" /etc/issue 2>/dev/null; then
release="alpine"
elif grep -q -E -i "debian" /etc/issue 2>/dev/null; then
release="Debian"
elif grep -q -E -i "ubuntu" /etc/issue 2>/dev/null; then
release="Ubuntu"
elif grep -q -E -i "centos|red hat|redhat" /etc/issue 2>/dev/null; then
release="Centos"
elif grep -q -E -i "openwrt" /proc/version 2>/dev/null; then
release="OpenWRT"
elif grep -q -E -i "debian" /proc/version 2>/dev/null; then
release="Debian"
elif grep -q -E -i "ubuntu" /proc/version 2>/dev/null; then
release="Ubuntu"
elif grep -q -E -i "centos|red hat|redhat" /proc/version 2>/dev/null; then
release="Centos"
else
site_release
fi
fi
if grep -q "^Architecture=" "$config_file"; then
Architecture=$(grep "^Architecture=" "$config_file" | cut -d'=' -f2)
else
Architecture=$(archAffix)
if [ "$Architecture" = "未知" ]; then
site_Architecture
fi
fi
if [ ${Architecture} = "termux" ]; then
#install net-tools
lanip=$(ifconfig | grep -Eo 'inet (192\.168|10\.|172\.(1[6-9]|2[0-9]|3[0-1]))[0-9.]+' | awk '{print $2}')
Androidps="${yellow} 安卓手机推荐使用 ${green}调试运行 ${yellow}来执行任务"
else
lanip=$(ip -4 addr | grep -Eo 'inet (192\.168|10\.|172\.(1[6-9]|2[0-9]|3[0-1]))[0-9.]+/[0-9]+' | awk '{print $2}' | cut -d'/' -f1)
fi
# 检测 $cfnat_file 文件夹是否存在
if [ -d $cfnat_file ]; then
# 检测 $cfnat_file/cfnat 文件是否存在
if [ -f $cfnat_file/cfnat ] && [ -f $cfnat_file/locations.json ]; then
InstallationStatus="${green}已安装"
OneclickInstallation="${red}一键卸载"
if [ -f "$config_file" ]; then
# 如果存在,读取 colo 字段内容
colo=$(grep '^colo=' "$config_file" | cut -d'=' -f2)
port=$(grep '^port=' "$config_file" | cut -d'=' -f2)
delay=$(grep '^delay=' "$config_file" | cut -d'=' -f2)
else
# 如果不存在,创建配置文件并写入内容
colo="SJC,LAX,HKG"
echo "colo=${colo}" >> "$config_file"
port="1234"
echo "port=${port}" >> "$config_file"
delay="300"
echo "delay=${delay}" >> "$config_file"
fi
cfnatcolo=$colo
cfnatport=$port
cfnatdelay=$delay
if [ -f "$cfnat_file/ips-v4.txt" ]; then
# 获取文件的行数
line_count=$(wc -l < "$cfnat_file/ips-v4.txt")
# 判断行数是否超过5000
if [ "$line_count" -gt 7000 ]; then
#echo "文件行数超过7000行"
IPLibrary="${skyblue}AS209242"
else
#echo "文件行数未超过7000行"
IPLibrary="${green}AS13335"
fi
else
up_as13335
fi
else
InstallationStatus="${red}未安装"
OneclickInstallation="${green}一键安装"
fi
else
InstallationStatus="${red}未安装"
OneclickInstallation="${green}一键安装"
fi
if [ "$release" = "OpenWRT" ]; then
cfnatpid=$(pgrep -f "./cfnat -colo")
# 检查是否找到了 PID
if [ -n "$cfnatpid" ]; then
statecfnat="${green}运行中"
else
statecfnat="${red}未运行"
fi
else
# 检测 $cfnat_file/cfnat 程序是否正在运行
if pgrep -x "cfnat" > /dev/null; then
# 如果正在运行,赋值
statecfnat="${green}运行中"
else
# 如果未运行,赋值
statecfnat="${red}未运行"
fi
fi
}
add_cron(){
if [ "${Architecture}" != "termux" ]; then
delete_cron
cron_cfnat="*/5 * * * * cd ~ && bash cfnat.sh $cfnatcolo"
(crontab -l; echo "$cron_cfnat") | crontab -
echo "添加 crontab 守护任务 $cron_cfnat"
fi
}
delete_cron(){
if [ "${Architecture}" != "termux" ]; then
crontab -l | grep -v 'bash cfnat.sh' | crontab -
echo "清理 crontab 守护任务"
fi
}
up_as13335(){
echo "下载IP库 AS13335"
# 下载AS13335.txt文件
curl -SL https://raw.cmliussss.com/cfnat/ips-v4.txt -o $cfnat_file/AS13335.txt
if [ "$release" = "OpenWRT" ]; then
mv $cfnat_file/AS13335.txt $cfnat_file/ips-v4.txt
echo "IP库更新为 AS13335"
else
# 获取下载的文件大小(以字节为单位)
file_size=$(stat -c%s "$cfnat_file/AS13335.txt")
# 检测文件是否大于10K(10240字节)
if [ $file_size -gt 10240 ]; then
# 文件大小超过10K,重命名为ips-v4.txt,覆盖已有文件
mv $cfnat_file/AS13335.txt $cfnat_file/ips-v4.txt
echo "IP库更新为 AS13335"
else
# 文件小于或等于10K,提示失败并删除下载的文件
echo "更新IP库失败"
rm $cfnat_file/AS13335.txt
fi
fi
}
config_cfnat(){
echo "电信 推荐 SJC,LAX"
echo "移动/联通 推荐 HKG"
# 读取并处理数据中心输入
read -p "输入筛选数据中心(多个数据中心用逗号隔开,留空则使用 SJC,LAX,HKG): " colo
colo=${colo:-"SJC,LAX,HKG"}
colo=${colo^^}
# 更新配置文件中的 colo 参数
if grep -q "^colo=" "$config_file"; then
sed -i "s/^colo=.*/colo=${colo}/" "$config_file"
else
echo "colo=${colo}" >> "$config_file"
fi
# 读取并处理端口输入
echo ""
read -p "输入本地监听端口(默认 1234): " port
port=${port:-1234}
# 更新配置文件中的 port 参数
if grep -q "^port=" "$config_file"; then
sed -i "s/^port=.*/port=${port}/" "$config_file"
else
echo "port=${port}" >> "$config_file"
fi
# 读取并处理延迟输入
echo ""
echo "电信 有效延迟推荐 300"
echo "移动/联通 有效延迟可尝试 100"
read -p "输入有效延迟(毫秒),超过此延迟将断开连接(默认 300): " delay
delay=${delay:-300}
# 更新配置文件中的 delay 参数
if grep -q "^delay=" "$config_file"; then
sed -i "s/^delay=.*/delay=${delay}/" "$config_file"
else
echo "delay=${delay}" >> "$config_file"
fi
}
kill_cfnat(){
if [ "$release" = "OpenWRT" ]; then
# 查询 ./cfnat 进程并获取其 PID
cfnatpid=$(pgrep -f "./cfnat -colo")
# 检查是否找到了 PID
if [ -n "$cfnatpid" ]; then
echo "./cfnat 进程正在运行,准备杀死进程..."
kill "$cfnatpid"
echo -e "${red}./cfnat 进程已被杀死。${re}"
else
echo "./cfnat 进程未在运行。"
fi
else
# 检测 $cfnat_file/cfnat 程序是否正在运行
if pgrep -x "cfnat" > /dev/null; then
echo "cfnat 进程正在运行,准备杀死进程..."
# 如果正在运行,结束该程序
pkill -x "cfnat"
echo -e "${red}cfnat 已终止。${re}"
fi
fi
}
go_cfnat(){
if [ "$OneclickInstallation" = "${green}一键安装" ]; then
install_cfnat
fi
check_cfnat
cd $cfnat_file && nohup ./cfnat -colo $cfnatcolo -port 443 -delay $cfnatdelay -ips 4 -addr "0.0.0.0:$cfnatport" >/dev/null 2>&1 &
}
state_cfnat(){
echo -e "${yellow} 系统: ${re}${release}${re}"
echo -e "${yellow} 架构: ${re}${Architecture}${re}"
echo -e "${yellow} IP库: ${IPLibrary}${re}"
echo -e "${yellow} 数据中心: ${re}${cfnatcolo}${re}"
echo -e "${yellow} 有效延迟: ${re}${cfnatdelay}ms${re}"
echo -e "${yellow} 本地服务: ${re}127.0.0.1:${cfnatport}${re}"
#echo -e "${yellow} 内网服务: ${re}${lanip}:${cfnatport}${re}"
# 将lanip转成数组
IFS=$'\n' read -rd '' -a ip_array <<< "$lanip"
# 输出结果
if [ ${#ip_array[@]} -eq 1 ]; then
echo -e "${yellow} 内网服务: ${re}${ip_array[0]}:$cfnatport${reset}"
else
echo -e "${yellow} 内网服务: ${re}${ip_array[0]}:$cfnatport${reset}"
for i in "${!ip_array[@]}"; do
if [ $i -ne 0 ]; then
echo " ${ip_array[$i]}:$cfnatport"
fi
done
fi
}
site_release(){
echo -e "${yellow} 设置系统信息...${re}"
echo -e "${yellow} 1. ${re}alpine"
echo -e "${yellow} 2. ${re}Centos"
echo -e "${yellow} 3. ${re}Debian"
echo -e "${yellow} 4. ${re}Ubuntu"
echo -e "${yellow} 5. ${re}OpenWRT"
read -p $'\033[1;91m请输入你的选择(默认 OpenWRT): \033[0m' choice_release
# 根据用户选择赋值给 release 变量
case $choice_release in
1)
release="alpine"
;;
2)
release="Centos"
;;
3)
release="Debian"
;;
4)
release="Ubuntu"
;;
5)
release="OpenWRT"
;;
*)
release="OpenWRT" # 默认值
;;
esac
if grep -q "^release=" "$config_file"; then
sed -i "s/^release=.*/release=${release}/" "$config_file"
else
echo "release=${release}" >> "$config_file"
fi
echo "你选择的系统是: $release"
}
site_Architecture(){
echo -e "${yellow} 设置架构信息...${re}"
echo -e "${yellow} 1. ${re}termux (安卓termux)"
echo -e "${yellow} 2. ${re}386 (老古董 32位x86软路由)"
echo -e "${yellow} 3. ${re}amd64 (64位x86软路由虚拟机)"
echo -e "${yellow} 4. ${re}arm (32为 老arm机器)"
echo -e "${yellow} 5. ${re}arm64 (硬路由刷机OpenWRT)"
echo -e "${yellow} 6. ${re}s390x"
echo -e "${yellow} 7. ${re}mips64"
read -p $'\033[1;91m请输入你的选择(默认 amd64): \033[0m' choice_Architecture
# 根据用户选择赋值给 release 变量
case $choice_Architecture in
1)
Architecture="termux"
;;
2)
Architecture="386"
;;
3)
Architecture="amd64"
;;
4)
Architecture="arm"
;;
5)
Architecture="arm64"
;;
6)
Architecture="s390x"
;;
7)
Architecture="mips64"
;;
*)
Architecture="amd64" # 默认值
;;
esac
if grep -q "^Architecture=" "$config_file"; then
sed -i "s/^Architecture=.*/Architecture=${Architecture}/" "$config_file"
else
echo "Architecture=${Architecture}" >> "$config_file"
fi
echo "你选择的架构是: $Architecture"
}
#########################梦开始的地方##############################
#无交互执行
if [ -n "$1" ]; then
check_cfnat
if [ "$OneclickInstallation" = "${green}一键安装" ]; then
install_cfnat
fi
cfnatcolo=${1^^}
# 检测配置文件是否存在
if [ -f "$config_file" ]; then
# 如果存在,读取 colo 字段内容
colo=$(grep '^colo=' "$config_file" | cut -d'=' -f2)
if [ $cfnatcolo = $colo ] && [ $statecfnat = "${green}运行中" ]; then
state_cfnat
echo -e "${green}cfnat 正在运行...${re}"
exit
else
kill_cfnat
fi
fi
# 更新配置文件中的 colo 参数
if grep -q "^colo=" "$config_file"; then
sed -i "s/^colo=.*/colo=${cfnatcolo}/" "$config_file"
else
echo "colo=${cfnatcolo}" >> "$config_file"
fi
cd $cfnat_file && nohup ./cfnat -colo $cfnatcolo -port 443 -delay $cfnatdelay -ips 4 -addr "0.0.0.0:$cfnatport" >/dev/null 2>&1 &
#echo "nohup ./cfnat -colo HKG -port 443 -delay 200 -ips 4 -addr "0.0.0.0:1234" >/dev/null 2>&1 &"
state_cfnat
echo -e "${green}cfnat 开始执行...${re}"
else
while true; do
check_cfnat
clear
echo "cfnat 原作者: https://t.me/CF_NAT/38840 缝合怪: cmliu"
echo "--------------------------------"
echo -e "${yellow} 状态: ${InstallationStatus} ${statecfnat} ${re}"
state_cfnat
echo "--------------------------------"
echo -e "${yellow} 1. ${OneclickInstallation}${re}"
echo "--------------------------------"
echo -e "${yellow} 2. 启动 cfnat ${re}"
echo -e "${yellow} 3. 停止 cfnat ${re}"
echo -e "${yellow} 4. 重启 cfnat ${re}"
echo -e "${yellow} 5. 配置 cfnat ${ps}${re}"
echo "--------------------------------"
echo -e "${yellow} 6. ${green}调试运行 cfnat ${re} ${Androidps}${re}"
echo -e "${yellow} 7. 手动设置系统架构${re}"
echo "--------------------------------"
echo -e "${yellow} 8. IP库更新为 ${green}AS13335${re}"
echo -e "${yellow} 9. IP库更新为 ${skyblue}AS209242 ${re}仅推荐${yellow}电信${re}使用"
echo "--------------------------------"
echo -e "\033[0;97m 0. 退出脚本"
echo -e "${yellow}--------------------------------${re}"
ps=""
siteps=""
read -p $'\033[1;91m请输入你的选择: \033[0m' choice
case $choice in
1)
clear
if [ "$OneclickInstallation" = "${red}一键卸载" ]; then
uninstall_cfnat
else
install_cfnat
fi
;;
2)
if [ ! -f "$config_file" ]; then
config_cfnat
fi
go_cfnat
add_cron
;;
3)
kill_cfnat
delete_cron
;;
4)
kill_cfnat
go_cfnat
add_cron
;;
5)
if [ "$OneclickInstallation" = "${green}一键安装" ]; then
install_cfnat
fi
config_cfnat
ps="${red}完成配置后需重启cfnat才能生效!"
;;
6)
if [ "$OneclickInstallation" = "${green}一键安装" ]; then
install_cfnat
elif [ $statecfnat = "${green}运行中" ]; then
kill_cfnat
delete_cron
fi
cd $cfnat_file && ./cfnat -colo $cfnatcolo -port 443 -delay $cfnatdelay -ips 4 -addr "0.0.0.0:$cfnatport"
;;
7)
kill_cfnat
uninstall_cfnat
mkdir $cfnat_file
site_release
site_Architecture
colo="SJC,LAX,HKG"
echo "colo=${colo}" >> "$config_file"
port="1234"
echo "port=${port}" >> "$config_file"
delay="300"
echo "delay=${delay}" >> "$config_file"
echo -e "${red}设置完成后需卸载重装 cfnat 才能生效!"
install_cfnat
;;
8)
up_as13335
;;
9)
# 下载AS209242.txt文件
curl -SL https://as209242.ip.txt.090227.xyz/AS209242.txt -o $cfnat_file/AS209242.txt
# 获取下载的文件大小(以字节为单位)
file_size=$(stat -c%s "$cfnat_file/AS209242.txt")
# 检测文件是否大于10K(10240字节)
if [ $file_size -gt 10240 ]; then
# 文件大小超过10K,重命名为ips-v4.txt,覆盖已有文件
mv $cfnat_file/AS209242.txt $cfnat_file/ips-v4.txt
echo "IP库更新为 AS209242"
else
# 文件小于或等于10K,提示失败并删除下载的文件
echo "更新IP库失败"
rm $cfnat_file/AS209242.txt
fi
;;
0)
clear
exit
;;
*)
read -p "无效的输入!"
ps=""
;;
esac
break_end
done
fi
暂无评论