#!/bin/bash # author HAITAO # time 2022/4/27 stty erase ^H # ----------函数区---------- ###预配置基础信息函数 function init { ##请提前定义网卡基础信息 #网卡名 eval echo "请输入网卡名!" && read eth_name echo ${eth_name} #网卡MAC地址 eval echo "请输入MAC地址!" && read MAC echo ${MAC} #MAC=00:16:3e:00:27:12 #辅助ip列表 eval echo "请输入辅助ip列表,用逗号隔开 例:10.0.80.136,10.0.80.137,10.0.80.138 " && read ip echo ${ip} #ip_list=(10.0.80.136 10.0.80.137 10.0.80.138 10.0.80.139) ip_list=(`echo $ip | tr ',' ' '` ) #子网掩码 echo "请输入子网掩码!" && read NETMASK echo ${NETMASK} #NETMASK=255.255.224.0 } ###查看基础信息函数 function echo_init { echo '网卡名':${eth_name} echo 'MAC': ${MAC} echo 'ip_list': ${ip_list[@]} echo 'NETMASK': ${NETMASK} } ###根据预配置设置网卡函数 function set_network { if [ ! -n "$eth_name" ] || [ ! -n "$MAC" ] || [ ! -n "$ip_list" ] || [ ! -n "$NETMASK" ];then echo "无配置信息,请重新配置网络信息!" sleep 2 clear continue else ##1.网卡配置 wget https://image-offline.oss-cn-hangzhou.aliyuncs.com/multi-nic-util/multi-nic-util-0.6.tgz && \ tar -zxvf multi-nic-util-0.6.tgz && \ cd multi-nic-util-0.6 && \ bash install.sh systemctl restart eni.service cat > /etc/sysconfig/network-scripts/ifcfg-${eth_name} <<EOF DEVICE=${eth_name} BOOTPROTO=dhcp ONBOOT=yes TYPE=Ethernet USERCTL=yes PEERDNS=no IPV6INIT=no PERSISTENT_DHCLIENT=yes HWADDR=${MAC} DEFROUTE=no EOF systemctl restart network #网卡主ip ip1=`ifconfig ${eth_name} | grep -oP '(?<=inet\s)\d+(\.\d+){3}'` #网卡路由标识 route_rule=`ip rule |grep ${ip1} | awk -F " " '{print $5}'` for i in "${!ip_list[@]}"; do echo "$i" "${ip_list[$i]}" cat > /etc/sysconfig/network-scripts/ifcfg-${eth_name}:${i} <<EOF DEVICE=${eth_name}:${i} TYPE=Ethernet BOOTPROTO=static ONBOOT=yes IPADDR=${ip_list[$i]} NETMASK=${NETMASK} EOF #添加ip路由 ip rule add from ${ip_list[$i]} table ${route_rule} done fi systemctl restart network } ###部署tinyproxy函数 function install_tinyproxy { ##安装tinyproxy yum -y install tinyproxy cat > /etc/tinyproxy/tinyproxy.conf <<EOF User tinyproxy Group tinyproxy Port 3120 Listen 0.0.0.0 BindSame yes Timeout 600 DefaultErrorFile "/usr/share/tinyproxy/default.html" StatFile "/usr/share/tinyproxy/stats.html" LogFile "/var/log/tinyproxy/tinyproxy.log" LogLevel Info PidFile "/var/run/tinyproxy/tinyproxy.pid" MaxClients 100 MinSpareServers 5 MaxSpareServers 20 StartServers 10 MaxRequestsPerChild 0 ViaProxyName "tinyproxy" ConnectPort 443 ConnectPort 563 ReverseOnly Yes ReverseMagic Yes EOF cat > /usr/lib/systemd/system/tinyproxy.service <<EOF [Unit] Description=Startup script for the tinyproxy server After=network.target [Service] Type=forking PIDFile=/var/run/tinyproxy/tinyproxy.pid ExecStart=/usr/sbin/tinyproxy -c /etc/tinyproxy/tinyproxy.conf ExecReload=/bin/kill -HUP $MAINPID ExecStartPost=/bin/sleep 0.1 KillMode=process [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl restart tinyproxy systemctl status tinyproxy for i in "${!ip_list[@]}"; do echo 测试地址:${ip_list[$i]} curl -s -x ${ip_list[$i]}:3120 ip.sb done } ###部署goproxy函数 function install_goproxy { eval echo "请输入网卡名!" && read net_name ##3.安装goproxy # 0.安装必须要的依赖 yum install wget -y || apt install wget -y function install_proxy { # 1.安装 proxy # 说明: https://github.com/snail007/goproxy/blob/master/README_ZH.md # 引用: https://mirrors.host900.com/https://github.com/snail007/goproxy/blob/master/install_auto.sh F="proxy-linux-amd64.tar.gz" set -e if [ -e /tmp/proxy ]; then rm -rf /tmp/proxy fi mkdir /tmp/proxy cd /tmp/proxy echo -e "\n>>> downloading ... $F\n" manual="https://snail.gitee.io/proxy/manual/" LAST_VERSION=$(curl --silent "https://mirrors.host900.com/https://api.github.com/repos/snail007/goproxy/releases/latest" | grep -Po '"tag_name":"\K.*?(?=")') wget -t 1 "https://mirrors.host900.com/https://github.com/snail007/goproxy/releases/download/${LAST_VERSION}/$F" echo -e ">>> installing ... \n" # #install proxy tar zxvf $F >/dev/null set +e killall -9 proxy >/dev/null 2>&1 set -e cp -f proxy /usr/bin/ chmod +x /usr/bin/proxy if [ ! -e /etc/proxy ]; then mkdir /etc/proxy cp blocked /etc/proxy cp direct /etc/proxy fi if [ ! -e /etc/proxy/proxy.crt ]; then cd /etc/proxy/ proxy keygen -C proxy >/dev/null 2>&1 fi rm -rf /tmp/proxy version=`proxy --version 2>&1` echo -e ">>> install done, thanks for using snail007/goproxy $version\n" echo -e ">>> install path /usr/bin/proxy\n" echo -e ">>> configuration path /etc/proxy\n" echo -e ">>> uninstall just exec : rm /usr/bin/proxy && rm -rf /etc/proxy\n" echo -e ">>> How to using? Please visit : $manual\n" } function add_service { # 2.添加为服务 cat > /etc/systemd/system/jasper-proxy-${net_name}.service <<EOF [Unit] Description=Provide Socket Proxy For 'JasPer' Wants=network.target After=network.target [Service] User=root PermissionsStartOnly=true Environment='EXT_IP=`curl --interface ${net_name} -s ifconfig.co`' Environment="LISTEN_ON=`ip a s ${net_name} | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`:3128" # socket5 mode # ExecStart=/usr/bin/proxy socks -g \$EXT_IP -t tcp -p \$LISTEN_ON # http mode ExecStart=/usr/bin/proxy http -g \$EXT_IP -p \$LISTEN_ON Restart=always RestartSec=15s TimeoutStartSec=30s [Install] WantedBy=multi-user.target EOF # 3.开机启动 systemctl daemon-reload systemctl enable jasper-proxy-${net_name}.service systemctl start jasper-proxy-${net_name}.service systemctl status jasper-proxy-${net_name}.service } #net_name=$1 #if [ ! -n "$net_name" ];then #echo -e "缺少\$1参数,无网络设备名称 " #sleep 1 #exit #fi proxy --version if [ $? != 0 ];then install_proxy add_service else add_service fi } ###检测代理函数 function check_proxy { echo "#########`date`##########" >> ./check.log eval echo "请输入检查代理的ip列表,用逗号隔开 例:10.0.80.136,10.0.80.137,10.0.80.138 " && read ip echo "请输入代理端口 例:3120 " && read port echo ${ip} #ip_list=(10.0.80.136 10.0.80.137 10.0.80.138 10.0.80.139) ip_list=(`echo $ip | tr ',' ' '` ) for i in "${!ip_list[@]}"; do eip=`curl -s -x ${ip_list[$i]}:${port} ip.sb` echo 检测: "${ip_list[$i]}" echo ${eip} echo ${ip_list[$i]} ${eip} >> ./check.log done echo "检测日志写入./check.log中" } # ----------主程序---------- echo "阿里云实例首次运行请先执行:选项1、2、3" sleep 1.5 while 1>0 do cat <<eof 1.配置基础网络信息 2.查看基础网络信息 3.阿里云多网卡配置(根据选项1中信息开始配置) ------------------------ ------------------------ 4.部署tinyproxy(多ip代理) 5.部署goproxy(单IP代理) 6.检测代理 7.退出脚本 eof eval echo "请输入数字选项!" && read var case "$var" in "1") init wait sleep 2 clear ;; "2") echo_init wait sleep 4 clear ;; "3") set_network sleep 2 clear ;; "4") install_tinyproxy sleep 2 clear ;; "5") install_goproxy sleep 2 clear ;; "6") check_proxy sleep 4 ;; "7") exit ;; *) echo "请输入数字选项,输入有误!" sleep 2 clear esac done