Skip to content

Commit

Permalink
Fix dns servers sticking around after switching to DHCP
Browse files Browse the repository at this point in the history
  • Loading branch information
annervisser committed May 2, 2022
1 parent 5c616a9 commit 3b6e7d3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
9 changes: 2 additions & 7 deletions gnome_dns_switcher/gnome_helpers/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ def get_dns(self) -> List[str]:
output = run_command('nmcli --get-values ip4.dns connection show {}'.format(self.uuid))
return output.split(' | ')

def set_dns(self, dns: List[str], reload=True):
def set_dns(self, dns: List[str]):
run_command('nmcli connection modify {} ipv4.dns "{}"'.format(self.uuid, ','.join(dns)))
self.set_dns_auto_mode(False)
if reload:
self.reload_connection()

def get_dns_auto_mode(self):
output = run_command('nmcli --get-values ipv4.ignore-auto-dns connection show {}'.format(self.uuid))
Expand All @@ -28,11 +25,9 @@ def get_dns_auto_mode(self):
return True
raise Exception('Invalid return value from dns auto mode')

def set_dns_auto_mode(self, auto: bool, reload=True):
def set_dns_auto_mode(self, auto: bool):
run_command('nmcli connection modify {} ipv4.ignore-auto-dns {}'
.format(self.uuid, 'no' if auto else 'yes')) # invert cause its ignore
if reload:
self.reload_connection()

def reload_connection(self):
run_command('nmcli connection up {}'.format(self.uuid))
Expand Down
4 changes: 4 additions & 0 deletions gnome_dns_switcher/switcher/dnsswitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ def switch_dns(self, conn: Connection, server: Server):

def set_call():
if server.dhcp:
conn.set_dns([])
conn.set_dns_auto_mode(True)
conn.reload_connection()
else:
conn.set_dns(server.ips)
conn.set_dns_auto_mode(False)
conn.reload_connection()

async_call(
set_call,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = gnome-dns-switcher
version = 1.2.0
version = 1.2.2
description = DNS Switcher for Ubuntu Gnome
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 3b6e7d3

Please sign in to comment.