2022-11-14 12:36:17 +08:00

144 lines
2.6 KiB
Bash

#bin/bash
#version 1.5
#date 2022年11月14日
####################
VERSION="1.5"
if [ -z $3 ];then
:
else
echo "wrong grammer!"
exit
fi
if [ -z $1 ];then
echo -n "Useage: -a to ADD port or -r to REMOVE port or -l to LIST ports !"
fi
if [ -z $2 ];then
:
else
if [ $2 -gt 0 ]&&[ $2 -lt 65535 ];then
PORT=$2
else
echo "please use a correct port!"
fi
fi
CheckPort (){
if [ -z ${PORT} ];then
echo " you set no port!"
exit
fi
}
OpenTcp (){
echo "adding..."
firewall-cmd --zone=public --add-port=${PORT}/tcp --permanent
echo "reloading..."
firewall-cmd --reload
}
OpenUdp (){
echo "adding..."
firewall-cmd --zone=public --add-port=${PORT}/udp --permanent
echo "reloading..."
firewall-cmd --reload
}
CloseTcp (){
echo "removinging..."
firewall-cmd --zone=public --remove-port=${PORT}/tcp --permanent
echo "reloading..."
firewall-cmd --reload
}
CloseUdp (){
echo "removinging..."
firewall-cmd --zone=public --remove-port=${PORT}/udp --permanent
echo "reloading..."
firewall-cmd --reload
}
GetVersion (){
LOCAL_VERSION=${VERSION}
REMOTE_VERSION=`curl -s https://mirror.liuyan.wang/shell/fwd.sh|grep "VERSION="|awk -F\" 'NR==1 {print $2}'`
}
Update (){
GetVersion
if [ "${LOCAL_VERSION}" != "${REMOTE_VERSION}" ];then
wget -O /usr/bin/fwd https://mirror.liuyan.wang/shell/fwd.sh && chmod +x /usr/bin/fwd
exit
else
echo "already newest version"
exit
fi
}
Help (){
cat<<EOF
Usage:
fwd [options]
Options:
-a, --add open a tcp port
-au, --add-udp open a udp port
-r, --remove close a tcp port
-ru, --remove-udp close a udp port
-l, --list show opened ports
start, start firewalld
stop, stop firewalld
status, see if started
update, update this command
-v, --version show version of local and remote
-h, --help display this help and exit
EOF
}
case $1 in
-a|--add)
CheckPort
OpenTcp
;;
-au)
CheckPort
OpenUdp
;;
-r|--remove)
CheckPort
CloseTcp
;;
-ru)
CheckPort
CloseUdp
;;
-l|--list)
firewall-cmd --list-port
;;
start)
systemctl start firewalld
;;
stop)
systemctl stop firewalld
;;
status)
firewall-cmd --state
systemctl status firewalld
;;
update)
Update
;;
-h|--help)
Help
;;
-v|--version)
GetVersion
echo "local version:${LOCAL_VERSION}"
echo "remote version:${REMOTE_VERSION}"
;;
*)
Help
;;
esac