forked from xtrafrancyz/vk-proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.go
335 lines (298 loc) · 8.77 KB
/
proxy.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bytes"
"crypto/tls"
"log"
"net/url"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
"github.com/vtosters/vk-proxy/bytefmt"
"github.com/vtosters/vk-proxy/replacer"
)
const (
readBufferSize = 8192
byteBufferPoolSetupRounds = 42000 // == bytebufferpool.calibrateCallsThreshold
byteBufferPoolSetupSize = 2097152 // 2**21
)
func init() {
// Настройка размера буфера для ответа
r := fasthttp.Response{}
for i := 0; i <= byteBufferPoolSetupRounds; i++ {
r.SetBodyString("")
if b := r.Body(); cap(b) != byteBufferPoolSetupSize {
r.SwapBody(make([]byte, byteBufferPoolSetupSize))
} else {
r.SwapBody(b[:cap(b)])
}
r.ResetBody()
}
// Настройка размера реплейсера
for i := 0; i <= byteBufferPoolSetupRounds; i++ {
b := replacer.AcquireBuffer()
if cap(b.B) != byteBufferPoolSetupSize {
b.B = make([]byte, byteBufferPoolSetupSize)
} else {
b.B = b.B[:cap(b.B)]
}
replacer.ReleaseBuffer(b)
}
}
var (
gzip = []byte("gzip")
vkProxyName = []byte("vk-proxy")
replaceContextPool = sync.Pool{New: func() interface{} {
return &replacer.ReplaceContext{}
}}
)
type ProxyConfig struct {
ReduceMemoryUsage bool
BaseDomain string
BaseStaticDomain string
LogVerbosity int
GzipUpstream bool
FilterFeed bool
AddUselessProxyMessage bool
}
type Proxy struct {
client *fasthttp.Client
server *fasthttp.Server
replacer *replacer.Replacer
tracker *tracker
config ProxyConfig
}
func NewProxy(config ProxyConfig) *Proxy {
p := &Proxy{
client: &fasthttp.Client{
Name: "vk-proxy",
ReadBufferSize: readBufferSize,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
ReadTimeout: 30 * time.Second,
WriteTimeout: 10 * time.Second,
DisablePathNormalizing: true,
NoDefaultUserAgentHeader: true,
MaxIdemponentCallAttempts: 0,
RetryIf: func(request *fasthttp.Request) bool {
return false
},
},
replacer: &replacer.Replacer{
ProxyBaseDomain: config.BaseDomain,
ProxyStaticDomain: config.BaseStaticDomain,
FilterFeed: config.FilterFeed,
AddUselessProxyMessage: config.AddUselessProxyMessage,
},
tracker: &tracker{
uniqueUsers: make(map[string]bool),
},
config: config,
}
p.server = &fasthttp.Server{
Handler: p.handleProxy,
ReduceMemoryUsage: config.ReduceMemoryUsage,
ReadBufferSize: readBufferSize,
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
IdleTimeout: 1 * time.Minute,
NoDefaultContentType: true,
DisablePreParseMultipartForm: true,
Name: "vk-proxy",
}
p.tracker.server = p.server
if p.config.LogVerbosity > 0 {
p.tracker.start()
}
return p
}
func (p *Proxy) ListenTCP(host string) error {
log.Printf("Starting server on http://%s", host)
return p.server.ListenAndServe(host)
}
func (p *Proxy) ListenUnix(path string) error {
log.Printf("Starting server on http://unix:%s", path)
return p.server.ListenAndServeUNIX(path, 0777)
}
func (p *Proxy) handleProxy(ctx *fasthttp.RequestCtx) {
defer func() {
if r := recover(); r != nil {
log.Printf("panic when proxying the request: %s%s", r, debug.Stack())
ctx.Error("500 Internal Server Error", 500)
}
}()
start := time.Now()
replaceContext := replaceContextPool.Get().(*replacer.ReplaceContext)
replaceContext.RequestCtx = ctx
replaceContext.Method = ctx.Method()
replaceContext.OriginHost = string(ctx.Request.Host())
if !p.prepareProxyRequest(ctx, replaceContext) {
ctx.Error("400 Bad Request", 400)
return
}
if (replaceContext.Host == "api.vk.com" || replaceContext.Host == "api.vk.ru") &&
(replaceContext.Path == "/away" || replaceContext.Path == "/away.php") {
p.handleAway(ctx)
return
}
err := p.client.Do(&ctx.Request, &ctx.Response)
if err == nil {
err = p.processProxyResponse(ctx, replaceContext)
}
replaceContext.Reset()
replaceContextPool.Put(replaceContext)
elapsed := time.Since(start).Round(100 * time.Microsecond)
if err != nil {
log.Printf("%s %s %s%s error: %s", elapsed, ctx.Request.Header.Method(), ctx.Host(), ctx.Path(), err)
if strings.Contains(err.Error(), "timed out") || strings.Contains(err.Error(), "timeout") {
ctx.Error("408 Request Timeout", 408)
} else {
ctx.Error("500 Internal Server Error", 500)
}
return
}
if p.config.LogVerbosity > 0 {
ip := ctx.Request.Header.Peek("CF-Connecting-IP") // Cloudflare
if ip == nil {
ip = ctx.Request.Header.Peek(fasthttp.HeaderXForwardedFor) // nginx
}
if ip == nil {
ip = []byte(ctx.RemoteIP().String()) // real
}
p.tracker.trackRequest(string(ip), len(ctx.Response.Body()))
}
if p.config.LogVerbosity == 2 {
log.Printf("%s %s %s%s %s", elapsed, ctx.Request.Header.Method(), ctx.Host(), ctx.Path(),
bytefmt.ByteSize(uint64(len(ctx.Response.Body()))))
} else if p.config.LogVerbosity == 3 {
log.Printf("%s %s %s%s %s\n%s", elapsed, ctx.Request.Header.Method(), ctx.Host(), ctx.Path(),
bytefmt.ByteSize(uint64(len(ctx.Response.Body()))), ctx.Response.Body())
}
}
func (p *Proxy) handleAway(ctx *fasthttp.RequestCtx) {
to := string(ctx.QueryArgs().Peek("to"))
if to == "" {
ctx.Error("Bad Request: 'to' argument is not set", 400)
return
}
to, err := url.QueryUnescape(to)
if err != nil {
ctx.Error("Bad Request: could not unescape url", 400)
return
}
ctx.Redirect(to, fasthttp.StatusMovedPermanently)
}
func (p *Proxy) prepareProxyRequest(ctx *fasthttp.RequestCtx, replaceContext *replacer.ReplaceContext) bool {
// Routing
req := &ctx.Request
uri := string(req.RequestURI())
host := ""
if strings.HasPrefix(uri, "/@") {
slashIndex := strings.IndexByte(uri[2:], '/')
if slashIndex == -1 {
return false
}
endpoint := uri[2 : slashIndex+2]
if endpoint != "vk.com" && endpoint != "vk.ru" &&
!strings.HasSuffix(endpoint, ".vk.com") &&
!strings.HasSuffix(endpoint, ".vk.ru") &&
!strings.HasSuffix(endpoint, ".vkuserphoto.ru") &&
!strings.HasSuffix(endpoint, ".vkuseraudio.net") &&
!strings.HasSuffix(endpoint, ".vkuseraudio.com") &&
!strings.HasSuffix(endpoint, ".mycdn.me") &&
endpoint != "api.ok.ru" {
return false
}
host = endpoint
uri = uri[2+slashIndex:]
req.SetRequestURI(uri)
} else if altHost := req.Header.Peek("Proxy-Host"); altHost != nil {
host = string(altHost)
switch host {
case "static.vk.ru":
case "oauth.vk.ru":
case "static.vk.com":
case "oauth.vk.com":
default:
return false
}
req.Header.Del("Proxy-Host")
} else {
host = "api.vk.com"
}
req.SetHost(host)
// Replace some request data
replaceContext.Host = host
replaceContext.Path = string(ctx.Path())
p.replacer.DoReplaceRequest(req, replaceContext)
// After req.URI() call it is impossible to modify URI
req.URI().SetScheme("https")
if p.config.GzipUpstream {
req.Header.SetBytesV(fasthttp.HeaderAcceptEncoding, gzip)
} else {
req.Header.Del(fasthttp.HeaderAcceptEncoding)
}
req.Header.Del(fasthttp.HeaderConnection)
return true
}
func (p *Proxy) processProxyResponse(ctx *fasthttp.RequestCtx, replaceContext *replacer.ReplaceContext) error {
res := &ctx.Response
res.Header.Del(fasthttp.HeaderSetCookie)
res.Header.Del(fasthttp.HeaderConnection)
res.Header.SetBytesV(fasthttp.HeaderServer, vkProxyName)
var buf *bytebufferpool.ByteBuffer
// Gunzip body if needed
if bytes.Contains(res.Header.Peek(fasthttp.HeaderContentEncoding), gzip) {
res.Header.Del(fasthttp.HeaderContentEncoding)
buf = replacer.AcquireBuffer()
_, err := fasthttp.WriteGunzip(buf, res.Body())
if err != nil {
replacer.ReleaseBuffer(buf)
return err
}
replacer.ReleaseBuffer(&bytebufferpool.ByteBuffer{
B: res.SwapBody(nil),
})
} else {
buf = &bytebufferpool.ByteBuffer{
B: res.SwapBody(nil),
}
}
buf = p.replacer.DoReplaceResponse(res, buf, replaceContext)
// avoid copying and save old buffer
buf.B = res.SwapBody(buf.B)
if cap(buf.B) > 10 {
replacer.ReleaseBuffer(buf)
}
return nil
}
type tracker struct {
lock sync.Mutex
requests uint32
bytes uint64
uniqueUsers map[string]bool
server *fasthttp.Server
}
func (t *tracker) start() {
go func() {
for range time.Tick(60 * time.Second) {
t.lock.Lock()
log.Printf("Requests: %d, Traffic: %s, Online: %d, Concurrency: %d",
t.requests, bytefmt.ByteSize(t.bytes), len(t.uniqueUsers),
t.server.GetCurrentConcurrency(),
)
t.requests = 0
t.bytes = 0
t.uniqueUsers = make(map[string]bool)
t.lock.Unlock()
}
}()
}
func (t *tracker) trackRequest(ip string, size int) {
t.lock.Lock()
t.uniqueUsers[ip] = true
t.requests++
t.bytes += uint64(size)
t.lock.Unlock()
}