+ POST /control/dhcp/reset: Reset DHCP configuration

This commit is contained in:
Simon Zolin 2019-10-14 12:12:06 +03:00
parent 477a4bbf54
commit df92941ae0
3 changed files with 44 additions and 4 deletions

View File

@ -28,6 +28,7 @@ Contents:
* "Enable DHCP" command * "Enable DHCP" command
* Static IP check/set * Static IP check/set
* Add a static lease * Add a static lease
* API: Reset DHCP configuration
* DNS access settings * DNS access settings
* List access settings * List access settings
* Set access settings * Set access settings
@ -543,6 +544,20 @@ Response:
200 OK 200 OK
### API: Reset DHCP configuration
Clear all DHCP leases and configuration settings.
DHCP server will be stopped if it's currently running.
Request:
POST /control/dhcp/reset
Response:
200 OK
## TLS ## TLS

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"os"
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
@ -451,6 +452,28 @@ func (s *Server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Requ
} }
} }
func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
err := s.Stop()
if err != nil {
log.Error("DHCP: Stop: %s", err)
}
err = os.Remove(s.conf.DBFilePath)
if err != nil && !os.IsNotExist(err) {
log.Error("DHCP: os.Remove: %s: %s", s.conf.DBFilePath, err)
}
oldconf := s.conf
s.conf = ServerConfig{}
s.conf.LeaseDuration = 86400
s.conf.ICMPTimeout = 1000
s.conf.WorkDir = oldconf.WorkDir
s.conf.HTTPRegister = oldconf.HTTPRegister
s.conf.ConfigModified = oldconf.ConfigModified
s.conf.DBFilePath = oldconf.DBFilePath
s.conf.ConfigModified()
}
func (s *Server) registerHandlers() { func (s *Server) registerHandlers() {
s.conf.HTTPRegister("GET", "/control/dhcp/status", s.handleDHCPStatus) s.conf.HTTPRegister("GET", "/control/dhcp/status", s.handleDHCPStatus)
s.conf.HTTPRegister("GET", "/control/dhcp/interfaces", s.handleDHCPInterfaces) s.conf.HTTPRegister("GET", "/control/dhcp/interfaces", s.handleDHCPInterfaces)
@ -458,4 +481,5 @@ func (s *Server) registerHandlers() {
s.conf.HTTPRegister("POST", "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer) s.conf.HTTPRegister("POST", "/control/dhcp/find_active_dhcp", s.handleDHCPFindActiveServer)
s.conf.HTTPRegister("POST", "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease) s.conf.HTTPRegister("POST", "/control/dhcp/add_static_lease", s.handleDHCPAddStaticLease)
s.conf.HTTPRegister("POST", "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease) s.conf.HTTPRegister("POST", "/control/dhcp/remove_static_lease", s.handleDHCPRemoveStaticLease)
s.conf.HTTPRegister("POST", "/control/dhcp/reset", s.handleReset)
} }

View File

@ -39,13 +39,14 @@ type ServerConfig struct {
SubnetMask string `json:"subnet_mask" yaml:"subnet_mask"` SubnetMask string `json:"subnet_mask" yaml:"subnet_mask"`
RangeStart string `json:"range_start" yaml:"range_start"` RangeStart string `json:"range_start" yaml:"range_start"`
RangeEnd string `json:"range_end" yaml:"range_end"` RangeEnd string `json:"range_end" yaml:"range_end"`
LeaseDuration uint `json:"lease_duration" yaml:"lease_duration"` // in seconds LeaseDuration uint32 `json:"lease_duration" yaml:"lease_duration"` // in seconds
WorkDir string `json:"-" yaml:"-"`
DBFilePath string `json:"-" yaml:"-"` // path to DB file
// IP conflict detector: time (ms) to wait for ICMP reply. // IP conflict detector: time (ms) to wait for ICMP reply.
// 0: disable // 0: disable
ICMPTimeout uint `json:"icmp_timeout_msec" yaml:"icmp_timeout_msec"` ICMPTimeout uint32 `json:"icmp_timeout_msec" yaml:"icmp_timeout_msec"`
WorkDir string `json:"-" yaml:"-"`
DBFilePath string `json:"-" yaml:"-"` // path to DB file
// Called when the configuration is changed by HTTP request // Called when the configuration is changed by HTTP request
ConfigModified func() `json:"-" yaml:"-"` ConfigModified func() `json:"-" yaml:"-"`