Skip to content

Commit

Permalink
Merge pull request #65 from ghostbsd/issue-64
Browse files Browse the repository at this point in the history
Improvement for switching nic when dettaching or attaching nics
  • Loading branch information
ericbsd committed Jun 21, 2021
2 parents 6e2da68 + 1afaea2 commit b3abf5d
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 104 deletions.
10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ def datafilelist(installbase, sourcebase):


share_networkmgr = [
'src/auto-switch.py',
'src/net_api.py',
'src/setup-nic.py',
'src/trayicon.py'
]

data_files = [
(f'{prefix}/etc/devd', ['src/setupnic.conf']),
(f'{prefix}/etc/xdg/autostart', ['src/networkmgr.desktop']),
(f'{prefix}/share/networkmgr', share_networkmgr),
(f'{prefix}/etc/sudoers.d', ['src/sudoers.d/networkmgr'])
]

if os.path.exists('/etc/devd'):
data_files.append((f'{prefix}/etc/devd', ['src/setupnic.conf']))
data_files.append((f'{prefix}/etc/devd', ['src/networkmgr.conf']))
if os.path.exists('/etc/devd-openrc'):
data_files.append((f'{prefix}/etc/devd-openrc', ['src/setupnic.conf']))
data_files.append((f'{prefix}/etc/devd-openrc', ['src/networkmgr.conf']))

data_files.extend(datafilelist(f'{prefix}/share/icons/hicolor', 'src/icons'))

Expand All @@ -51,7 +53,7 @@ def datafilelist(installbase, sourcebase):
package_dir={'': '.'},
data_files=data_files,
install_requires=['setuptools'],
scripts=['networkmgr', 'src/netcardmgr', 'src/setup-nic']
scripts=['networkmgr', 'src/netcardmgr']
)

run('gtk-update-icon-cache -f /usr/local/share/icons/hicolor', shell=True)
55 changes: 55 additions & 0 deletions src/auto-switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

import sys
import os
import re
from subprocess import Popen, PIPE

args = sys.argv
if len(args) != 2:
exit()
nic = args[1]

cmd = ["kenv", "rc_system"]
rc_system = Popen(cmd, stdout=PIPE, universal_newlines=True).stdout.read()
openrc = True if 'openrc' in rc_system else False

if openrc:
os.system(f'service dhcpcd.{nic} stop')

nics = Popen(
['ifconfig', '-l'],
stdout=PIPE,
close_fds=True,
universal_newlines=True
)
nics = nics.stdout.read().strip()

notnics_regex = r"(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
r"ppp|bridge|ixautomation|vm-ixautomation|wg)[0-9]+(\s*)"

recompile = re.compile(notnics_regex)
nics = recompile.sub('', nics).replace(nic, '').strip().

if not nics:
exit()

nic_list = nics.split()

for card in nic_list:
print(card)
ncard = 'ifconfig -l'
output = Popen(
['ifconfig', card],
stdout=PIPE,
close_fds=True,
universal_newlines=True
)
nic_ifconfig = output.stdout.read()
if 'status: active' in nic_ifconfig or 'status: associated' in nic_ifconfig:
if 'inet ' in nic_ifconfig or 'inet6' in nic_ifconfig:
if openrc:
os.system(f'service dhcpcd.{card} restart')
else:
os.system(f'service dhclient restart {card}')
break
20 changes: 20 additions & 0 deletions src/networkmgr.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,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";

action "/usr/local/share/networkmgr/auto-switch.py $subsystem";
};

attach 100 {
device-name "$wifi-driver-regex";
action "/usr/local/share/networkmgr/setup-nic.py $device-name";
};
84 changes: 0 additions & 84 deletions src/setup-nic

This file was deleted.

45 changes: 45 additions & 0 deletions src/setup-nic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3

import os
import re
import sys

args = sys.argv
if len(args) != 2:
exit(1)
nic = args[1]

rcconf = open('/etc/rc.conf', 'r').read()
if os.path.exists('/etc/rc.conf.local'):
rcconflocal = open('/etc/rc.conf.local', 'r').read()
else:
rcconflocal = "None"

notnics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
"ppp|bridge|ixautomation|vm-ixautomation|wg|wlan)[0-9]+"

# wifi_driver_regex is taken from devd.conf wifi-driver-regex
wifi_driver_regex = "(ath|bwi|bwn|ipw|iwi|iwm|iwn|malo|mwl|otus|ral|rsu|" \
"rtwn|rum|run|uath|upgt|ural|urtw|wi|wpi|wtap|zyd)[0-9]+"

if re.search(notnics_regex, nic):
exit(0)
if re.search(wifi_driver_regex, nic):
if not os.path.exists('/etc/wpa_supplicant.conf'):
open('/etc/wpa_supplicant.conf', 'a').close()
os.system('chown root:wheel /etc/wpa_supplicant.conf')
os.system('chmod 765 /etc/wpa_supplicant.conf')
for wlanNum in range(0, 9):
if f'wlan{wlanNum}' not in (rcconf + rcconflocal):
break
if f'wlans_{nic}=' not in (rcconf + rcconflocal):
rc = open('/etc/rc.conf', 'a')
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n')
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n')
rc.close()
else:
if f'ifconfig_{nic}=' not in (rcconf + rcconflocal):
rc = open('/etc/rc.conf', 'a')
rc.writelines(f'ifconfig_{nic}="DHCP"\n')
rc.close()
os.system(f'/etc/pccard_ether {nic} startchildren')
16 changes: 0 additions & 16 deletions src/setupnic.conf

This file was deleted.

0 comments on commit b3abf5d

Please sign in to comment.