-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make hostsfile cache shared + seperate dnsCache from hostsFile data * add max resolvers
- Loading branch information
1 parent
811e7df
commit e5ed9c0
Showing
7 changed files
with
186 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// metafiles are metadata files related to networking like | ||
// /etc/hosts etc | ||
package metafiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
fastdialer/hostsfile_unix.go → fastdialer/metafiles/hostsfile_unix.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fastdialer | ||
package metafiles | ||
|
||
// HostsFilePath in unix file os | ||
const HostsFilePath = "/etc/hosts" |
3 changes: 2 additions & 1 deletion
3
fastdialer/hostsfile_windows.go → fastdialer/metafiles/hostsfile_windows.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package fastdialer | ||
package metafiles | ||
|
||
const HostsFilePath = "${SystemRoot}/System32/drivers/etc/hosts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package metafiles | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
|
||
"github.com/projectdiscovery/hmap/store/hybrid" | ||
"github.com/projectdiscovery/utils/env" | ||
) | ||
|
||
type StorageType int | ||
|
||
const ( | ||
InMemory StorageType = iota | ||
Hybrid | ||
) | ||
|
||
var ( | ||
MaxHostsEntires = 4096 | ||
// LoadAllEntries is a switch when true loads all entries to hybrid storage | ||
// backend and uses it even if in-memory storage backend was requested | ||
LoadAllEntries = false | ||
) | ||
|
||
func init() { | ||
MaxHostsEntires = env.GetEnvOrDefault("HF_MAX_HOSTS", 4096) | ||
LoadAllEntries = env.GetEnvOrDefault("HF_LOAD_ALL", false) | ||
} | ||
|
||
// GetHostsFileDnsData returns the immutable dns data that is constant throughout the program | ||
// lifecycle and shouldn't be purged by cache etc. | ||
func GetHostsFileDnsData(storage StorageType) (*hybrid.HybridMap, error) { | ||
if LoadAllEntries { | ||
storage = Hybrid | ||
} | ||
switch storage { | ||
case InMemory: | ||
return getHFInMemory() | ||
case Hybrid: | ||
return getHFHybridStorage() | ||
} | ||
return nil, nil | ||
} | ||
|
||
var hostsMemOnce = &sync.Once{} | ||
|
||
// getImm | ||
func getHFInMemory() (*hybrid.HybridMap, error) { | ||
var hm *hybrid.HybridMap | ||
var err error | ||
hostsMemOnce.Do(func() { | ||
opts := hybrid.DefaultMemoryOptions | ||
hm, err = hybrid.New(opts) | ||
if err != nil { | ||
return | ||
} | ||
err = loadHostsFile(hm, MaxHostsEntires) | ||
if err != nil { | ||
hm.Close() | ||
return | ||
} | ||
}) | ||
return hm, nil | ||
} | ||
|
||
var hostsHybridOnce = &sync.Once{} | ||
|
||
func getHFHybridStorage() (*hybrid.HybridMap, error) { | ||
var hm *hybrid.HybridMap | ||
var err error | ||
hostsHybridOnce.Do(func() { | ||
opts := hybrid.DefaultHybridOptions | ||
opts.Cleanup = true | ||
hm, err = hybrid.New(opts) | ||
if err != nil { | ||
return | ||
} | ||
err = loadHostsFile(hm, -1) | ||
if err != nil { | ||
hm.Close() | ||
return | ||
} | ||
// set finalizer for cleanup | ||
runtime.SetFinalizer(hm, func(hm *hybrid.HybridMap) { | ||
_ = hm.Close() | ||
}) | ||
}) | ||
return hm, nil | ||
} |
Oops, something went wrong.