Created
October 7, 2021 15:40
-
-
Save dimaskiddo/96ceabd0553bd3d9ca46d09d31694bc1 to your computer and use it in GitHub Desktop.
Socat Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# SOCAT Port Forwarder Management Script | |
# Created By Dimas Restu Hidayanto (dimas.restu@student.upi.edu) | |
IS_NUMBER='^[0-9]+$' | |
if [ $UID != 0 ]; then | |
echo "Administrator Previlege Needed. Please Run This Program As An Administrator/Root User!" | |
exit 1 | |
fi | |
SOCAT_EXIST=$(which socat | wc -l) | |
if [ $SOCAT_EXIST -eq 0 ]; then | |
echo "Please Install Socat (socat) Command First!" | |
exit 1 | |
fi | |
if [ -z $1 ]; then | |
echo "Operation Not Found" | |
echo "Available Operations:" | |
echo "- add <external_port> <destination_ip> <destination_port>" | |
echo "- remove <external_port> <destination_ip> <destination_port>" | |
echo "- reload" | |
exit 0 | |
fi | |
case $1 in | |
"add") | |
if [ -z $2 ]; then | |
echo "Please Specifiy External Port" | |
exit 1 | |
else | |
if ! [[ $2 =~ $IS_NUMBER ]]; then | |
echo "External Port Must Be in Number Format!" | |
exit 1 | |
fi | |
if [[ -f /etc/socat-iface.list ]]; then | |
SOCAT_EXTERNAL_PORT_EXIST_IN_LIST=$(cat /etc/socat-iface.list | awk -F' ' '{print $1}' | grep "$2" | wc -l) | |
if [[ $SOCAT_EXTERNAL_PORT_EXIST_IN_LIST -gt 0 ]]; then | |
echo "External Port Already Exsist in Socat Interface, Please Use Another Port!" | |
exit 1 | |
fi | |
fi | |
fi | |
if [ -z $3 ]; then | |
echo "Please Specifiy Destination IP" | |
exit 1 | |
fi | |
if [ -z $4 ]; then | |
echo "Please Specifiy Destination Port" | |
exit 1 | |
else | |
if ! [[ $4 =~ $IS_NUMBER ]]; then | |
echo "Destination Port Must Be in Number Format!" | |
exit 1 | |
fi | |
fi | |
echo "Exposing Port to External Port" | |
socat TCP-LISTEN:$2,fork TCP:$3:$4 >> /dev/null 2>&1 & | |
echo "Adding to Socat Interface List" | |
echo "$2 $3 $4" >> /etc/socat-iface.list | |
;; | |
"remove") | |
if [ -z $2 ]; then | |
echo "Please Specifiy External Port" | |
exit 1 | |
else | |
if ! [[ $2 =~ $IS_NUMBER ]]; then | |
echo "External Port Must Be in Number Format!" | |
exit 1 | |
fi | |
fi | |
if [ -z $3 ]; then | |
echo "Please Specifiy Destination IP" | |
exit 1 | |
fi | |
if [ -z $4 ]; then | |
echo "Please Specifiy Destination Port" | |
exit 1 | |
else | |
if ! [[ $4 =~ $IS_NUMBER ]]; then | |
echo "Destination Port Must Be in Number Format!" | |
exit 1 | |
fi | |
fi | |
if [[ -f /etc/socat-iface.list ]]; then | |
SOCAT_IFACE_EXIST_IN_LIST=$(cat /etc/socat-iface.list | grep "$2 $3 $4" | wc -l) | |
if [[ $SOCAT_IFACE_EXIST_IN_LIST -eq 0 ]]; then | |
echo "Socat Interface Doesn't Exist, Please Re-Check Your Input First!" | |
exit 1 | |
fi | |
echo "Stoping Spesified Socat Process" | |
SOCAT_PID=$(ps -ef | grep "socat TCP-LISTEN:$2,fork TCP:$3:$4" | head -n 1 | awk -F' ' '{print $2}') | |
kill -9 $SOCAT_PID | |
echo "Removing From Socat Interface List" | |
sed -i -e "/$2 $3 $4/d" /etc/socat-iface.list | |
fi | |
;; | |
"reload") | |
echo "Stoping All Exsisting Socat Process" | |
killall socat | |
sleep 1 | |
if [[ -f /etc/socat-iface.list ]]; then | |
IFS=$'\n' | |
echo "Re-Exposing All Port to External Port" | |
for SOCAT_IFACE in $(cat /etc/socat-iface.list) | |
do | |
SOCAT_EXTERNAL_PORT=$(echo "$SOCAT_IFACE" | awk -F' ' '{print $1}') | |
SOCAT_DESTINATION_IP=$(echo "$SOCAT_IFACE" | awk -F' ' '{print $2}') | |
SOCAT_DESTINATION_PORT=$(echo "$SOCAT_IFACE" | awk -F' ' '{print $3}') | |
socat TCP-LISTEN:$SOCAT_EXTERNAL_PORT,fork TCP:$SOCAT_DESTINATION_IP:$SOCAT_DESTINATION_PORT >> /dev/null 2>&1 & | |
done | |
fi | |
;; | |
*) | |
echo "Operation Not Valid" | |
echo "Available Operations:" | |
echo "- add <external_port> <destination_ip> <destination_port>" | |
echo "- remove <external_port> <destination_ip> <destination_port>" | |
echo "- reload" | |
exit 0 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment