@@ -98,29 +98,57 @@ def parse_hosts(io)
98
98
end
99
99
end
100
100
101
+ DEFAULT_TIMEOUT = 5.0
102
+
101
103
# Parse the `resolv.conf` file and return a list of nameservers.
102
104
def self . parse_resolv_configuration ( path )
103
105
nameservers = [ ]
106
+ search = nil
107
+ ndots = 1
108
+ edns = nil
109
+ timeout = DEFAULT_TIMEOUT
110
+
104
111
File . open ( path ) do |file |
105
112
file . each do |line |
106
113
# Remove any comments:
107
114
line . sub! ( /[#;].*/ , '' )
108
115
109
116
# Extract resolv.conf command:
110
- keyword , *args = line . split ( /\s +/ )
117
+ keyword , *arguments = line . split ( /\s +/ )
111
118
112
119
case keyword
113
120
when 'nameserver'
114
- nameservers += args
121
+ nameservers . concat ( arguments )
122
+ when 'domain' , 'search'
123
+ search = arguments
124
+ when 'options'
125
+ arguments . each do |argument |
126
+ key , value = argument . split ( ':' , 2 )
127
+
128
+ case key
129
+ when 'ndots'
130
+ ndots = value . to_i
131
+ when 'edns0'
132
+ edns = 0
133
+ when 'timeout'
134
+ timeout = value . to_f
135
+ end
136
+ end
115
137
end
116
138
end
117
139
end
118
140
119
- return nameservers
141
+ return {
142
+ nameservers : nameservers ,
143
+ search : search ,
144
+ ndots : ndots ,
145
+ edns : edns ,
146
+ timeout : timeout ,
147
+ }
120
148
end
121
149
122
150
# Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with.
123
- def self . standard_connections ( nameservers , **options )
151
+ def self . endpoint_for ( nameservers , **options )
124
152
connections = [ ]
125
153
126
154
nameservers . each do |host |
@@ -132,21 +160,33 @@ def self.standard_connections(nameservers, **options)
132
160
end
133
161
134
162
# Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with. There is no equivalent facility to use the `hosts` file at present.
135
- def self . nameservers ( **options )
163
+ def self . resolver ( **options )
136
164
nameservers = [ ]
137
165
138
166
if File . exist? RESOLV_CONF
139
- nameservers = parse_resolv_configuration ( RESOLV_CONF )
167
+ options . update ( parse_resolv_configuration ( RESOLV_CONF ) )
168
+ nameservers = options . delete ( :nameservers )
140
169
elsif defined? ( Win32 ::Resolv ) and RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
141
170
search , nameservers = Win32 ::Resolv . get_resolv_info
171
+ options . update ( search : search )
142
172
end
143
173
144
- return standard_connections ( nameservers , **options )
145
- end
146
-
147
- # Get a list of default nameservers.
148
- def self . default_nameservers
149
- self . nameservers ( timeout : 5.0 )
174
+ if search = options [ :search ]
175
+ unless search . include? ( '.' )
176
+ search << nil
177
+ end
178
+ else
179
+ options [ :search ] = [ nil ]
180
+ end
181
+
182
+ timeout = options . delete ( :timeout ) || DEFAULT_TIMEOUT
183
+ endpoint = self . endpoint_for ( nameservers , timeout : timeout )
184
+
185
+ if block_given?
186
+ yield endpoint , **options
187
+ else
188
+ return Resolver . new ( endpoint , **options )
189
+ end
150
190
end
151
191
end
152
192
end
0 commit comments