add socks5 and configure dynamic export

master
livejq 2 years ago
parent cb3dfbad90
commit effb29a310
  1. 80
      SS5/dynamic.sh
  2. 229
      SS5/install.sh

@ -0,0 +1,80 @@
#!/usr/bin/env bash
#
# Description: Polling and realize multi-exit dynamic IP.
#
# Copyright (C) 2023 liveJQ <cloud@livejq.fun>
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
SKYBLUE='\033[0;36m'
PLAIN='\033[0m'
defaultGate=$(ip route | awk -F ' ' '/default/{print $3}')
matchAddr=${defaultGate%.*}
OLD_IFS="$IFS"
IFS=$'\n'
allAddr=($(ip route | grep src | awk -F ' ' {'print $9'}))
IFS="$OLD_IFS"
sumAddr=${#allAddr[@]}
echo -e "${GREEN}此主机共配置了 $sumAddr 个IP地址${PLAIN}"
snat=""
for address in ${allAddr[@]}
do
if [ $matchAddr = ${address%.*} ]
then
snat=$address
fi
done
if [ -z $snat ]
then
echo -e "${RED}请先配置好 IP 地址再运行此程序${PLAIN}" && exit 1
elif [ ! -x /usr/sbin/ss5 ]
then
echo -e "${RED}请先安装好 Socks5 再运行此程序${PLAIN}" && exit 1
fi
if [ $(iptables -t nat -L | grep SNAT | wc -l) -gt 0 ]
then
lastAddr=$(iptables -t nat -L | grep SNAT | awk -F: '{print $NF}')
for((mark=0;mark<sumAddr;mark++))
do
address=${allAddr[mark]}
newMark=$(( mark + 1 ))
if [[ $lastAddr = $address && $newMark -lt $sumAddr ]]
then
lastAddr=${allAddr[newMark]}
break
fi
if [ $newMark -eq $sumAddr ]
then
lastAddr=${allAddr[0]}
break
fi
done
uid=$(id $(cat /etc/opt/ss5/ss5_proc_user.txt) | awk -F '(' '{print $1}' | awk -F '=' '{print $2}')
ruleNum=$(iptables -t nat -L -n --line-number | grep SNAT | awk -F ' ' '/all/{print $NR}')
iptables -t nat -D POSTROUTING $ruleNum
iptables -t nat -A POSTROUTING -m mark --mark $uid -j SNAT --to-source $lastAddr
echo -e "${GREEN}出口已更新!当前为 $lastAddr${PLAIN}" && exit 0
else
read -rp "请输入一个 Socks5 用户名(默认user1):" user
read -rp "请输入此用户密码(默认password1):" password
read -rp "请输入 Socks5 端口(默认10001):" port
read -rp "请输入出口轮训间隔时间, 单位分钟(默认5):" min
echo "${user:-user1} ${password:-password1}" >>/etc/opt/ss5/ss5.passwd
echo "${user:-user1}" >/etc/opt/ss5/ss5_proc_user.txt
useradd ${user:-user1} -s /bin/false -p ${password:-password1}
uid=$(id ${user:-user1} | awk -F '(' '{print $1}' | awk -F '=' '{print $2}')
iptables -A INPUT -p tcp --dport ${port:-10001} -j ACCEPT
iptables -t mangle -A OUTPUT -m owner --uid-owner $uid -j MARK --set-mark $uid
iptables -t nat -A POSTROUTING -m mark --mark $uid -j SNAT --to-source ${allAddr[0]}
iptables-save >/etc/sysconfig/iptables_ss5.ipv4
cp $(dirname $(readlink -f "$0"))/dynamic.sh /opt
echo "*/${min:-5} * * * * root bash /opt/dynamic.sh" >>/etc/crontab
echo "iptables-restore </etc/sysconfig/iptables_ss5.ipv4" >>/etc/rc.local
ss5 -u ${user:-user1} -b $snat:${port:-10001}
[[ $(netstat -tnlp | grep ${port:-10001} | wc -l) -gt 0 ]] && echo -e "${GREEN}配置成功!${PLAIN}" && exit 0
fi

@ -0,0 +1,229 @@
#!/bin/sh
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#Check OS
if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ];then
OS=CentOS
[ -n "$(grep ' 7\.' /etc/redhat-release)" ] && CentOS_RHEL_version=7
[ -n "$(grep ' 6\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && CentOS_RHEL_version=6
[ -n "$(grep ' 5\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && CentOS_RHEL_version=5
elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ];then
OS=CentOS
CentOS_RHEL_version=6
elif [ -n "$(grep bian /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Debian' ];then
OS=Debian
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Debian_version=$(lsb_release -sr | awk -F. '{print $1}')
elif [ -n "$(grep Deepin /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Deepin' ];then
OS=Debian
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Debian_version=$(lsb_release -sr | awk -F. '{print $1}')
elif [ -n "$(grep Ubuntu /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Ubuntu' -o -n "$(grep 'Linux Mint' /etc/issue)" ];then
OS=Ubuntu
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Ubuntu_version=$(lsb_release -sr | awk -F. '{print $1}')
[ -n "$(grep 'Linux Mint 18' /etc/issue)" ] && Ubuntu_version=16
else
echo "Does not support this OS, Please contact the author! "
kill -9 $$
fi
#Install Basic Tools
if [[ ${OS} == Ubuntu ]];then
echo ""
echo "***********************"
echo "*目前不支持Ubuntu系统!*"
echo "*请使用CentOS搭建 *"
echo "**********************"
exit 0
apt-get install git unzip wget -y
fi
if [[ ${OS} == CentOS ]];then
yum install git unzip wget -y
fi
if [[ ${OS} == Debian ]];then
echo "***********************"
echo "*目前不支持Debian系统!*"
echo "*请使用CentOS搭建 *"
echo "**********************"
apt-get install git unzip wget -y
fi
#1.清理旧环境和配置新环境
Clear(){
unInstall
clear
echo "旧环境清理完毕!"
echo ""
echo "安装Socks5所依赖的组件,请稍等..."
yum -y install gcc gcc-c++ automake make pam-devel openldap-devel cyrus-sasl-devel openssl-devel
yum update -y nss curl libcurl
#配置环境变量
sed -i '$a export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ~/.bash_profile
source ~/.bash_profile
#关闭防火墙
newVersion=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
if [[ ${newVersion} = "7" ]] ; then
systemctl stop firewalld
systemctl disable firewalld
elif [[ ${newVersion} = "6" ]] ;then
service iptables stop
chkconfig iptables off
else
echo "Exception version"
fi
}
#2.下载Socks5服务
Download()
{
echo ""
echo "下载Socks5服务中..."
cd /root
git clone https://github.com/wyx176/Socks5
}
#3.安装Socks5服务程序
InstallSock5()
{
echo ""
echo "解压文件中..."
cd /root/Socks5
tar zxvf ./ss5-3.8.9-8.tar.gz
echo "安装中..."
cd /root/Socks5/ss5-3.8.9
./configure
make
make install
}
#4.安装控制面板配置参数
InstallPanel()
{
#cd /root/Socks5
mv /root/Socks5/service.sh /etc/opt/ss5/
mv /root/Socks5/user.sh /etc/opt/ss5/
mv /root/Socks5/version.txt /etc/opt/ss5/
mv /root/Socks5/ss5 /etc/sysconfig/
mv /root/Socks5/s5 /usr/local/bin/
chmod +x /usr/local/bin/s5
#设置默认用户名、默认开启帐号验证
uname="123456"
upasswd="654321"
port="5555"
confFile=/etc/opt/ss5/ss5.conf
echo -e $uname $upasswd >> /etc/opt/ss5/ss5.passwd
sed -i '87c auth 0.0.0.0/0 - u' $confFile
sed -i '203c permit u 0.0.0.0/0 - 0.0.0.0/0 - - - - -' $confFile
#添加开机启动
chmod +x /etc/init.d/ss5
chkconfig --add ss5
chkconfig --level 345 ss5 on
confFile=/etc/rc.d/init.d/ss5
sed -i '/echo -n "Starting ss5... "/a if [ ! -d "/var/run/ss5/" ];then mkdir /var/run/ss5/; fi' $confFile
sed -i '54c rm -rf /var/run/ss5/' $confFile
sed -i '18c [[ ${NETWORKING} = "no" ]] && exit 0' $confFile
#判断ss5文件夹是否存在、
if [ ! -d "/var/run/ss5/" ];then
mkdir /var/run/ss5/
echo "create ss5 success!"
else
echo "/ss5/ is OK!"
fi
}
#5.检测是否安装完整
check(){
cd /root
rm -rf /root/Socks5
rm -rf /root/install.sh
errorMsg=""
isError=false
if [ ! -f "/usr/local/bin/s5" ] ; then
errorMsg=${errorMsg}"001|"
isError=true
fi
if [ ! -f "/etc/opt/ss5/service.sh" ]; then
errorMsg=${errorMsg}"002|"
isError=true
fi
if [ ! -f "/etc/opt/ss5/user.sh" ]; then
errorMsg=${errorMsg}"003|"
isError=true
fi
if [ ! -f "/etc/opt/ss5/ss5.conf" ]; then
errorMsg=${errorMsg}"004|"
isError=true
fi
if [ "$isError" = "true" ] ; then
unInstall
clear
echo ""
echo "缺失文件,安装失败!!!"
echo "错误提示:"${errorMsg}
echo "发送邮件反馈bug :wyx176@gmail.com"
echo "或者添加Telegram群反馈"
echo "Telegram群:t.me/Socks55555"
exit 0
else
clear
echo ""
#service ss5 start
if [[ ${newVersion} = "7" ]] ; then
systemctl daemon-reload
fi
service ss5 start
echo ""
echo "Socks5安装完毕!"
echo ""
echo "输入"s5"启动Socks5控制面板"
echo ""
echo "默认用户名: "${uname}
echo "默认密码 : "${upasswd}
echo "默认端口 : "${port}
echo ""
echo "添加Telegram群组@Socks55555及时获取更新"
echo ""
exit 0
fi
}
#6.卸载
unInstall(){
service ss5 stop
rm -rf /run/ss5
rm -f /run/lock/subsys/ss5
rm -rf /etc/opt/ss5
rm -f /usr/local/bin/s5
rm -rf /usr/lib/ss5
rm -f /usr/sbin/ss5
rm -rf /usr/share/doc/ss5
rm -rf /root/ss5-3.8.9
rm -f /etc/sysconfig/ss5
rm -f /etc/rc.d/init.d/ss5
rm -f /etc/pam.d/ss5
rm -rf /var/log/ss5
}
Clear
Download
InstallSock5
InstallPanel
check
Loading…
Cancel
Save