From 66a13153181bd522d0f0dacc22a47b70a5790be5 Mon Sep 17 00:00:00 2001 From: hitsword Date: Wed, 13 Oct 2021 22:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E5=88=86=E4=BD=8E=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E5=92=8C=E4=B8=80=E8=88=AC=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hitsword --- udptools-lowdelay.sh | 620 +++++++++++++++++++++++++++++++++++++++++++ udptools.sh | 4 +- 2 files changed, 622 insertions(+), 2 deletions(-) create mode 100644 udptools-lowdelay.sh diff --git a/udptools-lowdelay.sh b/udptools-lowdelay.sh new file mode 100644 index 0000000..d4aa2bf --- /dev/null +++ b/udptools-lowdelay.sh @@ -0,0 +1,620 @@ +#!/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 "$PSKEY"|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 udpspeeder|grep "$PSKEY"|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} <