generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
57 lines (47 loc) · 1.35 KB
/
plugin.go
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
// Package plugin contains the Traefik plugin for adding headers based on the
// TLS information
package plugin
import (
"context"
"crypto/tls"
"errors"
"net/http"
)
var errMissingHeaderConfig = errors.New("missing header config: must set headers.cipher")
// Config the plugin configuration.
type Config struct {
Headers ConfigHeaders `json:"headers,omitempty"`
}
// ConfigHeaders defines the headers to use for the different values.
type ConfigHeaders struct {
Cipher string `json:"cipher,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
Headers: ConfigHeaders{},
}
}
// TLSHeadersPlugin is the main handler model for this Traefik plugin.
type TLSHeadersPlugin struct {
next http.Handler
headers ConfigHeaders
name string
}
// New created a new TLSHeadersPlugin.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.Headers == (ConfigHeaders{}) {
return nil, errMissingHeaderConfig
}
return &TLSHeadersPlugin{
headers: config.Headers,
next: next,
name: name,
}, nil
}
func (a *TLSHeadersPlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if a.headers.Cipher != "" && req.TLS != nil {
req.Header.Set(a.headers.Cipher, tls.CipherSuiteName(req.TLS.CipherSuite))
}
a.next.ServeHTTP(rw, req)
}