From 78483258e86ede220d1e46fef04fb6894ac721d8 Mon Sep 17 00:00:00 2001 From: ericbsd Date: Sat, 17 Feb 2024 20:00:11 -0400 Subject: [PATCH 1/3] Rework auto-switch.py, setup-nic.py, networkmgr.conf Updated the list of wifi in setup-nic.py --- src/auto-switch.py | 74 ++++++++++++++------------------------------- src/networkmgr.conf | 8 ++++- src/setup-nic.py | 45 ++++++++++++++++----------- 3 files changed, 57 insertions(+), 70 deletions(-) diff --git a/src/auto-switch.py b/src/auto-switch.py index 23c22a0..ad5bee4 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -1,4 +1,7 @@ #!/usr/local/bin/python3 +""" +auto-switch - is used to automatically switches the default interface go down. +""" import sys import os @@ -10,12 +13,20 @@ exit() nic = args[1] -cmd = ["kenv", "-q", "rc_system"] -rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read() -openrc = 'openrc' in rc_system +not_nics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|wlan" \ + r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)" -cmd = 'netstat -rn | grep default' -defautl_nic = Popen(cmd, stdout=PIPE, shell=True, universal_newlines=True).stdout.read() +default_nic = Popen( + 'netstat -rn | grep default', + shell=True, + universal_newlines=True +).stdout.read() + +# Stop the script if the nic is not valid or not in the default route. +if re.search(not_nics_regex, nic): + exit(0) +elif nic not in default_nic: + exit(0) nic_ifconfig = Popen( ['ifconfig', nic], @@ -24,54 +35,15 @@ universal_newlines=True ).stdout.read() -# Only stop dhclient if the status is not active or associated active_status = ( 'status: active' in nic_ifconfig, 'status: associated' in nic_ifconfig ) -if not any(active_status): - if openrc: - os.system(f'service dhcpcd.{nic} stop') - else: - if 'wlan' in nic: - os.system(f'service dhclient stop {nic}') - else: - os.system(f'service netif stop {nic}') - os.system('service routing restart') - -nics = Popen( - ['ifconfig', '-l', 'ether'], - stdout=PIPE, - close_fds=True, - universal_newlines=True -) - -notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ - r"ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)" - -nics_lelfover = nics.stdout.read().replace(nic, '').strip() -nic_list = sorted(re.sub(notnics_regex, '', nics_lelfover).strip().split()) -if not nic_list: - exit() - -for current_nic in nic_list: - output = Popen( - ['ifconfig', current_nic], - stdout=PIPE, - close_fds=True, - universal_newlines=True - ) - nic_ifconfig = output.stdout.read() - status_types = [ - 'active', - 'associated', - ] - found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig) - found_inet = re.search("inet(\s|6)", nic_ifconfig) - if found_status and found_inet: - if openrc: - os.system(f'service dhcpcd.{current_nic} restart') - else: - os.system(f'service dhclient restart {current_nic}') - break +# Stop the interface if it's not active or associated. +# This removes the interface from the default route. +# Restarting routing adds and nic if there is and other one that is active +# or associated. +if not any(active_status): + os.system(f'service netif stop {nic}') + os.system('service routing restart') diff --git a/src/networkmgr.conf b/src/networkmgr.conf index 23d67c3..4ab6061 100644 --- a/src/networkmgr.conf +++ b/src/networkmgr.conf @@ -3,14 +3,20 @@ notify 100 { match "system" "IFNET"; match "subsystem" "!(usbus|wlan)[0-9]+"; match "type" "ATTACH"; + action "/usr/local/share/networkmgr/setup-nic.py $subsystem"; +}; +notify 100 { + match "system" "IFNET"; + match "type" "LINK_UP"; + media-type "ethernet"; action "/usr/local/share/networkmgr/setup-nic.py $subsystem"; }; notify 100 { match "system" "IFNET"; + match "subsystem" "!(usbus|wlan)[0-9]+"; match "type" "LINK_DOWN"; - action "/usr/local/share/networkmgr/auto-switch.py $subsystem"; }; diff --git a/src/setup-nic.py b/src/setup-nic.py index 70d575a..512c9bb 100755 --- a/src/setup-nic.py +++ b/src/setup-nic.py @@ -5,6 +5,7 @@ import shutil import sys from pathlib import Path +from subprocess import Popen, PIPE def file_content(paths): @@ -21,25 +22,25 @@ def file_content(paths): nic = args[1] etc = Path(os.sep, "etc") -rcconf = etc / "rc.conf" -rcconflocal = etc / "rc.conf.local" +rc_conf = etc / "rc.conf" +rc_conf_local = etc / "rc.conf.local" wpa_supplicant = etc / "wpa_supplicant.conf" -rcconf_paths = [rcconf] +rc_conf_paths = [rc_conf] -if rcconflocal.exists(): - rcconf_paths.append(rcconflocal) +if rc_conf_local.exists(): + rc_conf_paths.append(rc_conf_local) -rcconf_content = file_content(rcconf_paths) +rc_conf_content = file_content(rc_conf_paths) -notnics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ +not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ "ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+" # wifi_driver_regex is taken from devd.conf wifi-driver-regex -wifi_driver_regex = "(ath|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|otus|" \ +wifi_driver_regex = "(ath|ath[0-9]+k|bwi|bwn|ipw|iwlwifi|iwi|iwm|iwn|malo|mwl|mt79|otus|" \ "ral|rsu|rtw|rtwn|rum|run|uath|upgt|ural|urtw|wpi|wtap|zyd)[0-9]+" -if re.search(notnics_regex, nic): +if re.search(not_nics_regex, nic): exit(0) if re.search(wifi_driver_regex, nic): @@ -48,14 +49,22 @@ def file_content(paths): shutil.chown(wpa_supplicant, user="root", group="wheel") wpa_supplicant.chmod(0o765) for wlanNum in range(0, 9): - if f'wlan{wlanNum}' not in rcconf_content: - break - if f'wlans_{nic}=' not in rcconf_content: - with rcconf.open('a') as rc: - rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') - rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') + if f'wlan{wlanNum}' not in rc_conf_content: + if f'wlans_{nic}=' not in rc_conf_content: + with rc_conf.open('a') as rc: + rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') + rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') + Popen(f'/etc/pccard_ether {nic} startchildren', shell=True) + else: - if f'ifconfig_{nic}=' not in rcconf_content: - with rcconf.open('a') as rc: + if f'ifconfig_{nic}=' not in rc_conf_content: + with rc_conf.open('a') as rc: rc.writelines(f'ifconfig_{nic}="DHCP"\n') -os.system(f'/etc/pccard_ether {nic} startchildren') + Popen('/etc/pccard_ether {nic} startchildren', shell=True) + else: + Popen( + f'service netif start {nic} ; ' + f'service dhclient start {nic} ; ' + f'service routing restart', + shell=True + ) From 325d94c085daef7d445d596f518cac75010531ef Mon Sep 17 00:00:00 2001 From: ericbsd Date: Sun, 18 Feb 2024 22:45:16 -0400 Subject: [PATCH 2/3] Improved the devd files further setup-nic.py and auto-switch.py. Added link-up.py to handle the linkup for ethernet with devd. --- NetworkMgr/configuration.py | 2 ++ setup.py | 1 + src/auto-switch.py | 41 +++++++++++++++++++++++++- src/link-up.py | 58 +++++++++++++++++++++++++++++++++++++ src/networkmgr.conf | 2 +- src/setup-nic.py | 15 ++++------ 6 files changed, 107 insertions(+), 12 deletions(-) create mode 100755 src/link-up.py diff --git a/NetworkMgr/configuration.py b/NetworkMgr/configuration.py index b535fae..38f6220 100755 --- a/NetworkMgr/configuration.py +++ b/NetworkMgr/configuration.py @@ -474,6 +474,8 @@ def update_system(self): defaultrouter_line = f'defaultrouter="{defaultrouter}"\n' self.remove_rc_conf_line(defaultrouter_line) restart_card_network(nic) + # sometimes the inet address isn't available immediately after dhcp is enabled. + start_static_network(nic, inet, netmask) wait_inet(nic) restart_rounting_and_dhcp(nic) diff --git a/setup.py b/setup.py index a362367..7be091b 100755 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ def datafilelist(installbase, sourcebase): networkmgr_share = [ 'src/auto-switch.py', + 'src/link-up.py', 'src/setup-nic.py' ] diff --git a/src/auto-switch.py b/src/auto-switch.py index ad5bee4..d36aae8 100755 --- a/src/auto-switch.py +++ b/src/auto-switch.py @@ -18,15 +18,28 @@ default_nic = Popen( 'netstat -rn | grep default', + stdout=PIPE, shell=True, universal_newlines=True ).stdout.read() +nics = Popen( + ['ifconfig', '-l', 'ether'], + stdout=PIPE, + close_fds=True, + universal_newlines=True +) + +nics_left_over = nics.stdout.read().replace(nic, '').strip() +nic_list = sorted(re.sub(not_nics_regex, '', nics_left_over).strip().split()) + # Stop the script if the nic is not valid or not in the default route. if re.search(not_nics_regex, nic): exit(0) elif nic not in default_nic: exit(0) +elif not nic_list: + exit(0) nic_ifconfig = Popen( ['ifconfig', nic], @@ -35,6 +48,13 @@ universal_newlines=True ).stdout.read() +dhcp = Popen( + ['sysrc', '-n', f'ifconfig_{nic}'], + stdout=PIPE, + close_fds=True, + universal_newlines=True +).stdout.read() + active_status = ( 'status: active' in nic_ifconfig, 'status: associated' in nic_ifconfig @@ -46,4 +66,23 @@ # or associated. if not any(active_status): os.system(f'service netif stop {nic}') - os.system('service routing restart') + if dhcp.strip() == 'DHCP': + for current_nic in nic_list: + output = Popen( + ['ifconfig', current_nic], + stdout=PIPE, + close_fds=True, + universal_newlines=True + ) + nic_ifconfig = output.stdout.read() + status_types = [ + 'active', + 'associated', + ] + found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig) + found_inet = re.search("inet(\s|6)", nic_ifconfig) + if found_status and found_inet: + os.system(f'service dhclient restart {current_nic}') + break + else: + os.system('service routing restart') diff --git a/src/link-up.py b/src/link-up.py new file mode 100755 index 0000000..e58d982 --- /dev/null +++ b/src/link-up.py @@ -0,0 +1,58 @@ +#!/usr/local/bin/python3 + +import os +import re +import sys +from subprocess import Popen, PIPE, run + +args = sys.argv +if len(args) != 2: + exit(1) +nic = args[1] + +not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \ + "ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+" + +# Stop the script if the nic is not valid. +if re.search(not_nics_regex, nic): + exit(0) + +dhcp = Popen( + ['sysrc', '-n', f'ifconfig_{nic}'], + stdout=PIPE, + close_fds=True, + universal_newlines=True +).stdout.read() + +if os.path.exists(f'/tmp/network-{nic}'): + network = open(f'/tmp/network-{nic}', 'r').read() + if 'attached' in network: + if dhcp.strip() == 'DHCP': + Popen(f'service dhclient quietstart {nic}', shell=True) + else: + Popen(f'service routing restart', shell=True) + with open(f'/tmp/network-{nic}', 'w') as network: + network.writelines(f'linked') + exit(0) + +nic_ifconfig = Popen( + ['ifconfig', nic], + stdout=PIPE, + close_fds=True, + universal_newlines=True +).stdout.read() + +if 'inet ' in nic_ifconfig: + Popen( + f'service routing restart ; ' + f'service dhclient restart {nic}', + shell=True + ) +else: + Popen( + f'service netif start {nic} ; ' + 'sleep 1 ; ' + f'service routing restart ; ' + f'service dhclient restart {nic}', + shell=True + ) diff --git a/src/networkmgr.conf b/src/networkmgr.conf index 4ab6061..d14bcee 100644 --- a/src/networkmgr.conf +++ b/src/networkmgr.conf @@ -10,7 +10,7 @@ notify 100 { match "system" "IFNET"; match "type" "LINK_UP"; media-type "ethernet"; - action "/usr/local/share/networkmgr/setup-nic.py $subsystem"; + action "/usr/local/share/networkmgr/link-up.py $subsystem"; }; notify 100 { diff --git a/src/setup-nic.py b/src/setup-nic.py index 512c9bb..b7189c4 100755 --- a/src/setup-nic.py +++ b/src/setup-nic.py @@ -54,17 +54,12 @@ def file_content(paths): with rc_conf.open('a') as rc: rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n') rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n') - Popen(f'/etc/pccard_ether {nic} startchildren', shell=True) - + break else: if f'ifconfig_{nic}=' not in rc_conf_content: with rc_conf.open('a') as rc: rc.writelines(f'ifconfig_{nic}="DHCP"\n') - Popen('/etc/pccard_ether {nic} startchildren', shell=True) - else: - Popen( - f'service netif start {nic} ; ' - f'service dhclient start {nic} ; ' - f'service routing restart', - shell=True - ) + with open(f'/tmp/network-{nic}', 'w') as network: + network.writelines(f'attached') + +Popen(f'/etc/pccard_ether {nic} startchildren', shell=True) From 0a3cd3f7d3bc352249786b5283ca4db114e4cd15 Mon Sep 17 00:00:00 2001 From: ericbsd Date: Mon, 19 Feb 2024 12:26:09 -0400 Subject: [PATCH 3/3] replace Enable Networking by Restart Networking. --- NetworkMgr/configuration.py | 4 ++-- NetworkMgr/net_api.py | 10 +++++++--- NetworkMgr/trayicon.py | 15 +++++---------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/NetworkMgr/configuration.py b/NetworkMgr/configuration.py index 38f6220..36d7bb1 100755 --- a/NetworkMgr/configuration.py +++ b/NetworkMgr/configuration.py @@ -9,7 +9,7 @@ defaultcard, nics_list, restart_card_network, - restart_rounting_and_dhcp, + restart_routing_and_dhcp, start_static_network, wait_inet ) @@ -477,7 +477,7 @@ def update_system(self): # sometimes the inet address isn't available immediately after dhcp is enabled. start_static_network(nic, inet, netmask) wait_inet(nic) - restart_rounting_and_dhcp(nic) + restart_routing_and_dhcp(nic) self.destroy() diff --git a/NetworkMgr/net_api.py b/NetworkMgr/net_api.py index df107f1..ccce511 100755 --- a/NetworkMgr/net_api.py +++ b/NetworkMgr/net_api.py @@ -49,7 +49,7 @@ def get_ssid(wificard): def nics_list(): - notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|" \ + not_nics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|" \ r"faith|ppp|bridge|wg)[0-9]+(\s*)|vm-[a-z]+(\s*)" nics = Popen( 'ifconfig -l', @@ -57,7 +57,7 @@ def nics_list(): stdout=PIPE, universal_newlines=True ).stdout.read().strip() - return sorted(re.sub(notnics_regex, '', nics).strip().split()) + return sorted(re.sub(not_nics_regex, '', nics).strip().split()) def ifcardconnected(netcard): @@ -184,6 +184,10 @@ def switch_default(nic): return +def restart_all_nics(widget): + run('service netif restart', shell=True) + + def stopallnetwork(): run('service netif stop', shell=True) @@ -201,7 +205,7 @@ def restart_card_network(netcard): run(f'service netif restart {netcard}', shell=True) -def restart_rounting_and_dhcp(netcard): +def restart_routing_and_dhcp(netcard): run('service routing restart', shell=True) sleep(1) run(f'service dhclient restart {netcard}', shell=True) diff --git a/NetworkMgr/trayicon.py b/NetworkMgr/trayicon.py index 9bb636a..7676bd2 100755 --- a/NetworkMgr/trayicon.py +++ b/NetworkMgr/trayicon.py @@ -12,6 +12,7 @@ stopnetworkcard, startnetworkcard, wifiDisconnection, + restart_all_nics, stopallnetwork, startallnetwork, connectToSsid, @@ -137,16 +138,10 @@ def nm_menu(self): self.menu.append(configure_item) self.menu.append(Gtk.SeparatorMenuItem()) wifinum += 1 - if self.cardinfo['service'] is False: - open_item = Gtk.MenuItem(_("Enable Networking")) - open_item.connect("activate", self.openNetwork) - self.menu.append(open_item) - else: - close_item = Gtk.MenuItem(_("Disable Networking")) - close_item.connect("activate", self.closeNetwork) - self.menu.append(close_item) - # else: - # print('service netif status not supported') + + open_item = Gtk.MenuItem(_("Restart Networking")) + open_item.connect("activate", restart_all_nics) + self.menu.append(open_item) close_manager = Gtk.MenuItem(_("Close Network Manager")) close_manager.connect("activate", self.stop_manager) self.menu.append(close_manager)