Skip to content

Commit

Permalink
home: add an option to disable autohosts
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Apr 12, 2021
1 parent 8b8319f commit 9058977
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to

### Added

- New startup option `--no-localhosts` to disable using OS-provided local hosts'
file as a source ([#1947]).
- The ability to set up custom upstreams to resolve PTR queries for local
addresses and to disable the automatic resolving of clients' addresses
([#2704]).
Expand Down Expand Up @@ -54,6 +56,7 @@ and this project adheres to

[#1273]: https://github.com/AdguardTeam/AdGuardHome/issues/1273
[#1401]: https://github.com/AdguardTeam/AdGuardHome/issues/1401
[#1947]: https://github.com/AdguardTeam/AdGuardHome/issues/1947
[#2385]: https://github.com/AdguardTeam/AdGuardHome/issues/2385
[#2393]: https://github.com/AdguardTeam/AdGuardHome/issues/2393
[#2412]: https://github.com/AdguardTeam/AdGuardHome/issues/2412
Expand Down
12 changes: 10 additions & 2 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.
}

clients.dhcpServer = dhcpServer
clients.autoHosts = autoHosts
if autoHosts != nil {
clients.autoHosts = autoHosts
}
clients.addFromConfig(objects)

if !clients.testing {
clients.addFromDHCP()
if clients.dhcpServer != nil {
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
}
clients.autoHosts.SetOnChanged(clients.onHostsChanged)
if clients.autoHosts != nil {
clients.autoHosts.SetOnChanged(clients.onHostsChanged)
}
}
}

Expand Down Expand Up @@ -692,6 +696,10 @@ func (clients *clientsContainer) rmHostsBySrc(src clientSource) {
// addFromHostsFile fills the client-hostname pairing index from the system's
// hosts files.
func (clients *clientsContainer) addFromHostsFile() {
if clients.autoHosts == nil {
return
}

hosts := clients.autoHosts.List()

clients.lock.Lock()
Expand Down
2 changes: 1 addition & 1 deletion internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func initDNSServer() error {
Context.queryLog = querylog.New(conf)

filterConf := config.DNS.DnsfilterConf
filterConf.AutoHosts = &Context.autoHosts
filterConf.AutoHosts = Context.autoHosts
filterConf.ConfigModified = onConfigModified
filterConf.HTTPRegister = httpRegister
Context.dnsFilter = dnsfilter.New(&filterConf, nil)
Expand Down
10 changes: 6 additions & 4 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type homeContext struct {
filters Filtering // DNS filtering module
web *Web // Web (HTTP, HTTPS) module
tls *TLSMod // TLS module
autoHosts util.AutoHosts // IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files
autoHosts *util.AutoHosts // IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files
updater *updater.Updater

subnetDetector *aghnet.SubnetDetector
Expand Down Expand Up @@ -186,8 +186,6 @@ func setupConfig(args options) {
log.Fatalf("can't initialize dhcp module")
}

Context.autoHosts.Init("")

Context.updater = updater.NewUpdater(&updater.Config{
Client: Context.client,
Version: version.Version(),
Expand All @@ -200,7 +198,11 @@ func setupConfig(args options) {
ConfName: config.getConfigFilename(),
})

Context.clients.Init(config.Clients, Context.dhcpServer, &Context.autoHosts)
if !args.noLocalHosts {
Context.autoHosts = &util.AutoHosts{}
Context.autoHosts.Init("")
Context.clients.Init(config.Clients, Context.dhcpServer, Context.autoHosts)
}
config.Clients = nil

if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
Expand Down
15 changes: 15 additions & 0 deletions internal/home/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type options struct {
disableMemoryOptimization bool

glinetMode bool // Activate GL-Inet compatibility mode

// noLocalHosts flag should be provided when /etc/hosts file shouldn't be
// used.
noLocalHosts bool
}

// functions used for their side-effects
Expand Down Expand Up @@ -220,6 +224,16 @@ var helpArg = arg{
func(o options) []string { return nil },
}

var noAutohostsArg = arg{
description: "Do not use the OS-provided hosts.",
longName: "no-localhosts",
shortName: "",
updateWithValue: nil,
updateNoValue: func(o options) (options, error) { o.noLocalHosts = true; return o, nil },
effect: nil,
serialize: func(o options) []string { return boolSliceOrNil(o.noLocalHosts) },
}

func init() {
args = []arg{
configArg,
Expand All @@ -236,6 +250,7 @@ func init() {
glinetArg,
versionArg,
helpArg,
noAutohostsArg,
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/util/autohosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (a *AutoHosts) Init(hostsFn string) {

// Start - start module
func (a *AutoHosts) Start() {
if a == nil {
return
}

log.Debug("Start AutoHosts module")

a.updateHosts()
Expand All @@ -113,6 +117,10 @@ func (a *AutoHosts) Start() {

// Close - close module
func (a *AutoHosts) Close() {
if a == nil {
return
}

if a.watcher != nil {
_ = a.watcher.Close()
}
Expand Down

0 comments on commit 9058977

Please sign in to comment.