diff --git a/NetworkMgr/configuration.py b/NetworkMgr/configuration.py index b535fae..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 ) @@ -474,8 +474,10 @@ 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) + 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) 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 23c22a0..d36aae8 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,35 +13,16 @@ 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() - -nic_ifconfig = Popen( - ['ifconfig', nic], +default_nic = Popen( + 'netstat -rn | grep default', stdout=PIPE, - close_fds=True, + shell=True, 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, @@ -46,32 +30,59 @@ 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_left_over = nics.stdout.read().replace(nic, '').strip() +nic_list = sorted(re.sub(not_nics_regex, '', nics_left_over).strip().split()) -nics_lelfover = nics.stdout.read().replace(nic, '').strip() -nic_list = sorted(re.sub(notnics_regex, '', nics_lelfover).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) -if not nic_list: - exit() +nic_ifconfig = Popen( + ['ifconfig', nic], + stdout=PIPE, + close_fds=True, + universal_newlines=True +).stdout.read() + +dhcp = Popen( + ['sysrc', '-n', f'ifconfig_{nic}'], + stdout=PIPE, + close_fds=True, + universal_newlines=True +).stdout.read() -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 +active_status = ( + 'status: active' in nic_ifconfig, + 'status: associated' in nic_ifconfig +) + +# 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}') + 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 23d67c3..d14bcee 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_DOWN"; + match "type" "LINK_UP"; + media-type "ethernet"; + action "/usr/local/share/networkmgr/link-up.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..b7189c4 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,17 @@ 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') + break 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') + with open(f'/tmp/network-{nic}', 'w') as network: + network.writelines(f'attached') + +Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)