Ejemplo n.º 1
0
def shutdown_server():
    print('Shutting down')
    hostapd.stop()
    dnsmasq.stop()
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
def stop(subssl, subhostapd, restart_nm=False):
    # Checking subssl
    if not isinstance(subssl, subprocess.Popen):
        raise Exception("[subssl] not a process. Aborting")

    # Checking subhostapd
    if not isinstance(subhostapd, subprocess.Popen):
        raise Exception("[subhostapd] not a process. Aborting")

    # Stopping processes (hostapd & sslsplit)
    subssl.kill()
    subhostapd.kill()

    # Disabling ip forwarding
    res = net.ip_forward(enable=False)
    style.print_call_info(res, 'ip_forward', 'Disabled IP forwarding')

    # Stopping dnsmasq
    res = dnsmasq.stop()
    style.print_call_info(res, 'dnsmasq', 'Stopped dnsmasq')

    # Restoring iptables rules
    res = iptables.restore(iptables.TMP_RULES)
    style.print_call_info(res, 'iptables', 'Restored iptables rules')

    # Removing tmp file
    unlink(iptables.TMP_RULES)

    # Restarting NetworkManager (if it was running initially)
    if restart_nm:
        res = subprocess.call(['systemctl', 'start', 'NetworkManager'])
        style.print_call_info(res, 'NetworkManager',
                              'Restarted NetworkManager')
Ejemplo n.º 4
0
def cleanup():
    print("Cleaning up prior to exit.")
    dnsmasq.stop()
    netman.stop_hotspot()