@@ -170,3 +170,90 @@ func TestTrace(t *testing.T) {
170
170
_ , err := client .Trace ("www.projectdiscovery.io" , dns .TypeA , 100 )
171
171
require .Nil (t , err , "could not resolve dns" )
172
172
}
173
+
174
+ func TestInternalIPDetectionWithHostsFile (t * testing.T ) {
175
+ CheckInternalIPs = true
176
+ defer func () { CheckInternalIPs = false }()
177
+
178
+ options := Options {
179
+ BaseResolvers : []string {"8.8.8.8:53" },
180
+ MaxRetries : 3 ,
181
+ Hostsfile : true ,
182
+ }
183
+
184
+ client , err := NewWithOptions (options )
185
+ require .NoError (t , err )
186
+
187
+ client .knownHosts = map [string ][]string {
188
+ "localhost" : {"127.0.0.1" , "::1" },
189
+ "internal.test" : {"192.168.1.100" , "10.0.0.1" },
190
+ "external.test" : {"8.8.8.8" },
191
+ }
192
+
193
+ testCases := []struct {
194
+ host string
195
+ expectedInternal bool
196
+ expectedInternalIP []string
197
+ }{
198
+ {
199
+ host : "localhost" ,
200
+ expectedInternal : true ,
201
+ expectedInternalIP : []string {"127.0.0.1" , "::1" },
202
+ },
203
+ {
204
+ host : "internal.test" ,
205
+ expectedInternal : true ,
206
+ expectedInternalIP : []string {"192.168.1.100" , "10.0.0.1" },
207
+ },
208
+ {
209
+ host : "external.test" ,
210
+ expectedInternal : false ,
211
+ },
212
+ }
213
+
214
+ for _ , tc := range testCases {
215
+ t .Run (tc .host , func (t * testing.T ) {
216
+ result , err := client .QueryMultiple (tc .host , []uint16 {dns .TypeA , dns .TypeAAAA })
217
+ require .NoError (t , err )
218
+ require .True (t , result .HostsFile )
219
+
220
+ if tc .expectedInternal {
221
+ assert .True (t , result .HasInternalIPs , "HasInternalIPs should be true for %s" , tc .host )
222
+ assert .ElementsMatch (t , tc .expectedInternalIP , result .InternalIPs , "InternalIPs should match for %s" , tc .host )
223
+ } else {
224
+ assert .False (t , result .HasInternalIPs , "HasInternalIPs should be false for %s" , tc .host )
225
+ assert .Empty (t , result .InternalIPs , "InternalIPs should be empty for %s" , tc .host )
226
+ }
227
+ })
228
+ }
229
+ }
230
+
231
+ func TestInternalIPDetectionJSONOutput (t * testing.T ) {
232
+ CheckInternalIPs = true
233
+ defer func () { CheckInternalIPs = false }()
234
+
235
+ options := Options {
236
+ BaseResolvers : []string {"8.8.8.8:53" },
237
+ MaxRetries : 3 ,
238
+ Hostsfile : true ,
239
+ }
240
+
241
+ client , err := NewWithOptions (options )
242
+ require .NoError (t , err )
243
+
244
+ client .knownHosts = map [string ][]string {
245
+ "localhost" : {"127.0.0.1" },
246
+ }
247
+
248
+ result , err := client .QueryMultiple ("localhost" , []uint16 {dns .TypeA })
249
+ require .NoError (t , err )
250
+
251
+ jsonOutput , err := result .JSON ()
252
+ require .NoError (t , err )
253
+
254
+ t .Logf ("JSON output for localhost with internal IP detection:\n %s" , jsonOutput )
255
+
256
+ assert .Contains (t , jsonOutput , `"has_internal_ips":true` )
257
+ assert .Contains (t , jsonOutput , `"internal_ips":["127.0.0.1"]` )
258
+ assert .Contains (t , jsonOutput , `"hosts_file":true` )
259
+ }
0 commit comments