Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking enhancements and bugfixes #225

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fcbb350
Added option for specifying the server bind addr, defaulting to 10.42…
kueblc Jul 2, 2019
0e7c1f0
Modify dnsmasq config to only bind to 10.42.42.1 as suggested in #187
kueblc Jul 2, 2019
48bdc07
Added config for mosquitto so it only binds to 10.42.42.1
kueblc Jul 2, 2019
9c8db57
Added a check to ensure that the configured network interface exists
kueblc Jul 2, 2019
d11f94f
Backup hostapd config #61
kueblc Jul 2, 2019
54af5eb
Check for wpa_supplicant #19 #84 #172 #208 and many more
kueblc Jul 2, 2019
22bdbb5
Exit server cleanly on SIGINT (^C) to reduce error message noise
kueblc Jul 4, 2019
5114b1e
Merge branch 'master' into fix-net-inf
kueblc Sep 21, 2019
153c8ed
Start dnsmasq with command line options rather than a config file
kueblc Sep 26, 2019
0f8764f
Revert changes to mosquitto, apparently the config is not respected a…
kueblc Oct 3, 2019
e1126b8
Closing AP should be the last thing we do, as the other scripts are b…
kueblc Oct 3, 2019
4cc1e2e
Don't modify NetworkManager.conf, just stop NetworkManager temporarily
kueblc Oct 3, 2019
8d3de79
Merge branch 'master' into fix-net-inf
kueblc Oct 3, 2019
b4e893c
Removed ETH and added GATEWAY to config
kueblc Oct 3, 2019
5bd8e45
Wait for gateway to be available before starting web server et al
kueblc Oct 3, 2019
155dfda
Merge branch 'master' into fix-net-inf
kueblc Oct 4, 2019
7076363
Connect psk frontend to gateway instead of 0.0.0.0 to mitigate potent…
kueblc Oct 5, 2019
5e3db7e
Added check for AP mode support
kueblc Oct 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions config.txt
Original file line number Diff line number Diff line change
@@ -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
21 changes: 18 additions & 3 deletions scripts/fake-registration-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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__":
Expand Down
3 changes: 2 additions & 1 deletion scripts/psk-frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
104 changes: 40 additions & 64 deletions scripts/setup_ap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 5 additions & 3 deletions start_flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stop_flash.sh
Original file line number Diff line number Diff line change
@@ -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