Пример #1
0
def main(address, port, ui_path, simulate, delete_connections):

    # See if caller wants to delete all existing connections first
    if delete_connections and not simulate:
        netman.delete_all_wifi_connections()

    # Check if we are already connected, if so we are done.
    if netman.have_active_internet_connection() and not simulate:
        print('Already connected to the internet, nothing to do, exiting.')
        sys.exit()

    # Get list of available AP from net man.
    # Must do this AFTER deleting any existing connections (above),
    # and BEFORE starting our hotspot (or the hotspot will be the only thing
    # in the list).
    ssids = netman.get_list_of_access_points()

    # Start the hotspot
    if not netman.start_hotspot() and not simulate:
        print('Error starting hotspot, exiting.')
        sys.exit(1)

    # Start dnsmasq (to advertise us as a router so captured portal pops up
    # on the users machine to vend our UI in our http server)
    if not simulate:
        dnsmasq.start()

    # Find the ui directory which is up one from where this file is located.
    web_dir = os.path.join(os.path.dirname(__file__), ui_path)
    print(f'HTTP serving directory: {web_dir} on {address}:{port}')

    # Change to this directory so the HTTPServer returns the index.html in it
    # by default when it gets a GET.
    os.chdir(web_dir)

    # Host:Port our HTTP server listens on
    server_address = (address, port)

    # Custom request handler class (so we can pass in our own args)
    MyRequestHandlerClass = RequestHandlerClassFactory(simulate, address,
                                                       ssids)

    # Start an HTTP server to serve the content in the ui dir and handle the
    # POST request in the handler class.
    print(
        f'Waiting for a connection to our hotspot {netman.get_hotspot_SSID()} ...'
    )
    httpd = MyHTTPServer(web_dir, server_address, MyRequestHandlerClass)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        dnsmasq.stop()
        netman.stop_hotspot()
        httpd.server_close()
Пример #2
0
def cleanup():
    print("Cleaning up prior to exit.")
    dnsmasq.stop()
    netman.stop_hotspot()
Пример #3
0
        def do_POST(self):
            content_length = int(self.headers['Content-Length'])
            body = self.rfile.read(content_length)
            self.send_response(200)
            self.end_headers()
            response = BytesIO()
            fields = parse_qs(body.decode('utf-8'))
            print(f'POST received: {fields}')

            # Parse the form post
            FORM_SSID = 'ssid'
            FORM_HIDDEN_SSID = 'hidden-ssid'
            FORM_USERNAME = '******'
            FORM_PASSWORD = '******'

            if FORM_SSID not in fields:
                print(f'Error: POST is missing {FORM_SSID} field.')
                return

            ssid = fields[FORM_SSID][0]
            password = None
            username = None
            if FORM_HIDDEN_SSID in fields:
                ssid = fields[FORM_HIDDEN_SSID][0]  # override with hidden name
            if FORM_USERNAME in fields:
                username = fields[FORM_USERNAME][0]
            if FORM_PASSWORD in fields:
                password = fields[FORM_PASSWORD][0]

            # Look up the ssid in the list we sent, to find out its security
            # type for the new connection we have to make
            conn_type = netman.CONN_TYPE_SEC_NONE  # Open, no auth AP

            if FORM_HIDDEN_SSID in fields:
                conn_type = netman.CONN_TYPE_SEC_PASSWORD  # Assumption...

            for s in self.ssids:
                if FORM_SSID in s and ssid == s[FORM_SSID]:
                    if s['security'] == "ENTERPRISE":
                        conn_type = netman.CONN_TYPE_SEC_ENTERPRISE
                    elif s['security'] == "NONE":
                        conn_type = netman.CONN_TYPE_SEC_NONE
                    else:
                        # all others need a password
                        conn_type = netman.CONN_TYPE_SEC_PASSWORD
                    break

            # Stop the hotspot
            netman.stop_hotspot()

            # Connect to the user's selected AP
            success = netman.connect_to_AP(conn_type=conn_type, ssid=ssid, \
                    username=username, password=password)

            if success:
                response.write(b'OK\n')
            else:
                response.write(b'ERROR\n')
            self.wfile.write(response.getvalue())

            # Handle success or failure of the new connection
            if success:
                print(f'Connected!  Exiting app.')
                sys.exit()
            else:
                print(f'Connection failed, restarting the hotspot.')
                if not self.simulate:
                    # Update the list of SSIDs since we are not connected
                    self.ssids = netman.get_list_of_access_points()

                    # Start the hotspot again
                    netman.start_hotspot()