Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding initial work for DNS and static IP configuration #97

Merged
merged 33 commits into from Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
89c1bb6
Added configur option to active interfaces. Associated the configure …
frostygoth Jan 13, 2020
1e9648b
Moved Todo to projects.
frostygoth Jan 13, 2020
ddcc152
Modified calls from trayicon.py to netcardmgr. Updated netcardmgr to …
frostygoth Jan 17, 2020
7e5e1ba
Just experimenting with some GTK formatting.
frostygoth Jan 19, 2020
a824174
Fixed my interface selection issue.
frostygoth Jan 19, 2020
8f73ac4
updated .gitignore to exclude .vscode dir
frostygoth Jan 19, 2020
cc25a1b
Completed the Combo Box and functionality to list valid configurable …
frostygoth Jan 20, 2020
631929d
Added entry fields for IP addresses, but I think they need to be text…
frostygoth Jan 21, 2020
a43b028
Added remaining fields to the IPv4 configuration tab. Need to add a s…
frostygoth Jan 23, 2020
d36a15f
Finally happy with the rough UI layout. Now I just need to associate …
frostygoth Jan 24, 2020
0b33574
Almost completely one with the get-interface-settings function. Havin…
frostygoth Jan 27, 2020
8715b03
Got the screen to print the initial data to the window with the curre…
frostygoth Feb 5, 2020
e111151
typo change.
frostygoth Feb 5, 2020
10e6267
Merge branch 'netcard01' into master
ericbsd May 4, 2020
c52ea90
Merge pull request #31 from frostygoth/master
ericbsd May 4, 2020
51ec977
Merge branch 'master' into netcard01
ericbsd Jun 16, 2021
d59c36d
renamed net card and sent it has a library
ericbsd Jun 16, 2021
80614c0
Merge branch 'master' into netcard01
ericbsd Sep 6, 2021
2bb698d
Move netcardmgr to share_networkmgr
ericbsd Feb 21, 2022
ea3d334
Remove OpenRC support from networkmgr
ericbsd Feb 21, 2022
f9479c3
Added better IPV4 UI integration
ericbsd Feb 21, 2022
b3ed03b
remove unused code in netcardmgr.py
ericbsd Feb 21, 2022
47d9028
Added ipv6 lock entry on SLAAC
ericbsd Feb 23, 2022
836734a
Merge branch 'master' into netcard01
ericbsd Jan 14, 2023
1f21602
adding some UI and backend improvement
ericbsd Mar 11, 2023
1dbbc56
Merge branch 'master' into netcard01
ericbsd Jun 3, 2023
3244db4
Adding functionality to configuration static IP and DNS
ericbsd Jun 24, 2023
02745b5
Fixed issues with wlan and rename netcardmgr to network_configuration
ericbsd Jul 23, 2023
a34aa30
Adding save button trigger on entry changes
ericbsd Jul 24, 2023
4a57c5f
Merge branch 'master' into netcard01
ericbsd Jul 30, 2023
5799865
Merge branch 'master' into netcard01
ericbsd Jul 30, 2023
45eee64
Merge remote-tracking branch 'origin/master' into netcard01
ericbsd Aug 16, 2023
7b9d5af
Fixed NetworkMgr module
ericbsd Aug 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions .gitignore
Expand Up @@ -36,15 +36,17 @@ venv*/*
# Pycharm stuff
.idea/*

# VSCode
.vscode/*

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Editor Configuration Files
.vscode

# python core dump
python*.core

2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2014, GhostBSD
Copyright (c) 2012-2023, GhostBSD
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
Empty file added NetworkMgr/__init__.py
Empty file.
515 changes: 515 additions & 0 deletions NetworkMgr/configuration.py

Large diffs are not rendered by default.

147 changes: 56 additions & 91 deletions src/net_api.py → NetworkMgr/net_api.py
@@ -1,52 +1,8 @@
#!/usr/bin/env python3
"""
Copyright (c) 2014-2019, GhostBSD. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistribution's of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistribution's in binary form must reproduce the above
copyright notice,this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""

from subprocess import Popen, PIPE, run
from sys import path
import os
#!/usr/bin/env python

from subprocess import Popen, PIPE, run, check_output
import re
from time import sleep
path.append("/usr/local/share/networkmgr")


cmd = "kenv | grep rc_system"
rc_system = Popen(cmd, shell=True, stdout=PIPE, universal_newlines=True)

if 'openrc' in rc_system.stdout.read():
openrc = True
rc = 'rc-'
network = 'network'
else:
openrc = False
rc = ''
network = 'netif'


def card_online(netcard):
Expand Down Expand Up @@ -96,7 +52,7 @@ def nics_list():
notnics_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 ether',
'ifconfig -l',
shell=True,
stdout=PIPE,
universal_newlines=True
Expand All @@ -117,16 +73,7 @@ def barpercent(sn):


def network_service_state():
if openrc:
status = Popen(
f'{rc}service {network} status',
shell=True,
stdout=PIPE,
universal_newlines=True
)
return 'status: started' in status.stdout.read()
else:
return False
return False


def networkdictionary():
Expand Down Expand Up @@ -232,61 +179,67 @@ def switch_default(nic):
).stdout.read()
if 'status: active' in nic_info or 'status: associated' in nic_info:
if 'inet ' in nic_info or 'inet6' in nic_info:
if openrc:
os.system(f'service dhcpcd.{card} restart')
else:
os.system(f'service dhclient restart {card}')
run(f'service dhclient restart {card}', shell=True)
break
return


def stopallnetwork():
os.system(f'{rc}service {network} stop')
run('service netif stop', shell=True)


def startallnetwork():
os.system(f'{rc}service {network} start')
run('service netif start', shell=True)


def stopnetworkcard(netcard):
if openrc:
os.system(f'ifconfig {netcard} down')
else:
os.system(f'service netif stop {netcard}')
switch_default(netcard)
run(f'service netif stop {netcard}', shell=True)
switch_default(netcard)


def restart_card_network(netcard):
run(f'service netif restart {netcard}', shell=True)


def restart_rounting_and_dhcp(netcard):
run('service routing restart', shell=True)
sleep(1)
run(f'service dhclient restart {netcard}', shell=True)


def start_static_network(netcard, inet, netmask):
run(f'ifconfig {netcard} inet {inet} netmask {netmask}', shell=True)
sleep(1)
run('service routing restart', shell=True)


def startnetworkcard(netcard):
if openrc:
os.system(f'ifconfig {netcard} up')
os.system(f'{rc}service dhcpcd.{netcard} restart')
else:
os.system(f'service netif start {netcard}')
sleep(1)
os.system('service routing restart')
os.system(f'service dhclient start {netcard}')
run(f'service netif start {netcard}', shell=True)
sleep(1)
run('service routing restart', shell=True)
run(f'service dhclient start {netcard}', shell=True)


def wifiDisconnection(wificard):
os.system(f'ifconfig {wificard} down')
os.system(f"ifconfig {wificard} ssid 'none'")
os.system(f'ifconfig {wificard} up')
run(f'ifconfig {wificard} down', shell=True)
run(f"ifconfig {wificard} ssid 'none'", shell=True)
run(f'ifconfig {wificard} up', shell=True)


def disableWifi(wificard):
os.system(f'ifconfig {wificard} down')
run(f'ifconfig {wificard} down', shell=True)


def enableWifi(wificard):
os.system(f'ifconfig {wificard} up')
os.system(f'ifconfig {wificard} up scan')
run(f'ifconfig {wificard} up', shell=True)
run(f'ifconfig {wificard} up scan', shell=True)


def connectToSsid(name, wificard):
os.system('killall wpa_supplicant')
run('killall wpa_supplicant', shell=True)
# service
sleep(0.5)
os.system(f"ifconfig {wificard} ssid '{name}'")
run(f"ifconfig {wificard} ssid '{name}'", shell=True)
sleep(0.5)
wpa_supplicant = run(
f'wpa_supplicant -B -i {wificard} -c /etc/wpa_supplicant.conf',
Expand Down Expand Up @@ -320,7 +273,7 @@ def delete_ssid_wpa_supplicant_config(ssid):
wpa_supplicant_conf.close()


def wlan_status(card):
def nic_status(card):
out = Popen(
f'ifconfig {card} | grep status:',
shell=True, stdout=PIPE,
Expand All @@ -330,8 +283,20 @@ def wlan_status(card):


def start_dhcp(wificard):
if openrc:
os.system(f'dhcpcd -x {wificard}')
os.system(f'dhcpcd {wificard}')
else:
os.system(f'dhclient {wificard}')
run(f'dhclient {wificard}', shell=True)


def wait_inet(card):
IPREGEX = r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
status = 'associated' if 'wlan' in card else 'active'
while nic_status(card) != status:
sleep(0.1)
print(nic_status(card))
while True:
ifcmd = f"ifconfig -f inet:dotted {card}"
ifoutput = check_output(ifcmd.split(" "), universal_newlines=True)
print(ifoutput)
re_ip = re.search(fr'inet {IPREGEX}', ifoutput)
if re_ip and '0.0.0.0' not in re_ip.group():
print(re_ip)
break
88 changes: 88 additions & 0 deletions NetworkMgr/query.py
@@ -0,0 +1,88 @@
#!/usr/bin/env python

from subprocess import check_output
import re
import os


def get_interface_settings(active_nic):
interface_settings = {}
rc_conf = open("/etc/rc.conf", "r").read()
DHCPSearch = re.findall(fr'^ifconfig_{active_nic}=".*DHCP', rc_conf, re.MULTILINE)
print(f"DHCPSearch is {DHCPSearch} and the length is {len(DHCPSearch)}")
if len(DHCPSearch) < 1:
DHCPStatusOutput = "Manual"
else:
DHCPStatusOutput = "DHCP"

IPREGEX = r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'

ifcmd = f"ifconfig -f inet:dotted {active_nic}"
ifoutput = check_output(ifcmd.split(" "), universal_newlines=True)
re_ip = re.search(fr'inet {IPREGEX}', ifoutput)
if re_ip:
if_ip = re_ip.group().replace("inet ", "").strip()
re_netmask = re.search(fr'netmask {IPREGEX}', ifoutput)
if_netmask = re_netmask.group().replace("netmask ", "").strip()
re_broadcast = re.search(fr'broadcast {IPREGEX}', ifoutput)
if_broadcast = re_broadcast.group().replace("broadcast ", "").strip()
else:
if_ip = ""
if_netmask = ""
if_broadcast = ""
if (DHCPStatusOutput == "DHCP"):
dhclient_leases = f"/var/db/dhclient.leases.{active_nic}"

if os.path.exists(dhclient_leases) is False:
print("DHCP is enabled, but we're unable to read the lease "
f"file a /var/db/dhclient.leases.{active_nic}")
gateway = ""
else:
dh_lease = open(dhclient_leases, "r").read()
re_gateway = re.search(fr"option routers {IPREGEX}", dh_lease)
gateway = re_gateway.group().replace("option routers ", "")
else:
rc_conf = open('/etc/rc.conf', 'r').read()
re_gateway = re.search(fr'^defaultrouter="{IPREGEX}"', rc_conf, re.MULTILINE)
if re_gateway:
gateway = re_gateway.group().replace('"', "")
gateway = gateway.replace('defaultrouter=', "")
else:
gateway = ""

if os.path.exists('/etc/resolv.conf'):
resolv_conf = open('/etc/resolv.conf').read()
nameservers = re.findall(fr'^nameserver {IPREGEX}', str(resolv_conf), re.MULTILINE)
print(nameservers)

re_domain_search = re.findall('search [a-zA-Z.]*', str(resolv_conf))
if len(re_domain_search) < 1:
re_domain_search = re.findall('domain (.*)', resolv_conf)
domain_search = str(re_domain_search).replace("domain ", "")
domain_search = domain_search.replace("'", "")
domain_search = domain_search.replace("[", "")
domain_search = domain_search.replace("]", "")
domain_search = domain_search.replace('search', '').strip()
else:
domain_search = ''
nameservers = []

interface_settings["Active Interface"] = active_nic
interface_settings["Assignment Method"] = DHCPStatusOutput
interface_settings["Interface IP"] = if_ip
interface_settings["Interface Subnet Mask"] = if_netmask
interface_settings["Broadcast Address"] = if_broadcast
interface_settings["Default Gateway"] = gateway
interface_settings["Search Domain"] = domain_search

for num in range(len(nameservers)):
interface_settings[
f"DNS Server {num + 1}"
] = str(nameservers[(num)]).replace("nameserver", "").strip()
# if DNS Server 1 and 2 are missing create them with empty string
if "DNS Server 1" not in interface_settings:
interface_settings["DNS Server 1"] = ""
if "DNS Server 2" not in interface_settings:
interface_settings["DNS Server 2"] = ""

return interface_settings