-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
192 lines (166 loc) · 3.93 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Package plugindemo a demo plugin.
package traefik_ip2region
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
)
// searcher cached
var searcher *xdb.Searcher
// Headers part of the configuration
type Headers struct {
Country string `yaml:"country"`
Province string `yaml:"province"`
City string `yaml:"city"`
ISP string `yaml:"isp"`
}
// Config the plugin configuration.
type Config struct {
DBPath string `yaml:"dbPath,omitempty"`
Headers *Headers `yaml:"headers"`
Ban Rules `yaml:"ban"`
Whitelist Rules `yaml:"whitelist"`
}
// Rules
type Rules struct {
Enabled bool `yaml:"enabled"`
Country []string `yaml:"country"`
Province []string `yaml:"province"`
City []string `yaml:"city"`
UserAgent []string `yaml:"userAgent"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
DBPath: "ip2region.xdb",
Headers: &Headers{Country: "X-Ip2region-Country", Province: "X-Ip2region-Province", City: "X-Ip2region-City", ISP: "X-Ip2region-Isp"},
}
}
// TraefikIp2Region a Demo plugin.
type TraefikIp2Region struct {
Next http.Handler
Name string
Headers *Headers
Ban Rules
Whitelist Rules
}
// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
err := loadXdb(config.DBPath)
if err != nil {
return nil, err
}
return &TraefikIp2Region{
Next: next,
Name: name,
Headers: config.Headers,
Ban: config.Ban,
Whitelist: config.Whitelist,
}, nil
}
func (a *TraefikIp2Region) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ipStr := req.RemoteAddr
tmp, _, err := net.SplitHostPort(ipStr)
if err == nil {
ipStr = tmp
}
var data []string = make([]string, 5)
// 国家|区域|省份|城市|ISP
region, err := searcher.SearchByStr(ipStr)
if err == nil {
data = strings.Split(region, "|")
if len(data) != 5 {
data = make([]string, 5)
}
}
// add headers
// 国家|区域|省份|城市|ISP
req.Header.Add(a.Headers.Country, data[0])
req.Header.Add(a.Headers.Province, data[2])
req.Header.Add(a.Headers.City, data[3])
req.Header.Add(a.Headers.ISP, data[4])
// Ban
if a.Ban.Enabled {
// country
for _, v := range a.Ban.Country {
if v == data[0] {
rw.WriteHeader(http.StatusForbidden)
return
}
}
// province
for _, v := range a.Ban.Province {
if v == data[2] {
rw.WriteHeader(http.StatusForbidden)
return
}
}
// city
for _, v := range a.Ban.City {
if v == data[3] {
rw.WriteHeader(http.StatusForbidden)
return
}
}
// UserAgent
for _, v := range a.Ban.UserAgent {
if v == req.UserAgent() {
rw.WriteHeader(http.StatusForbidden)
return
}
}
}
// Whitelist
if a.Whitelist.Enabled {
// country
for _, v := range a.Whitelist.Country {
if v == data[0] {
a.Next.ServeHTTP(rw, req)
return
}
}
// province
for _, v := range a.Whitelist.Province {
if v == data[2] {
a.Next.ServeHTTP(rw, req)
return
}
}
// city
for _, v := range a.Whitelist.City {
if v == data[3] {
a.Next.ServeHTTP(rw, req)
return
}
}
// UserAgent
for _, v := range a.Whitelist.UserAgent {
if v == req.UserAgent() {
a.Next.ServeHTTP(rw, req)
return
}
}
// if the ip is not in the whitelist, return 403
rw.WriteHeader(http.StatusForbidden)
return
}
a.Next.ServeHTTP(rw, req)
}
func loadXdb(dbPath string) error {
if searcher == nil {
// 1、从 dbPath 加载整个 xdb 到内存
cBuff, err := xdb.LoadContentFromFile(dbPath)
if err != nil {
return fmt.Errorf("failed to load content from `%s`: %s", dbPath, err)
}
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
searcher, err = xdb.NewWithBuffer(cBuff)
if err != nil {
return fmt.Errorf("failed to create searcher with content: %s", err)
}
}
return nil
}