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 User from app.network_utils import ( connect_network, disconnect_network, killswich_disaable, killswich_enable, killswitch_status, scan_networks, vpn_connect, vpn_connected, vpn_disconnect, ) @app.route("/") @app.route("/index") @login_required def index(): return render_template("index.html", networks=scan_networks(), vpn=vpn_connected(), killswitch=killswitch_status()) @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("/connect/&", methods=["GET", "POST"]) @login_required def connect(ssid, security): if security == "WPA2": return redirect(url_for("connectwpa", ssid=ssid)) result = connect_network(ssid, security, "") if result: return render_template("message.html", message="Successfully connected to {}".format(ssid)) return render_template("message.html", message="Failt to connected to {}".format(ssid)) @app.route("/wpa/", methods=["GET", "POST"]) @login_required def connectwpa(ssid): form = WPAForm() if form.validate_on_submit(): result = connect_network(ssid, "WPA2", form.password.data) if result: return render_template("message.html", message="Successfully connected to {}".format(ssid)) return render_template("message.html", message="Failt to connected to {}".format(ssid)) return render_template("wpa.html", title="WPA Password", form=form) @app.route("/disconnect/", methods=["GET", "POST"]) @login_required def disconnect(ssid): if disconnect_network(): return render_template("message.html", message="Sucessfully disconnected from {}".format(ssid)) return render_template("message.html", message="Failt to disconnect from {}".format(ssid)) @app.route("/vpndconnect/", methods=["GET", "POST"]) @login_required def vpnconnect(): if vpn_connect(): return render_template("message.html", message="Sucessfully Connected to VPN") return render_template("message.html", message="Failed to Connect to VPN") @app.route("/vpndisconnect/", methods=["GET", "POST"]) @login_required def vpndisconnect(): if vpn_disconnect(): return render_template("message.html", message="Sucessfully disconnected from VPN") return render_template("message.html", message="Failed to disconnect from VPN") @app.route("/ksenable/", methods=["GET", "POST"]) @login_required def ksenable(): if killswich_enable(): return render_template("message.html", message="Sucessfully Enabled KillSwitch") return render_template("message.html", message="Failed to Enable KillSwitch") @app.route("/ksdisable/", methods=["GET", "POST"]) @login_required def ksdisable(): if killswich_disaable(): return render_template("message.html", message="Sucessfully Disabled KillSwitch") return render_template("message.html", message="Failed to Disable KillSwitch")