Skip to content

Commit

Permalink
more updates for the new config format
Browse files Browse the repository at this point in the history
  • Loading branch information
f18m committed Dec 3, 2024
1 parent cfe3a86 commit 2541e69
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
48 changes: 24 additions & 24 deletions dhcp-clients-webapp-backend/pkg/uibackend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,51 +132,51 @@ func (b *AddonConfig) UnmarshalJSON(data []byte) error {

// JSON parse
var cfg struct {
IpAddressReservations []struct {
DhcpIpAddressReservations []struct {
Name string `json:"name"`
Mac string `json:"mac"`
IP string `json:"ip"`
Link string `json:"link"`
} `json:"ip_address_reservations"`
} `json:"dhcp_ip_address_reservations"`

DhcpClientsFriendlyNames []struct {
Name string `json:"name"`
Mac string `json:"mac"`
Link string `json:"link"`
} `json:"dhcp_clients_friendly_names"`

DhcpRange struct {
StartIP string `json:"start_ip"`
EndIP string `json:"end_ip"`
} `json:"dhcp_range"`

LogDHCP bool `json:"log_dhcp"`
LogWebUI bool `json:"log_web_ui"`

WebUIPort int `json:"web_ui_port"`

DefaultLease string `json:"default_lease"`
AddressReservationLease string `json:"address_reservation_lease"`
DhcpServer struct {
StartIP string `json:"start_ip"`
EndIP string `json:"end_ip"`
LogDHCP bool `json:"log_requests"`
DefaultLease string `json:"default_lease"`
AddressReservationLease string `json:"address_reservation_lease"`
} `json:"dhcp_server"`

WebUI struct {
Log bool `json:"log_requests"`
Port int `json:"port"`
} `json:"web_ui"`
}
err := json.Unmarshal(data, &cfg)
if err != nil {
return err
}

// convert DHCP IP strings to net.IP
b.dhcpStartIP = net.ParseIP(cfg.DhcpRange.StartIP)
b.dhcpEndIP = net.ParseIP(cfg.DhcpRange.EndIP)
b.dhcpStartIP = net.ParseIP(cfg.DhcpServer.StartIP)
b.dhcpEndIP = net.ParseIP(cfg.DhcpServer.EndIP)
if b.dhcpStartIP == nil || b.dhcpEndIP == nil {
return fmt.Errorf("invalid DHCP range found in addon config file")
}

// ensure we have a valid port for web UI
if cfg.WebUIPort <= 0 || cfg.WebUIPort > 32768 {
return fmt.Errorf("invalid web UI port number: %d", cfg.WebUIPort)
if cfg.WebUI.Port <= 0 || cfg.WebUI.Port > 32768 {
return fmt.Errorf("invalid web UI port number: %d", cfg.WebUI.Port)
}

// convert IP address reservations to a map indexed by IP
for _, r := range cfg.IpAddressReservations {
for _, r := range cfg.DhcpIpAddressReservations {
ipAddr, err := netip.ParseAddr(r.IP)
if err != nil {
return fmt.Errorf("invalid IP address found inside 'ip_address_reservations': %s", r.IP)
Expand Down Expand Up @@ -232,11 +232,11 @@ func (b *AddonConfig) UnmarshalJSON(data []byte) error {
}

// copy basic settings
b.logDHCP = cfg.LogDHCP
b.logWebUI = cfg.LogWebUI
b.webUIPort = cfg.WebUIPort
b.defaultLease = cfg.DefaultLease
b.addressReservationLease = cfg.AddressReservationLease
b.logDHCP = cfg.DhcpServer.LogDHCP
b.logWebUI = cfg.WebUI.Log
b.webUIPort = cfg.WebUI.Port
b.defaultLease = cfg.DhcpServer.DefaultLease
b.addressReservationLease = cfg.DhcpServer.AddressReservationLease

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions rootfs/etc/cont-init.d/10-dnsmasq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function ipvalid() {
}

function dnsresolve() {
NTP_SERVERS="$(jq --raw-output '.network.ntp[]' ${ADDON_CONFIG} 2>/dev/null)"
NTP_SERVERS="$(jq --raw-output '.dhcp_network.ntp[]' ${ADDON_CONFIG} 2>/dev/null)"
if [[ ! -z "${NTP_SERVERS}" ]]; then
bashio::log.info "NTP servers are ${NTP_SERVERS/$'\n'/,}"

Expand All @@ -52,7 +52,7 @@ function dnsresolve() {
NTP_SERVERS_RESOLVED=${NTP_SERVERS_RESOLVED::-1}

# add DNS-resolved IP addresses
jq -c ".network.ntp_resolved=[$NTP_SERVERS_RESOLVED]" ${ADDON_CONFIG} >${ADDON_CONFIG_RESOLVED}
jq -c ".dhcp_network.ntp_resolved=[$NTP_SERVERS_RESOLVED]" ${ADDON_CONFIG} >${ADDON_CONFIG_RESOLVED}
fi
else
cp ${ADDON_CONFIG} ${ADDON_CONFIG_RESOLVED}
Expand Down Expand Up @@ -84,7 +84,7 @@ function reset_dhcp_leases_database_if_just_rebooted() {
fi
}

should_reset_on_reboot=$(bashio::config 'reset_dhcp_lease_database_on_reboot')
should_reset_on_reboot=$(bashio::config '.dhcp_server.reset_dhcp_lease_database_on_reboot')
if $should_reset_on_reboot ; then
reset_dhcp_leases_database_if_just_rebooted
fi
Expand Down
36 changes: 24 additions & 12 deletions test-options.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"default_lease": "12h",
"address_reservation_lease": "24h",
"ip_address_reservations": [
"interface": "enp3s0",
"dhcp_ip_address_reservations": [
{
"mac": "aa:bb:cc:dd:ee:00",
"name": "static-ip-important-host",
Expand All @@ -22,21 +21,24 @@
"link": "https://{{ .ip }}/page-{{ .hostname }}"
}
],
"dhcp_range": {
"dhcp_server": {
"default_lease": "12h",
"address_reservation_lease": "24h",
"start_ip": "192.168.1.50",
"end_ip": "192.168.1.100"
"end_ip": "192.168.1.100",
"log_requests": true,
"reset_dhcp_lease_database_on_reboot": false
},
"network": {
"dhcp_network": {
"broadcast": "192.168.1.255",
"gateway": "192.168.1.254",
"interface": "enp3s0",
"netmask": "255.255.255.0",
"subnet": "192.168.1.0",
"dns": [
"dns_servers": [
"8.8.8.8",
"8.8.4.4"
],
"ntp": [
"ntp_servers": [
"0.2.3.4",
"ntp.pool.org",
"0.europe.ntp.pool.org",
Expand All @@ -45,7 +47,17 @@
"1.2.3.4"
]
},
"log_dhcp": true,
"log_web_ui": true,
"web_ui_port": 8976
"dns_server": {
"enable": true,
"cache_size": 10000,
"log_requests": true
},
"dns_upstream_servers": [
"8.8.8.8",
"8.8.4.4"
],
"web_ui": {
"log_requests": true,
"port": 8976
}
}
2 changes: 1 addition & 1 deletion test-startepoch
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1730118060
1733260315

0 comments on commit 2541e69

Please sign in to comment.