+ parse "config dnsmasq"
This commit is contained in:
@@ -1619,6 +1619,9 @@ If started as:
|
|||||||
option limit '150'
|
option limit '150'
|
||||||
option leasetime '12h'
|
option leasetime '12h'
|
||||||
|
|
||||||
|
config dnsmasq
|
||||||
|
option leasefile '/tmp/dhcp.leases'
|
||||||
|
|
||||||
* Write this yaml configuration:
|
* Write this yaml configuration:
|
||||||
|
|
||||||
dhcp:
|
dhcp:
|
||||||
@@ -1630,6 +1633,7 @@ If started as:
|
|||||||
range_end: "192.168.8.249"
|
range_end: "192.168.8.249"
|
||||||
lease_duration: 86400
|
lease_duration: 86400
|
||||||
icmp_timeout_msec: 1000
|
icmp_timeout_msec: 1000
|
||||||
|
dnsmasq_leasefile "/tmp/dhcp.leases"
|
||||||
|
|
||||||
* Read `/etc/config/dhcp`:
|
* Read `/etc/config/dhcp`:
|
||||||
|
|
||||||
@@ -1654,16 +1658,6 @@ If started as:
|
|||||||
- IP1
|
- IP1
|
||||||
- IP2
|
- 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:
|
And service script starts AGH like this:
|
||||||
|
|
||||||
.../AdGuardHome --import-openwrt-config
|
.../AdGuardHome --import-openwrt-config
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ type openwrtConfig struct {
|
|||||||
ipaddr string
|
ipaddr string
|
||||||
|
|
||||||
// dhcp:
|
// dhcp:
|
||||||
dhcpStart string
|
dhcpStart string
|
||||||
dhcpLimit string
|
dhcpLimit string
|
||||||
dhcpLeasetime string
|
dhcpLeasetime string
|
||||||
|
dhcpDnsmasqLeaseFile string
|
||||||
|
|
||||||
// dhcp static leases:
|
// dhcp static leases:
|
||||||
leases []dhcpd.Lease
|
leases []dhcpd.Lease
|
||||||
@@ -64,7 +65,7 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) {
|
|||||||
line, err := r.ReadString('\n')
|
line, err := r.ReadString('\n')
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
if state == 2 {
|
if state >= 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
state = 0
|
state = 0
|
||||||
@@ -82,6 +83,8 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) {
|
|||||||
state = 2 // found the needed interface
|
state = 2 // found the needed interface
|
||||||
} else if word2 == "dhcp" {
|
} else if word2 == "dhcp" {
|
||||||
state = 3
|
state = 3
|
||||||
|
} else if word2 == "dnsmasq" {
|
||||||
|
state = 4
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,6 +116,15 @@ func (oc *openwrtConfig) readConf(data []byte, section string, iface string) {
|
|||||||
case "leasetime":
|
case "leasetime":
|
||||||
oc.dhcpLeasetime = word3
|
oc.dhcpLeasetime = word3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
if word1 != "option" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch word2 {
|
||||||
|
case "leasefile":
|
||||||
|
oc.dhcpDnsmasqLeaseFile = word3
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -269,6 +281,7 @@ func (oc *openwrtConfig) Process() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
oc.readConf(data, "dhcp", "lan")
|
oc.readConf(data, "dhcp", "lan")
|
||||||
|
oc.readConf(data, "dnsmasq", "")
|
||||||
|
|
||||||
err = oc.prepareOutput()
|
err = oc.prepareOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -318,6 +331,7 @@ func importOpenwrtConfig(configFn string) error {
|
|||||||
config.DHCP.RangeStart = oc.rangeStart
|
config.DHCP.RangeStart = oc.rangeStart
|
||||||
config.DHCP.RangeEnd = oc.rangeEnd
|
config.DHCP.RangeEnd = oc.rangeEnd
|
||||||
config.DHCP.LeaseDuration = oc.leaseDur
|
config.DHCP.LeaseDuration = oc.leaseDur
|
||||||
|
config.DHCP.DnsmasqFilePath = oc.dhcpDnsmasqLeaseFile
|
||||||
|
|
||||||
err = config.write()
|
err = config.write()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
|
|
||||||
func TestReadConf(t *testing.T) {
|
func TestReadConf(t *testing.T) {
|
||||||
oc := openwrtConfig{}
|
oc := openwrtConfig{}
|
||||||
|
|
||||||
|
// "interface"
|
||||||
data := []byte(` config interface 'lan'
|
data := []byte(` config interface 'lan'
|
||||||
option netmask '255.255.255.0'
|
option netmask '255.255.255.0'
|
||||||
option ipaddr '192.168.8.1'`)
|
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, "255.255.255.0", oc.netmask)
|
||||||
assert.Equal(t, "192.168.8.1", oc.ipaddr)
|
assert.Equal(t, "192.168.8.1", oc.ipaddr)
|
||||||
|
|
||||||
|
// "dhcp"
|
||||||
data = []byte(` config dhcp 'unknown'
|
data = []byte(` config dhcp 'unknown'
|
||||||
|
|
||||||
config dhcp 'lan'
|
config dhcp 'lan'
|
||||||
@@ -28,6 +31,7 @@ config dhcp 'unknown'`)
|
|||||||
assert.Equal(t, "150", oc.dhcpLimit)
|
assert.Equal(t, "150", oc.dhcpLimit)
|
||||||
assert.Equal(t, "12h", oc.dhcpLeasetime)
|
assert.Equal(t, "12h", oc.dhcpLeasetime)
|
||||||
|
|
||||||
|
// resolv.conf
|
||||||
data = []byte(` # comment
|
data = []byte(` # comment
|
||||||
nameserver abab::1234
|
nameserver abab::1234
|
||||||
|
|
||||||
@@ -36,6 +40,7 @@ nameserver 1.2.3.4`)
|
|||||||
assert.Equal(t, "abab::1234", oc.nameservers[0])
|
assert.Equal(t, "abab::1234", oc.nameservers[0])
|
||||||
assert.Equal(t, "1.2.3.4", oc.nameservers[1])
|
assert.Equal(t, "1.2.3.4", oc.nameservers[1])
|
||||||
|
|
||||||
|
// prepareOutput()
|
||||||
err := oc.prepareOutput()
|
err := oc.prepareOutput()
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "br-lan", oc.iface)
|
assert.Equal(t, "br-lan", oc.iface)
|
||||||
@@ -73,6 +78,7 @@ nameserver 1.2.3.4`)
|
|||||||
assert.True(t, oc.prepareOutput() != nil)
|
assert.True(t, oc.prepareOutput() != nil)
|
||||||
oc.dhcpLeasetime = tmp
|
oc.dhcpLeasetime = tmp
|
||||||
|
|
||||||
|
// dhcp static leases
|
||||||
data = []byte(`config host '123412341234'
|
data = []byte(`config host '123412341234'
|
||||||
option mac '12:34:12:34:12:34'
|
option mac '12:34:12:34:12:34'
|
||||||
option ip '192.168.8.2'
|
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, "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, "192.168.8.2", oc.leases[0].IP.String())
|
||||||
assert.Equal(t, "hostname", oc.leases[0].Hostname)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user