Skip to content

Commit

Permalink
user agent up
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Bialy committed Feb 22, 2023
1 parent dbc99a9 commit ccfc67d
Show file tree
Hide file tree
Showing 20 changed files with 1,227 additions and 725 deletions.
12 changes: 10 additions & 2 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ services:
command:
- "--log.level=DEBUG"
- "--accesslog"
- "--accesslog.filepath=/var/log/traefik/access.log"
# - "--accesslog.filepath=/var/log/traefik/access.log"
# - --accesslog.format=json
- --accesslog.fields.defaultmode=keep
- --accesslog.fields.names.ClientUsername=drop
- --accesslog.fields.headers.defaultmode=keep
- --accesslog.fields.headers.names.User-Agent=redact
- --accesslog.fields.headers.names.Authorization=drop
- --accesslog.fields.headers.names.Content-Type=keep
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
Expand All @@ -27,7 +34,8 @@ services:
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.middlewares.cfdevicedetect.plugin.cfdevicedetect.autodetect=true"
- "traefik.http.middlewares.cfdevicedetect.plugin.traefik-cf-device-detector.autodetect=true"

- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.middlewares=cfdevicedetect@docker"
- "traefik.http.routers.whoami.entrypoints=web"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/davewhit3/traefik-cf-device-detector

go 1.19

require github.com/mileusna/useragent v1.2.1
require github.com/mssola/useragent v1.0.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/mileusna/useragent v1.2.1 h1:p3RJWhi3LfuI6BHdddojREyK3p6qX67vIfOVMnUIVr0=
github.com/mileusna/useragent v1.2.1/go.mod h1:3d8TOmwL/5I8pJjyVDteHtgDGcefrFUX4ccGOMKNYYc=
github.com/mssola/useragent v1.0.0 h1:WRlDpXyxHDNfvZaPEut5Biveq86Ze4o4EMffyMxmH5o=
github.com/mssola/useragent v1.0.0/go.mod h1:hz9Cqz4RXusgg1EdI4Al0INR62kP7aPSRNHnpU+b85Y=
18 changes: 13 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package traefik_cf_device_detector

import (
"context"
"log"
"net/http"
"os"
"strconv"

"github.com/mileusna/useragent"
"github.com/mssola/useragent"
)

const (
Expand Down Expand Up @@ -36,24 +38,30 @@ func CreateConfig() *Config {

// CfDeviceDetector a CfDeviceDetector plugin.
type CfDeviceDetector struct {
log *log.Logger
next http.Handler
name string
}

// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
logger := log.New(os.Stdout, "[CfDeviceDetector] ", 0)

logger.Printf("configured!")

return &CfDeviceDetector{
log: logger,
next: next,
name: name,
}, nil
}

func (mw *CfDeviceDetector) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ua := useragent.Parse(req.Header.Get(UserAgentHeader))
ua := useragent.New(req.Header.Get(UserAgentHeader))

req.Header.Set(DeviceIsMobileHeader, strconv.FormatBool(ua.Mobile))
req.Header.Set(DeviceIsDesktopHeader, strconv.FormatBool(ua.Desktop))
req.Header.Set(DeviceIsTabletHeader, strconv.FormatBool(ua.Tablet))
req.Header.Set(DeviceIsMobileHeader, strconv.FormatBool(ua.Mobile()))
req.Header.Set(DeviceIsDesktopHeader, strconv.FormatBool(!ua.Mobile()))
req.Header.Set(DeviceIsTabletHeader, strconv.FormatBool(false))
req.Header.Set(DeviceIsSmartTVHeader, strconv.FormatBool(false))

mw.next.ServeHTTP(rw, req)
Expand Down
4 changes: 0 additions & 4 deletions vendor/github.com/mileusna/useragent/.gitignore

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/mileusna/useragent/LICENSE.md

This file was deleted.

99 changes: 0 additions & 99 deletions vendor/github.com/mileusna/useragent/README.md

This file was deleted.

81 changes: 0 additions & 81 deletions vendor/github.com/mileusna/useragent/is.go

This file was deleted.

Loading

0 comments on commit ccfc67d

Please sign in to comment.