MCPcopy Index your code
hub / github.com/sshuttle/sshuttle / resolvconf_nameservers

Function resolvconf_nameservers

sshuttle/helpers.py:66–115  ·  view source on GitHub ↗

Retrieves a list of tuples (address type, address as a string) of the DNS servers used by the system to resolve hostnames. If parameter is False, DNS servers are retrieved from only /etc/resolv.conf. This behavior makes sense for the sshuttle server. If parameter is True, we re

(systemd_resolved)

Source from the content-addressed store, hash-verified

64
65
66def resolvconf_nameservers(systemd_resolved):
67 """Retrieves a list of tuples (address type, address as a string) of
68 the DNS servers used by the system to resolve hostnames.
69
70 If parameter is False, DNS servers are retrieved from only
71 /etc/resolv.conf. This behavior makes sense for the sshuttle
72 server.
73
74 If parameter is True, we retrieve information from both
75 /etc/resolv.conf and /run/systemd/resolve/resolv.conf (if it
76 exists). This behavior makes sense for the sshuttle client.
77
78 """
79
80 # Historically, we just needed to read /etc/resolv.conf.
81 #
82 # If systemd-resolved is active, /etc/resolv.conf will point to
83 # localhost and the actual DNS servers that systemd-resolved uses
84 # are stored in /run/systemd/resolve/resolv.conf. For programs
85 # that use the localhost DNS server, having sshuttle read
86 # /etc/resolv.conf is sufficient. However, resolved provides other
87 # ways of resolving hostnames (such as via dbus) that may not
88 # route requests through localhost. So, we retrieve a list of DNS
89 # servers that resolved uses so we can intercept those as well.
90 #
91 # For more information about systemd-resolved, see:
92 # https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html
93 #
94 # On machines without systemd-resolved, we expect opening the
95 # second file will fail.
96 files = ['/etc/resolv.conf']
97 if systemd_resolved:
98 files += ['/run/systemd/resolve/resolv.conf']
99
100 nsservers = []
101 for f in files:
102 this_file_nsservers = []
103 try:
104 for line in open(f):
105 words = line.lower().split()
106 if len(words) >= 2 and words[0] == 'nameserver':
107 this_file_nsservers.append(family_ip_tuple(words[1]))
108 debug2("Found DNS servers in %s: %s" %
109 (f, [n[1] for n in this_file_nsservers]))
110 nsservers += this_file_nsservers
111 except OSError as e:
112 debug3("Failed to read %s when looking for DNS servers: %s" %
113 (f, e.strerror))
114
115 return nsservers
116
117
118def windows_nameservers():

Callers 2

mainFunction · 0.90
get_random_nameserverFunction · 0.85

Calls 3

family_ip_tupleFunction · 0.85
debug2Function · 0.85
debug3Function · 0.85

Tested by

no test coverage detected