-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflarewarp.go
161 lines (145 loc) · 3.58 KB
/
cloudflarewarp.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Package cloudflarewarp Traefik Plugin.
package cloudflarewarp
import (
"context"
"encoding/json"
"net"
"net/http"
"github.com/pyksid/cloudflarewarp/ips"
)
const (
xRealIP = "X-Real-Ip"
xCfTrusted = "X-Is-Trusted"
xForwardFor = "X-Forwarded-For"
xForwardProto = "X-Forwarded-Proto"
cfConnectingIP = "CF-Connecting-IP"
cfVisitor = "CF-Visitor"
)
// Config the plugin configuration.
type Config struct {
TrustIP []string `json:"trustip,omitempty"`
DisableDefaultCFIPs bool `json:"disabledefaultcfips,omitempty"`
}
// TrustResult for Trust IP test result.
type TrustResult struct {
isFatal bool
isError bool
trusted bool
directIP string
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
TrustIP: []string{},
DisableDefaultCFIPs: false,
}
}
// RealIPOverWriter is a plugin that overwrite true IP.
type RealIPOverWriter struct {
next http.Handler
name string
TrustIP []*net.IPNet
}
// CFVisitorHeader definition for the header value.
type CFVisitorHeader struct {
Scheme string `json:"scheme"`
}
// New created a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
ipOverWriter := &RealIPOverWriter{
next: next,
name: name,
}
if config.TrustIP != nil {
for _, v := range config.TrustIP {
_, trustip, err := net.ParseCIDR(v)
if err != nil {
return nil, err
}
ipOverWriter.TrustIP = append(ipOverWriter.TrustIP, trustip)
}
}
if !config.DisableDefaultCFIPs {
for _, v := range ips.CFIPs() {
_, trustip, err := net.ParseCIDR(v)
if err != nil {
return nil, err
}
ipOverWriter.TrustIP = append(ipOverWriter.TrustIP, trustip)
}
}
return ipOverWriter, nil
}
func (r *RealIPOverWriter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
trustResult := r.trust(req.RemoteAddr)
if trustResult.isFatal {
http.Error(rw, "Unknown source", http.StatusInternalServerError)
return
}
if trustResult.isError {
http.Error(rw, "Unknown source", http.StatusBadRequest)
return
}
if trustResult.directIP == "" {
http.Error(rw, "Unknown source", http.StatusUnprocessableEntity)
return
}
if trustResult.trusted {
if req.Header.Get(cfVisitor) != "" {
var cfVisitorValue CFVisitorHeader
if err := json.Unmarshal([]byte(req.Header.Get(cfVisitor)), &cfVisitorValue); err != nil {
req.Header.Set(xCfTrusted, "danger")
req.Header.Del(cfVisitor)
req.Header.Del(cfConnectingIP)
r.next.ServeHTTP(rw, req)
return
}
req.Header.Set(xForwardProto, cfVisitorValue.Scheme)
}
req.Header.Set(xCfTrusted, "yes")
req.Header.Set(xForwardFor, req.Header.Get(cfConnectingIP))
req.Header.Set(xRealIP, req.Header.Get(cfConnectingIP))
} else {
req.Header.Set(xCfTrusted, "no")
req.Header.Set(xRealIP, trustResult.directIP)
req.Header.Del(cfVisitor)
req.Header.Del(cfConnectingIP)
}
r.next.ServeHTTP(rw, req)
}
func (r *RealIPOverWriter) trust(s string) *TrustResult {
temp, _, err := net.SplitHostPort(s)
if err != nil {
return &TrustResult{
isFatal: true,
isError: true,
trusted: false,
directIP: "",
}
}
ip := net.ParseIP(temp)
if ip == nil {
return &TrustResult{
isFatal: false,
isError: true,
trusted: false,
directIP: "",
}
}
for _, network := range r.TrustIP {
if network.Contains(ip) {
return &TrustResult{
isFatal: false,
isError: false,
trusted: true,
directIP: ip.String(),
}
}
}
return &TrustResult{
isFatal: false,
isError: false,
trusted: false,
directIP: ip.String(),
}
}