go-captiveportal/arp/identifier.go

43 lines
676 B
Go

package arp
import (
"context"
"net/http"
"strings"
"github.com/pkg/errors"
)
type Identifier struct {
arp *Table
}
func (i *Identifier) Watch(ctx context.Context, config WatchConfig) error {
return i.arp.Watch(ctx, config)
}
func (i *Identifier) Identify(r *http.Request) (string, error) {
ip := strings.SplitN(r.RemoteAddr, ":", 2)[0]
mac, err := i.arp.FindMACByIP(ip)
if err != nil {
if errors.Is(err, ErrNotFound) {
return "", nil
}
return "", errors.WithStack(err)
}
return mac, nil
}
func (i *Identifier) OnOffline(fn func(id string)) {
i.arp.OnOffline(fn)
}
func NewIdentifier() *Identifier {
return &Identifier{
arp: NewTable(),
}
}