├── README.md ├── brook-pf-mod.sh ├── brook-pf_centos └── brook-pf_debian /README.md: -------------------------------------------------------------------------------- 1 | # Brook端口转发一键脚本再次修改版 2 | Brook 端口转发 一键管理脚本再次修改版 基于逗比,yulewang版本修改而来。 3 | 4 | 去掉了更新之类的功能。删除iptables端口放行规则,更换为全允许。 5 | 6 | 解决之前脚本不支持CNAME的问题,将DDNS监测周期更换为1min。 7 | 8 | ----------------------------------------------------------------------------- 9 | 10 | ## 写在前面 11 | 一提到转发,大家最先想到的应该都是内核态方式,最常用的也就是iptables。因为这种实现方式对系统资源的消耗都比较少,性能比起软件转发更有优势。 12 | 13 | **为什么会有此脚本** 14 | 15 | 之前我也是力推iptables,但在DDNS实现上较为复杂。有大佬写了很好用的脚本,但规则一多,要么删除不完全,要么删掉一大堆本来不应该删掉的规则。使用过程中难免会在调试阶段删除/添加规则,删除某一条后发现突然少了20多条规则,这样的事应该都不想在正常使用中遇到吧。正好我看到逗比的Brook转发脚本被大佬修改支持DDNS,试了一下发现CNAME无法使用,所以结合了其他脚本的域名拿IP方法,构成了此脚本(拼接怪 16 | 17 | **此脚本优势** 18 | 19 | 类似于nftables,Brook的转发内容都是写在文件内(.conf),方便在各机器中同步规则,不会出现删除多,删除不干净。直接编辑配置文件保存后重启Brook即可重新加载规则。 20 | 21 | **此脚本劣势** 22 | 23 | 首先即是无法转发端口段,如要转发端口段则请避免使用此转发脚本。其次对系统的资源(CPU,RAM)有一定占用,当然比隧道还是好多了xx 24 | 25 | ## 使用方法 26 | ```shell 27 | wget -qO brook-pf-mod.sh https://raw.githubusercontent.com/monret/brook/master/brook-pf-mod.sh && chmod +x brook-pf-mod.sh && bash brook-pf-mod.sh 28 | ``` 29 | 执行结果: 30 | ``` 31 | Brook 端口转发 一键管理脚本修改版(DDNS支持) [v1.0.1] 32 | 1. 安装 Brook 33 | 2. 卸载 Brook 34 | ———————————— 35 | 3. 启动 Brook 36 | 4. 停止 Brook 37 | 5. 重启 Brook 38 | ———————————— 39 | 6. 设置 Brook 端口转发 40 | 7. 查看 Brook 端口转发 41 | 8. 查看 Brook 日志 42 | 9. 监控 Brook 运行状态(如果使用DDNS必须打开) 43 | ———————————— 44 | 10. 安装CNAME依赖(若添加DDNS出现异常) 45 | 11. 安装服务脚本(执行安装Brook后请勿重复安装) 46 | 12. iptables一键放行 47 | ———————————— 48 | 当前状态: 未安装 49 | 请输入数字 [0-12]: 50 | ``` 51 | 按1后回车即可自动安装完成。 52 | 53 | **如需开启DDNS支持,请在安装完成后按9开启运行监控。** 54 | 55 | ## 手动安装 56 | 因国内下载github资源速度缓慢,建议国内机器使用手动安装来使用。 57 | 58 | 根据您的服务器系统版本下载对应版本。(一般都是第一个) 59 | ``` 60 | https://github.com/txthinking/brook/releases 61 | ``` 62 | 创建并将下载的二进制文件上传到 **/usr/local/brook-pf** 目录 63 | 64 | 在目录建立 **brook.conf** 和 **brook.log** 两个文件,不要写任何内容。(除非你明白你在做什么) 65 | 66 | 保证目录下有3个文件后赋予权限: 67 | ``` 68 | chmod +x /usr/local/brook-pf 69 | ``` 70 | 接下来进入脚本依次执行第10,11,12项。 71 | 72 | 在执行第11项时可能会出现无法下载的问题,请根据系统类型下载项目内的**brook-pf_debian** 或 **brook-pf_centos** 73 | 74 | 下载完后改名为 **brook-pf**,上传至 **/etc/init.d/** ,然后执行: 75 | ``` 76 | chmod +x /etc/init.d/brook-pf 77 | ``` 78 | Enjoy~ 79 | -------------------------------------------------------------------------------- /brook-pf-mod.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 3 | export PATH 4 | 5 | #================================================= 6 | # System Required: CentOS/Debian/Ubuntu 7 | # Description: Brook 8 | # Version: 1.0.1 9 | # Author: Toyo, yulewang(DDNS features), monret(CNAME, iptables) 10 | # Blog: https://doub.io/wlzy-jc37/ 11 | #================================================= 12 | 13 | sh_ver="1.0.1" 14 | filepath=$(cd "$(dirname "$0")"; pwd) 15 | file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}') 16 | file="/usr/local/brook-pf" 17 | brook_file="/usr/local/brook-pf/brook" 18 | brook_conf="/usr/local/brook-pf/brook.conf" 19 | brook_log="/usr/local/brook-pf/brook.log" 20 | Crontab_file="/usr/bin/crontab" 21 | 22 | Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" 23 | Info="${Green_font_prefix}[信息]${Font_color_suffix}" 24 | Error="${Red_font_prefix}[错误]${Font_color_suffix}" 25 | Tip="${Green_font_prefix}[注意]${Font_color_suffix}" 26 | 27 | check_root(){ 28 | [[ $EUID != 0 ]] && echo -e "${Error} 当前非ROOT账号(或没有ROOT权限),无法继续操作,请更换ROOT账号或使用 ${Green_background_prefix}sudo su${Font_color_suffix} 命令获取临时ROOT权限(执行后可能会提示输入当前账号的密码)。" && exit 1 29 | } 30 | #检查系统 31 | check_sys(){ 32 | if [[ -f /etc/redhat-release ]]; then 33 | release="centos" 34 | elif cat /etc/issue | grep -q -E -i "debian"; then 35 | release="debian" 36 | elif cat /etc/issue | grep -q -E -i "ubuntu"; then 37 | release="ubuntu" 38 | elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then 39 | release="centos" 40 | elif cat /proc/version | grep -q -E -i "debian"; then 41 | release="debian" 42 | elif cat /proc/version | grep -q -E -i "ubuntu"; then 43 | release="ubuntu" 44 | elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then 45 | release="centos" 46 | fi 47 | bit=`uname -m` 48 | } 49 | Install_Tools(){ 50 | echo "正在安装依赖...." 51 | if [[ ${release} == "centos" ]]; then 52 | yum install bind-utils -y &> /dev/null 53 | else 54 | apt-get install dnsutils -y &> /dev/null 55 | fi 56 | echo "安装完成" 57 | } 58 | check_installed_status(){ 59 | [[ ! -e ${brook_file} ]] && echo -e "${Error} Brook 没有安装,请检查 !" && exit 1 60 | } 61 | check_crontab_installed_status(){ 62 | if [[ ! -e ${Crontab_file} ]]; then 63 | echo -e "${Error} Crontab 没有安装,开始安装..." 64 | if [[ ${release} == "centos" ]]; then 65 | yum install crond -y 66 | else 67 | apt-get install cron -y 68 | fi 69 | if [[ ! -e ${Crontab_file} ]]; then 70 | echo -e "${Error} Crontab 安装失败,请检查!" && exit 1 71 | else 72 | echo -e "${Info} Crontab 安装成功!" 73 | fi 74 | fi 75 | } 76 | check_pid(){ 77 | PID=$(ps -ef| grep "brook relays"| grep -v grep| grep -v ".sh"| grep -v "init.d"| grep -v "service"| awk '{print $2}') 78 | } 79 | check_new_ver(){ 80 | echo -e "请输入要下载安装的 Brook 版本号 ${Green_font_prefix}[ 格式是日期,例如: v20180909 ]${Font_color_suffix} 81 | 版本列表请去这里获取:${Green_font_prefix}[ https://github.com/txthinking/brook/releases ]${Font_color_suffix}" 82 | read -e -p "直接回车即自动获取:" brook_new_ver 83 | if [[ -z ${brook_new_ver} ]]; then 84 | brook_new_ver=$(wget -qO- https://api.github.com/repos/txthinking/brook/releases| grep "tag_name"| head -n 1| awk -F ":" '{print $2}'| sed 's/\"//g;s/,//g;s/ //g') 85 | [[ -z ${brook_new_ver} ]] && echo -e "${Error} Brook 最新版本获取失败!" && exit 1 86 | echo -e "${Info} 检测到 Brook 最新版本为 [ ${brook_new_ver} ]" 87 | else 88 | echo -e "${Info} 开始下载 Brook [ ${brook_new_ver} ] 版本!" 89 | fi 90 | } 91 | check_domain_ip_change(){ 92 | Modify_success="0" 93 | user_all=$(cat ${brook_conf}|sed '/^\s*$/d') 94 | user_num=$(echo -e "${user_all}"|wc -l) 95 | for((integer = 1; integer <= ${user_num}; integer++)) 96 | do 97 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 98 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 99 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 100 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 101 | user_domain_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $5}') 102 | if [ ! -z "$user_domain_pf" ]; then 103 | ip=$(host -t a $user_domain_pf|grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"|head -1) 104 | if [ -n "$ip" ]; then 105 | echo -e "Check domain IP: $ip" 106 | else 107 | echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Could not resolve hostname [${user_domain_pf}] !" | tee -a ${brook_log} 108 | continue 109 | fi 110 | 111 | if [[ ${user_ip_pf} != ${ip} ]]; then 112 | echo -e "${user_domain_pf}的IP发生变化, ${user_ip_pf} ===> ${ip}" 113 | echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] ${user_domain_pf}的IP发生变化, ${user_ip_pf} ===> ${ip}" | tee -a ${brook_log} 114 | sed -i -e "s/${user_port} ${user_ip_pf} ${user_port_pf} ${user_Enabled_pf} ${user_domain_pf}/${user_port} ${ip} ${user_port_pf} ${user_Enabled_pf} ${user_domain_pf}/g" ${brook_conf} 115 | Modify_success="1" 116 | else 117 | echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] ${user_domain_pf} 的IP未发生变化: ${ip}" | tee -a ${brook_log} 118 | fi 119 | fi 120 | done 121 | if [[ ${Modify_success} = "1" ]]; then 122 | echo -e "有IP发生了变化,正在重启Brook" 123 | Restart_brook 124 | fi 125 | } 126 | Download_brook(){ 127 | [[ ! -e ${file} ]] && mkdir ${file} 128 | cd ${file} 129 | if [[ ${bit} == "x86_64" ]]; then 130 | wget --no-check-certificate -N "https://github.com/txthinking/brook/releases/download/${brook_new_ver}/brook" 131 | else 132 | wget --no-check-certificate -N "https://github.com/txthinking/brook/releases/download/${brook_new_ver}/brook_linux_386" 133 | mv brook_linux_386 brook 134 | fi 135 | [[ ! -e "brook" ]] && echo -e "${Error} Brook 下载失败 !" && exit 1 136 | chmod +x brook 137 | } 138 | Service_brook(){ 139 | if [[ ${release} = "centos" ]]; then 140 | if ! wget --no-check-certificate https://raw.githubusercontent.com/monret/brook/master/brook-pf_centos -O /etc/init.d/brook-pf; then 141 | echo -e "${Error} Brook服务 管理脚本下载失败 !" && exit 1 142 | fi 143 | chmod +x /etc/init.d/brook-pf 144 | chkconfig --add brook-pf 145 | chkconfig brook-pf on 146 | else 147 | if ! wget --no-check-certificate https://raw.githubusercontent.com/monret/brook/master/brook-pf_debian -O /etc/init.d/brook-pf; then 148 | echo -e "${Error} Brook服务 管理脚本下载失败 !" && exit 1 149 | fi 150 | chmod +x /etc/init.d/brook-pf 151 | update-rc.d -f brook-pf defaults 152 | fi 153 | echo -e "${Info} Brook服务 管理脚本下载完成 !" 154 | } 155 | Installation_dependency(){ 156 | \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 157 | } 158 | Read_config(){ 159 | [[ ! -e ${brook_conf} ]] && echo -e "${Error} Brook 配置文件不存在 !" && exit 1 160 | user_all=$(cat ${brook_conf}) 161 | user_all_num=$(echo "${user_all}"|wc -l) 162 | [[ -z ${user_all} ]] && echo -e "${Error} Brook 配置文件中用户配置为空 !" && exit 1 163 | } 164 | Set_pf_Enabled(){ 165 | echo -e "立即启用该端口转发,还是禁用? [Y/n]" 166 | read -e -p "(默认: Y 启用):" pf_Enabled_un 167 | [[ -z ${pf_Enabled_un} ]] && pf_Enabled_un="y" 168 | if [[ ${pf_Enabled_un} == [Yy] ]]; then 169 | bk_Enabled="1" 170 | else 171 | bk_Enabled="0" 172 | fi 173 | } 174 | Set_port_Modify(){ 175 | while true 176 | do 177 | echo -e "请选择并输入要修改的 Brook 端口转发本地监听端口 [1-65535]" 178 | read -e -p "(默认取消):" bk_port_Modify 179 | [[ -z "${bk_port_Modify}" ]] && echo "取消..." && exit 1 180 | echo $((${bk_port_Modify}+0)) &>/dev/null 181 | if [[ $? -eq 0 ]]; then 182 | if [[ ${bk_port_Modify} -ge 1 ]] && [[ ${bk_port_Modify} -le 65535 ]]; then 183 | check_port "${bk_port_Modify}" 184 | if [[ $? == 0 ]]; then 185 | break 186 | else 187 | echo -e "${Error} 该本地监听端口不存在 [${bk_port_Modify}] !" 188 | fi 189 | else 190 | echo "输入错误, 请输入正确的端口。" 191 | fi 192 | else 193 | echo "输入错误, 请输入正确的端口。" 194 | fi 195 | done 196 | } 197 | Set_port(){ 198 | while true 199 | do 200 | echo -e "请输入 Brook 本地监听端口 [1-65535](端口不能重复,避免冲突)" 201 | read -e -p "(默认取消):" bk_port 202 | [[ -z "${bk_port}" ]] && echo "已取消..." && exit 1 203 | echo $((${bk_port}+0)) &>/dev/null 204 | if [[ $? -eq 0 ]]; then 205 | if [[ ${bk_port} -ge 1 ]] && [[ ${bk_port} -le 65535 ]]; then 206 | echo && echo "========================" 207 | echo -e " 本地监听端口 : ${Red_background_prefix} ${bk_port} ${Font_color_suffix}" 208 | echo "========================" && echo 209 | break 210 | else 211 | echo "输入错误, 请输入正确的端口。" 212 | fi 213 | else 214 | echo "输入错误, 请输入正确的端口。" 215 | fi 216 | done 217 | } 218 | Set_IP_pf(){ 219 | echo "请输入被转发的 IP :" 220 | read -e -p "(默认取消):" bk_ip_pf 221 | [[ -z "${bk_ip_pf}" ]] && echo "已取消..." && exit 1 222 | echo && echo "========================" 223 | echo -e " 被转发IP : ${Red_background_prefix} ${bk_ip_pf} ${Font_color_suffix}" 224 | echo "========================" && echo 225 | } 226 | Set_DOMAIN_pf(){ 227 | echo "请输入被转发的 域名 :" 228 | read -e -p "(默认取消):" bk_domain_pf 229 | [[ -z "${bk_domain_pf}" ]] && echo "已取消..." && exit 1 230 | echo && echo "========================" 231 | echo -e " 被转发域名 : ${Red_background_prefix} ${bk_domain_pf} ${Font_color_suffix}" 232 | echo "========================" && echo 233 | } 234 | Set_port_pf(){ 235 | while true 236 | do 237 | echo -e "请输入 Brook 被转发的端口 [1-65535]" 238 | read -e -p "(默认取消):" bk_port_pf 239 | [[ -z "${bk_port_pf}" ]] && echo "已取消..." && exit 1 240 | echo $((${bk_port_pf}+0)) &>/dev/null 241 | if [[ $? -eq 0 ]]; then 242 | if [[ ${bk_port_pf} -ge 1 ]] && [[ ${bk_port_pf} -le 65535 ]]; then 243 | echo && echo "========================" 244 | echo -e " 被转发端口 : ${Red_background_prefix} ${bk_port_pf} ${Font_color_suffix}" 245 | echo "========================" && echo 246 | break 247 | else 248 | echo "输入错误, 请输入正确的端口。" 249 | fi 250 | else 251 | echo "输入错误, 请输入正确的端口。" 252 | fi 253 | done 254 | } 255 | Set_brook(){ 256 | check_installed_status 257 | echo && echo -e "你要做什么? 258 | ${Green_font_prefix}0.${Font_color_suffix} 添加 端口转发(域名) 259 | ${Green_font_prefix}1.${Font_color_suffix} 添加 端口转发 260 | ${Green_font_prefix}2.${Font_color_suffix} 删除 端口转发 261 | ${Green_font_prefix}3.${Font_color_suffix} 修改 端口转发 262 | ${Green_font_prefix}4.${Font_color_suffix} 启用/禁用 端口转发 263 | 264 | ${Tip} 本地监听端口不能重复,被转发的IP或端口可重复!" && echo 265 | read -e -p "(默认: 取消):" bk_modify 266 | [[ -z "${bk_modify}" ]] && echo "已取消..." && exit 1 267 | if [[ ${bk_modify} == "1" ]]; then 268 | Add_pf 269 | elif [[ ${bk_modify} == "2" ]]; then 270 | Del_pf 271 | elif [[ ${bk_modify} == "3" ]]; then 272 | Modify_pf 273 | elif [[ ${bk_modify} == "4" ]]; then 274 | Modify_Enabled_pf 275 | elif [[ ${bk_modify} == "0" ]]; then 276 | Add_pf_with_domin 277 | else 278 | echo -e "${Error} 请输入正确的数字(0-4)" && exit 1 279 | fi 280 | } 281 | check_port(){ 282 | check_port_1=$1 283 | user_all=$(cat ${brook_conf}|sed '1d;/^\s*$/d') 284 | #[[ -z "${user_all}" ]] && echo -e "${Error} Brook 配置文件中用户配置为空 !" && exit 1 285 | check_port_statu=$(echo "${user_all}"|awk '{print $1}'|grep -w "${check_port_1}") 286 | if [[ ! -z "${check_port_statu}" ]]; then 287 | return 0 288 | else 289 | return 1 290 | fi 291 | } 292 | list_port(){ 293 | port_Type=$1 294 | user_all=$(cat ${brook_conf}|sed '/^\s*$/d') 295 | if [[ -z "${user_all}" ]]; then 296 | if [[ "${port_Type}" == "ADD" ]]; then 297 | echo -e "${Info} 目前 Brook 配置文件中用户配置为空。" 298 | else 299 | echo -e "${Info} 目前 Brook 配置文件中用户配置为空。" && exit 1 300 | fi 301 | else 302 | user_num=$(echo -e "${user_all}"|wc -l) 303 | for((integer = 1; integer <= ${user_num}; integer++)) 304 | do 305 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 306 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 307 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 308 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 309 | if [[ ${user_Enabled_pf} == "0" ]]; then 310 | user_Enabled_pf_1="${Red_font_prefix}禁用${Font_color_suffix}" 311 | else 312 | user_Enabled_pf_1="${Green_font_prefix}启用${Font_color_suffix}" 313 | fi 314 | user_list_all=${user_list_all}"本地监听端口: ${Green_font_prefix}"${user_port}"${Font_color_suffix}\t 被转发IP: ${Green_font_prefix}"${user_ip_pf}"${Font_color_suffix}\t 被转发端口: ${Green_font_prefix}"${user_port_pf}"${Font_color_suffix}\t 状态: ${user_Enabled_pf_1}\n" 315 | user_IP="" 316 | done 317 | ip=$(wget -qO- -t1 -T2 ipinfo.io/ip) 318 | if [[ -z "${ip}" ]]; then 319 | ip=$(wget -qO- -t1 -T2 api.ip.sb/ip) 320 | if [[ -z "${ip}" ]]; then 321 | ip=$(wget -qO- -t1 -T2 members.3322.org/dyndns/getip) 322 | if [[ -z "${ip}" ]]; then 323 | ip="VPS_IP" 324 | fi 325 | fi 326 | fi 327 | echo -e "当前端口转发总数: ${Green_background_prefix} "${user_num}" ${Font_color_suffix} 当前服务器IP: ${Green_background_prefix} "${ip}" ${Font_color_suffix}" 328 | echo -e "${user_list_all}" 329 | echo -e "========================\n" 330 | fi 331 | } 332 | Add_pf_with_domin(){ 333 | while true 334 | do 335 | list_port "ADD" 336 | Set_port 337 | check_port "${bk_port}" 338 | [[ $? == 0 ]] && echo -e "${Error} 该本地监听端口已使用 [${bk_port}] !" && exit 1 339 | Set_DOMAIN_pf 340 | Set_port_pf 341 | Set_pf_Enabled 342 | Resolve_Hostname_To_IP 343 | echo "${bk_port} ${ip} ${bk_port_pf} ${bk_Enabled} ${bk_domain_pf}" >> ${brook_conf} 344 | Add_success=$(cat ${brook_conf}| grep ${bk_port}) 345 | if [[ -z "${Add_success}" ]]; then 346 | echo -e "${Error} 端口转发 添加失败 ${Green_font_prefix}[端口: ${bk_port} 被转发域名和端口: ${ip}:${bk_port_pf}]${Font_color_suffix} " 347 | break 348 | else 349 | echo -e "${Info} 端口转发 添加成功 ${Green_font_prefix}[端口: ${bk_port} 被转发域名和端口: ${ip}:${bk_port_pf}]${Font_color_suffix}\n" 350 | read -e -p "是否继续 添加端口转发配置?[Y/n]:" addyn 351 | [[ -z ${addyn} ]] && addyn="y" 352 | if [[ ${addyn} == [Nn] ]]; then 353 | Restart_brook 354 | break 355 | else 356 | echo -e "${Info} 继续 添加端口转发配置..." 357 | user_list_all="" 358 | fi 359 | fi 360 | done 361 | } 362 | Add_pf(){ 363 | while true 364 | do 365 | list_port "ADD" 366 | Set_port 367 | check_port "${bk_port}" 368 | [[ $? == 0 ]] && echo -e "${Error} 该本地监听端口已使用 [${bk_port}] !" && exit 1 369 | Set_IP_pf 370 | Set_port_pf 371 | Set_pf_Enabled 372 | echo "${bk_port} ${bk_ip_pf} ${bk_port_pf} ${bk_Enabled}" >> ${brook_conf} 373 | Add_success=$(cat ${brook_conf}| grep ${bk_port}) 374 | if [[ -z "${Add_success}" ]]; then 375 | echo -e "${Error} 端口转发 添加失败 ${Green_font_prefix}[端口: ${bk_port} 被转发IP和端口: ${bk_ip_pf}:${bk_port_pf}]${Font_color_suffix} " 376 | break 377 | else 378 | echo -e "${Info} 端口转发 添加成功 ${Green_font_prefix}[端口: ${bk_port} 被转发IP和端口: ${bk_ip_pf}:${bk_port_pf}]${Font_color_suffix}\n" 379 | read -e -p "是否继续 添加端口转发配置?[Y/n]:" addyn 380 | [[ -z ${addyn} ]] && addyn="y" 381 | if [[ ${addyn} == [Nn] ]]; then 382 | Restart_brook 383 | break 384 | else 385 | echo -e "${Info} 继续 添加端口转发配置..." 386 | user_list_all="" 387 | fi 388 | fi 389 | done 390 | } 391 | Del_pf(){ 392 | while true 393 | do 394 | list_port 395 | Set_port 396 | check_port "${bk_port}" 397 | [[ $? == 1 ]] && echo -e "${Error} 该本地监听端口不存在 [${bk_port}] !" && exit 1 398 | sed -i "/^${bk_port} /d" ${brook_conf} 399 | Del_success=$(cat ${brook_conf}| grep ${bk_port}) 400 | if [[ ! -z "${Del_success}" ]]; then 401 | echo -e "${Error} 端口转发 删除失败 ${Green_font_prefix}[端口: ${bk_port}]${Font_color_suffix} " 402 | break 403 | else 404 | port=${bk_port} 405 | echo -e "${Info} 端口转发 删除成功 ${Green_font_prefix}[端口: ${bk_port}]${Font_color_suffix}\n" 406 | port_num=$(cat ${brook_conf}|sed '/^\s*$/d'|wc -l) 407 | if [[ ${port_num} == 0 ]]; then 408 | echo -e "${Error} 已无任何端口 !" 409 | check_pid 410 | if [[ ! -z ${PID} ]]; then 411 | Stop_brook 412 | fi 413 | break 414 | else 415 | read -e -p "是否继续 删除端口转发配置?[Y/n]:" delyn 416 | [[ -z ${delyn} ]] && delyn="y" 417 | if [[ ${delyn} == [Nn] ]]; then 418 | Restart_brook 419 | break 420 | else 421 | echo -e "${Info} 继续 删除端口转发配置..." 422 | user_list_all="" 423 | fi 424 | fi 425 | fi 426 | done 427 | } 428 | Modify_pf(){ 429 | list_port 430 | Set_port_Modify 431 | echo -e "\n${Info} 开始输入新端口... \n" 432 | Set_port 433 | check_port "${bk_port}" 434 | [[ $? == 0 ]] && echo -e "${Error} 该端口已存在 [${bk_port}] !" && exit 1 435 | Set_IP_pf 436 | Set_port_pf 437 | sed -i "/^${bk_port_Modify} /d" ${brook_conf} 438 | Set_pf_Enabled 439 | echo "${bk_port} ${bk_ip_pf} ${bk_port_pf} ${bk_Enabled}" >> ${brook_conf} 440 | Modify_success=$(cat ${brook_conf}| grep "${bk_port} ${bk_ip_pf} ${bk_port_pf} ${bk_Enabled}") 441 | if [[ -z "${Modify_success}" ]]; then 442 | echo -e "${Error} 端口转发 修改失败 ${Green_font_prefix}[端口: ${bk_port} 被转发IP和端口: ${bk_ip_pf}:${bk_port_pf}]${Font_color_suffix}" 443 | exit 1 444 | else 445 | port=${bk_port_Modify} 446 | Restart_brook 447 | echo -e "${Info} 端口转发 修改成功 ${Green_font_prefix}[端口: ${bk_port} 被转发IP和端口: ${bk_ip_pf}:${bk_port_pf}]${Font_color_suffix}\n" 448 | fi 449 | } 450 | Modify_Enabled_pf(){ 451 | list_port 452 | Set_port_Modify 453 | user_pf_text=$(cat ${brook_conf}|sed '/^\s*$/d'|grep "${bk_port_Modify}") 454 | user_port_text=$(echo ${user_pf_text}|awk '{print $1}') 455 | user_ip_pf_text=$(echo ${user_pf_text}|awk '{print $2}') 456 | user_port_pf_text=$(echo ${user_pf_text}|awk '{print $3}') 457 | user_Enabled_pf_text=$(echo ${user_pf_text}|awk '{print $4}') 458 | if [[ ${user_Enabled_pf_text} == "0" ]]; then 459 | echo -e "该端口转发已${Red_font_prefix}禁用${Font_color_suffix},是否${Green_font_prefix}启用${Font_color_suffix}? [Y/n]" 460 | read -e -p "(默认: Y 启用):" user_Enabled_pf_text_un 461 | [[ -z ${user_Enabled_pf_text_un} ]] && user_Enabled_pf_text_un="y" 462 | if [[ ${user_Enabled_pf_text_un} == [Yy] ]]; then 463 | user_Enabled_pf_text_1="1" 464 | sed -i "/^${bk_port_Modify} /d" ${brook_conf} 465 | echo "${user_port_text} ${user_ip_pf_text} ${user_port_pf_text} ${user_Enabled_pf_text_1}" >> ${brook_conf} 466 | Modify_Enabled_success=$(cat ${brook_conf}| grep "${user_port_text} ${user_ip_pf_text} ${user_port_pf_text} ${user_Enabled_pf_text_1}") 467 | if [[ -z "${Modify_Enabled_success}" ]]; then 468 | echo -e "${Error} 端口转发 启用失败 ${Green_font_prefix}[端口: ${user_port_text} 被转发IP和端口: ${user_ip_pf_text}:${user_port_pf_text}]${Font_color_suffix}" 469 | exit 1 470 | else 471 | echo -e "${Info} 端口转发 启用成功 ${Green_font_prefix}[端口: ${user_port_text} 被转发IP和端口: ${user_ip_pf_text}:${user_port_pf_text}]${Font_color_suffix}\n" 472 | Restart_brook 473 | fi 474 | else 475 | echo "已取消..." && exit 0 476 | fi 477 | else 478 | echo -e "该端口转发已${Green_font_prefix}启用${Font_color_suffix},是否${Red_font_prefix}禁用${Font_color_suffix}? [Y/n]" 479 | read -e -p "(默认: Y 禁用):" user_Enabled_pf_text_un 480 | [[ -z ${user_Enabled_pf_text_un} ]] && user_Enabled_pf_text_un="y" 481 | if [[ ${user_Enabled_pf_text_un} == [Yy] ]]; then 482 | user_Enabled_pf_text_1="0" 483 | sed -i "/^${bk_port_Modify} /d" ${brook_conf} 484 | echo "${user_port_text} ${user_ip_pf_text} ${user_port_pf_text} ${user_Enabled_pf_text_1}" >> ${brook_conf} 485 | Modify_Enabled_success=$(cat ${brook_conf}| grep "${user_port_text} ${user_ip_pf_text} ${user_port_pf_text} ${user_Enabled_pf_text_1}") 486 | if [[ -z "${Modify_Enabled_success}" ]]; then 487 | echo -e "${Error} 端口转发 禁用失败 ${Green_font_prefix}[端口: ${user_port_text} 被转发IP和端口: ${user_ip_pf_text}:${user_port_pf_text}]${Font_color_suffix}" 488 | exit 1 489 | else 490 | echo -e "${Info} 端口转发 禁用成功 ${Green_font_prefix}[端口: ${user_port_text} 被转发IP和端口: ${user_ip_pf_text}:${user_port_pf_text}]${Font_color_suffix}\n" 491 | Restart_brook 492 | fi 493 | else 494 | echo "已取消..." && exit 0 495 | fi 496 | fi 497 | } 498 | Install_brook(){ 499 | check_root 500 | [[ -e ${brook_file} ]] && echo -e "${Error} 检测到 Brook 已安装 !" && exit 1 501 | echo -e "${Info} 开始安装/配置 依赖..." 502 | Installation_dependency 503 | Install_Tools 504 | echo -e "${Info} 开始检测最新版本..." 505 | check_new_ver 506 | echo -e "${Info} 开始下载/安装..." 507 | Download_brook 508 | echo -e "${Info} 开始下载/安装 服务脚本(init)..." 509 | Service_brook 510 | echo -e "${Info} 开始写入 配置文件..." 511 | echo "" > ${brook_conf} 512 | echo -e "${Info} 开始设置 iptables防火墙..." 513 | Set_iptables 514 | echo -e "${Info} Brook 安装完成!默认配置文件为空,请选择 [设置 Brook 端口转发 - 添加 端口转发] 来添加端口转发。" 515 | } 516 | Start_brook(){ 517 | check_installed_status 518 | check_pid 519 | [[ ! -z ${PID} ]] && echo -e "${Error} Brook 正在运行,请检查 !" && exit 1 520 | /etc/init.d/brook-pf start 521 | } 522 | Stop_brook(){ 523 | check_installed_status 524 | check_pid 525 | [[ -z ${PID} ]] && echo -e "${Error} Brook 没有运行,请检查 !" && exit 1 526 | /etc/init.d/brook-pf stop 527 | } 528 | Restart_brook(){ 529 | check_installed_status 530 | check_pid 531 | [[ ! -z ${PID} ]] && /etc/init.d/brook-pf stop 532 | /etc/init.d/brook-pf start 533 | } 534 | Uninstall_brook(){ 535 | check_installed_status 536 | echo -e "确定要卸载 Brook ? [y/N]\n" 537 | read -e -p "(默认: n):" unyn 538 | [[ -z ${unyn} ]] && unyn="n" 539 | if [[ ${unyn} == [Yy] ]]; then 540 | check_pid 541 | [[ ! -z $PID ]] && kill -9 ${PID} 542 | if [[ -e ${brook_conf} ]]; then 543 | user_all=$(cat ${brook_conf}|sed '/^\s*$/d') 544 | user_all_num=$(echo "${user_all}"|wc -l) 545 | if [[ ! -z ${user_all} ]]; then 546 | for((integer = 1; integer <= ${user_all_num}; integer++)) 547 | do 548 | port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 549 | done 550 | fi 551 | fi 552 | if [[ ! -z $(crontab -l | grep "brook-pf-mod.sh monitor") ]]; then 553 | crontab_monitor_brook_cron_stop 554 | fi 555 | rm -rf ${file} 556 | if [[ ${release} = "centos" ]]; then 557 | chkconfig --del brook-pf 558 | else 559 | update-rc.d -f brook-pf remove 560 | fi 561 | rm -rf /etc/init.d/brook-pf 562 | echo && echo "Brook 卸载完成 !" && echo 563 | else 564 | echo && echo "卸载已取消..." && echo 565 | fi 566 | } 567 | View_Log(){ 568 | check_installed_status 569 | [[ ! -e ${brook_log} ]] && echo -e "${Error} Brook 日志文件不存在 !" && exit 1 570 | echo && echo -e "${Tip} 按 ${Red_font_prefix}Ctrl+C${Font_color_suffix} 终止查看日志(正常情况是没有使用日志记录的)" && echo -e "如果需要查看完整日志内容,请用 ${Red_font_prefix}cat ${brook_log}${Font_color_suffix} 命令。" && echo 571 | tail -f ${brook_log} 572 | } 573 | Set_crontab_monitor_brook(){ 574 | check_installed_status 575 | check_crontab_installed_status 576 | crontab_monitor_brook_status=$(crontab -l|grep "brook-pf-mod.sh monitor") 577 | if [[ -z "${crontab_monitor_brook_status}" ]]; then 578 | echo && echo -e "当前监控模式: ${Green_font_prefix}未开启${Font_color_suffix}" && echo 579 | echo -e "确定要开启 ${Green_font_prefix}Brook 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 Brook 服务端)[Y/n]" 580 | read -e -p "(默认: y):" crontab_monitor_brook_status_ny 581 | [[ -z "${crontab_monitor_brook_status_ny}" ]] && crontab_monitor_brook_status_ny="y" 582 | if [[ ${crontab_monitor_brook_status_ny} == [Yy] ]]; then 583 | crontab_monitor_brook_cron_start 584 | else 585 | echo && echo " 已取消..." && echo 586 | fi 587 | else 588 | echo && echo -e "当前监控模式: ${Green_font_prefix}已开启${Font_color_suffix}" && echo 589 | echo -e "确定要关闭 ${Green_font_prefix}Brook 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 Brook 服务端)[y/N]" 590 | read -e -p "(默认: n):" crontab_monitor_brook_status_ny 591 | [[ -z "${crontab_monitor_brook_status_ny}" ]] && crontab_monitor_brook_status_ny="n" 592 | if [[ ${crontab_monitor_brook_status_ny} == [Yy] ]]; then 593 | crontab_monitor_brook_cron_stop 594 | else 595 | echo && echo " 已取消..." && echo 596 | fi 597 | fi 598 | } 599 | crontab_monitor_brook_cron_start(){ 600 | crontab -l > "$file_1/crontab.bak" 601 | sed -i "/brook-pf-mod.sh monitor/d" "$file_1/crontab.bak" 602 | echo -e "\n*/1 * * * * /bin/bash $file_1/brook-pf-mod.sh monitor" >> "$file_1/crontab.bak" 603 | crontab "$file_1/crontab.bak" 604 | rm -r "$file_1/crontab.bak" 605 | cron_config=$(crontab -l | grep "brook-pf-mod.sh monitor") 606 | if [[ -z ${cron_config} ]]; then 607 | echo -e "${Error} Brook 服务端运行状态监控功能 启动失败 !" && exit 1 608 | else 609 | echo -e "${Info} Brook 服务端运行状态监控功能 启动成功 !" 610 | fi 611 | } 612 | crontab_monitor_brook_cron_stop(){ 613 | crontab -l > "$file_1/crontab.bak" 614 | sed -i "/brook-pf-mod.sh monitor/d" "$file_1/crontab.bak" 615 | crontab "$file_1/crontab.bak" 616 | rm -r "$file_1/crontab.bak" 617 | cron_config=$(crontab -l | grep "brook-pf-mod.sh monitor") 618 | if [[ ! -z ${cron_config} ]]; then 619 | echo -e "${Error} Brook 服务端运行状态监控功能 停止失败 !" && exit 1 620 | else 621 | echo -e "${Info} Brook 服务端运行状态监控功能 停止成功 !" 622 | fi 623 | } 624 | crontab_monitor_brook(){ 625 | check_domain_ip_change 626 | check_installed_status 627 | check_pid 628 | echo "${PID}" 629 | if [[ -z ${PID} ]]; then 630 | echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 检测到 Brook服务端 未运行 , 开始启动..." | tee -a ${brook_log} 631 | /etc/init.d/brook-pf start 632 | sleep 1s 633 | check_pid 634 | if [[ -z ${PID} ]]; then 635 | echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Brook服务端 启动失败..." | tee -a ${brook_log} 636 | else 637 | echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Brook服务端 启动成功..." | tee -a ${brook_log} 638 | fi 639 | else 640 | echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Brook服务端 进程运行正常..." | tee -a ${brook_log} 641 | fi 642 | } 643 | Set_iptables(){ 644 | if [[ ${release} == "centos" ]]; then 645 | systemctl stop firewalld &> /dev/null 646 | systemctl mask firewalld &> /dev/null 647 | service iptables save 648 | chkconfig --level 2345 iptables on 649 | iptables -P INPUT ACCEPT 650 | iptables -P OUTPUT ACCEPT 651 | iptables -P FORWARD ACCEPT 652 | service iptables save 653 | else 654 | iptables-save > /etc/iptables.up.rules 655 | iptables -P INPUT ACCEPT 656 | iptables -P OUTPUT ACCEPT 657 | iptables -P FORWARD ACCEPT 658 | iptables-save > /etc/iptables.up.rules 659 | fi 660 | echo "iptables放行完成" 661 | } 662 | Resolve_Hostname_To_IP(){ 663 | ip=$(host -t a $bk_domain_pf|grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"|head -1) 664 | if [ -n "$ip" ]; then 665 | echo -e " IP: $ip" 666 | else 667 | echo -e "${Error} Could not resolve hostname [${bk_domain_pf}] !" && exit 1 668 | fi 669 | } 670 | check_sys 671 | action=$1 672 | if [[ "${action}" == "monitor" ]]; then 673 | crontab_monitor_brook 674 | else 675 | echo && echo -e " Brook 端口转发 一键管理脚本修改版(DDNS支持) ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix} 676 | 677 | ${Green_font_prefix} 1.${Font_color_suffix} 安装 Brook 678 | ${Green_font_prefix} 2.${Font_color_suffix} 卸载 Brook 679 | ———————————— 680 | ${Green_font_prefix} 3.${Font_color_suffix} 启动 Brook 681 | ${Green_font_prefix} 4.${Font_color_suffix} 停止 Brook 682 | ${Green_font_prefix} 5.${Font_color_suffix} 重启 Brook 683 | ———————————— 684 | ${Green_font_prefix} 6.${Font_color_suffix} 设置 Brook 端口转发 685 | ${Green_font_prefix} 7.${Font_color_suffix} 查看 Brook 端口转发 686 | ${Green_font_prefix} 8.${Font_color_suffix} 查看 Brook 日志 687 | ${Green_font_prefix} 9.${Font_color_suffix} 监控 Brook 运行状态(如果使用DDNS必须打开) 688 | ———————————— 689 | ${Green_font_prefix}10.${Font_color_suffix} 安装CNAME依赖(若添加DDNS出现异常) 690 | ${Green_font_prefix}11.${Font_color_suffix} 安装服务脚本(执行安装Brook后请勿重复安装) 691 | ${Green_font_prefix}12.${Font_color_suffix} iptables一键放行 692 | ————————————" && echo 693 | if [[ -e ${brook_file} ]]; then 694 | check_pid 695 | if [[ ! -z "${PID}" ]]; then 696 | echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 并 ${Green_font_prefix}已启动${Font_color_suffix}" 697 | else 698 | echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 但 ${Red_font_prefix}未启动${Font_color_suffix}" 699 | fi 700 | else 701 | echo -e " 当前状态: ${Red_font_prefix}未安装${Font_color_suffix}" 702 | fi 703 | echo 704 | read -e -p " 请输入数字 [0-12]:" num 705 | case "$num" in 706 | 1) 707 | Install_brook 708 | ;; 709 | 2) 710 | Uninstall_brook 711 | ;; 712 | 3) 713 | Start_brook 714 | ;; 715 | 4) 716 | Stop_brook 717 | ;; 718 | 5) 719 | Restart_brook 720 | ;; 721 | 6) 722 | Set_brook 723 | ;; 724 | 7) 725 | check_installed_status 726 | list_port 727 | ;; 728 | 8) 729 | View_Log 730 | ;; 731 | 9) 732 | Set_crontab_monitor_brook 733 | ;; 734 | 10) 735 | Install_Tools 736 | ;; 737 | 11) 738 | Service_brook 739 | ;; 740 | 12) 741 | Set_iptables 742 | ;; 743 | *) 744 | echo "请输入正确数字 [0-12]" 745 | ;; 746 | esac 747 | fi -------------------------------------------------------------------------------- /brook-pf_centos: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # chkconfig: 2345 90 10 3 | # description: Brook-pf 4 | 5 | ### BEGIN INIT INFO 6 | # Provides: Brook-pf 7 | # Required-Start: $network $syslog 8 | # Required-Stop: $network 9 | # Default-Start: 2 3 4 5 10 | # Default-Stop: 0 1 6 11 | # Short-Description: Lightweight port forwarding tool 12 | # Description: Start or stop the Brook-pf 13 | ### END INIT INFO 14 | 15 | NAME="Brook-pf" 16 | NAME_BIN="brook relays" 17 | FILE="/usr/local/brook-pf" 18 | CONF="${FILE}/brook.conf" 19 | LOG="${FILE}/brook.log" 20 | 21 | Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" 22 | Info="${Green_font_prefix}[信息]${Font_color_suffix}" 23 | Error="${Red_font_prefix}[错误]${Font_color_suffix}" 24 | RETVAL=0 25 | 26 | check_running(){ 27 | PID=`ps -ef |grep "${NAME_BIN}" |grep -v "grep" | grep -v ".sh"| grep -v "init.d" |grep -v "service" |awk '{print $2}'` 28 | if [[ ! -z ${PID} ]]; then 29 | return 0 30 | else 31 | return 1 32 | fi 33 | } 34 | read_config(){ 35 | [[ ! -e ${CONF} ]] && echo -e "${Error} Brook 配置文件不存在 !" && exit 1 36 | user_all=$(cat ${CONF}|sed '/^\s*$/d') 37 | user_all_num=$(echo "${user_all}"|wc -l) 38 | [[ -z ${user_all} ]] && echo -e "${Error} Brook 配置文件中用户配置为空 !" && exit 1 39 | } 40 | View_User(){ 41 | for((integer = 1; integer <= ${user_all_num}; integer++)) 42 | do 43 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 44 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 45 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 46 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 47 | if [[ ${user_Enabled_pf} == "0" ]]; then 48 | user_Enabled_pf_1="${Red_font_prefix}禁用${Font_color_suffix}" 49 | else 50 | user_Enabled_pf_1="${Green_font_prefix}启用${Font_color_suffix}" 51 | fi 52 | user_list_all=${user_list_all}"本地监听端口: ${Green_font_prefix}"${user_port}"${Font_color_suffix}\t 被转发IP: ${Green_font_prefix}"${user_ip_pf}"${Font_color_suffix}\t 被转发端口: ${Green_font_prefix}"${user_port_pf}"${Font_color_suffix}\t 状态: ${user_Enabled_pf_1}\n" 53 | user_IP="" 54 | done 55 | ip=$(wget -qO- -t1 -T2 ipinfo.io/ip) 56 | if [[ -z "${ip}" ]]; then 57 | ip=$(wget -qO- -t1 -T2 api.ip.sb/ip) 58 | if [[ -z "${ip}" ]]; then 59 | ip=$(wget -qO- -t1 -T2 members.3322.org/dyndns/getip) 60 | if [[ -z "${ip}" ]]; then 61 | ip="VPS_IP" 62 | fi 63 | fi 64 | fi 65 | echo -e "当前端口转发总数: ${Green_background_prefix} "${user_all_num}" ${Font_color_suffix} 当前服务器IP: ${Green_background_prefix} "${ip}" ${Font_color_suffix}" 66 | echo -e "${user_list_all}" 67 | echo -e "========================\n" 68 | } 69 | do_start(){ 70 | check_running 71 | if [[ $? -eq 0 ]]; then 72 | echo -e "${Info} ${NAME} (PID ${PID}) 正在运行..." && exit 0 73 | else 74 | read_config 75 | cd ${FILE} 76 | echo -e "${Info} ${NAME} 启动中..." 77 | ulimit -n 51200 78 | servers_all="" 79 | for((integer = 1; integer <= ${user_all_num}; integer++)) 80 | do 81 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 82 | if [[ ${user_Enabled_pf} == "0" ]]; then 83 | continue 84 | fi 85 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 86 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 87 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 88 | servers_all="${servers_all}-l \":${user_port} ${user_ip_pf}:${user_port_pf}\" " 89 | done 90 | eval nohup ./brook relays $(echo ${servers_all}) >> "${LOG}" 2>&1 & 91 | sleep 2s 92 | check_running 93 | if [[ $? -eq 0 ]]; then 94 | echo -e "${Info} ${NAME} 启动成功 !" 95 | View_User 96 | else 97 | echo -e "${Error} ${NAME} 启动失败 !" 98 | fi 99 | fi 100 | } 101 | do_stop(){ 102 | check_running 103 | if [[ $? -eq 0 ]]; then 104 | kill -9 ${PID} 105 | RETVAL=$? 106 | if [[ $RETVAL -eq 0 ]]; then 107 | echo -e "${Info} ${NAME} 停止成功 !" 108 | else 109 | echo -e "${Error} ${NAME} 停止失败 !" 110 | fi 111 | else 112 | echo -e "${Info} ${NAME} 未运行" 113 | RETVAL=1 114 | fi 115 | } 116 | do_status(){ 117 | check_running 118 | if [[ $? -eq 0 ]]; then 119 | read_config 120 | View_User 121 | echo -e "${Info} ${NAME} (PID ${PID}) 正在运行..." 122 | else 123 | echo -e "${Info} ${NAME} 未运行 !" 124 | RETVAL=1 125 | fi 126 | } 127 | do_restart(){ 128 | do_stop 129 | sleep 2s 130 | do_start 131 | } 132 | case "$1" in 133 | start|stop|restart|status) 134 | do_$1 135 | ;; 136 | *) 137 | echo -e "使用方法: $0 { start | stop | restart | status }" 138 | RETVAL=1 139 | ;; 140 | esac 141 | exit $RETVAL -------------------------------------------------------------------------------- /brook-pf_debian: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: Brook-pf 5 | # Required-Start: $network $local_fs $remote_fs 6 | # Required-Stop: $network $local_fs $remote_fs 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: Lightweight port forwarding tool 10 | # Description: Start or stop the Brook-pf 11 | ### END INIT INFO 12 | 13 | NAME="Brook-pf" 14 | NAME_BIN="brook relays" 15 | FILE="/usr/local/brook-pf" 16 | CONF="${FILE}/brook.conf" 17 | LOG="${FILE}/brook.log" 18 | 19 | Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" 20 | Info="${Green_font_prefix}[信息]${Font_color_suffix}" 21 | Error="${Red_font_prefix}[错误]${Font_color_suffix}" 22 | RETVAL=0 23 | 24 | check_running(){ 25 | PID=`ps -ef |grep "${NAME_BIN}" |grep -v "grep" | grep -v ".sh"| grep -v "init.d" |grep -v "service" |awk '{print $2}'` 26 | if [[ ! -z ${PID} ]]; then 27 | return 0 28 | else 29 | return 1 30 | fi 31 | } 32 | read_config(){ 33 | [[ ! -e ${CONF} ]] && echo -e "${Error} Brook 配置文件不存在 !" && exit 1 34 | user_all=$(cat ${CONF}|sed '/^\s*$/d') 35 | user_all_num=$(echo "${user_all}"|wc -l) 36 | [[ -z ${user_all} ]] && echo -e "${Error} Brook 配置文件中用户配置为空 !" && exit 1 37 | } 38 | View_User(){ 39 | for((integer = 1; integer <= ${user_all_num}; integer++)) 40 | do 41 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 42 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 43 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 44 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 45 | if [[ ${user_Enabled_pf} == "0" ]]; then 46 | user_Enabled_pf_1="${Red_font_prefix}禁用${Font_color_suffix}" 47 | else 48 | user_Enabled_pf_1="${Green_font_prefix}启用${Font_color_suffix}" 49 | fi 50 | user_list_all=${user_list_all}"本地监听端口: ${Green_font_prefix}"${user_port}"${Font_color_suffix}\t 被转发IP: ${Green_font_prefix}"${user_ip_pf}"${Font_color_suffix}\t 被转发端口: ${Green_font_prefix}"${user_port_pf}"${Font_color_suffix}\t 状态: ${user_Enabled_pf_1}\n" 51 | user_IP="" 52 | done 53 | ip=$(wget -qO- -t1 -T2 ipinfo.io/ip) 54 | if [[ -z "${ip}" ]]; then 55 | ip=$(wget -qO- -t1 -T2 api.ip.sb/ip) 56 | if [[ -z "${ip}" ]]; then 57 | ip=$(wget -qO- -t1 -T2 members.3322.org/dyndns/getip) 58 | if [[ -z "${ip}" ]]; then 59 | ip="VPS_IP" 60 | fi 61 | fi 62 | fi 63 | echo -e "当前端口转发总数: ${Green_background_prefix} "${user_all_num}" ${Font_color_suffix} 当前服务器IP: ${Green_background_prefix} "${ip}" ${Font_color_suffix}" 64 | echo -e "${user_list_all}" 65 | echo -e "========================\n" 66 | } 67 | do_start(){ 68 | check_running 69 | if [[ $? -eq 0 ]]; then 70 | echo -e "${Info} ${NAME} (PID ${PID}) 正在运行..." && exit 0 71 | else 72 | read_config 73 | cd ${FILE} 74 | echo -e "${Info} ${NAME} 启动中..." 75 | ulimit -n 51200 76 | servers_all="" 77 | for((integer = 1; integer <= ${user_all_num}; integer++)) 78 | do 79 | user_Enabled_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $4}') 80 | if [[ ${user_Enabled_pf} == "0" ]]; then 81 | continue 82 | fi 83 | user_port=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $1}') 84 | user_ip_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $2}') 85 | user_port_pf=$(echo "${user_all}"|sed -n "${integer}p"|awk '{print $3}') 86 | servers_all="${servers_all}-l \":${user_port} ${user_ip_pf}:${user_port_pf}\" " 87 | done 88 | eval nohup ./brook relays $(echo ${servers_all}) >> "${LOG}" 2>&1 & 89 | sleep 2s 90 | check_running 91 | if [[ $? -eq 0 ]]; then 92 | echo -e "${Info} ${NAME} 启动成功 !" 93 | View_User 94 | else 95 | echo -e "${Error} ${NAME} 启动失败 !" 96 | fi 97 | fi 98 | } 99 | do_stop(){ 100 | check_running 101 | if [[ $? -eq 0 ]]; then 102 | kill -9 ${PID} 103 | RETVAL=$? 104 | if [[ $RETVAL -eq 0 ]]; then 105 | echo -e "${Info} ${NAME} 停止成功 !" 106 | else 107 | echo -e "${Error} ${NAME} 停止失败 !" 108 | fi 109 | else 110 | echo -e "${Info} ${NAME} 未运行" 111 | RETVAL=1 112 | fi 113 | } 114 | do_status(){ 115 | check_running 116 | if [[ $? -eq 0 ]]; then 117 | read_config 118 | View_User 119 | echo -e "${Info} ${NAME} (PID ${PID}) 正在运行..." 120 | else 121 | echo -e "${Info} ${NAME} 未运行 !" 122 | RETVAL=1 123 | fi 124 | } 125 | do_restart(){ 126 | do_stop 127 | sleep 2s 128 | do_start 129 | } 130 | case "$1" in 131 | start|stop|restart|status) 132 | do_$1 133 | ;; 134 | *) 135 | echo -e "使用方法: $0 { start | stop | restart | status }" 136 | RETVAL=1 137 | ;; 138 | esac 139 | exit $RETVAL --------------------------------------------------------------------------------