def connected_timeout(dummy: int) -> None: active_ssid: Optional[str] active_ssid = nm.get_active_ssid(modemgr.get_state_device("CONNECTED")) log.debug("connected_timeout comparing {} to {}".format( connection, active_ssid)) if connection != active_ssid: log.warning("Connection lost on timeout") dev = modemgr.get_state_device("CONNECTED") set_state("CONNECTING", candidate_connections(dev)) if modemgr.get_mode() == modemgr.MULTI_MODE: wpa.check_wpa(modemgr.get_ap_device().Interface) active_ssid = nm.get_active_ssid(modemgr.get_state_device("HOTSPOT")) if not active_ssid: log.warning("Hotspot lost on timeout") set_state("HOTSPOT") defroute_devname: Optional[str] = routemgr.defroute_dev() ap_dev: NetworkManager.Device = modemgr.get_ap_device() link_dev: NetworkManager.Device = modemgr.get_link_device() if defroute_devname == nm.device_name(ap_dev): # default route is bad. Disconnect link and count on state # processing to restore log.error("AP is holding default route while CONNECTED, kicking") nm.disconnect(link_dev)
def state_callback(state: str, action: str) -> None: if (state, action) == ("HOTSPOT", "start"): log.debug("Running iptables commands for HOTSPOT") run_cmds(end_cmds) run_cmds(start_cmds) if modemgr.get_mode() == modemgr.MULTI_MODE: run_cmds(appliance_clear) log.debug("Done with iptables commands for HOTSPOT") elif (state, action) == ("CONNECTED", "start"): log.debug("Running iptables commands for CONNECTED") run_cmds(end_cmds) if modemgr.get_mode() == modemgr.MULTI_MODE: run_cmds(appliance_clear) run_cmds(appliance_cmds) defaultdev: Optional[str] = routemgr.defroute_dev() apdev: str = modemgr.get_ap_device().Interface if defaultdev and defaultdev != apdev: run_cmds([ "iptables -w -t nat -I COMITUP-FWD -o " "{} -j MASQUERADE".format(defaultdev) ], ) log.debug("Done with iptables commands for CONNECTED")
def connected_timeout(): log.debug("states: Calling nm.get_active_ssid()") if connection != nm.get_active_ssid(modemgr.get_state_device('CONNECTED')): log.warning("Connection lost on timeout") set_state('HOTSPOT') if modemgr.get_mode() == modemgr.MULTI_MODE: wpa.check_wpa(modemgr.get_ap_device().Interface)
def hotspot_timeout(dummy: int) -> None: if iwscan.ap_conn_count() == 0 or modemgr.get_mode() != "single": log.debug("Periodic connection attempt") dev = modemgr.get_state_device("CONNECTED") conn_list: List[str] = candidate_connections(dev) if conn_list: set_state("CONNECTING", conn_list) else: log.info("No candidates - skipping CONNECTING scan") else: log.info("AP active - skipping CONNECTING scan") wpa.check_wpa(modemgr.get_ap_device().Interface)
def hotspot_timeout(): if iwscan.ap_conn_count() == 0 or modemgr.get_mode() != 'single': log.debug('Periodic connection attempt') dev = modemgr.get_state_device('CONNECTED') conn_list = candidate_connections(dev) if conn_list: set_state('CONNECTING', conn_list) else: log.info('No candidates - skipping CONNECTING scan') else: log.info('AP active - skipping CONNECTING scan') wpa.check_wpa(modemgr.get_ap_device().Interface)
def run_dns(confpath): log.debug("Running dnsmasq using {}".format(confpath)) kill_dns(pidpath, signal.SIGTERM) dev = modemgr.get_ap_device().Interface cmd = "dnsmasq --conf-file={0} --interface={1}".format(confpath, dev) for _ in range(5): cp = subprocess.run(cmd.split()) if cp.returncode == 0: break time.sleep(.1)
def init_states(hosts, callbacks): global hotspot_name nmmon.init_nmmon() set_hosts(*hosts) for callback in callbacks: add_state_callback(callback) hotspot_name = dns_to_conn(hosts[0]) assure_hotspot(hotspot_name, modemgr.get_ap_device()) timeout_add(10 * 1000, state_monitor)
def init_states(hosts, callbacks, hotspot_pw): global hotspot_name nmmon.init_nmmon() set_hosts(*hosts) for callback in callbacks: add_state_callback(callback) hotspot_name = dns_to_conn(hosts[0]) assure_hotspot(hotspot_name, modemgr.get_ap_device()) # Set an early kick to set CONNECTING mode set_state('HOTSPOT') timeout_add(5 * 1000, hotspot_timeout, state_id)
def init_states( hosts: List[str], callbacks: List[Callable[[str, str], None]], hotspot_pw: str, ) -> None: global hotspot_name, conn_list, connection, startup nmmon.init_nmmon() set_hosts(*hosts) for callback in callbacks: add_state_callback(callback) hotspot_name = dns_to_conn(hosts[0]) assure_hotspot(hotspot_name, modemgr.get_ap_device(), hotspot_pw) startup = True set_state("HOTSPOT")
def main(): handler = logging.StreamHandler(stream=None) log.addHandler(handler) log.setLevel(logging.DEBUG) log.info('starting') init_nmmon() def up(): print("wifi up") def down(): print("wifi down") enable(modemgr.get_ap_device(), up, down) loop = MainLoop() loop.run()
def init_nmmon(): set_device_listeners( modemgr.get_ap_device(), modemgr.get_link_device() )
def run_cmds(cmds): linkdev = nm.device_name(modemgr.get_link_device()) apdev = nm.device_name(modemgr.get_ap_device()) for cmd in cmds: subprocess.call(cmd.format(link=linkdev, ap=apdev), shell=True)