+ /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 ### API: Reset DHCP configuration
Clear all DHCP leases and configuration settings. Clear all DHCP leases and configuration settings.
DHCP server will be stopped if it's currently running.
Request: Request:
POST /control/dhcp/reset POST /control/dhcp/reset
{
"what": "all" | "leases"
}
Response: Response:
200 OK 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 ## 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) { 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 { 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) err = os.Remove(s.conf.DBFilePath)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
log.Error("DHCP: os.Remove: %s: %s", s.conf.DBFilePath, 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() { func (s *Server) registerHandlers() {

View File

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