@@ -48,29 +48,39 @@ class Fatal(Exception):
48
48
pass
49
49
50
50
51
- def resolvconf_nameservers ():
52
- """Retrieves a list of tuples (address type, address as a string) that
53
- the current system uses to resolve hostnames from /etc/resolv.conf
54
- and possibly other files.
51
+ def resolvconf_nameservers (systemd_resolved ):
52
+ """Retrieves a list of tuples (address type, address as a string) of
53
+ the DNS servers used by the system to resolve hostnames.
54
+
55
+ If parameter is False, DNS servers are retrieved from only
56
+ /etc/resolv.conf. This behavior makes sense for the sshuttle
57
+ server.
58
+
59
+ If parameter is True, we retrieve information from both
60
+ /etc/resolv.conf and /run/systemd/resolve/resolv.conf (if it
61
+ exists). This behavior makes sense for the sshuttle client.
62
+
55
63
"""
56
64
57
65
# Historically, we just needed to read /etc/resolv.conf.
58
66
#
59
67
# If systemd-resolved is active, /etc/resolv.conf will point to
60
68
# localhost and the actual DNS servers that systemd-resolved uses
61
69
# are stored in /run/systemd/resolve/resolv.conf. For programs
62
- # that use the localhost DNS server, only reading /etc/resolv.conf
63
- # is sufficient. However, resolved provides other ways of
64
- # resolving hostnames (such as via dbus) that may not route
65
- # requests through localhost. So, we retrieve a list of DNS
70
+ # that use the localhost DNS server, having sshuttle read
71
+ # /etc/resolv.conf is sufficient. However, resolved provides other
72
+ # ways of resolving hostnames (such as via dbus) that may not
73
+ # route requests through localhost. So, we retrieve a list of DNS
66
74
# servers that resolved uses so we can intercept those as well.
67
75
#
68
76
# For more information about systemd-resolved, see:
69
77
# https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html
70
78
#
71
79
# On machines without systemd-resolved, we expect opening the
72
80
# second file will fail.
73
- files = ['/etc/resolv.conf' , '/run/systemd/resolve/resolv.conf' ]
81
+ files = ['/etc/resolv.conf' ]
82
+ if systemd_resolved :
83
+ files += ['/run/systemd/resolve/resolv.conf' ]
74
84
75
85
nsservers = []
76
86
for f in files :
@@ -90,8 +100,12 @@ def resolvconf_nameservers():
90
100
return nsservers
91
101
92
102
93
- def resolvconf_random_nameserver ():
94
- lines = resolvconf_nameservers ()
103
+ def resolvconf_random_nameserver (systemd_resolved ):
104
+ """Return a random nameserver selected from servers produced by
105
+ resolvconf_nameservers(). See documentation for
106
+ resolvconf_nameservers() for a description of the parameter.
107
+ """
108
+ lines = resolvconf_nameservers (systemd_resolved )
95
109
if lines :
96
110
if len (lines ) > 1 :
97
111
# don't import this unless we really need it
0 commit comments