pub fn new(
        nm: &'a NetworkBackend,
        config: &crate::config::Config,
        wifi_sta_active_connection: dbus::Path<'static>,
        wifi_access_points: Vec<WifiConnection>,
        timeout: Duration,
    ) -> Result<(Portal<'a>, tokio::sync::oneshot::Sender<()>), CaptivePortalError> {
        let (http_server, http_exit) = http_server::HttpServer::new(
            SocketAddrV4::new(config.gateway.clone(), config.listening_port),
            nm.clone(),
            config.get_ui_directory(),
        );

        let mut state = http_server.state.lock().expect("Lock http_state mutex for portal");
        state.connections.0.extend(wifi_access_points);
        drop(state);

        let http_state = http_server.state.clone();

        let (mut dns_server, dns_exit) =
            dns_server::CaptiveDnsServer::new(SocketAddrV4::new(config.gateway.clone(), config.dns_port));
        let (mut dhcp_server, dhcp_exit) =
            dhcp_server::DHCPServer::new(SocketAddrV4::new(config.gateway.clone(), config.dhcp_port));

        tokio::spawn(async move {
            if let Err(e) = dns_server.run().await {
                error!("{}", e);
            }
        });
        tokio::spawn(async move {
            if let Err(e) = dhcp_server.run().await {
                error!("{}", e);
            }
        });

        let nm_clone = nm.clone();
        tokio::spawn(async move {
            let stream = ap_changed_stream(&nm_clone).await;
            let mut stream = match stream {
                Err(e) => {
                    error!("{}", e);
                    return;
                },
                Ok(stream) => stream,
            };
            for event in stream.next().await {
                let access_point = nm_clone.access_point(event.path).await;
                if let Ok(access_point) = access_point {
                    if access_point.is_own {
                        continue;
                    }
                    let event = WifiConnectionEvent {
                        event: event.event,
                        access_point,
                    };
                    http_server::update_network(http_state.clone(), event).await;
                }
            }
        });

        let (exit_handler, exit_receiver) = tokio::sync::oneshot::channel::<()>();

        let portal = Portal {
            http_server: Box::pin(http_server.run()),
            dns_exit,
            dhcp_exit,
            exit_receiver: Some(exit_receiver),
            http_exit: Some(http_exit),
            timeout: Some(nm.wait_for_connectivity(config.internet_connectivity, timeout).boxed()),
            hotspot_stopped_fut: Some(nm.on_hotspot_stopped(wifi_sta_active_connection).boxed()),
        };

        Ok((portal, exit_handler))
    }
/// The configuration should contain a ui_directory, if the UI is not embedded. If that is not set,
/// the environment variable CARGO_MANIFEST_DIR will be used, which is only useful during development.
The configuration should contain a ui_directory, if the UI is not embedded. If that is not set,
the environment variable CARGO_MANIFEST_DIR will be used, which is only useful during development.
The configuration should contain a ui_directory if the UI is not embedded . If that is not set the environment variable CARGO_MANIFEST_DIR will be used which is only useful during development .
pub fn new(
        nm: &'a NetworkBackend,
        config: &crate::config::Config,
        wifi_sta_active_connection: dbus::Path<'static>,
        wifi_access_points: Vec<WifiConnection>,
        timeout: Duration,
    ) -> Result<(Portal<'a>, tokio::sync::oneshot::Sender<()>), CaptivePortalError> {
        let (http_server, http_exit) = http_server::HttpServer::new(
            SocketAddrV4::new(config.gateway.clone(), config.listening_port),
            nm.clone(),
            config.get_ui_directory(),
        );
        let mut state = http_server.state.lock().expect("Lock http_state mutex for portal");
        state.connections.0.extend(wifi_access_points);
        drop(state);
        let http_state = http_server.state.clone();
        let (mut dns_server, dns_exit) =
            dns_server::CaptiveDnsServer::new(SocketAddrV4::new(config.gateway.clone(), config.dns_port));
        let (mut dhcp_server, dhcp_exit) =
            dhcp_server::DHCPServer::new(SocketAddrV4::new(config.gateway.clone(), config.dhcp_port));
        tokio::spawn(async move {
            if let Err(e) = dns_server.run().await {
                error!("{}", e);
            }
        });
        tokio::spawn(async move {
            if let Err(e) = dhcp_server.run().await {
                error!("{}", e);
            }
        });
        let nm_clone = nm.clone();
        tokio::spawn(async move {
            let stream = ap_changed_stream(&nm_clone).await;
            let mut stream = match stream {
                Err(e) => {
                    error!("{}", e);
                    return;
                },
                Ok(stream) => stream,
            };
            for event in stream.next().await {
                let access_point = nm_clone.access_point(event.path).await;
                if let Ok(access_point) = access_point {
                    if access_point.is_own {
                        continue;
                    }
                    let event = WifiConnectionEvent {
                        event: event.event,
                        access_point,
                    };
                    http_server::update_network(http_state.clone(), event).await;
                }
            }
        });
        let (exit_handler, exit_receiver) = tokio::sync::oneshot::channel::<()>();
        let portal = Portal {
            http_server: Box::pin(http_server.run()),
            dns_exit,
            dhcp_exit,
            exit_receiver: Some(exit_receiver),
            http_exit: Some(http_exit),
            timeout: Some(nm.wait_for_connectivity(config.internet_connectivity, timeout).boxed()),
            hotspot_stopped_fut: Some(nm.on_hotspot_stopped(wifi_sta_active_connection).boxed()),
        };
        Ok((portal, exit_handler))
    }
