-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatadome.go
More file actions
103 lines (88 loc) · 2.95 KB
/
datadome.go
File metadata and controls
103 lines (88 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package module_traefik_package
import (
"context"
"fmt"
"net/http"
modulego "github.com/datadome/module-go-package/v2"
)
const (
ModuleName = "Traefik"
ModuleVersion = "1.0.1"
)
type Config struct {
ServerSideKey string `json:"serverSideKey"`
EnableGraphQLSupport *bool `json:"enableGraphQLSupport,omitempty"`
EnableReferrerRestoration *bool `json:"enableReferrerRestoration,omitempty"`
Endpoint *string `json:"endpoint,omitempty"`
MaximumBodySize *int `json:"maximumBodySize,omitempty"`
Timeout *int `json:"timeout,omitempty"`
UrlPatternExclusion *string `json:"urlPatternExclusion,omitempty"`
UrlPatternInclusion *string `json:"urlPatternInclusion,omitempty"`
UseXForwardedHost *bool `json:"useXForwardedHost,omitempty"`
}
func CreateConfig() *Config {
return &Config{}
}
type DataDomePlugin struct {
next http.Handler
name string
datadomeClient *modulego.Client
}
func loadOptionsFromConfig(config *Config) []modulego.Option {
var options []modulego.Option
if config.EnableGraphQLSupport != nil {
options = append(options, modulego.WithGraphQLSupport(*config.EnableGraphQLSupport))
}
if config.EnableReferrerRestoration != nil {
options = append(options, modulego.WithReferrerRestoration(*config.EnableReferrerRestoration))
}
if config.Endpoint != nil {
options = append(options, modulego.WithEndpoint(*config.Endpoint))
}
if config.MaximumBodySize != nil {
options = append(options, modulego.WithMaximumBodySize(*config.MaximumBodySize))
}
if config.Timeout != nil {
options = append(options, modulego.WithTimeout(*config.Timeout))
}
if config.UrlPatternExclusion != nil {
options = append(options, modulego.WithUrlPatternExclusion(*config.UrlPatternExclusion))
}
if config.UrlPatternInclusion != nil {
options = append(options, modulego.WithUrlPatternInclusion(*config.UrlPatternInclusion))
}
if config.UseXForwardedHost != nil {
options = append(options, modulego.WithXForwardedHost(*config.UseXForwardedHost))
}
return options
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
serverSideKey := config.ServerSideKey
options := loadOptionsFromConfig(config)
ddClient, err := modulego.NewClient(
serverSideKey,
options...,
)
if err != nil {
return nil, fmt.Errorf("failed to create DataDome client: %w", err)
}
ddClient.ModuleName = ModuleName
ddClient.ModuleVersion = ModuleVersion
return &DataDomePlugin{
next: next,
name: name,
datadomeClient: ddClient,
}, nil
}
func (m *DataDomePlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Example of payload, values should come from the req
isBlocked, err := m.datadomeClient.DatadomeProtect(rw, req)
if err != nil {
fmt.Println("error when requesting DataDome", err)
}
if isBlocked {
fmt.Println("request blocked by DataDome")
return
}
m.next.ServeHTTP(rw, req)
}