2020-10-16 17:27:44 +02:00
|
|
|
package captiveportal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-10-18 15:28:34 +02:00
|
|
|
"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) {
|
2020-10-18 15:28:34 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
|
2020-10-16 17:27:44 +02:00
|
|
|
matches, os, err := matcher.Match(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
2020-10-18 15:28:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-18 15:28:34 +02:00
|
|
|
id, err := s.identifier.Identify(r)
|
2020-10-16 17:27:44 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, ErrClientIdentificationFailed.Error()))
|
|
|
|
}
|
|
|
|
|
2020-10-18 15:28:34 +02:00
|
|
|
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) {
|
2020-10-18 15:28:34 +02:00
|
|
|
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) {
|
2020-10-18 15:28:34 +02:00
|
|
|
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
|
2020-10-18 15:28:34 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|