+ /dhcp/reset: clear leases

This commit is contained in:
Simon Zolin 2020-06-01 11:20:58 +03:00
parent 93b99039c0
commit 4b5f8537be
3 changed files with 56 additions and 13 deletions

View file

@ -646,16 +646,25 @@ Response:
### 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
{
"what": "all" | "leases"
}
Response:
200 OK
`what`:
* all:
Clear all DHCP leases and configuration settings.
DHCP server will be stopped if it's currently running.
* leases:
Clear all DHCP leases
## TLS

View file

@ -415,26 +415,50 @@ func (s *Server) handleDHCPRemoveStaticLease(w http.ResponseWriter, r *http.Requ
}
}
type resetJSON struct {
What string `json:"what"`
}
func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
err := s.Stop()
req := resetJSON{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Error("DHCP: Stop: %s", err)
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
return
}
switch req.What {
case "all":
err = s.Stop()
if err != nil {
log.Error("DHCP: Stop: %s", err)
}
oldconf := s.conf
s.conf = ServerConfig{}
s.conf.WorkDir = oldconf.WorkDir
s.conf.HTTPRegister = oldconf.HTTPRegister
s.conf.ConfigModified = oldconf.ConfigModified
s.conf.DBFilePath = oldconf.DBFilePath
s.conf.ConfigModified()
case "leases":
//
default:
httpError(r, w, http.StatusBadRequest, "unsupported 'what' value")
return
}
s.srv4.ResetLeases(nil)
s.srv6.ResetLeases(nil)
err = os.Remove(s.conf.DBFilePath)
if err != nil && !os.IsNotExist(err) {
log.Error("DHCP: os.Remove: %s: %s", s.conf.DBFilePath, err)
} else if err == nil {
log.Debug("DHCP: Deleted leases file")
}
oldconf := s.conf
s.conf = ServerConfig{}
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() {

View file

@ -397,6 +397,11 @@ paths:
- dhcp
operationId: dhcpReset
summary: Reset DHCP configuration
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/DhcpResetRequest"
responses:
"200":
description: OK
@ -1366,6 +1371,11 @@ components:
type: string
description: Set if static=no
example: ""
DhcpResetRequest:
type: object
properties:
what:
type: string
DnsAnswer:
type: object
description: DNS answer section