diff --git a/config.txt b/config.txt index 1940ecb..f9c6f1e 100644 --- a/config.txt +++ b/config.txt @@ -1,10 +1,8 @@ -# Please input the good wlan device (most of the time it is wlan0 or wlan1) +# Please input the wlan device to be used (most of the time it is wlan0 or wlan1) WLAN=wlan0 -# The ETH device should be connected to the internet but it should also work if it is a local network only -ETH=eth0 - # Here you could change the WIFI-name and password but most likely most scripts won't work after # Because the WIFI-credentials are hardcoded in the esp8266-ota-flash-convert AP=vtrust-flash PASS=flashmeifyoucan +GATEWAY=10.42.42.1 diff --git a/scripts/fake-registration-server.py b/scripts/fake-registration-server.py index abd7132..4c311fc 100755 --- a/scripts/fake-registration-server.py +++ b/scripts/fake-registration-server.py @@ -11,10 +11,18 @@ from tornado.options import define, options, parse_command_line define("port", default=80, help="run on the given port", type=int) +define("addr", default="10.42.42.1", help="run on the given ip", type=str) define("debug", default=True, help="run in debug mode") define("secKey", default="0000000000000000", help="key used for encrypted communication") import os +import signal + +def exit_cleanly(signal, frame): + print("Received SIGINT, exiting...") + exit(0) + +signal.signal(signal.SIGINT, exit_cleanly) from Crypto.Cipher import AES pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16) @@ -244,9 +252,16 @@ def main(): #static_path=os.path.join(os.path.dirname(__file__), "static"), debug=options.debug, ) - app.listen(options.port) - print("Listening on port "+str(options.port)) - tornado.ioloop.IOLoop.current().start() + try: + app.listen(options.port, options.addr) + print("Listening on " + str(options.addr) + ":" + str(options.port)) + tornado.ioloop.IOLoop.current().start() + except OSError as err: + print("Could not start server on port " + str(options.port)) + if err.errno is 98: # EADDRINUSE + print("Close the process on this port and try again") + else: + print(err) if __name__ == "__main__": diff --git a/scripts/psk-frontend.py b/scripts/psk-frontend.py index 82b478c..56cbfa5 100755 --- a/scripts/psk-frontend.py +++ b/scripts/psk-frontend.py @@ -87,7 +87,8 @@ def data_ready_cb(self, s): def main(): - proxies = [PskFrontend('', 443, '127.0.0.1', 80), PskFrontend('', 8886, '127.0.0.1', 1883)] + gateway = '10.42.42.1' + proxies = [PskFrontend(gateway, 443, gateway, 80), PskFrontend(gateway, 8886, gateway, 1883)] while True: diff --git a/scripts/setup_ap.sh b/scripts/setup_ap.sh index 53995b0..41f9138 100755 --- a/scripts/setup_ap.sh +++ b/scripts/setup_ap.sh @@ -3,47 +3,35 @@ # Source config . ../config.txt -if test -d /etc/NetworkManager; then - echo "Backing up NetworkManager.cfg..." - sudo cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.backup - - cat <<- EOF > /etc/NetworkManager/NetworkManager.conf - [main] - plugins=keyfile - - [keyfile] - unmanaged-devices=interface-name:$WLAN - EOF - - echo "Restarting NetworkManager..." - sudo service network-manager restart +if ! iw list | grep -q "* AP"; then + echo "AP mode not supported!" + echo "Please attach a WiFi card that supports AP mode." + exit 1 fi -sudo ifconfig $WLAN up -echo "Backing up /etc/dnsmasq.conf..." -sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup +echo -n "Checking for network interface $WLAN... " +if [ -e /sys/class/net/$WLAN ]; then + echo "Found." +else + echo "Not found!" + echo -n "Please edit WLAN in config.txt to one of: " + ls -m /sys/class/net + exit 1 +fi +wpa_supplicant_pid=$(pidof wpa_supplicant) +if [ -n "$wpa_supplicant_pid" ]; then + echo "Attempting to stop wpa_supplicant" + sudo kill $wpa_supplicant_pid +fi -echo "Writing dnsmasq config file..." -echo "Creating new /etc/dnsmasq.conf..." -cat <<- EOF >/etc/dnsmasq.conf - # disables dnsmasq reading any other files like /etc/resolv.conf for nameservers - no-resolv - # Interface to bind to - interface=$WLAN - #Specify starting_range,end_range,lease_time - dhcp-range=10.42.42.10,10.42.42.40,12h - # dns addresses to send to the clients - server=9.9.9.9 - server=1.1.1.1 - address=/tuya.com/10.42.42.1 - address=/tuyaeu.com/10.42.42.1 - address=/tuyaus.com/10.42.42.1 - address=/tuyacn.com/10.42.42.1 -EOF +if test -d /etc/NetworkManager; then + echo "Stopping NetworkManager..." + sudo service network-manager stop +fi echo "Writing hostapd config file..." -cat <<- EOF >/etc/hostapd/hostapd.conf +cat <<- EOF >hostapd.conf interface=$WLAN driver=nl80211 ssid=$AP @@ -60,39 +48,27 @@ cat <<- EOF >/etc/hostapd/hostapd.conf EOF echo "Configuring AP interface..." -sudo ifconfig $WLAN up 10.42.42.1 netmask 255.255.255.0 -echo "Applying iptables rules..." -sudo iptables --flush -sudo iptables --table nat --flush -sudo iptables --delete-chain -sudo iptables --table nat --delete-chain -sudo iptables --table nat --append POSTROUTING --out-interface $ETH -j MASQUERADE -sudo iptables --append FORWARD --in-interface $WLAN -j ACCEPT +sudo ifconfig $WLAN down +sudo ifconfig $WLAN up $GATEWAY netmask 255.255.255.0 +sudo ip route add 255.255.255.255 dev $WLAN echo "Starting DNSMASQ server..." -sudo /etc/init.d/dnsmasq stop > /dev/null 2>&1 +sudo dnsmasq \ + --no-resolv \ + --interface=$WLAN \ + --bind-interfaces \ + --listen-address=$GATEWAY \ + --dhcp-range=10.42.42.10,10.42.42.40,12h \ + --address=/#/$GATEWAY + +echo "Starting AP on $WLAN..." +sudo hostapd hostapd.conf +echo "AP closed" + +echo "Stopping DNSMASQ server..." sudo pkill dnsmasq -sudo dnsmasq - -sudo sysctl -w net.ipv4.ip_forward=1 > /dev/null 2>&1 - -sudo ip route add 255.255.255.255 dev $WLAN - - -echo "Starting AP on $WLAN in screen terminal..." -sudo hostapd /etc/hostapd/hostapd.conf if test -d /etc/NetworkManager; then - sudo rm /etc/NetworkManager/NetworkManager.conf > /dev/null 2>&1 - sudo mv /etc/NetworkManager/NetworkManager.conf.backup /etc/NetworkManager/NetworkManager.conf + echo "Restarting NetworkManager..." sudo service network-manager restart fi -sudo /etc/init.d/dnsmasq stop > /dev/null 2>&1 -sudo pkill dnsmasq -sudo rm /etc/dnsmasq.conf > /dev/null 2>&1 -sudo mv /etc/dnsmasq.conf.backup /etc/dnsmasq.conf > /dev/null 2>&1 -sudo rm /etc/dnsmasq.hosts > /dev/null 2>&1 -sudo iptables --flush -sudo iptables --flush -t nat -sudo iptables --delete-chain -sudo iptables --table nat --delete-chain diff --git a/start_flash.sh b/start_flash.sh index d9cb179..7387259 100755 --- a/start_flash.sh +++ b/start_flash.sh @@ -35,14 +35,16 @@ if [ "$REPLY" != "yes" ]; then exit fi echo "======================================================" -echo " Starting AP in a screen" +echo -n " Starting AP in a screen" $screen_with_log smarthack-wifi.log -S smarthack-wifi -m -d ./setup_ap.sh -echo " Stopping any apache web server" -sudo service apache2 stop >/dev/null 2>&1 +while ! ping -c 1 -W 1 -n $GATEWAY &> /dev/null; do + printf . +done echo " Starting web server in a screen" $screen_with_log smarthack-web.log -S smarthack-web -m -d ./fake-registration-server.py echo " Starting Mosquitto in a screen" sudo service mosquitto stop >/dev/null 2>&1 +sudo pkill mosquitto $screen_with_log smarthack-mqtt.log -S smarthack-mqtt -m -d mosquitto -v echo " Starting PSK frontend in a screen" $screen_with_log smarthack-psk.log -S smarthack-psk -m -d ./psk-frontend.py -v diff --git a/stop_flash.sh b/stop_flash.sh index 12550f5..e6e2e06 100755 --- a/stop_flash.sh +++ b/stop_flash.sh @@ -1,8 +1,8 @@ #!/bin/bash -echo "Stopping AP in a screen" -sudo screen -S smarthack-wifi -X stuff '^C' sudo screen -S smarthack-web -X stuff '^C' sudo screen -S smarthack-smartconfig -X stuff '^C' sudo screen -S smarthack-mqtt -X stuff '^C' sudo screen -S smarthack-psk -X stuff '^C' +echo "Closing AP" +sudo pkill hostapd