99 lines
3.1 KiB
Python
99 lines
3.1 KiB
Python
import subprocess
|
|
from pprint import pformat
|
|
|
|
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 Network, Networks, User
|
|
from config import debug
|
|
|
|
CMD_SCAN = "sudo nmcli -t -f SSID,SIGNAL,IN-USE,SECURITY -e yes -m tab device wifi list ifname wlxf81a6719febb --rescan yes"
|
|
CMD_JOIN = "sudo nmcli device wifi connect *SSID* ifname wlxf81a6719febb"
|
|
CMD_JOINPW = "sudo nmcli device wifi connect *SSID* ifname wlxf81a6719febb password *PASSWORD*"
|
|
# Error: Connection activation failed: (7) Secrets were required, but not provided.
|
|
# Device 'wlxf81a6719febb' successfully activated with '11111-1111-11111-111111-11111111'
|
|
|
|
|
|
def scan_networks():
|
|
scan = []
|
|
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)
|
|
|
|
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(
|
|
network.split(":")[0],
|
|
network.split(":")[1],
|
|
"Yes" if network.split(":")[2] == "*" else "",
|
|
"None" if network.split(":")[3].strip() == "" else "WPA2",
|
|
)
|
|
results.append(item)
|
|
|
|
table = Networks(results)
|
|
table.border = True
|
|
return render_template("index.html", table=table)
|
|
|
|
|
|
@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):
|
|
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:
|
|
return "Successfully connected to {}".format(ssid)
|
|
|
|
return "Failed to connect 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))
|
|
|
|
cmd = CMD_JOIN.replace("*SSID*", ssid)
|
|
return cmd
|