-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.go
More file actions
150 lines (126 loc) · 3.49 KB
/
proxy.go
File metadata and controls
150 lines (126 loc) · 3.49 KB
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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/sandertv/gophertunnel/minecraft/auth"
)
type ProxyRequest struct {
Method string `json:"method"`
URL string `json:"url"`
Headers map[string][]string `json:"headers,omitempty"`
BodyBase64 string `json:"body_base64,omitempty"`
}
type ProxyResponse struct {
Status int `json:"status"`
StatusText string `json:"status_text"`
Headers map[string][]string `json:"headers"`
BodyBase64 string `json:"body_base64"`
}
type WorkerProxyTransport struct {
WorkerURL string
WorkerSecret string
Base http.RoundTripper
}
func installWorkerProxy() {
workerURL := "https://cloudflare-proxy.restartfu.workers.dev/proxy"
workerSecret := os.Getenv("PROXY_SHARED_SECRET")
baseTransport := http.DefaultTransport
proxyTransport := &WorkerProxyTransport{
WorkerURL: workerURL,
WorkerSecret: workerSecret,
Base: baseTransport,
}
proxyClient := &http.Client{
Transport: proxyTransport,
}
http.DefaultClient = proxyClient
http.DefaultTransport = proxyTransport
auth.SetHTTPClient(proxyClient)
}
func (t *WorkerProxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL == nil {
return nil, fmt.Errorf("nil request URL")
}
base := t.Base
if base == nil {
base = http.DefaultTransport
}
if strings.HasPrefix(req.URL.String(), t.WorkerURL) {
return base.RoundTrip(req)
}
host := strings.ToLower(req.URL.Hostname())
if host == "localhost" || host == "127.0.0.1" || host == "::1" {
return base.RoundTrip(req)
}
if req.URL.Scheme != "https" {
return nil, fmt.Errorf("only https URLs are allowed through the worker proxy: %s", req.URL.String())
}
var bodyBytes []byte
if req.Body != nil {
var err error
bodyBytes, err = io.ReadAll(req.Body)
if err != nil {
return nil, err
}
}
headers := make(map[string][]string, len(req.Header))
for k, v := range req.Header {
vv := make([]string, len(v))
copy(vv, v)
headers[k] = vv
}
payload := ProxyRequest{
Method: req.Method,
URL: req.URL.String(),
Headers: headers,
}
if len(bodyBytes) > 0 {
payload.BodyBase64 = base64.StdEncoding.EncodeToString(bodyBytes)
}
payloadJSON, err := json.Marshal(payload)
if err != nil {
return nil, err
}
workerReq, err := http.NewRequest(http.MethodPost, t.WorkerURL, bytes.NewReader(payloadJSON))
if err != nil {
return nil, err
}
workerReq.Header.Set("Content-Type", "application/json")
if t.WorkerSecret != "" {
workerReq.Header.Set("Authorization", "Bearer "+t.WorkerSecret)
}
workerResp, err := base.RoundTrip(workerReq)
if err != nil {
return nil, err
}
defer workerResp.Body.Close()
raw, err := io.ReadAll(workerResp.Body)
if err != nil {
return nil, err
}
if workerResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("worker returned %s: %s", workerResp.Status, string(raw))
}
var proxyResp ProxyResponse
if err := json.Unmarshal(raw, &proxyResp); err != nil {
return nil, err
}
respBody, err := base64.StdEncoding.DecodeString(proxyResp.BodyBase64)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: proxyResp.Status,
Status: fmt.Sprintf("%d %s", proxyResp.Status, proxyResp.StatusText),
Header: http.Header(proxyResp.Headers),
Body: io.NopCloser(bytes.NewReader(respBody)),
ContentLength: int64(len(respBody)),
Request: req,
}, nil
}