Skip to content

Commit

Permalink
Merge pull request #104 from ghostbsd/ghostbsd/issues#94
Browse files Browse the repository at this point in the history
Reworked NetworkMgr devd files and replaced Enable Networking by Restart Networking.
  • Loading branch information
ericbsd authored Feb 19, 2024
2 parents f0dddd1 + 0a3cd3f commit af782d4
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 85 deletions.
6 changes: 4 additions & 2 deletions NetworkMgr/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
defaultcard,
nics_list,
restart_card_network,
restart_rounting_and_dhcp,
restart_routing_and_dhcp,
start_static_network,
wait_inet
)
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 7 additions & 3 deletions NetworkMgr/net_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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',
shell=True,
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):
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
15 changes: 5 additions & 10 deletions NetworkMgr/trayicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
stopnetworkcard,
startnetworkcard,
wifiDisconnection,
restart_all_nics,
stopallnetwork,
startallnetwork,
connectToSsid,
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def datafilelist(installbase, sourcebase):

networkmgr_share = [
'src/auto-switch.py',
'src/link-up.py',
'src/setup-nic.py'
]

Expand Down
111 changes: 61 additions & 50 deletions src/auto-switch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/local/bin/python3
"""
auto-switch - is used to automatically switches the default interface go down.
"""

import sys
import os
Expand All @@ -10,68 +13,76 @@
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,
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_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')
58 changes: 58 additions & 0 deletions src/link-up.py
Original file line number Diff line number Diff line change
@@ -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
)
10 changes: 8 additions & 2 deletions src/networkmgr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};

Expand Down
40 changes: 22 additions & 18 deletions src/setup-nic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import sys
from pathlib import Path
from subprocess import Popen, PIPE


def file_content(paths):
Expand All @@ -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):
Expand All @@ -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)

0 comments on commit af782d4

Please sign in to comment.