pub fn new ( nm : & ' a NetworkBackend , config : & crate :: config :: Config , wifi_sta_active_connection : dbus :: Path < ' static > , wifi_access_points : Vec < WifiConnection > , timeout : Duration , ) -> Result < ( Portal < ' a > , tokio :: sync :: oneshot :: Sender < ( ) > ) , CaptivePortalError > { let ( http_server , http_exit ) = http_server :: HttpServer :: new ( SocketAddrV4 :: new ( config . gateway . clone ( ) , config . listening_port ) , nm . clone ( ) , config . get_ui_directory ( ) , ) ; let mut state = http_server . state . lock ( ) . expect ( " " ) ; state . connections . 0 . extend ( wifi_access_points ) ; drop ( state ) ; let http_state = http_server . state . clone ( ) ; let ( mut dns_server , dns_exit ) = dns_server :: CaptiveDnsServer :: new ( SocketAddrV4 :: new ( config . gateway . clone ( ) , config . dns_port ) ) ; let ( mut dhcp_server , dhcp_exit ) = dhcp_server :: DHCPServer :: new ( SocketAddrV4 :: new ( config . gateway . clone ( ) , config . dhcp_port ) ) ; tokio :: spawn ( async move { if let Err ( e ) = dns_server . run ( ) . await { error ! ( " " e ) ; } } ) ; tokio :: spawn ( async move { if let Err ( e ) = dhcp_server . run ( ) . await { error ! ( " " e ) ; } } ) ; let nm_clone = nm . clone ( ) ; tokio :: spawn ( async move { let stream = ap_changed_stream ( & nm_clone ) . await ; let mut stream = match stream { Err ( e ) => { error ! ( " " e ) ; return ; } , Ok ( stream ) => stream , } ; for event in stream . next ( ) . await { let access_point = nm_clone . access_point ( event . path ) . await ; if let Ok ( access_point ) = access_point { if access_point . is_own { continue ; } let event = WifiConnectionEvent { event : event . event , access_point , } ; http_server :: update_network ( http_state . clone ( ) , event ) . await ; } } } ) ; let ( exit_handler , exit_receiver ) = tokio :: sync :: oneshot :: channel :: < ( ) > ( ) ; let portal = Portal { http_server : Box :: pin ( http_server . run ( ) ) , dns_exit , dhcp_exit , exit_receiver : Some ( exit_receiver ) , http_exit : Some ( http_exit ) , timeout : Some ( nm . wait_for_connectivity ( config . internet_connectivity , timeout ) . boxed ( ) ) , hotspot_stopped_fut : Some ( nm . on_hotspot_stopped ( wifi_sta_active_connection ) . boxed ( ) ) , } ; Ok ( ( portal , exit_handler ) ) }
The configuration should contain a ui_directory, if the UI is not embedded.
The configuration should contain a ui_directory if the UI is not embedded .
nm
&'a NetworkBackend
config
&crate::config::Config
wifi_sta_active_connection
dbus::Path<'static>
wifi_access_points
Vec<WifiConnection>
timeout
Duration
nm
&'a NetworkBackend
config
&crate::config::Config
wifi_sta_active_connection
dbus::Path<'static>
wifi_access_points
Vec<WifiConnection>
timeout
Duration