-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (71 loc) · 2.09 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
package main
import (
"log"
"time"
"os"
"github.com/valyala/fasthttp"
"strconv"
"strings"
)
var timeout, _ = strconv.Atoi(os.Getenv("TIMEOUT"))
var retries, _ = strconv.Atoi(os.Getenv("RETRIES"))
var port = os.Getenv("PORT")
var client *fasthttp.Client
func main() {
h := requestHandler
client = &fasthttp.Client{
ReadTimeout: time.Duration(timeout) * time.Second,
MaxIdleConnDuration: 60 * time.Second,
}
if err := fasthttp.ListenAndServe(":" + port, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
val, ok := os.LookupEnv("KEY")
if ok && string(ctx.Request.Header.Peek("PROXYKEY")) != val {
ctx.SetStatusCode(407)
ctx.SetBody([]byte("Missing or invalid PROXYKEY header."))
return
}
if len(strings.SplitN(string(ctx.Request.Header.RequestURI())[1:], "/", 2)) < 2 {
ctx.SetStatusCode(400)
ctx.SetBody([]byte("URL format invalid."))
return
}
response := makeRequest(ctx, 1)
defer fasthttp.ReleaseResponse(response)
body := response.Body()
ctx.SetBody(body)
ctx.SetStatusCode(response.StatusCode())
response.Header.VisitAll(func (key, value []byte) {
ctx.Response.Header.Set(string(key), string(value))
})
}
func makeRequest(ctx *fasthttp.RequestCtx, attempt int) *fasthttp.Response {
if attempt > retries {
resp := fasthttp.AcquireResponse()
resp.SetBody([]byte("Proxy failed to connect. Please try again."))
resp.SetStatusCode(500)
return resp
}
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.Header.SetMethod(string(ctx.Method()))
url := strings.SplitN(string(ctx.Request.Header.RequestURI())[1:], "/", 2)
req.SetRequestURI("https://" + url[0] + ".roblox.com/" + url[1])
req.SetBody(ctx.Request.Body())
ctx.Request.Header.VisitAll(func (key, value []byte) {
req.Header.Set(string(key), string(value))
})
req.Header.Set("User-Agent", "RoProxy")
req.Header.Del("Roblox-Id")
resp := fasthttp.AcquireResponse()
err := client.Do(req, resp)
if err != nil {
fasthttp.ReleaseResponse(resp)
return makeRequest(ctx, attempt + 1)
} else {
return resp
}
}