-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
132 lines (108 loc) · 2.25 KB
/
backend.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
package main
import (
"context"
"encoding/json"
"errors"
"net"
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"
)
type Backend struct {
URL *url.URL
}
type BackendList []*Backend
var backendClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 3 * time.Second,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 4,
MaxConnsPerHost: 0,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 2 * time.Second,
MaxResponseHeaderBytes: 2 * 1024,
ForceAttemptHTTP2: true,
},
}
var (
ErrNotAllowed = errors.New("not allowed")
)
func (b *Backend) String() string {
return b.URL.String()
}
func (b *Backend) MarshalJSON() ([]byte, error) {
return json.Marshal(b.String())
}
func (b *Backend) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
parsed, err := url.Parse(str)
if err != nil {
return err
}
b.URL = parsed
return nil
}
func (b *Backend) Do(ctx context.Context, r *http.Request) error {
req, _ := http.NewRequestWithContext(ctx, "GET", "", nil)
req.URL = &url.URL{
Scheme: b.URL.Scheme,
Opaque: b.URL.Opaque,
User: b.URL.User,
Host: b.URL.Host,
Path: b.URL.Path,
RawPath: b.URL.RawPath,
RawQuery: r.URL.RawQuery,
}
// Copy headers
headers := []string{
"X-Session-Id",
"X-Real-Ip",
"X-Real-Path",
"X-Real-Origin",
"X-Real-Ua",
}
for _, header := range headers {
if v := r.Header.Get(header); v != "" {
req.Header.Set(header, v)
}
}
response, err := backendClient.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return ErrNotAllowed
}
return nil
}
func (bl BackendList) Check(r *http.Request) bool {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
var (
allow atomic.Bool
wg sync.WaitGroup
)
for _, backend := range bl {
wg.Add(1)
go func(ctx context.Context, b *Backend) {
defer wg.Done()
if err := b.Do(ctx, r); err == nil {
allow.Store(true)
cancel()
}
}(ctx, backend)
}
wg.Wait()
return allow.Load()
}