go-captiveportal/service.go

77 lines
1.3 KiB
Go
Raw Normal View History

2020-10-16 17:27:44 +02:00
package captiveportal
import (
"net/http"
"github.com/pkg/errors"
)
type Service struct {
captivePortalURL string
options *Options
registry *Registry
}
func (s *Service) ClientID(r *http.Request) (string, error) {
id, err := s.options.Identifier.Identify(r)
if err != nil {
return "", errors.Wrap(err, ErrClientIdentificationFailed.Error())
}
return id, nil
}
func (s *Service) IsCaptive(r *http.Request) (bool, error) {
id, err := s.ClientID(r)
if err != nil {
return false, errors.WithStack(err)
}
return s.registry.IsCaptive(id), nil
}
func (s *Service) Release(r *http.Request) error {
id, err := s.ClientID(r)
if err != nil {
return errors.WithStack(err)
}
s.registry.Release(id)
return nil
}
func (s *Service) Lie(r *http.Request) error {
id, err := s.ClientID(r)
if err != nil {
return errors.WithStack(err)
}
s.registry.Lie(id)
return nil
}
func (s *Service) ClientOS(r *http.Request) (OS, error) {
id, err := s.ClientID(r)
if err != nil {
return OSUnknown, errors.WithStack(err)
}
return s.registry.ClientOS(id), nil
}
func New(captivePortalURL string, opts ...OptionsFunc) *Service {
options := DefaultOptions()
for _, o := range opts {
o(options)
}
return &Service{
captivePortalURL: captivePortalURL,
options: options,
registry: NewRegistry(),
}
}