From da2a8daff24e74d9321041789b89723caa65686d Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Thu, 23 Apr 2020 12:13:41 +0300 Subject: [PATCH] + parse "config dnsmasq" --- AGHTechDoc.md | 14 ++++---------- home/openwrt.go | 22 ++++++++++++++++++---- home/openwrt_test.go | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/AGHTechDoc.md b/AGHTechDoc.md index 9ad3430b..3effe6b3 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -1619,6 +1619,9 @@ If started as: option limit '150' option leasetime '12h' + config dnsmasq + option leasefile '/tmp/dhcp.leases' + * Write this yaml configuration: dhcp: @@ -1630,6 +1633,7 @@ If started as: range_end: "192.168.8.249" lease_duration: 86400 icmp_timeout_msec: 1000 + dnsmasq_leasefile "/tmp/dhcp.leases" * Read `/etc/config/dhcp`: @@ -1654,16 +1658,6 @@ If started as: - IP1 - IP2 -* Read `/etc/config/dhcp`: - - config dnsmasq - option leasefile '/tmp/dhcp.leases' - -* Write this yaml configuration: - - dhcp: - dnsmasq_leasefile "/tmp/dhcp.leases" - And service script starts AGH like this: .../AdGuardHome --import-openwrt-config diff --git a/home/openwrt.go b/home/openwrt.go index 0ba63abf..41524ef6 100644 --- a/home/openwrt.go +++ b/home/openwrt.go @@ -19,9 +19,10 @@ type openwrtConfig struct { ipaddr string // dhcp: - dhcpStart string - dhcpLimit string - dhcpLeasetime string + dhcpStart string + dhcpLimit string + dhcpLeasetime string + dhcpDnsmasqLeaseFile string // dhcp static leases: leases []dhcpd.Lease @@ -64,7 +65,7 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) { line, err := r.ReadString('\n') line = strings.TrimSpace(line) if len(line) == 0 { - if state == 2 { + if state >= 2 { return } state = 0 @@ -82,6 +83,8 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) { state = 2 // found the needed interface } else if word2 == "dhcp" { state = 3 + } else if word2 == "dnsmasq" { + state = 4 } } } @@ -113,6 +116,15 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) { case "leasetime": oc.dhcpLeasetime = word3 } + + case 4: + if word1 != "option" { + break + } + switch word2 { + case "leasefile": + oc.dhcpDnsmasqLeaseFile = word3 + } } if err != nil { @@ -269,6 +281,7 @@ func (oc *openwrtConfig) Process() error { return err } oc.readConf(data, "dhcp", "lan") + oc.readConf(data, "dnsmasq", "") err = oc.prepareOutput() if err != nil { @@ -318,6 +331,7 @@ func importOpenwrtConfig(configFn string) error { config.DHCP.RangeStart = oc.rangeStart config.DHCP.RangeEnd = oc.rangeEnd config.DHCP.LeaseDuration = oc.leaseDur + config.DHCP.DnsmasqFilePath = oc.dhcpDnsmasqLeaseFile err = config.write() if err != nil { diff --git a/home/openwrt_test.go b/home/openwrt_test.go index 1c9d95ce..55f32903 100644 --- a/home/openwrt_test.go +++ b/home/openwrt_test.go @@ -8,6 +8,8 @@ import ( func TestReadConf(t *testing.T) { oc := openwrtConfig{} + + // "interface" data := []byte(` config interface 'lan' option netmask '255.255.255.0' option ipaddr '192.168.8.1'`) @@ -15,6 +17,7 @@ option ipaddr '192.168.8.1'`) assert.Equal(t, "255.255.255.0", oc.netmask) assert.Equal(t, "192.168.8.1", oc.ipaddr) + // "dhcp" data = []byte(` config dhcp 'unknown' config dhcp 'lan' @@ -28,6 +31,7 @@ config dhcp 'unknown'`) assert.Equal(t, "150", oc.dhcpLimit) assert.Equal(t, "12h", oc.dhcpLeasetime) + // resolv.conf data = []byte(` # comment nameserver abab::1234 @@ -36,6 +40,7 @@ nameserver 1.2.3.4`) assert.Equal(t, "abab::1234", oc.nameservers[0]) assert.Equal(t, "1.2.3.4", oc.nameservers[1]) + // prepareOutput() err := oc.prepareOutput() assert.Equal(t, nil, err) assert.Equal(t, "br-lan", oc.iface) @@ -73,6 +78,7 @@ nameserver 1.2.3.4`) assert.True(t, oc.prepareOutput() != nil) oc.dhcpLeasetime = tmp + // dhcp static leases data = []byte(`config host '123412341234' option mac '12:34:12:34:12:34' option ip '192.168.8.2' @@ -82,4 +88,17 @@ option name 'hostname'`) assert.Equal(t, "12:34:12:34:12:34", oc.leases[0].HWAddr.String()) assert.Equal(t, "192.168.8.2", oc.leases[0].IP.String()) assert.Equal(t, "hostname", oc.leases[0].Hostname) + + // "dnsmasq" + // Note: "config dnsmasq ''" will also work + data = []byte(` +config dhcp 'unknown' + option asdf '100' + +config dnsmasq + option asdf '100' + option leasefile '/tmp/dhcp.leases' + option leasetime '12h'`) + oc.readConf(data, "dnsmasq", "") + assert.Equal(t, "/tmp/dhcp.leases", oc.dhcpDnsmasqLeaseFile) }