From badbe6b080219ac4895fc2d825c80038429d3ab4 Mon Sep 17 00:00:00 2001 From: Harald Heigl Date: Sat, 30 Jan 2021 21:37:00 +0100 Subject: [PATCH 1/6] changed docker-image to alpine --- .env-template | 6 +++++ .gitignore | 3 ++- Dockerfile | 10 ++++---- README.md | 30 +++++++++++------------- docker-compose.yml | 12 ++++++++++ docker/bin/{config.sh => config-tuya.sh} | 0 docker/bin/stop | 2 -- docker/bin/{start => tuya-start} | 2 +- docker/docker-compose.sample.yml | 12 ---------- install_prereq.sh | 2 +- 10 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 .env-template create mode 100644 docker-compose.yml rename docker/bin/{config.sh => config-tuya.sh} (100%) delete mode 100755 docker/bin/stop rename docker/bin/{start => tuya-start} (53%) delete mode 100644 docker/docker-compose.sample.yml diff --git a/.env-template b/.env-template new file mode 100644 index 0000000..db0f07c --- /dev/null +++ b/.env-template @@ -0,0 +1,6 @@ +#Attention: "host" means your computer where you are running docker on + +WLAN=wlan0 #must match the name of your wlan-interface on your host, you may find it with ifconfig +AP=vtrust-flash #the name of the created AP, can be anything you want +GATEWAY=10.42.42.1 #gateway address, leave it here +LOCALBACKUPDIR=./data/backups #location on your host where you want to store backuos of the old firmware & logs diff --git a/.gitignore b/.gitignore index edd8578..b206fa5 100644 --- a/.gitignore +++ b/.gitignore @@ -109,8 +109,9 @@ node_modules/ device-info.txt scripts/smartconfig/package-lock.json -# firmware backups directory +# firmware backups directory and data-file backups/ +data # flag files eula_accepted diff --git a/Dockerfile b/Dockerfile index 521d307..0f724dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,13 @@ -FROM phusion/baseimage:master +FROM alpine:3.13 -RUN apt-get update && apt-get install -y sudo iproute2 iputils-ping +RUN apk add --update bash git iw dnsmasq hostapd screen curl py3-pip py3-wheel python3-dev mosquitto haveged net-tools openssl openssl-dev gcc musl-dev linux-headers sudo coreutils grep iproute2 -RUN echo '* libraries/restart-without-asking boolean true' | sudo debconf-set-selections +RUN python3 -m pip install --upgrade paho-mqtt tornado git+https://github.com/drbild/sslpsk.git pycryptodomex COPY docker/bin /usr/bin/ COPY . /usr/bin/tuya-convert -RUN cd /usr/bin/tuya-convert && ./install_prereq.sh +WORKDIR "/usr/bin/tuya-convert" -RUN mkdir -p /etc/service/tuya && cd /etc/service/tuya && ln -s /usr/bin/config.sh run +ENTRYPOINT ["tuya-start"] diff --git a/README.md b/README.md index d530d91..301b537 100644 --- a/README.md +++ b/README.md @@ -83,26 +83,24 @@ Requirements: * docker is installed * docker-compose is installed -Create docker image: +Preparations: * git clone https://github.com/ct-Open-Source/tuya-convert * cd tuya-convert -* docker build -t tuya:latest . +* if you have already cloned this repo just cd into the directory and execute `git pull` +* cp .env-template .env +* adjust the created .env-file, it contains usage information as comments -Setup docker-compose: -* copy docker/docker-compose.sample.yml to a new folder you created, the file should be named docker-compose.yml -* you may adjust this docker-compose.yml, if necessary: - * environment-variables may be different, for example network-adapter may be different from wlan0 - * adjust the volume folder, where you want your backups stored +Building and running your container: +* `docker-compose build && docker-compose run --rm tuya` +* This directly starts into tuya. If you press ctrl+break or exit tuya after flashing, your container is closed and deleted -Run the image: -* go into the folder you copied docker-compose.yml -* docker-compose up -d -* docker-compose exec tuya start -* tuya-convert now starts within docker - -Stop the image: -* docker-compose exec tuya stop -* docker-compose down +Troubleshooting: +* Q: Where are my logs after flashing? A: The folder can be adjusted in .env with LOCALBACKUPDIR, the path here may be relative or absolute +* Q: I don't want that my container is deleted after running tuya, I need this for troubleshooting! How do I accomplish this? A: Just remove --rm from `docker-compose run --rm tuya` +* Q: I want to start the container, but instead of starting tuya immediately I want to get into bash. Is this possible? A: Yes just start the container with `docker-compose run --entrypoint bash tuya`. +* Q: I want to rebuild my docker-image, even if there are no changes. Is this possible? A: Just start `docker-compose build --no-cache` instead of `docker-compose build`! Don't do this all the time, this is a time consuming process ... +* Q: I can't connect to my USB, PCI, ... network card. How do I get this working? A: You may have an error in your .env-File. The WLAN-variable should reflect the name of your network interface on your host. Execute ifconfig and look through your interfaces. +* Q: I can't get an IP-address and or connection on my phone, what's the problem? A: You may look into smarthack-wifi.log (location is set in .env with LOCALBACKUPDIR) or possible stop your firewall (e.g. NixOS seems to have a problem here). It may be a problem with a wrongly set network interface (see previous question) ## CONTRIBUTING diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e59f569 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3' +services: + tuya: + build: . + privileged: true + network_mode: "host" + environment: + WLAN: ${WLAN} + AP: ${AP} + GATEWAY: ${GATEWAY} + volumes: + - $LOCALBACKUPDIR:/usr/bin/tuya-convert/backups diff --git a/docker/bin/config.sh b/docker/bin/config-tuya.sh similarity index 100% rename from docker/bin/config.sh rename to docker/bin/config-tuya.sh diff --git a/docker/bin/stop b/docker/bin/stop deleted file mode 100755 index 355829b..0000000 --- a/docker/bin/stop +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -pkill -SIGINT -f start_flash.sh diff --git a/docker/bin/start b/docker/bin/tuya-start similarity index 53% rename from docker/bin/start rename to docker/bin/tuya-start index d000b3c..f0cb3f9 100755 --- a/docker/bin/start +++ b/docker/bin/tuya-start @@ -1,3 +1,3 @@ #!/bin/bash -cd /usr/bin/tuya-convert +config-tuya.sh ./start_flash.sh diff --git a/docker/docker-compose.sample.yml b/docker/docker-compose.sample.yml deleted file mode 100644 index f9e6128..0000000 --- a/docker/docker-compose.sample.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '3' -services: - tuya: - image: tuya:latest - privileged: true - network_mode: "host" - environment: - - WLAN=wlan0 - - AP=vtrust-flash - - GATEWAY=10.42.42.1 - volumes: - - ./data/backups:/usr/bin/tuya-convert/backups diff --git a/install_prereq.sh b/install_prereq.sh index d7f8242..c021319 100755 --- a/install_prereq.sh +++ b/install_prereq.sh @@ -3,7 +3,7 @@ set -e sudo apt-get update -sudo apt-get install -y git iw dnsmasq rfkill hostapd screen curl build-essential python3-pip python3-setuptools python3-wheel python3-dev mosquitto haveged net-tools libssl-dev +sudo apt-get install -y git iw dnsmasq rfkill hostapd screen curl build-essential python3-pip python3-setuptools python3-wheel python3-dev mosquitto haveged net-tools libssl-dev iproute2 iputils-ping sudo -H python3 -m pip install --upgrade paho-mqtt tornado git+https://github.com/drbild/sslpsk.git pycryptodomex From 458faf3d5563ba0fadb773a97d367bffb41044ad Mon Sep 17 00:00:00 2001 From: NickVeld Date: Mon, 26 Apr 2021 03:03:15 -0400 Subject: [PATCH 2/6] screen<=4.5 generates smarthack-*.log now too --- scripts/old_screen_with_log.sh | 21 +++++++++++++++++++++ start_flash.sh | 4 +--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100755 scripts/old_screen_with_log.sh diff --git a/scripts/old_screen_with_log.sh b/scripts/old_screen_with_log.sh new file mode 100755 index 0000000..f85c071 --- /dev/null +++ b/scripts/old_screen_with_log.sh @@ -0,0 +1,21 @@ +# Run screen with a custom .screenrc that defines the name of "logfile" + +screen_minor=${1} +screen_logfile_name=${2} +screen_other_options=${@:3} + +if [ "$screen_minor" -gt 5 ]; then + echo "Info: you have the modern enough version" \ + "to use the \"-Logfile\" flag of \"screen\"" +elif [ "$screen_minor" -eq 5 ]; then + screen_with_log="sudo screen -L" +else + screen_with_log="sudo screen -L -t" +fi + +echo "logfile ${screen_logfile_name}" > ${screen_logfile_name}.screenrc + +${screen_with_log} ${screen_logfile_name} \ + -c ${screen_logfile_name}.screenrc ${screen_other_options} + +rm ${screen_logfile_name}.screenrc diff --git a/start_flash.sh b/start_flash.sh index be84ea1..9bb2dfe 100755 --- a/start_flash.sh +++ b/start_flash.sh @@ -10,10 +10,8 @@ setup () { screen_minor=$(screen --version | cut -d . -f 2) if [ "$screen_minor" -gt 5 ]; then screen_with_log="sudo screen -L -Logfile" - elif [ "$screen_minor" -eq 5 ]; then - screen_with_log="sudo screen -L" else - screen_with_log="sudo screen -L -t" + screen_with_log="./old_screen_with_log.sh ${screen_minor}" fi echo "======================================================" echo -n " Starting AP in a screen" From 63746fb6a3d31378efcc2655aa14921cab2a5f84 Mon Sep 17 00:00:00 2001 From: NickVeld Date: Wed, 28 Apr 2021 07:13:53 +0300 Subject: [PATCH 3/6] README.md: new tested specification --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d530d91..ca2ff0c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ These scripts were tested in * a Raspberry Pi 3B / 3B+ Raspberry Pi OS Buster (previously called Raspbian) and its internal Wifi chip * a Raspberry Pi 3B+ + USB-WIFI with an image from [here](https://www.offensive-security.com/kali-linux-arm-images/) * Ubuntu 18.04.3 64Bit in VirtualBox on Win10 with a cheap RTL8188CU Wifi Adapter connected to the VM +* Ubuntu Mate 18.04.5 32bit in VirtualBox on Win 10 with cheap Ralink 802.11n WLAN (MediaTek RT5370) WiFi Adapter connected to VM Any Linux with a Wifi adapter which can act as an Access Point should also work. Please note that we have tested the Raspberry Pi with clean installations only. If you use your Raspberry Pi for anything else, we recommend using another SD card with a clean installation. From eaaa1e71cccd20900245c29323240dc70e764511 Mon Sep 17 00:00:00 2001 From: Matthew Schinckel Date: Tue, 27 Jul 2021 11:23:05 +0100 Subject: [PATCH 4/6] Force ping to use a specific address. This helps when there are multiple networks available on the device running tuya-convert, as it will force the pings to go via the interface that the device will convert to, not the default interface. --- start_flash.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start_flash.sh b/start_flash.sh index be84ea1..5b71c7b 100755 --- a/start_flash.sh +++ b/start_flash.sh @@ -75,7 +75,7 @@ while true; do # The intermediate firmware will request 10.42.42.42 # Do NOT change this address!!! # It will NOT make it install and will break this script - while ! ping -c 1 -W 1 -n 10.42.42.42 &> /dev/null; do + while ! ping -c 1 -W 1 -n 10.42.42.42 -S 10.42.42.1 &> /dev/null; do printf . if (( --i == 0 )); then echo From f905ca87165b5da25a46ff4e9252fdd94ef45e8c Mon Sep 17 00:00:00 2001 From: sarumpaet Date: Thu, 23 Sep 2021 14:53:59 +0200 Subject: [PATCH 5/6] fix detecting used ports in Docker Processes blocking ports that run on the host are not visible within a Docker container. This was misdetected as the port being available. Print an error and abort instead. --- scripts/setup_checks.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/setup_checks.sh b/scripts/setup_checks.sh index f806274..9146b89 100755 --- a/scripts/setup_checks.sh +++ b/scripts/setup_checks.sh @@ -67,7 +67,8 @@ check_port () { port="$2" reason="$3" echo -n "Checking ${protocol^^} port $port... " - process_pid=$(sudo ss -lnp -A "$protocol" "sport = :$port" | grep -Po "(?<=pid=)(\d+)" | head -n1) + ss_output=$(sudo ss -lnp -A "$protocol" "sport = :$port") + process_pid=$(echo "$ss_output" | grep -Po "(?<=pid=)(\d+)" | head -n1) if [ -n "$process_pid" ]; then process_name=$(ps -p "$process_pid" -o comm=) echo "Occupied by $process_name with PID $process_pid." @@ -98,6 +99,12 @@ check_port () { sleep 1 fi else + if [ $(echo "$ss_output" | wc -l) -gt 1 ]; then + echo "Occupied by unknown process." + echo "Port $port is needed to $reason" + echo "Aborting due to occupied port" + exit 1 + fi echo "Available." fi } From 03e9da38df01190dbea943256f96923997579780 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Sun, 10 Oct 2021 12:41:13 +0300 Subject: [PATCH 6/6] Try alternate NetworkManager-naming 'service network-manager' may fail on newer Linux-distros due to the service actually being called 'NetworkManager' now. This patch tries both and warns the user, if it still fails. --- scripts/setup_ap.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/setup_ap.sh b/scripts/setup_ap.sh index c29fc1b..f9f13f8 100755 --- a/scripts/setup_ap.sh +++ b/scripts/setup_ap.sh @@ -26,7 +26,15 @@ setup () { if test -d /etc/NetworkManager; then echo "Stopping NetworkManager..." - sudo service network-manager stop + sudo service network-manager stop 2>/dev/null + if [ $? ] + then + sudo service NetworkManager stop 2>/dev/null + if [ $? ] + then + echo "** Failed to stop NetworkManager, AP may not work! **" + fi + fi fi echo "Configuring AP interface..." @@ -62,7 +70,15 @@ cleanup () { if test -d /etc/NetworkManager; then echo "Restarting NetworkManager..." - sudo service network-manager restart + sudo service network-manager restart 2>/dev/null + if [ $? ] + then + sudo service NetworkManager restart 2>/dev/null + if [ $? ] + then + echo "** Failed to restart NetworkManager: network may not be functional! **" + fi + fi fi }