2019-03-15 11:56:33 +01:00
|
|
|
import io, socket, subprocess
|
|
|
|
from requests import post, get
|
|
|
|
from flask import request, Flask
|
2019-03-20 20:51:18 +01:00
|
|
|
#hostapd system
|
2019-03-15 11:56:33 +01:00
|
|
|
|
|
|
|
#http://192.168.43.155:5000/setup?ssid=cimaphone&password=cimakodu30&id=1
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2019-03-20 20:51:18 +01:00
|
|
|
def ap(switch):
|
|
|
|
pass
|
|
|
|
|
2019-03-15 11:56:33 +01:00
|
|
|
def writeconfig(ssid, password):
|
|
|
|
rtline = "\n"
|
|
|
|
with io.open("/etc/wpa_supplicant/wpa_supplicant.conf", "w", encoding="utf8") as note:
|
2019-03-22 08:45:27 +01:00
|
|
|
conf = str()
|
|
|
|
for i in ["ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\ncountry=FR\nnetwork={\nssid=\"", ssid, "\"\nscan_ssid=1\npsk=\"", password, "\"\n}"]:
|
|
|
|
conf += i
|
2019-03-15 11:56:33 +01:00
|
|
|
note.write(conf)
|
2019-03-20 20:51:18 +01:00
|
|
|
ap(False)
|
2019-03-15 11:56:33 +01:00
|
|
|
subprocess.check_call(["sudo", "wpa_cli", "-i", "wlan0", "reconfigure"])
|
|
|
|
|
|
|
|
|
|
|
|
def testinternet():
|
|
|
|
result = True
|
|
|
|
try:
|
|
|
|
socket.gethostbyname("www.google.com")
|
|
|
|
except:
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
|
|
|
@app.route("/setup")
|
|
|
|
def web_setup():
|
|
|
|
if not (request.args.get("ssid") and request.args.get("password") and request.args.get("id")):
|
|
|
|
return "Error"
|
|
|
|
else:
|
|
|
|
writeconfig(request.args.get("ssid"), request.args.get("password"))
|
|
|
|
if testinternet():
|
2019-03-22 08:45:27 +01:00
|
|
|
mac = io.open("/sys/class/net/wlan0/address").read()
|
|
|
|
id = request.args.get("id")
|
|
|
|
r = get(f"http://flifloo.ddns.net:5000/locksetup?mac={mac}&id={id}")
|
2019-03-15 11:56:33 +01:00
|
|
|
else:
|
2019-03-20 20:51:18 +01:00
|
|
|
ap(True)
|
2019-03-15 11:56:33 +01:00
|
|
|
return "Cant connect"
|
2019-03-20 20:51:18 +01:00
|
|
|
return "Done"
|
2019-03-15 11:56:33 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-03-20 20:51:18 +01:00
|
|
|
app.run(debug=True, port=5000, host="0.0.0.0")
|