erm
This commit is contained in:
@@ -3,7 +3,6 @@ from config import Config
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from flask_login import LoginManager
|
||||
from app.network_utils import cleanup_network
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
@@ -12,6 +11,4 @@ migrate = Migrate(app, db)
|
||||
login = LoginManager(app)
|
||||
login.login_view = "login"
|
||||
|
||||
cleanup_network()
|
||||
|
||||
from app import routes2, models
|
||||
from app import routes, models
|
||||
|
||||
@@ -28,12 +28,28 @@ ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
"""
|
||||
|
||||
|
||||
import logging
|
||||
import shlex
|
||||
|
||||
def get_logger():
|
||||
"""Returns a logger"""
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
return logging.getLogger()
|
||||
|
||||
|
||||
def run_subprocess(cmd, check=True, delay=0):
|
||||
cmd_split = cmd.split(" ")
|
||||
output = subprocess.run(cmd_split, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=check).stdout.decode("utf-8")
|
||||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
return output
|
||||
"""Runs a subprocess command"""
|
||||
try:
|
||||
cmd_split = shlex.split(cmd)
|
||||
output = subprocess.run(
|
||||
cmd_split, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=check, text=True, encoding="utf-8"
|
||||
)
|
||||
if delay > 0:
|
||||
time.sleep(delay)
|
||||
return output.stdout
|
||||
except subprocess.CalledProcessError as e:
|
||||
get_logger().error("Subprocess command failed: %s", e)
|
||||
return None
|
||||
|
||||
|
||||
def run_subprocess_interface(cmd, check=True):
|
||||
@@ -112,38 +128,47 @@ def parse_iwlist(iwlist_output, current_sid):
|
||||
|
||||
def scan_networks():
|
||||
"""Scans for Networks"""
|
||||
output = run_subprocess("sudo iwgetid", check=False)
|
||||
output = run_subprocess("sudo iwgetid")
|
||||
if output is None:
|
||||
get_logger().error("Failed to get current network SSID.")
|
||||
current = ""
|
||||
else:
|
||||
current = output.partition("ESSID:")[2].strip().strip('"')
|
||||
|
||||
current = output.partition("ESSID:")[2].strip().strip('"')
|
||||
|
||||
worked = False
|
||||
while not worked:
|
||||
try:
|
||||
output = run_subprocess("sudo iwlist {} scan".format(ClientInterface))
|
||||
worked = True
|
||||
except:
|
||||
try:
|
||||
output = run_subprocess("sudo ifup {}".format(ClientInterface))
|
||||
output = None
|
||||
while output is None:
|
||||
output = run_subprocess(f"sudo iwlist {ClientInterface} scan")
|
||||
if output is None:
|
||||
get_logger().info("Failed to scan networks, trying to bring interface up.")
|
||||
ifup_output = run_subprocess(f"sudo ifup {ClientInterface}")
|
||||
if ifup_output is None:
|
||||
get_logger().error("Failed to bring interface up.")
|
||||
# Avoid a tight loop if `ifup` fails
|
||||
time.sleep(5)
|
||||
else:
|
||||
time.sleep(1)
|
||||
except:
|
||||
pass
|
||||
scan = parse_iwlist(output, current)
|
||||
|
||||
scan = parse_iwlist(output, current)
|
||||
return scan
|
||||
|
||||
|
||||
def connect_network(ssid, security, password):
|
||||
"""Connects to a network"""
|
||||
with open("/etc/network/interfaces", "r") as source:
|
||||
lines = source.readlines()
|
||||
with open("/etc/network/interfaces", "w") as source:
|
||||
for line in lines:
|
||||
source.write(
|
||||
re.sub(r"^iface {} inet manual".format(ClientInterface), "iface {} inet dhcp".format(ClientInterface), line)
|
||||
re.sub(r"^iface {} inet manual".format(ClientInterface), f"iface {ClientInterface} inet dhcp", line)
|
||||
)
|
||||
|
||||
run_subprocess_interface("sudo /usr/sbin/ifdown {}")
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifdown {}") is None:
|
||||
get_logger().error("Failed to bring interface down.")
|
||||
return False
|
||||
|
||||
run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant")
|
||||
if run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant") is None:
|
||||
get_logger().error("Failed to stop wpa_supplicant.")
|
||||
return False
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
if security == "WPA2":
|
||||
@@ -151,96 +176,124 @@ def connect_network(ssid, security, password):
|
||||
else:
|
||||
f.write(wpafile_nowpa.format(ssid))
|
||||
|
||||
try:
|
||||
run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant")
|
||||
run_subprocess_interface("sudo /usr/sbin/ifup {}")
|
||||
except:
|
||||
if run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant") is None:
|
||||
get_logger().error("Failed to start wpa_supplicant.")
|
||||
return False
|
||||
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifup {}") is None:
|
||||
get_logger().error("Failed to bring interface up.")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def disconnect_network():
|
||||
run_subprocess_interface("sudo /usr/sbin/ifdown {}")
|
||||
"""Disconnects from a network"""
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifdown {}") is None:
|
||||
get_logger().error("Failed to bring interface down.")
|
||||
return False
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile_none)
|
||||
|
||||
run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant")
|
||||
if run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant") is None:
|
||||
get_logger().error("Failed to stop wpa_supplicant.")
|
||||
return False
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(
|
||||
re.sub(r"^iface {} inet dhcp".format(ClientInterface), "iface {} inet manual".format(ClientInterface), line)
|
||||
re.sub(r"^iface {} inet dhcp".format(ClientInterface), f"iface {ClientInterface} inet manual", line)
|
||||
)
|
||||
|
||||
try:
|
||||
run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant")
|
||||
run_subprocess_interface("sudo /usr/sbin/ifup {}")
|
||||
except:
|
||||
if run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant") is None:
|
||||
get_logger().error("Failed to start wpa_supplicant.")
|
||||
return False
|
||||
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifup {}") is None:
|
||||
get_logger().error("Failed to bring interface up.")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def cleanup_network():
|
||||
run_subprocess_interface("sudo /usr/sbin/ifdown {}")
|
||||
"""Cleans up the network"""
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifdown {}") is None:
|
||||
get_logger().error("Failed to bring interface down.")
|
||||
return False
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile_none)
|
||||
|
||||
run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant")
|
||||
if run_subprocess("sudo /usr/bin/systemctl stop wpa_supplicant") is None:
|
||||
get_logger().error("Failed to stop wpa_supplicant.")
|
||||
return False
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(
|
||||
re.sub(r"^iface {} inet dhcp".format(ClientInterface), "iface {} inet manual".format(ClientInterface), line)
|
||||
re.sub(r"^iface {} inet dhcp".format(ClientInterface), f"iface {ClientInterface} inet manual", line)
|
||||
)
|
||||
|
||||
try:
|
||||
run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant")
|
||||
run_subprocess_interface("sudo /usr/sbin/ifup {}")
|
||||
except:
|
||||
if run_subprocess("sudo /usr/bin/systemctl start wpa_supplicant") is None:
|
||||
get_logger().error("Failed to start wpa_supplicant.")
|
||||
return False
|
||||
|
||||
if run_subprocess_interface("sudo /usr/sbin/ifup {}") is None:
|
||||
get_logger().error("Failed to bring interface up.")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def vpn_connected():
|
||||
if run_subprocess("nordvpn status").find("Disconnected") != -1:
|
||||
"""Checks if the VPN is connected"""
|
||||
output = run_subprocess("nordvpn status")
|
||||
if output is None:
|
||||
return False
|
||||
return True
|
||||
return "Disconnected" not in output
|
||||
|
||||
|
||||
def vpn_connect():
|
||||
if run_subprocess("nordvpn c").find("connected to") != -1:
|
||||
return True
|
||||
return False
|
||||
"""Connects to the VPN"""
|
||||
output = run_subprocess("nordvpn c")
|
||||
if output is None:
|
||||
return False
|
||||
return "connected to" in output
|
||||
|
||||
|
||||
def vpn_disconnect():
|
||||
if run_subprocess("nordvpn d").find("You are disconnected from NordVPN") != -1:
|
||||
return True
|
||||
return False
|
||||
"""Disconnects from the VPN"""
|
||||
output = run_subprocess("nordvpn d")
|
||||
if output is None:
|
||||
return False
|
||||
return "You are disconnected from NordVPN" in output
|
||||
|
||||
|
||||
def killswitch_status():
|
||||
if run_subprocess("nordvpn settings").find("Kill Switch: disabled") != -1:
|
||||
return False
|
||||
return True
|
||||
"""Checks the status of the killswitch"""
|
||||
output = run_subprocess("nordvpn settings")
|
||||
if output is None:
|
||||
return True # Assume enabled for safety
|
||||
return "Kill Switch: disabled" not in output
|
||||
|
||||
|
||||
def killswich_enable():
|
||||
if run_subprocess("nordvpn set killswitch on", delay=2).find("Kill Switch is set to 'enabled' successfully") != -1:
|
||||
return True
|
||||
return False
|
||||
"""Enables the killswitch"""
|
||||
output = run_subprocess("nordvpn set killswitch on", delay=2)
|
||||
if output is None:
|
||||
return False
|
||||
return "Kill Switch is set to 'enabled' successfully" in output
|
||||
|
||||
|
||||
def killswich_disaable():
|
||||
if run_subprocess("nordvpn set killswitch off", delay=2).find("Kill Switch is set to 'disabled' successfully") != -1:
|
||||
return True
|
||||
return False
|
||||
"""Disables the killswitch"""
|
||||
output = run_subprocess("nordvpn set killswitch off", delay=2)
|
||||
if output is None:
|
||||
return False
|
||||
return "Kill Switch is set to 'disabled' successfully" in output
|
||||
|
||||
@@ -1,322 +0,0 @@
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from config import RPI, ClientInterface, Debug
|
||||
from flask import flash, redirect, render_template, url_for
|
||||
from flask_login import current_user, login_required, login_user, logout_user
|
||||
|
||||
from app import app
|
||||
from app.forms import LoginForm, WPAForm
|
||||
from app.models import ConfigTable, Passwords, User
|
||||
|
||||
CMD_SCAN = "sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlan1 --rescan yes"
|
||||
CMD_JOIN = "sudo nmcli device wifi connect *SSID* ifname wlan1"
|
||||
CMD_JOINPW = "sudo nmcli device wifi connect *SSID* ifname wlan1 password *PASSWORD*"
|
||||
CMD_DISCONNECT = "sudo nmcli device disconnect wlan1"
|
||||
# Error: Connection activation failed: (7) Secrets were required, but not provided.
|
||||
# Device 'wlxf81a6719febb' successfully activated with '11111-1111-11111-111111-11111111'
|
||||
|
||||
|
||||
def parse_iwlist(iwlist_output, current):
|
||||
data = []
|
||||
cell = []
|
||||
for line in iwlist_output.splitlines():
|
||||
if line.find(" Cell ") != -1 and cell != []:
|
||||
data.append(cell)
|
||||
cell = []
|
||||
elif line.find("Scan completed :") > 0:
|
||||
pass
|
||||
else:
|
||||
cell.append(line)
|
||||
|
||||
try:
|
||||
del data[0][0]
|
||||
except:
|
||||
pass
|
||||
|
||||
cells = []
|
||||
|
||||
for a in data:
|
||||
cell = {}
|
||||
for line in a:
|
||||
line = line.strip()
|
||||
if line.find("ESSID:") != -1:
|
||||
if line.partition("ESSID:")[2].strip('"') != "":
|
||||
cell["SSID"] = line.partition("ESSID:")[2].strip('"')
|
||||
if cell["SSID"] == current:
|
||||
cell["Connected"] = "Yes"
|
||||
else:
|
||||
cell["Connected"] = ""
|
||||
if line.partition("Signal level=")[2].split("/")[0] != "" != -1:
|
||||
cell["Signal"] = line.partition("Signal level=")[2].split("/")[0]
|
||||
if line.find("Encryption key:") != -1:
|
||||
if line.find(":on") != -1:
|
||||
cell["WPA"] = "WPA2"
|
||||
else:
|
||||
cell["WPA"] = "None"
|
||||
cells.append(cell)
|
||||
|
||||
return cells
|
||||
|
||||
|
||||
def scan_networks():
|
||||
scan = []
|
||||
|
||||
if not RPI:
|
||||
output = subprocess.run(CMD_SCAN.split(" "), stdout=subprocess.PIPE).stdout.decode("utf-8")
|
||||
for line in output.splitlines():
|
||||
t = line.split(":")
|
||||
if t[0] != "":
|
||||
scan.append(line)
|
||||
else:
|
||||
output = subprocess.run(["sudo", "iwgetid"], stdout=subprocess.PIPE).stdout.decode("utf-8")
|
||||
current = output.partition("ESSID:")[2].strip().strip('"')
|
||||
|
||||
output = subprocess.run(["sudo", "iwlist", ClientInterface, "scan"], stdout=subprocess.PIPE).stdout.decode("utf-8")
|
||||
scan = parse_iwlist(output, current)
|
||||
# a = 0
|
||||
# scan = []
|
||||
# for line in output.splitlines():
|
||||
# if a == 0:
|
||||
# connected = " "
|
||||
# if line.partition("ESSID:")[2].strip('"') != "":
|
||||
# a = 1
|
||||
# ssid = line.partition("ESSID:")[2].strip('"')
|
||||
# if ssid == current:
|
||||
# connected = "*"
|
||||
# if a == 1:
|
||||
# if line.find("Encryption key:off") != -1:
|
||||
# password = ""
|
||||
# a = 2
|
||||
# elif line.find("Encryption key:on") != -1:
|
||||
# password = "WPA2"
|
||||
# a = 2
|
||||
# if a == 2:
|
||||
# if line.partition("Signal level=")[2].split("/")[0] != "":
|
||||
# signal = line.partition("Signal level=")[2].split("/")[0]
|
||||
# a = 0
|
||||
# scan.append(ssid + ":" + signal + ":" + connected + ":" + password)
|
||||
return scan
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@app.route("/index")
|
||||
@login_required
|
||||
def index():
|
||||
results = []
|
||||
scan = ["rpi:100: :WPA2", "Home:94:*:WPA2", "HOME2:48: :WPA2", "BT:23: :"]
|
||||
if not Debug:
|
||||
scan = scan_networks()
|
||||
|
||||
# for network in scan:
|
||||
# item = [
|
||||
# network.split(":", maxsplit=1)[0],
|
||||
# network.split(":")[1],
|
||||
# "Yes" if network.split(":")[2] == "*" else "",
|
||||
# "None" if network.split(":")[3].strip() == "" else "WPA2",
|
||||
# ]
|
||||
# results.append(item)
|
||||
|
||||
item = []
|
||||
for network in scan:
|
||||
item = [network["SSID"], network["Signal"], network["Connected"], network["WPA"]]
|
||||
results.append(item)
|
||||
|
||||
# table = Networks(results)
|
||||
# table.border = True
|
||||
t = {}
|
||||
for a in results:
|
||||
t["ssid"] = a[0]
|
||||
|
||||
return render_template("index.html", index_table=results)
|
||||
|
||||
|
||||
@app.route("/login", methods=["GET", "POST"])
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for("index"))
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=form.username.data).first()
|
||||
if user is None or not user.check_password(form.password.data):
|
||||
flash("Invalid username or password")
|
||||
return redirect(url_for("login"))
|
||||
login_user(user)
|
||||
return redirect(url_for("index"))
|
||||
return render_template("login.html", title="Sign In", form=form)
|
||||
|
||||
|
||||
@app.route("/logout")
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for("index"))
|
||||
|
||||
|
||||
@app.route("/wpa/<ssid>", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def wpa(ssid):
|
||||
# wlan0: flags=4098<BROADCAST,MULTICAST> mtu 1500
|
||||
# wlan1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
|
||||
if not RPI:
|
||||
form = WPAForm()
|
||||
if form.validate_on_submit():
|
||||
cmd = CMD_JOINPW.replace("*SSID*", ssid)
|
||||
cmd = cmd.replace("*PASSWORD*", form.password.data)
|
||||
|
||||
output = subprocess.run(cmd.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
|
||||
if output.find("Error") == -1 and output != "":
|
||||
return render_template("message.html", message="Successfully connected to {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Failed to connect to {}".format(ssid))
|
||||
|
||||
return render_template("wpa.html", title="WPA Password", form=form)
|
||||
else:
|
||||
form = WPAForm()
|
||||
if form.validate_on_submit():
|
||||
wpafile = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
network={{
|
||||
ssid="{}"
|
||||
psk="{}"
|
||||
key_mgmt=WPA-PSK
|
||||
}}
|
||||
""".format(
|
||||
ssid, form.password.data
|
||||
)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet manual", "iface wlan0 inet dhcp", line))
|
||||
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "stop", "wpa_supplicant"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile)
|
||||
|
||||
try:
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "start", "wpa_supplicant"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=20,
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifup", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20
|
||||
).stdout.decode("utf-8")
|
||||
except:
|
||||
return render_template("message.html", message="Failt to connected to {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Successfully connected to {}".format(ssid))
|
||||
|
||||
return render_template("wpa.html", title="WPA Password", form=form)
|
||||
|
||||
|
||||
@app.route("/connect/<string:ssid>&<string:security>", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def connect(ssid, security):
|
||||
if security == "WPA2":
|
||||
return redirect(url_for("wpa", ssid=ssid))
|
||||
|
||||
# network={
|
||||
# ssid="my ssid with spaces"
|
||||
# key_mgmt=NONE
|
||||
# }
|
||||
|
||||
if not RPI:
|
||||
cmd = CMD_JOIN.replace("*SSID*", ssid)
|
||||
|
||||
output = subprocess.run(cmd.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
|
||||
if output.find("Error") == -1:
|
||||
return render_template("message.html", message="Successfully connected to {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Failed to connect to {}".format(ssid))
|
||||
else:
|
||||
wpafile = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
network={{
|
||||
ssid="{}"
|
||||
key_mgmt=NONE
|
||||
}}
|
||||
""".format(
|
||||
ssid
|
||||
)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet manual", "iface wlan0 inet dhcp", line))
|
||||
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "stop", "wpa_supplicant"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile)
|
||||
|
||||
try:
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "start", "wpa_supplicant"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=20,
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifup", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20
|
||||
).stdout.decode("utf-8")
|
||||
except:
|
||||
return render_template("message.html", message="Failt to connected to {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Successfully connected to {}".format(ssid))
|
||||
|
||||
|
||||
@app.route("/disconnect/<string:ssid>", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def disconnect(ssid):
|
||||
if not RPI:
|
||||
output = subprocess.run(CMD_DISCONNECT.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
|
||||
if output.find("successfully disconnected") != -1:
|
||||
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Failed to Disconnect from {}".format(ssid))
|
||||
else:
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifdown", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
).stdout.decode("utf-8")
|
||||
|
||||
wpafile = """country=GB # Your 2-digit country code
|
||||
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
||||
"""
|
||||
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "wt") as f:
|
||||
f.write(wpafile)
|
||||
|
||||
with open("/etc/network/interfaces", "r") as sources:
|
||||
lines = sources.readlines()
|
||||
with open("/etc/network/interfaces", "w") as sources:
|
||||
for line in lines:
|
||||
sources.write(re.sub(r"^iface wlan0 inet dhcp", "iface wlan0 inet manual", line))
|
||||
|
||||
try:
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/bin/systemctl", "restart", "wpa_supplicant"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=20,
|
||||
).stdout.decode("utf-8")
|
||||
output = subprocess.run(
|
||||
["sudo", "/usr/sbin/ifup", ClientInterface], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20
|
||||
).stdout.decode("utf-8")
|
||||
except:
|
||||
return render_template("message.html", message="Failt to disconnect from {}".format(ssid))
|
||||
|
||||
return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid))
|
||||
@@ -1,65 +1,56 @@
|
||||
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
|
||||
<meta http-equiv="Pragma" content="no-cache" />
|
||||
<meta http-equiv="Expires" content="0" />
|
||||
<style type="text/css">
|
||||
.tg {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
|
||||
.tg td {
|
||||
border-color: black;
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 14px;
|
||||
overflow: hidden;
|
||||
padding: 10px 5px;
|
||||
word-break: normal;
|
||||
}
|
||||
|
||||
.tg th {
|
||||
border-color: black;
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 14px;
|
||||
font-weight: normal;
|
||||
overflow: hidden;
|
||||
padding: 10px 5px;
|
||||
word-break: normal;
|
||||
}
|
||||
|
||||
.tg .tg-0lax {
|
||||
text-align: left;
|
||||
vertical-align: top
|
||||
}
|
||||
</style>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Travel Router</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div>
|
||||
Travel Router:
|
||||
<a href="{{ url_for('index') }}">Home</a>
|
||||
{% if not current_user.is_anonymous %}
|
||||
<a href="{{ url_for('logout') }}">Logout</a>
|
||||
{% if vpn %}
|
||||
<a href="{{ url_for('vpndisconnect') }}">Disconnect from VPN</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('vpnconnect') }}">Connect to VPN</a>
|
||||
{% endif %}
|
||||
{% if killswitch %}
|
||||
<a href="{{ url_for('ksdisable') }}">Disable KillSwitch</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('ksenable') }}">Enable KillSwitch</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
<nav class="navbar navbar-expand-lg bg-body-tertiary">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="{{ url_for('index') }}">Travel Router</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
||||
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('index') }}">Home</a>
|
||||
</li>
|
||||
{% if not current_user.is_anonymous %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('logout') }}">Logout</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
{% if vpn %}
|
||||
<a class="nav-link" href="{{ url_for('vpndisconnect') }}">Disconnect from VPN</a>
|
||||
{% else %}
|
||||
<a class="nav-link" href="{{ url_for('vpnconnect') }}">Connect to VPN</a>
|
||||
{% endif %}
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
{% if killswitch %}
|
||||
<a class="nav-link" href="{{ url_for('ksdisable') }}">Disable KillSwitch</a>
|
||||
{% else %}
|
||||
<a class="nav-link" href="{{ url_for('ksenable') }}">Enable KillSwitch</a>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<hr>
|
||||
{% block content %}{% endblock %}
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
||||
crossorigin="anonymous"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -2,15 +2,17 @@
|
||||
|
||||
{% block content %}
|
||||
<h1>Networks</h1>
|
||||
<table class="tg">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="tg-0lax">SSID</th>
|
||||
<th class="tg-0lax">Quality</th>
|
||||
<th class="tg-0lax">Connected</th>
|
||||
<th class="tg-0lax">Security</th>
|
||||
<th class="tg-0lax">Action</th>
|
||||
<th scope="col">SSID</th>
|
||||
<th scope="col">Quality</th>
|
||||
<th scope="col">Connected</th>
|
||||
<th scope="col">Security</th>
|
||||
<th scope="col">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{%- for row in networks %}
|
||||
<tr>
|
||||
<td> {{ row['SSID'] }} </td>
|
||||
@@ -19,14 +21,13 @@
|
||||
<td> {{ row['WPA'] }} </td>
|
||||
|
||||
{% if row["Connected"] == "Yes" %}
|
||||
<!-- {{ "User is logged in" if loggedin else "User is not logged in" }} -->
|
||||
<td><a href={{"/disconnect/" + row["SSID"] }}>Disconnect</a></td>
|
||||
<td><a href={{"/disconnect/" + row["SSID"] }} class="btn btn-secondary">Disconnect</a></td>
|
||||
{% else %}
|
||||
<td><a href={{"/connect/" + row["SSID"] + "&" + row["WPA"]}}>Connect</a></td>
|
||||
<td><a href={{"/connect/" + row["SSID"] + "&" + row["WPA"]}} class="btn btn-primary">Connect</a></td>
|
||||
{% endif %}
|
||||
|
||||
</tr>
|
||||
{%- endfor %}
|
||||
</thead>
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
||||
@@ -3,20 +3,20 @@
|
||||
<h1>Sign In</h1>
|
||||
<form action="" method="post" novalidate>
|
||||
{{ form.hidden_tag() }}
|
||||
<p>
|
||||
{{ form.username.label }}<br>
|
||||
{{ form.username(size=32) }}<br>
|
||||
<div class="mb-3">
|
||||
<label for="username" class="form-label">{{ form.username.label }}</label>
|
||||
<input type="text" class="form-control" id="username" name="username" size="32">
|
||||
{% for error in form.username.errors %}
|
||||
<span style="color: red;">[{{ error }}]</span>
|
||||
<div class="invalid-feedback">{{ error }}</div>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p>
|
||||
{{ form.password.label }}<br>
|
||||
{{ form.password(size=32) }}<br>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">{{ form.password.label }}</label>
|
||||
<input type="password" class="form-control" id="password" name="password" size="32">
|
||||
{% for error in form.password.errors %}
|
||||
<span style="color: red;">[{{ error }}]</span>
|
||||
<div class="invalid-feedback">{{ error }}</div>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">{{ form.submit.label }}</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -1,5 +1,7 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h2>{{message}}</h2>
|
||||
<div class="alert alert-info" role="alert">
|
||||
{{message}}
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -3,14 +3,17 @@
|
||||
<h1>Password</h1>
|
||||
<form action="" method="post" novalidate>
|
||||
{{ form.hidden_tag() }}
|
||||
<p>
|
||||
{{ form.password.label }}<br>
|
||||
{{ form.password(size=32) }}<br>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">{{ form.password.label }}</label>
|
||||
<input type="password" class="form-control" id="password" name="password" size="32">
|
||||
{% for error in form.password.errors %}
|
||||
<span style="color: red;">[{{ error }}]</span>
|
||||
<div class="invalid-feedback">{{ error }}</div>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<p>{{ form.remember() }} {{ form.remember.label }}</p>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</div>
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="remember" name="remember">
|
||||
<label class="form-check-label" for="remember">{{ form.remember.label }}</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">{{ form.submit.label }}</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user