go-captiveportal/middleware.go

82 lines
1.5 KiB
Go
Raw Normal View History

2020-10-16 17:27:44 +02:00
package captiveportal
import (
"net/http"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
2020-10-16 17:27:44 +02:00
)
func (s *Service) Middleware() func(next http.Handler) http.Handler {
registry := s.registry
matcher := s.options.Matcher
liar := s.options.Liar
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2020-10-16 17:27:44 +02:00
matches, os, err := matcher.Match(r)
if err != nil {
panic(errors.WithStack(err))
}
logger.Debug(
ctx, "applied captive portal matching",
logger.F("os", os),
logger.F("url", r.URL.String()),
logger.F("host", r.Host),
logger.F("matches", matches),
)
2020-10-16 17:27:44 +02:00
if !matches {
next.ServeHTTP(w, r)
return
}
id, err := s.identifier.Identify(r)
2020-10-16 17:27:44 +02:00
if err != nil {
panic(errors.Wrap(err, ErrClientIdentificationFailed.Error()))
}
logger.Debug(
ctx, "identified captive portal client",
logger.F("remoteAddr", r.RemoteAddr),
logger.F("id", id),
)
2020-10-16 17:27:44 +02:00
registry.Touch(id, os)
if registry.IsLying(id) {
logger.Debug(
ctx, "lying to captive portal client",
logger.F("remoteAddr", r.RemoteAddr),
logger.F("id", id),
)
2020-10-16 17:27:44 +02:00
liar.Handle(os, w, r)
return
}
if registry.IsCaptive(id) {
logger.Debug(
ctx, "capture captive portal client",
logger.F("remoteAddr", r.RemoteAddr),
logger.F("id", id),
)
2020-10-16 17:27:44 +02:00
// Redirect to configured URL
http.Redirect(w, r, s.redirectURL, http.StatusTemporaryRedirect)
2020-10-16 17:27:44 +02:00
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}