From 6b3e83f4ca6ecfa7f3dbc755c59d33d199a2935b Mon Sep 17 00:00:00 2001 From: schizza Date: Sat, 27 Apr 2024 21:42:41 +0200 Subject: [PATCH 1/3] iptables script In stations firmware 1.0 is bug for sending data to designated port. This script will forward incoming connections on port 80 to 8123 for stations IP --- .../sws12500/iptables_redirect.sh | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 custom_components/sws12500/iptables_redirect.sh diff --git a/custom_components/sws12500/iptables_redirect.sh b/custom_components/sws12500/iptables_redirect.sh new file mode 100755 index 0000000..ed34e25 --- /dev/null +++ b/custom_components/sws12500/iptables_redirect.sh @@ -0,0 +1,62 @@ +#!/bin/zsh + +# Script pro přesměrování portu pro stanici SWS12500 + +STATION_IP = 192.168.2.95 +HA = 192.168.2.219 +SRC_PORT = 80 +DST_PORT = 8123 + +INSTALL_IPTABLES = 0 +APK_MISSING = 0 + +echo "Spoštím iptables pro 80 -> 8123 přesměrování" + +# Máme nainstalované iptables? + +echo -n "Kontrola zda jsou dostupné iptable ... " +IPTABLES='$(type -p "iptables")' +if ! [ -f "$IPTABLES" ]; then + echo "chybí" + INSTALL_IPTABLES = 1 +else + echo "OK" +fi + +# Máme apk? +echo -n "Kontrola zda je dostupný apk ..." +APK='$(type -p "apk")' +if ! [ -f "$APK" ]; then + echo "chybí" + APK_MISSING = 1 +else + echo "OK" +fi + +if [ APK_MISSING == 1 -a INSTALL_IPTABLES == 1 ] + echo "Nelze nakonfigurovat IP Tables. iptables chybí a chybí i instalační aplikace apk!!" + exit 1 +fi + +if [ INSTALL_IPTABLES == 1 -a APK_MISSING == 0] + runinstall=(apk add iptables) + echo -n "Spouštím instalaci iptables ... ${runinstall[@]} ... " + ${runinstall[@]} + EXIT_STATUS=$? + if [ $EXIT_STATUS -ne 0 ] + echo "Instalace iptables se nezdařila!" + exit $EXIT_STATUS + fi + runiptables=(iptables -t nat -I PREROUTING --src $STATION_IP --dst $HA -p tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT) + echo -n "Spouštím iptables ... ${runiptables[@]} ..." + ${runiptables[@]} + EXIT_STATUS=$? + if [ $EXIT_STATUS -ne 0 ] + echo "Přidní pravidla do iptables se nezdařilo!" + exit $EXIT_STATUS + fi +fi +echo "iptables jsou nastaveny na přesměrování portu $SRC_PORT -> $DST_PORT pro stanici na IP: $STATION_IP" +exit + + From fa6caf4c226d89dce7d920237d27b4bed37fc588 Mon Sep 17 00:00:00 2001 From: schizza Date: Sun, 28 Apr 2024 02:35:04 +0200 Subject: [PATCH 2/3] Script fixed. * Duplicity rule test * color loging --- .../sws12500/iptables_redirect.sh | 121 +++++++++++------- 1 file changed, 72 insertions(+), 49 deletions(-) diff --git a/custom_components/sws12500/iptables_redirect.sh b/custom_components/sws12500/iptables_redirect.sh index ed34e25..3729cf3 100755 --- a/custom_components/sws12500/iptables_redirect.sh +++ b/custom_components/sws12500/iptables_redirect.sh @@ -1,62 +1,85 @@ -#!/bin/zsh +#!/bin/bash # Script pro přesměrování portu pro stanici SWS12500 -STATION_IP = 192.168.2.95 -HA = 192.168.2.219 -SRC_PORT = 80 -DST_PORT = 8123 +# set -e -INSTALL_IPTABLES = 0 -APK_MISSING = 0 +STATION_IP=192.168.2.95 +HA=192.168.2.219 +SRC_PORT=80 +DST_PORT=8123 -echo "Spoštím iptables pro 80 -> 8123 přesměrování" +INSTALL_IPTABLES=0 +APK_MISSING=0 -# Máme nainstalované iptables? +RED_COLOR='\033[0;31m' +GREEN_COLOR='\033[0;32m' +GREEN_YELLOW='\033[1;33m' +NO_COLOR='\033[0m' -echo -n "Kontrola zda jsou dostupné iptable ... " -IPTABLES='$(type -p "iptables")' -if ! [ -f "$IPTABLES" ]; then - echo "chybí" - INSTALL_IPTABLES = 1 -else - echo "OK" -fi -# Máme apk? -echo -n "Kontrola zda je dostupný apk ..." -APK='$(type -p "apk")' -if ! [ -f "$APK" ]; then - echo "chybí" - APK_MISSING = 1 -else - echo "OK" -fi +function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}";} +function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";} +function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; } -if [ APK_MISSING == 1 -a INSTALL_IPTABLES == 1 ] - echo "Nelze nakonfigurovat IP Tables. iptables chybí a chybí i instalační aplikace apk!!" - exit 1 -fi - -if [ INSTALL_IPTABLES == 1 -a APK_MISSING == 0] - runinstall=(apk add iptables) - echo -n "Spouštím instalaci iptables ... ${runinstall[@]} ... " - ${runinstall[@]} - EXIT_STATUS=$? - if [ $EXIT_STATUS -ne 0 ] - echo "Instalace iptables se nezdařila!" - exit $EXIT_STATUS - fi - runiptables=(iptables -t nat -I PREROUTING --src $STATION_IP --dst $HA -p tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT) - echo -n "Spouštím iptables ... ${runiptables[@]} ..." - ${runiptables[@]} - EXIT_STATUS=$? - if [ $EXIT_STATUS -ne 0 ] - echo "Přidní pravidla do iptables se nezdařilo!" - exit $EXIT_STATUS +function check () { + echo -n "Kontrola dostupnosti $1 ... " + if [ -z "$(command -v "$1")" ]; then + error "'$1' není nainstalován." $2 + return 1 fi + info "OK." + return 0 +} + + +echo +echo "**************************************************************" +echo "* *" +echo -e "* ${GREEN_YELLOW}Spouštím iptables přesměrování pro port $SRC_PORT -> $DST_PORT ${NO_COLOR} *" +echo "* *" +echo "**************************************************************" +echo + +# Máme nainstalované iptables a apk? + +check "iptables" false +INSTALL_IPTABLES=$? + +check "apk" false + +APK_MISSING=$? + + +if [ $APK_MISSING -eq 1 ] && [ $INSTALL_IPTABLES -eq 1 ]; then + error "Nelze nakonfigurovat IP Tables. \n'iptables' chybí a chybí i instalační aplikace 'apk'!!\n" fi -echo "iptables jsou nastaveny na přesměrování portu $SRC_PORT -> $DST_PORT pro stanici na IP: $STATION_IP" -exit +if [ $INSTALL_IPTABLES -eq 1 ] && [ $APK_MISSING -eq 0 ]; then + declare -a RUNINSTALL=(apk add iptables) + echo -n "Spouštím instalaci iptables ... ${RUNINSTAll[@]} ... " + ${RUNINSTALL[@]} + EXIT_STATUS=$? + if [ $EXIT_STATUS -ne 0 ]; then + warn "Chybový kód instalace: $EXIT_STATUS" + error "Instalace iptables se nezdařila!" + else + info "'iptables' úspěšně nainstalovány." + fi +fi +declare -a RULE=(PREROUTING -t nat -s $STATION_IP -d $HA -p tcp -m tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT) +echo -ne "Spouštím iptables ... " +$(iptables -C ${RULE[@]} 2>/dev/null) +if [ $? -eq 0 ]; then + warn "Pravidlo je již v iptables zapsáno." +else + $(iptables -I ${RULE[@]}) +fi +EXIT_STATUS=$? +if [ $EXIT_STATUS -ne 0 ]; then + warn "Chybový kód iptables: ${EXIT_STATUS} " + error "Přidání pravidla do iptables se nezdařilo!" +fi + +info "iptables jsou nastaveny na přesměrování portu $SRC_PORT -> $DST_PORT pro stanici na IP: $STATION_IP" From 69909e228c588630497f9629246cc9bb9dfa0c45 Mon Sep 17 00:00:00 2001 From: schizza Date: Sun, 28 Apr 2024 15:37:53 +0200 Subject: [PATCH 3/3] Installation script and tweaks in iptables script. * added installation script * iptables script translated to english and minor tweaks --- install_iptables.sh | 148 ++++++++++++++++++ ...tables_redirect.sh => iptables_redirect.sh | 48 +++--- 2 files changed, 174 insertions(+), 22 deletions(-) create mode 100644 install_iptables.sh rename custom_components/sws12500/iptables_redirect.sh => iptables_redirect.sh (56%) diff --git a/install_iptables.sh b/install_iptables.sh new file mode 100644 index 0000000..f3f851b --- /dev/null +++ b/install_iptables.sh @@ -0,0 +1,148 @@ +#!/bin/bash + +# Installation script for iptables redirect + +RED_COLOR='\033[0;31m' +GREEN_COLOR='\033[0;32m' +GREEN_YELLOW='\033[1;33m' +NO_COLOR='\033[0m' + +ST_PORT=80 + +LINK="" + +P_HA=true +P_ST=true + +declare -a HA_PATHS=( + "$PWD" + "$PWD/config" + "/config" + "/homeassistant" + "$HOME/.homeassistant" + "/usr/share/hassio/homeassistant" +) + +function info() { echo -e $2 "${GREEN_COLOR}$1${NO_COLOR}"; } +function warn() { echo -e $2 "${GREEN_YELLOW}$1${NO_COLOR}"; } +function error() { + echo -e "${RED_COLOR}$1${NO_COLOR}" + if [ "$2" != "false" ]; then exit 1; fi +} + +function check() { + echo -n "Checking dependencies: '$1' ... " + if [ -z "$(command -v "$1")" ]; then + error "not installed" $2 + false + else + info "OK." + true + fi +} + +function validate_ip() { + + if [[ "$1" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then + true + else + false + fi +} + +function validate_num() { + if [[ "$1" =~ ^[0-9]+$ ]]; then true; else false; fi +} + +function validate_dest() { + echo "Validating host '$1' ... " + if ping -c 4; then + info "OK" + true + else + error "cannot reach" false + false + fi +} + +function cont() { + + while true; do + warn "$1" + warn "Do you want to continue? [y/N]: " -n + read -n 1 YN + YN=${YN:-N} + case $YN in + [Yy]) return 0 ;; + [Nn]) error "\nExiting." ;; + *) error "\nInvalid response.\n" false ;; + esac + done +} + +echo +echo "**************************************************************" +echo "* *" +echo -e "* ${GREEN_YELLOW}Installation for iptables_redirect.sh ${NO_COLOR} *" +echo "* *" +echo "**************************************************************" +echo + +check "wget" +check "sed" +check "ping" false && { PING=true; } || { PING=false; } + +echo -n "Trying to find Home Assitant ... " +for PATH in "${HA_PATHS[@]}"; do + if [ -n "$HA_PATH" ]; then + break + fi + + if [ -f "$PATH/.HA_VERSION" ]; then + HA_PATH="$PATH" + fi +done + +#[ -z $HA_PATH ] && { error "Home Assistant not found!"; } +info "found at $HA_PATH" + +while true; do + read -r -p "Your station's IP: " ST_IP + if validate_ip $ST_IP; then break; fi + warn "Provide valid IP address." +done + +while true; do + read -r -p "Home Assistant's IP: " HA_IP + if validate_ip $HA_IP; then break; fi + warn "Provide valid IP address." +done + +while true; do + read -r -p "Home Assistant's port [8123]: " HA_PORT + HA_PORT=${HA_PORT:-8123} + if validate_num $HA_PORT && ((HA_PORT >= 1 && HA_PORT <= 65535)); then + break + fi + warn "Provide valid port number." +done + +if $PING; then + validate_dest $HA_IP || { cont "Home Assistant host is unreachable."; P_HA=false; } + validate_dest $ST_IP || { cont "Station is unreachable."; P_ST=false; } +fi + +info "\nYour configuration:" +info " Home Assistant at: $HA_PATH" +info " Home Assistant server at: $HA_IP:$HA_PORT" -n +if $PING; then + if $P_HA; then info " (ping OK)"; else error " (unreachable)" false; fi +else + error " (not tested)" false +fi +info " Station at: ${ST_IP}:$ST_PORT" -n +if $PING; then + if $P_ST; then info " (ping OK)"; else error " (unreachable)" false; fi +else + error " (not tested)" false +fi diff --git a/custom_components/sws12500/iptables_redirect.sh b/iptables_redirect.sh similarity index 56% rename from custom_components/sws12500/iptables_redirect.sh rename to iptables_redirect.sh index 3729cf3..3ad002d 100755 --- a/custom_components/sws12500/iptables_redirect.sh +++ b/iptables_redirect.sh @@ -1,13 +1,18 @@ #!/bin/bash +# Script for frowarding SWS 12500 station's destination port 80 +# to your Home Assistant's instance port (8123) +# +# Workaround for station's firmware 1.0 bug +# +# # Script pro přesměrování portu pro stanici SWS12500 -# set -e -STATION_IP=192.168.2.95 -HA=192.168.2.219 -SRC_PORT=80 -DST_PORT=8123 +STATION_IP=[_STATION_IP_] +HA=[_HA_] +SRC_PORT=[_SRC_PORT_] +DST_PORT=[_DST_PORT_] INSTALL_IPTABLES=0 APK_MISSING=0 @@ -23,9 +28,9 @@ function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";} function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; } function check () { - echo -n "Kontrola dostupnosti $1 ... " + echo -n "Checking dependencies: '$1' ... " if [ -z "$(command -v "$1")" ]; then - error "'$1' není nainstalován." $2 + error "not installed" $2 return 1 fi info "OK." @@ -33,53 +38,52 @@ function check () { } -echo +echo echo "**************************************************************" echo "* *" -echo -e "* ${GREEN_YELLOW}Spouštím iptables přesměrování pro port $SRC_PORT -> $DST_PORT ${NO_COLOR} *" +echo -e "* ${GREEN_YELLOW}Running iptables forward for port $SRC_PORT -> $DST_PORT ${NO_COLOR} *" echo "* *" echo "**************************************************************" echo -# Máme nainstalované iptables a apk? +# Check for dependencies check "iptables" false INSTALL_IPTABLES=$? check "apk" false - APK_MISSING=$? if [ $APK_MISSING -eq 1 ] && [ $INSTALL_IPTABLES -eq 1 ]; then - error "Nelze nakonfigurovat IP Tables. \n'iptables' chybí a chybí i instalační aplikace 'apk'!!\n" + error "Could not install and run iptables.\n'apk' installer is missing and 'iptables' are not installed.\n" fi if [ $INSTALL_IPTABLES -eq 1 ] && [ $APK_MISSING -eq 0 ]; then declare -a RUNINSTALL=(apk add iptables) - echo -n "Spouštím instalaci iptables ... ${RUNINSTAll[@]} ... " + echo -n "Installing 'iptables' ... ${RUNINSTALL[@]} ... " ${RUNINSTALL[@]} EXIT_STATUS=$? if [ $EXIT_STATUS -ne 0 ]; then - warn "Chybový kód instalace: $EXIT_STATUS" - error "Instalace iptables se nezdařila!" + warn "apk error code: $EXIT_STATUS" + error "Installation of iptables failed!" else - info "'iptables' úspěšně nainstalovány." + info "'iptables' installed successfully." fi fi declare -a RULE=(PREROUTING -t nat -s $STATION_IP -d $HA -p tcp -m tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT) -echo -ne "Spouštím iptables ... " +echo -n "Chceking for existing rule in iptables ... " $(iptables -C ${RULE[@]} 2>/dev/null) if [ $? -eq 0 ]; then - warn "Pravidlo je již v iptables zapsáno." + warn "Rule is already present in PREROUTING chain." else + echo -n "Inserting iptables rule to PREROUTING chain ... " $(iptables -I ${RULE[@]}) fi EXIT_STATUS=$? if [ $EXIT_STATUS -ne 0 ]; then - warn "Chybový kód iptables: ${EXIT_STATUS} " - error "Přidání pravidla do iptables se nezdařilo!" + warn "iptables error code: ${EXIT_STATUS} " + error "Rule could not be added!" fi -info "iptables jsou nastaveny na přesměrování portu $SRC_PORT -> $DST_PORT pro stanici na IP: $STATION_IP" - +info "iptables are set to forward port $SRC_PORT -> $DST_PORT for station's IP: $STATION_IP" \ No newline at end of file