Skip to content

Commit

Permalink
Add -advertise flag.
Browse files Browse the repository at this point in the history
This enables us to have a different advertise IP in /etc/hosts compared
to the local bind (via `-host`) flag.

This is useful when binding to 127.0.0.1 behind a web server but running
with a Pihole.
  • Loading branch information
issmirnov committed Mar 13, 2021
1 parent a3ced18 commit 99fe6ed
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ With this config, you can use the following queries:
- `-config` - path to config file. Default is `./c.yml`
- `-port` - port to bind to. Default is 8927. Use 80 in standalone mode.
- `-host` - default is 127.0.0.1. Use 0.0.0.0 for a public server.
- `-advertise` - which address to use when populating `/etc/hosts`.
This is useful when running zap behind `dnsmasq`, so that the host bind and advertised address can differ.


### DNS management via /etc/hosts

Expand Down
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func main() {
var (
configName = flag.String("config", "c.yml", "config file")
port = flag.Int("port", 8927, "port to bind to")
host = flag.String("host", "127.0.0.1", "host interface")
host = flag.String("host", "127.0.0.1", "host address to bind to")
advertise = flag.String("advertise", "127.0.0.1", "IP to advertise, used in /etc/hosts")
v = flag.Bool("v", false, "print version info")
validate = flag.Bool("validate", false, "load config file and check for errors")
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func main() {
os.Exit(0)
}

context := &zap.Context{Config: c}
context := &zap.Context{Config: c, Advertise: *advertise}
zap.UpdateHosts(context) // sync changes since last run.

// Enable hot reload.
Expand Down
2 changes: 1 addition & 1 deletion cmd/zap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func UpdateHosts(c *Context) {
replacement.WriteString(delimStart)
children := c.Config.ChildrenMap()
for k := range children {
replacement.WriteString(fmt.Sprintf("127.0.0.1 %s\n", k))
replacement.WriteString(fmt.Sprintf("%s %s\n", c.Advertise, k))
}
replacement.WriteString(delimEnd)

Expand Down
7 changes: 5 additions & 2 deletions cmd/zap/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
)

type Context struct {
// Json container with path configs
// Config is a Json container with path configs
Config *gabs.Container

// Enables safe hot reloading of Config.
// ConfigMtx Enables safe hot reloading of Config.
ConfigMtx sync.Mutex

// Advertise IP, used in /etc/hosts in case bind address differs.
Advertise string
}

type CtxWrapper struct {
Expand Down

0 comments on commit 99fe6ed

Please sign in to comment.