def vpn_psk(): form = vpnPskForm() if request.method == "GET": return render_template("vpn_psk.html", form=form) elif request.method == "POST": if form.validate(): vpn_server = form.vpn_server.data user_id = form.user_id.data user_psk = form.user_psk.data set_vpn_params(vpn_server, user_id, user_psk) restart_vpn() if vpn_status(): flash("VPN settings saved and VPN restarted!", "success") return redirect(url_for("status")) else: flash( "VPN settings saved and VPN restarted! Unable to establish VPN connection.", "error") return render_template("vpn_psk.html", form=form) else: flash("Error! " + str(form.data), "error") return render_template("vpn_psk.html", form=form)
def status(): form = statusForm() if request.method == "GET": # check to see if network and vpn are active, red=not active, green=active internet_status_color = "green" if internet_status() else "red" vpn_status_color = "green" if vpn_status() else "red" return render_template("status.html", form=form, internet_status_color=internet_status_color, vpn_status_color=vpn_status_color)
def login(): form = loginForm() if (request.method == "GET"): return render_template("login.html", form=form) elif (request.method == "POST"): if (form.validate()): username = form.username.data password = form.password.data if (user_validate_credentials(username, password)): user = User() user.id = username flask_login.login_user(user) #check to see if default credentials are being used. If so, redirect to change password page. if (user_validate_credentials("admin", "gosecure")): flash("Please change the default password.", "notice") return redirect(url_for("user")) else: internet_status_bool = internet_status() vpn_status_bool = vpn_status() vpn_configuration_status_bool = vpn_configuration_status() #check to see if network is up. If not, redirect to network page if (internet_status_bool == False and vpn_configuration_status_bool == True): flash("Internet is not reachable.", "notice") return redirect(url_for("wifi")) #check to see if network and vpn are up. If not, redirect to initial setup page elif (internet_status_bool == False and vpn_status_bool == False): return redirect(url_for("initial_setup")) #check to see if vpn is up. If not, redirect to vpn page elif (vpn_status_bool == False): flash("VPN is not established.", "notice") return redirect(url_for("vpn_psk")) else: return redirect( request.args.get("next") or url_for("status")) else: flash("Invalid username or password. Please try again.", "error") return render_template("login.html", form=form) else: flash_form_errors(form) return render_template("login.html", form=form)
def login(): form = loginForm() if request.method == "GET": return render_template("login.html", form=form) elif request.method == "POST": if form.validate(): username = form.username.data password = form.password.data if user_validate_credentials(username, password): user = User() user.id = username flask_login.login_user(user) # check to see if default credentials are being used. If so, redirect to change password page. if user_validate_credentials("admin", "gosecure"): flash("Please change the default password.", "notice") return redirect(url_for("user")) else: internet_status_bool = internet_status() vpn_status_bool = vpn_status() vpn_configuration_status_bool = vpn_configuration_status() # check to see if network is up. If not, redirect to network page if internet_status_bool is False and vpn_configuration_status_bool is True: flash("Internet is not reachable.", "notice") return redirect(url_for("wifi")) # check to see if network and vpn are up. If not, redirect to initial setup page elif internet_status_bool is False and vpn_status_bool is False: return redirect(url_for("initial_setup")) # check to see if vpn is up. If not, redirect to vpn page elif vpn_status_bool is False: flash("VPN is not established.", "notice") return redirect(url_for("vpn_psk")) else: return redirect(request.args.get("next") or url_for("status")) else: flash("Invalid username or password. Please try again.", "error") return render_template("login.html", form=form) else: flash_form_errors(form) return render_template("login.html", form=form)
def vpn_psk(): form = vpnPskForm() if(request.method == "GET"): return render_template("vpn_psk.html", form=form) elif(request.method == "POST"): if(form.validate()): vpn_server = form.vpn_server.data user_id = form.user_id.data user_psk = form.user_psk.data set_vpn_params(vpn_server, user_id, user_psk) restart_vpn() if(vpn_status()): flash("VPN settings saved and VPN restarted!", "success") return redirect(url_for("status")) else: flash("VPN settings saved and VPN restarted! Unable to establish VPN connection.", "error") return render_template("vpn_psk.html", form=form) else: flash("Error! " + str(form.data), "error") return render_template("vpn_psk.html", form=form)
if restart_vpn(): return "VPN service restarted, VPN is ESTABLISHED" else: return "VPN service restarted, VPN is NOT ESTABLISHED" else: return "Error! Invalid Action!" else: return "415 Unsupported Media Type - Use application/json" else: return "Only POST method is supported. Refer to the API Documentation" if __name__ == "__main__": app.secret_key = os.urandom(24) # Just because OCD if vpn_status(): turn_on_led_green() # if SSL key and certificate pair do not exist, create them. if (os.path.exists("ssl.key") and os.path.exists("ssl.crt")) is not True: os.system('openssl genrsa 2048 > ssl.key') os.system( 'openssl req -new -x509 -nodes -sha256 -days 1095 -subj "/C=US/O=goSecure/CN=goSecureClient" -key ssl.key > ssl.crt' ) os.system('sudo chown pi:pi ssl.crt ssl.key') os.system('sudo chmod 440 ssl.crt ssl.key') app.run(host="192.168.50.1", port=443, ssl_context=("ssl.crt", "ssl.key"))