2018-06-28 13:39:48 +02:00
|
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2018-05-27 17:30:42 +02:00
|
|
|
|
// Binary dhcp4d hands out DHCPv4 leases to clients.
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-07-20 10:53:41 +02:00
|
|
|
|
"bytes"
|
2020-01-31 19:00:51 +01:00
|
|
|
|
"context"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
"encoding/json"
|
|
|
|
|
"flag"
|
2019-01-06 18:02:01 +01:00
|
|
|
|
"fmt"
|
2019-07-20 10:53:41 +02:00
|
|
|
|
"html/template"
|
2020-01-31 18:30:18 +01:00
|
|
|
|
"io"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
"io/ioutil"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
"os"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
"os/signal"
|
2020-06-12 17:53:13 -07:00
|
|
|
|
"path"
|
2020-01-31 19:00:51 +01:00
|
|
|
|
"path/filepath"
|
2019-07-20 10:53:41 +02:00
|
|
|
|
"sort"
|
2020-01-31 18:30:18 +01:00
|
|
|
|
"strings"
|
2020-01-31 18:23:19 +01:00
|
|
|
|
"sync"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
"syscall"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
"time"
|
2021-06-03 21:05:33 +02:00
|
|
|
|
"unicode"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
|
|
|
|
|
"github.com/gokrazy/gokrazy"
|
2019-01-06 15:23:09 +01:00
|
|
|
|
"github.com/google/renameio"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
"github.com/krolaw/dhcp4"
|
|
|
|
|
"github.com/krolaw/dhcp4/conn"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
|
2018-07-09 08:54:04 +02:00
|
|
|
|
"github.com/rtr7/router7/internal/dhcp4d"
|
2018-10-23 08:49:29 +02:00
|
|
|
|
"github.com/rtr7/router7/internal/multilisten"
|
2020-07-06 17:01:50 -07:00
|
|
|
|
"github.com/rtr7/router7/internal/netconfig"
|
2018-07-09 08:54:04 +02:00
|
|
|
|
"github.com/rtr7/router7/internal/notify"
|
2019-01-06 18:02:01 +01:00
|
|
|
|
"github.com/rtr7/router7/internal/oui"
|
2018-07-09 08:54:04 +02:00
|
|
|
|
"github.com/rtr7/router7/internal/teelogger"
|
2018-05-27 17:30:42 +02:00
|
|
|
|
)
|
|
|
|
|
|
2020-06-12 17:53:13 -07:00
|
|
|
|
var (
|
|
|
|
|
log = teelogger.NewConsole()
|
|
|
|
|
nonExpiredLeases = promauto.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "non_expired_leases",
|
|
|
|
|
Help: "Number of non-expired DHCP leases",
|
|
|
|
|
})
|
2018-06-09 15:10:11 +02:00
|
|
|
|
|
2020-07-06 17:01:50 -07:00
|
|
|
|
iface = flag.String("interface", "lan0", "ethernet interface to listen for DHCPv4 requests on")
|
|
|
|
|
perm = flag.String("perm", "/perm", "path to replace /perm")
|
|
|
|
|
domain = flag.String("domain", "lan", "domain name for your network")
|
2020-06-12 17:53:13 -07:00
|
|
|
|
)
|
2018-10-23 08:49:29 +02:00
|
|
|
|
|
|
|
|
|
func updateNonExpired(leases []*dhcp4d.Lease) {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
nonExpired := 0
|
|
|
|
|
for _, l := range leases {
|
|
|
|
|
if l.Expired(now) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
nonExpired++
|
|
|
|
|
}
|
|
|
|
|
nonExpiredLeases.Set(float64(nonExpired))
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 17:53:13 -07:00
|
|
|
|
var ouiDB = oui.NewDB(path.Join(*perm, "/dhcp4d/oui"))
|
2019-01-06 18:02:01 +01:00
|
|
|
|
|
2020-01-31 18:23:19 +01:00
|
|
|
|
var (
|
|
|
|
|
leasesMu sync.Mutex
|
|
|
|
|
leases []*dhcp4d.Lease
|
|
|
|
|
)
|
2019-07-20 10:53:41 +02:00
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
timefmt = func(t time.Time) string {
|
|
|
|
|
return t.Format("2006-01-02 15:04")
|
|
|
|
|
}
|
|
|
|
|
leasesTmpl = template.Must(template.New("").Funcs(template.FuncMap{
|
|
|
|
|
"timefmt": timefmt,
|
|
|
|
|
"since": func(t time.Time) string {
|
|
|
|
|
dur := time.Since(t)
|
|
|
|
|
if dur.Hours() > 24 {
|
|
|
|
|
return timefmt(t)
|
|
|
|
|
}
|
|
|
|
|
return dur.Truncate(1 * time.Second).String()
|
|
|
|
|
},
|
2022-03-12 17:38:16 +01:00
|
|
|
|
"zero": func(t time.Time) bool {
|
|
|
|
|
return t.IsZero()
|
|
|
|
|
},
|
2019-07-20 10:53:41 +02:00
|
|
|
|
}).Parse(`<!DOCTYPE html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
<title>DHCPv4 status</title>
|
|
|
|
|
<style type="text/css">
|
|
|
|
|
body {
|
|
|
|
|
margin-left: 1em;
|
|
|
|
|
}
|
|
|
|
|
td, th {
|
|
|
|
|
padding-left: 1em;
|
|
|
|
|
padding-right: 1em;
|
|
|
|
|
padding-bottom: .25em;
|
|
|
|
|
}
|
|
|
|
|
td:first-child, th:first-child {
|
|
|
|
|
padding-left: .25em;
|
|
|
|
|
}
|
|
|
|
|
td:last-child, th:last-child {
|
|
|
|
|
padding-right: .25em;
|
|
|
|
|
}
|
|
|
|
|
th {
|
|
|
|
|
padding-top: 1em;
|
|
|
|
|
text-align: left;
|
|
|
|
|
}
|
2019-08-30 09:06:21 +02:00
|
|
|
|
span.active, span.expired, span.static, span.hostname-override {
|
2019-07-20 10:53:41 +02:00
|
|
|
|
min-width: 5em;
|
|
|
|
|
display: inline-block;
|
|
|
|
|
text-align: center;
|
|
|
|
|
border: 1px solid grey;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
}
|
|
|
|
|
span.active {
|
|
|
|
|
background-color: #00f000;
|
|
|
|
|
}
|
|
|
|
|
span.expired {
|
|
|
|
|
background-color: #f00000;
|
|
|
|
|
}
|
2019-08-30 09:06:21 +02:00
|
|
|
|
span.hostname-override {
|
|
|
|
|
min-width: 1em;
|
|
|
|
|
background-color: orange;
|
|
|
|
|
}
|
2019-07-20 10:53:41 +02:00
|
|
|
|
.ipaddr, .hwaddr {
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
}
|
|
|
|
|
tr:nth-child(even) {
|
|
|
|
|
background: #eee;
|
|
|
|
|
}
|
2020-04-12 10:33:11 +02:00
|
|
|
|
form {
|
|
|
|
|
display: inline;
|
|
|
|
|
}
|
2019-07-20 10:53:41 +02:00
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
{{ define "table" }}
|
|
|
|
|
<tr>
|
|
|
|
|
<th>IP address</th>
|
|
|
|
|
<th>Hostname</th>
|
|
|
|
|
<th>MAC address</th>
|
|
|
|
|
<th>Vendor</th>
|
2022-03-04 13:49:50 -08:00
|
|
|
|
<th>VendorIdentifier</th>
|
2019-07-20 10:53:41 +02:00
|
|
|
|
<th>Expiry</th>
|
2022-03-12 17:38:16 +01:00
|
|
|
|
<th>Last ACK</th>
|
2019-07-20 10:53:41 +02:00
|
|
|
|
</tr>
|
|
|
|
|
{{ range $idx, $l := . }}
|
|
|
|
|
<tr>
|
|
|
|
|
<td class="ipaddr">{{$l.Addr}}</td>
|
2019-08-30 09:06:21 +02:00
|
|
|
|
<td>
|
2020-04-12 10:33:11 +02:00
|
|
|
|
<form action="/sethostname" method="post">
|
|
|
|
|
<input type="hidden" name="hardwareaddr" value="{{$l.HardwareAddr}}">
|
|
|
|
|
<input type="text" name="hostname" value="{{$l.Hostname}}">
|
|
|
|
|
</form>
|
2019-08-30 09:06:21 +02:00
|
|
|
|
{{ if (ne $l.HostnameOverride "") }}
|
|
|
|
|
<span class="hostname-override">!</span>
|
|
|
|
|
{{ end }}
|
|
|
|
|
</td>
|
2019-07-20 10:53:41 +02:00
|
|
|
|
<td class="hwaddr">{{$l.HardwareAddr}}</td>
|
|
|
|
|
<td>{{$l.Vendor}}</td>
|
2022-03-04 13:49:50 -08:00
|
|
|
|
<td>{{$l.VendorIdentifier}}</td>
|
2019-07-20 10:53:41 +02:00
|
|
|
|
<td title="{{ timefmt $l.Expiry }}">
|
2022-03-12 17:38:16 +01:00
|
|
|
|
{{ if (not (zero $l.LastACK)) }}
|
|
|
|
|
{{ timefmt $l.LastACK }}
|
|
|
|
|
{{ if $l.Active }}
|
|
|
|
|
<span class="active">active</span>
|
|
|
|
|
{{ end }}
|
2019-07-20 10:53:41 +02:00
|
|
|
|
{{ if $l.Expired }}
|
|
|
|
|
<span class="expired">expired</span>
|
|
|
|
|
{{ end }}
|
|
|
|
|
{{ end }}
|
2022-03-04 13:49:50 -08:00
|
|
|
|
|
2019-07-20 10:53:41 +02:00
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
{{ end }}
|
|
|
|
|
{{ end }}
|
|
|
|
|
|
2022-03-12 17:38:16 +01:00
|
|
|
|
<h1>Static Leases</h1>
|
2019-07-20 10:53:41 +02:00
|
|
|
|
<table cellpadding="0" cellspacing="0">
|
|
|
|
|
{{ template "table" .StaticLeases }}
|
2022-03-12 17:38:16 +01:00
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<h1>Dynamic Leases</h1>
|
|
|
|
|
<table cellpadding="0" cellspacing="0">
|
2019-07-20 10:53:41 +02:00
|
|
|
|
{{ template "table" .DynamicLeases }}
|
|
|
|
|
</table>
|
2022-03-12 17:38:16 +01:00
|
|
|
|
|
2019-07-20 10:53:41 +02:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`))
|
|
|
|
|
)
|
|
|
|
|
|
2018-06-09 15:10:11 +02:00
|
|
|
|
func loadLeases(h *dhcp4d.Handler, fn string) error {
|
|
|
|
|
b, err := ioutil.ReadFile(fn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-07-20 10:53:41 +02:00
|
|
|
|
|
2020-01-31 18:23:19 +01:00
|
|
|
|
leasesMu.Lock()
|
|
|
|
|
defer leasesMu.Unlock()
|
2018-06-09 15:25:42 +02:00
|
|
|
|
if err := json.Unmarshal(b, &leases); err != nil {
|
2018-06-09 15:10:11 +02:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
h.SetLeases(leases)
|
2018-10-23 08:49:29 +02:00
|
|
|
|
updateNonExpired(leases)
|
2019-01-06 18:02:01 +01:00
|
|
|
|
|
2020-01-31 18:25:02 +01:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var httpListeners = multilisten.NewPool()
|
|
|
|
|
|
|
|
|
|
func updateListeners() error {
|
|
|
|
|
hosts, err := gokrazy.PrivateInterfaceAddrs()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-06-12 17:53:13 -07:00
|
|
|
|
if net1, err := multilisten.IPv6Net1(*perm); err == nil {
|
2020-01-31 18:25:02 +01:00
|
|
|
|
hosts = append(hosts, net1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpListeners.ListenAndServe(hosts, func(host string) multilisten.Listener {
|
|
|
|
|
return &http.Server{Addr: net.JoinHostPort(host, "8067")}
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 19:00:51 +01:00
|
|
|
|
type srv struct {
|
|
|
|
|
errs chan error
|
|
|
|
|
leases func(newLeases []*dhcp4d.Lease, latest *dhcp4d.Lease)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newSrv(permDir string) (*srv, error) {
|
2020-12-19 13:33:46 +01:00
|
|
|
|
mayqtt := MQTT()
|
|
|
|
|
|
2020-01-31 18:25:02 +01:00
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
if err := updateListeners(); err != nil {
|
2020-01-31 19:00:51 +01:00
|
|
|
|
return nil, err
|
2020-01-31 18:25:02 +01:00
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(ch, syscall.SIGUSR1)
|
|
|
|
|
for range ch {
|
|
|
|
|
if err := updateListeners(); err != nil {
|
|
|
|
|
log.Printf("updateListeners: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2020-01-31 19:00:51 +01:00
|
|
|
|
if err := os.MkdirAll(filepath.Join(permDir, "dhcp4d"), 0755); err != nil {
|
|
|
|
|
return nil, err
|
2020-01-31 18:25:02 +01:00
|
|
|
|
}
|
|
|
|
|
errs := make(chan error)
|
|
|
|
|
ifc, err := net.InterfaceByName(*iface)
|
|
|
|
|
if err != nil {
|
2020-01-31 19:00:51 +01:00
|
|
|
|
return nil, err
|
2020-01-31 18:25:02 +01:00
|
|
|
|
}
|
2020-07-06 17:01:50 -07:00
|
|
|
|
|
|
|
|
|
serverIP, err := netconfig.LinkAddress(permDir, *iface)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-10 22:05:40 -07:00
|
|
|
|
serverIP = serverIP.To4()
|
2020-07-06 17:01:50 -07:00
|
|
|
|
var domainSearch []byte
|
|
|
|
|
domainSearch, err = dhcp4d.CompressNames("lan.", *domain)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
options := dhcp4.Options{
|
2020-08-16 18:19:34 -07:00
|
|
|
|
dhcp4.OptionSubnetMask: []byte{255, 255, 255, 0},
|
|
|
|
|
dhcp4.OptionRouter: []byte(serverIP),
|
|
|
|
|
dhcp4.OptionDomainNameServer: []byte(serverIP),
|
|
|
|
|
dhcp4.OptionNetworkTimeProtocolServers: []byte(serverIP),
|
|
|
|
|
dhcp4.OptionDomainName: []byte(*domain),
|
|
|
|
|
dhcp4.OptionDomainSearch: domainSearch,
|
2020-07-06 17:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handler, err := dhcp4d.NewHandler(permDir, ifc, *iface, nil, options)
|
2020-01-31 18:25:02 +01:00
|
|
|
|
if err != nil {
|
2020-01-31 19:00:51 +01:00
|
|
|
|
return nil, err
|
2020-01-31 18:25:02 +01:00
|
|
|
|
}
|
2020-01-31 19:00:51 +01:00
|
|
|
|
if err := loadLeases(handler, filepath.Join(permDir, "dhcp4d/leases.json")); err != nil {
|
|
|
|
|
return nil, err
|
2020-01-31 18:25:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 10:33:11 +02:00
|
|
|
|
http.HandleFunc("/sethostname", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
http.Error(w, "want POST", http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hwaddr := r.FormValue("hardwareaddr")
|
|
|
|
|
if hwaddr == "" {
|
|
|
|
|
http.Error(w, "missing hardwareaddr parameter", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
hostname := r.FormValue("hostname")
|
|
|
|
|
if hostname == "" {
|
|
|
|
|
http.Error(w, "missing hostname parameter", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-23 09:32:42 +01:00
|
|
|
|
if err := handler.SetHostname(hwaddr, hostname); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-12 10:33:11 +02:00
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
|
})
|
|
|
|
|
|
2020-01-31 18:30:18 +01:00
|
|
|
|
http.HandleFunc("/lease/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
hostname := strings.TrimPrefix(r.URL.Path, "/lease/")
|
|
|
|
|
if hostname == "" {
|
|
|
|
|
http.Error(w, "syntax: /lease/<hostname>", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
leasesMu.Lock()
|
|
|
|
|
defer leasesMu.Unlock()
|
|
|
|
|
var lease *dhcp4d.Lease
|
|
|
|
|
for _, l := range leases {
|
|
|
|
|
if l.Hostname != hostname {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
lease = l
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if lease == nil {
|
|
|
|
|
http.Error(w, "no lease found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(lease)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-06 18:48:19 +01:00
|
|
|
|
w.Header().Set("X-Lease-Active", fmt.Sprint(lease.Expiry.After(time.Now().Add(handler.LeasePeriod*2/3))))
|
2020-01-31 18:30:18 +01:00
|
|
|
|
if _, err := io.Copy(w, bytes.NewReader(b)); err != nil {
|
|
|
|
|
log.Printf("/lease/%s: %v", hostname, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2019-01-06 18:02:01 +01:00
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2019-02-19 07:50:39 +01:00
|
|
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "bad request", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); ip.IsLoopback() && xff != "" {
|
|
|
|
|
ip = net.ParseIP(xff)
|
|
|
|
|
}
|
|
|
|
|
if !gokrazy.IsInPrivateNet(ip) {
|
|
|
|
|
http.Error(w, fmt.Sprintf("access from %v forbidden", ip), http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-07-20 10:53:41 +02:00
|
|
|
|
|
|
|
|
|
type tmplLease struct {
|
|
|
|
|
dhcp4d.Lease
|
|
|
|
|
|
|
|
|
|
Vendor string
|
|
|
|
|
Expired bool
|
|
|
|
|
Static bool
|
2022-03-12 17:38:16 +01:00
|
|
|
|
Active bool
|
2019-07-20 10:53:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 18:23:19 +01:00
|
|
|
|
leasesMu.Lock()
|
|
|
|
|
defer leasesMu.Unlock()
|
2019-07-20 10:53:41 +02:00
|
|
|
|
static := make([]tmplLease, 0, len(leases))
|
|
|
|
|
dynamic := make([]tmplLease, 0, len(leases))
|
2022-03-12 17:38:16 +01:00
|
|
|
|
now := time.Now()
|
2019-07-20 10:53:41 +02:00
|
|
|
|
tl := func(l *dhcp4d.Lease) tmplLease {
|
|
|
|
|
return tmplLease{
|
|
|
|
|
Lease: *l,
|
|
|
|
|
Vendor: ouiDB.Lookup(l.HardwareAddr[:8]),
|
2022-03-12 17:38:16 +01:00
|
|
|
|
Expired: l.Expired(now),
|
|
|
|
|
Active: l.Active(now),
|
2019-07-20 10:53:41 +02:00
|
|
|
|
Static: l.Expiry.IsZero(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-06 18:02:01 +01:00
|
|
|
|
for _, l := range leases {
|
2019-07-20 10:53:41 +02:00
|
|
|
|
if l.Expiry.IsZero() {
|
|
|
|
|
static = append(static, tl(l))
|
|
|
|
|
} else {
|
|
|
|
|
dynamic = append(dynamic, tl(l))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sort.Slice(static, func(i, j int) bool {
|
|
|
|
|
return static[i].Num < static[j].Num
|
|
|
|
|
})
|
|
|
|
|
sort.Slice(dynamic, func(i, j int) bool {
|
|
|
|
|
return !dynamic[i].Expiry.Before(dynamic[j].Expiry)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err := leasesTmpl.Execute(w, struct {
|
|
|
|
|
StaticLeases []tmplLease
|
|
|
|
|
DynamicLeases []tmplLease
|
|
|
|
|
}{
|
|
|
|
|
StaticLeases: static,
|
|
|
|
|
DynamicLeases: dynamic,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
2019-01-06 18:02:01 +01:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2019-07-20 10:53:41 +02:00
|
|
|
|
handler.Leases = func(newLeases []*dhcp4d.Lease, latest *dhcp4d.Lease) {
|
2020-01-31 18:23:19 +01:00
|
|
|
|
leasesMu.Lock()
|
|
|
|
|
defer leasesMu.Unlock()
|
2019-07-20 10:53:41 +02:00
|
|
|
|
leases = newLeases
|
2018-06-24 11:56:39 +02:00
|
|
|
|
log.Printf("DHCPACK %+v", latest)
|
2018-05-27 17:30:42 +02:00
|
|
|
|
b, err := json.Marshal(leases)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs <- err
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-07-20 10:49:53 +02:00
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
if err := json.Indent(&out, b, "", "\t"); err == nil {
|
|
|
|
|
b = out.Bytes()
|
|
|
|
|
}
|
2020-05-09 16:58:57 -04:00
|
|
|
|
if err := renameio.WriteFile(filepath.Join(permDir, "dhcp4d/leases.json"), b, 0644); err != nil {
|
2018-05-27 17:30:42 +02:00
|
|
|
|
errs <- err
|
|
|
|
|
}
|
2018-10-23 08:49:29 +02:00
|
|
|
|
updateNonExpired(leases)
|
2020-06-12 17:53:13 -07:00
|
|
|
|
if err := notify.Process(path.Join(path.Dir(os.Args[0]), "/dnsd"), syscall.SIGUSR1); err != nil {
|
2018-05-27 17:30:42 +02:00
|
|
|
|
log.Printf("notifying dnsd: %v", err)
|
|
|
|
|
}
|
2020-12-19 13:33:46 +01:00
|
|
|
|
|
|
|
|
|
// Publish the DHCP lease as JSON to MQTT, if configured:
|
|
|
|
|
leaseVal := struct {
|
2022-05-01 18:56:51 -07:00
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
|
HardwareAddr string `json:"hardware_addr"`
|
|
|
|
|
Expiration time.Time `json:"expiration"`
|
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
|
VendorIdentifier string `json:"vendor_identifier"`
|
2020-12-19 13:33:46 +01:00
|
|
|
|
}{
|
2022-05-01 18:56:51 -07:00
|
|
|
|
Addr: latest.Addr.String(),
|
|
|
|
|
HardwareAddr: latest.HardwareAddr,
|
|
|
|
|
Expiration: latest.Expiry.In(time.UTC),
|
2022-10-19 19:03:07 -07:00
|
|
|
|
Start: latest.LastACK.In(time.UTC),
|
2022-05-01 18:56:51 -07:00
|
|
|
|
VendorIdentifier: latest.VendorIdentifier,
|
2020-12-19 13:33:46 +01:00
|
|
|
|
}
|
|
|
|
|
leaseJSON, err := json.Marshal(leaseVal)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2021-01-07 22:52:58 +01:00
|
|
|
|
// MQTT requires valid UTF-8 and some brokers don’t cope well with
|
|
|
|
|
// invalid UTF-8: https://github.com/fhmq/hmq/issues/104
|
|
|
|
|
identifier := strings.ToValidUTF8(latest.Hostname, "")
|
2021-06-03 21:05:33 +02:00
|
|
|
|
// Some MQTT clients (e.g. mosquitto_pub) don’t cope well with topic
|
|
|
|
|
// names containing non-printable characters (see also
|
|
|
|
|
// https://twitter.com/zekjur/status/1347295676909158400):
|
|
|
|
|
identifier = strings.Map(func(r rune) rune {
|
|
|
|
|
if unicode.IsPrint(r) {
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}, identifier)
|
2020-12-19 13:33:46 +01:00
|
|
|
|
if identifier == "" {
|
|
|
|
|
identifier = latest.HardwareAddr
|
|
|
|
|
}
|
2020-12-31 16:42:12 +01:00
|
|
|
|
select {
|
|
|
|
|
case mayqtt <- PublishRequest{
|
2020-12-19 13:33:46 +01:00
|
|
|
|
Topic: "router7/dhcp4d/lease/" + identifier,
|
|
|
|
|
Retained: true,
|
|
|
|
|
Payload: leaseJSON,
|
2020-12-31 16:42:12 +01:00
|
|
|
|
}:
|
|
|
|
|
default:
|
|
|
|
|
// Channel not ready? skip publishing this lease (best-effort).
|
|
|
|
|
// This is an easy way of breaking circular dependencies between
|
|
|
|
|
// MQTT broker and DHCP server, and avoiding deadlocks.
|
2020-12-19 13:33:46 +01:00
|
|
|
|
}
|
2018-05-27 17:30:42 +02:00
|
|
|
|
}
|
2019-07-20 10:50:30 +02:00
|
|
|
|
conn, err := conn.NewUDP4BoundListener(*iface, ":67")
|
2018-05-27 17:30:42 +02:00
|
|
|
|
if err != nil {
|
2020-01-31 19:00:51 +01:00
|
|
|
|
return nil, err
|
2018-05-27 17:30:42 +02:00
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
errs <- dhcp4.Serve(conn, handler)
|
|
|
|
|
}()
|
2020-01-31 19:00:51 +01:00
|
|
|
|
return &srv{
|
|
|
|
|
errs,
|
|
|
|
|
handler.Leases,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *srv) run(ctx context.Context) error {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
case err := <-s.errs:
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-05-27 17:30:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// TODO: drop privileges, run as separate uid?
|
|
|
|
|
flag.Parse()
|
2020-06-12 17:53:13 -07:00
|
|
|
|
srv, err := newSrv(*perm)
|
2020-01-31 19:00:51 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := srv.run(context.Background()); err != nil {
|
2018-05-27 17:30:42 +02:00
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|