#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #判断系统 #useradd -M -s /sbin/nologin -d /usr/local/udptools udptools if [[ -e /etc/debian_version ]]; then OS=debian GROUPNAME=nogroup RCLOCAL='/etc/rc.local' IPTABLES=`dpkg --get-selections | grep iptables` elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then OS=centos GROUPNAME=nobody RCLOCAL='/etc/rc.d/rc.local' IPTABLES=`rpm -qa |grep iptables` else echo "只支持Debian\Ubuntu\CentOS系统" exit fi #检查依赖IPTABLES if [ "$IPTABLES" == '' ]; then if [[ "$OS" = 'debian' ]]; then apt-get update apt-get install iptables -y else yum install iptables -y fi fi #检查目录 if [ ! -d "/usr/local/udptools" ]; then echo "未安装udptools,请先安装." exit fi if [ ! -d "/usr/local/udptools/pid" ]; then mkdir /usr/local/udptools/pid fi if [ ! -d "/usr/local/udptools/log" ]; then mkdir /usr/local/udptools/log fi if [ ! -d "/usr/local/udptools/conf" ]; then mkdir /usr/local/udptools/conf fi build_Udp2raw_Server() { #写入Udp2Raw配置 cat > /usr/local/udptools/conf/udp2raw-s${MPORT}.conf < /usr/local/udptools/udp2raw-s${MPORT}.sh <> /usr/local/udptools/udp2raw-s${MPORT}.sh <<"EOF" checkSet(){ #获取监听端口 SERVER_PORT=`cat $CONFIG_FILE | grep '\-l ' | awk -F ":" '{print $2}'` #检查iptables规则 IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT` echo "IPTABLES_DEBUG" >> $LOG_FILE echo $IPTALBES >> $LOG_FILE if [ ! -n "$IPTALBES" ]; then echo "Adding iptables rules." #添加iptables规则 RULES=`$BIN_FILE --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule` $RULES fi #赋权 setcap cap_net_raw+ep $BIN_FILE } status(){ PID=`ps aux|grep $CONFIG_FILE|grep -v sudo|grep -v grep | awk '{print $2}'` if [ ! -n "$PID" ]; then rm -f $PID_FILE echo "$PROG已停止." else echo $PID > $PID_FILE echo "$PROG已启动. PID: $PID" fi } start(){ #启动进程 nohup $BIN_FILE --keep-rule --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 & #checkSet #sudo -u nobody -b $BIN_FILE --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 #Centos8无法nobody运行 status } stop(){ #结束进程 status > /dev/null 2>&1 PID=`cat $PID_FILE` kill $PID >/dev/null 2>&1 sleep 1 status } showLog(){ cat $LOG_FILE | tail -n 50 } case "$1" in start) echo "Starting $PROG..." start ;; stop) echo "Stopping $PROG..." stop ;; restart) echo "Stopping $PROG..." stop sleep 2 echo "Starting $PROG..." start ;; status) status ;; log) showLog ;; *) echo "Usage: $PROG {start|stop|restart|status|log}" ;; esac exit 0 EOF chmod +x /usr/local/udptools/udp2raw-s${MPORT}.sh #判断服务模式 if pgrep systemd-journal > /dev/null; then if [ ! -f "/usr/lib/systemd/system/udp2raw-server@.service" ]; then cp ./systemctl/service/udp2raw-server@.service /usr/lib/systemd/system/ fi systemctl enable udp2raw-server@${MPORT}.service systemctl start udp2raw-server@${MPORT}.service else cat > /etc/init.d/udp2raw-s${MPORT} < /usr/local/udptools/udpspeeder-s${MPORT}.sh <> /usr/local/udptools/udpspeeder-s${MPORT}.sh <<"EOF" status(){ PID=`ps aux|grep udpspeeder|grep ${MPORT}|grep -v sudo|grep -v grep | awk '{print $2}'` if [ ! -n "$PID" ]; then rm -f $PID_FILE echo "$PROG已停止." else echo $PID > $PID_FILE echo "$PROG已启动. PID: $PID" fi } start(){ #启动进程 #sudo -u nobody -b $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 nohup $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 & status } stop(){ #结束进程 status > /dev/null 2>&1 PID=`cat $PID_FILE` kill $PID >/dev/null 2>&1 sleep 1 status } showLog(){ cat $LOG_FILE | tail -n 50 } case "$1" in start) echo "Starting $PROG..." start ;; stop) echo "Stopping $PROG..." stop ;; restart) echo "Stopping $PROG..." stop sleep 2 echo "Starting $PROG..." start ;; status) status ;; log) showLog ;; *) echo "Usage: $PROG {start|stop|restart|status|log}" ;; esac exit 0 EOF chmod +x /usr/local/udptools/udpspeeder-s${MPORT}.sh #判断服务模式 if pgrep systemd-journal > /dev/null; then if [ ! -f "/usr/lib/systemd/system/udpspeeder-server@.service" ]; then cp ./systemctl/service/udpspeeder-server@.service /usr/lib/systemd/system/ fi systemctl enable udpspeeder-server@${MPORT}.service systemctl start udpspeeder-server@${MPORT}.service else cat > /etc/init.d/udpspeeder-s${MPORT} < /usr/local/udptools/conf/udp2raw-c${MPORT}.conf < /usr/local/udptools/udp2raw-c${MPORT}.sh <> /usr/local/udptools/udp2raw-c${MPORT}.sh <<"EOF" checkSet(){ #获取监听端口 SERVER_PORT=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $2}'` SERVER_ADDRESS=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $1}' | awk '{print $2}'` #检查iptables规则 IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT | grep $SERVER_ADDRESS` echo "IPTABLES_DEBUG" >> $LOG_FILE echo $IPTALBES >> $LOG_FILE if [ ! -n "$IPTALBES" ]; then echo "Adding iptables rules." #添加iptables规则 RULES=`$BIN_FILE --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule` $RULES fi #赋权 setcap cap_net_raw+ep $BIN_FILE } status(){ PID=`ps aux|grep $CONFIG_FILE|grep -v sudo|grep -v grep | awk '{print $2}'` if [ ! -n "$PID" ]; then rm -f $PID_FILE echo "$PROG已停止." else echo $PID > $PID_FILE echo "$PROG已启动. PID: $PID" fi } start(){ #启动进程 nohup $BIN_FILE --keep-rule --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 & #checkSet #sudo -u nobody -b $BIN_FILE --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1 #Centos8无法nobody运行 status } stop(){ #结束进程 status > /dev/null 2>&1 PID=`cat $PID_FILE` kill $PID >/dev/null 2>&1 sleep 1 status } showLog(){ cat $LOG_FILE | tail -n 50 } case "$1" in start) echo "Starting $PROG..." start ;; stop) echo "Stopping $PROG..." stop ;; restart) echo "Stopping $PROG..." stop sleep 2 echo "Starting $PROG..." start ;; status) status ;; log) showLog ;; *) echo "Usage: $PROG {start|stop|restart|status|log}" ;; esac exit 0 EOF chmod +x /usr/local/udptools/udp2raw-c${MPORT}.sh #判断服务模式 if pgrep systemd-journal > /dev/null; then if [ ! -f "/usr/lib/systemd/system/udp2raw-client@.service" ]; then cp ./systemctl/service/udp2raw-client@.service /usr/lib/systemd/system/ fi systemctl enable udp2raw-client@${MPORT}.service systemctl start udp2raw-client@${MPORT}.service else cat > /etc/init.d/udp2raw-c${MPORT} < /usr/local/udptools/udpspeeder-c${MPORT}.sh <> /usr/local/udptools/udpspeeder-c${MPORT}.sh <<"EOF" status(){ PID=`ps aux|grep -e "$CONFIG"|grep -v sudo|grep -v grep | awk '{print $2}'` if [ ! -n "$PID" ]; then rm -f $PID_FILE echo "$PROG已停止." else echo $PID > $PID_FILE echo "$PROG已启动. PID: $PID" fi } start(){ #启动进程 #sudo -u nobody -b $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 nohup $BIN_FILE $CONFIG >> $LOG_FILE 2>&1 & status } stop(){ #结束进程 status > /dev/null 2>&1 PID=`cat $PID_FILE` kill $PID >/dev/null 2>&1 sleep 1 status } showLog(){ cat $LOG_FILE | tail -n 50 } case "$1" in start) echo "Starting $PROG..." start ;; stop) echo "Stopping $PROG..." stop ;; restart) echo "Stopping $PROG..." stop sleep 2 echo "Starting $PROG..." start ;; status) status ;; log) showLog ;; *) echo "Usage: $PROG {start|stop|restart|status|log}" ;; esac exit 0 EOF chmod +x /usr/local/udptools/udpspeeder-c${MPORT}.sh #判断服务模式 if pgrep systemd-journal > /dev/null; then if [ ! -f "/usr/lib/systemd/system/udpspeeder-client@.service" ]; then cp ./systemctl/service/udpspeeder-client@.service /usr/lib/systemd/system/ fi systemctl enable udpspeeder-client@${MPORT}.service systemctl start udpspeeder-client@${MPORT}.service else cat > /etc/init.d/udpspeeder-c${MPORT} <