-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (172 loc) · 6.17 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
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
)
const (
// Use in Response
OverwriteRequestUrl = "overwrite-request-url" // example: overwrite-request-url: https://example.com
ExposeResponseHeader = "expose-response-header" // example: expose-response-header: set-cookie
OverwriteRequestHeaderPrefix = "overwrite-request-header-" // example: overwrite-request-header:cookie: example-cookie=example
OverwriteResponseHeaderPrefix = "overwrite-response-header-" // example: overwrite-response-header:Access-Control-Allow-Origin: *
OverwriteResponseStatusCode = "overwrite-response-status-code" // example: overwrite-response-status-code: 200
// Use in Response
ExposedResponseHeader = "exposed-header-" // 用于在 response 中暴露 ExposeResponseHeader 指定的 header
)
type AgentOptions struct {
OverwriteRequestURL string
ExposeResponseHeader []string
OverwriteResponseStatusCode *int
OverwriteRequestHeader map[string][]string
OverwriteResponseHeader map[string][]string
}
func ParseOptionsFormHeader(header *http.Header) *AgentOptions {
var agentOptions AgentOptions
var supportedOptionPrefixList = []string{
OverwriteRequestUrl,
ExposeResponseHeader,
OverwriteResponseStatusCode,
OverwriteRequestHeaderPrefix,
OverwriteResponseHeaderPrefix,
}
for headerKey, headerValues := range *header {
lowerHeaderKey := strings.ToLower(headerKey)
for _, supportedOptionPrefix := range supportedOptionPrefixList {
if strings.HasPrefix(lowerHeaderKey, supportedOptionPrefix) {
// OverwriteRequestURL
if supportedOptionPrefix == OverwriteRequestUrl {
agentOptions.OverwriteRequestURL = headerValues[0]
}
// ExposeResponseHeader
if supportedOptionPrefix == ExposeResponseHeader {
agentOptions.ExposeResponseHeader = headerValues
}
// OverwriteResponseStatusCode
if supportedOptionPrefix == OverwriteResponseStatusCode {
if statusCode, err := strconv.Atoi(headerValues[0]); err == nil {
agentOptions.OverwriteResponseStatusCode = &statusCode
} else {
log.Print(err)
}
}
// OverwriteRequestHeader
if supportedOptionPrefix == OverwriteRequestHeaderPrefix {
if agentOptions.OverwriteRequestHeader == nil {
agentOptions.OverwriteRequestHeader = make(map[string][]string)
}
overwriteKey, _ := strings.CutPrefix(lowerHeaderKey, OverwriteRequestHeaderPrefix)
agentOptions.OverwriteRequestHeader[overwriteKey] = headerValues
}
// OverwriteResponseHeader
if supportedOptionPrefix == OverwriteResponseHeaderPrefix {
if agentOptions.OverwriteResponseHeader == nil {
agentOptions.OverwriteResponseHeader = make(map[string][]string)
}
overwriteKey, _ := strings.CutPrefix(lowerHeaderKey, OverwriteResponseHeaderPrefix)
agentOptions.OverwriteResponseHeader[overwriteKey] = headerValues
}
// 清理请求的 header 便于后面使用
header.Del(lowerHeaderKey)
}
}
}
return &agentOptions
}
func AutoProxy(w http.ResponseWriter, request *http.Request) {
// 设置跨域
if request.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
return
}
agentOptions := ParseOptionsFormHeader(&request.Header)
log.Printf("Received a request: %v", agentOptions)
// 处理 OverwriteRequestURL
if agentOptions.OverwriteRequestURL == "" {
err := fmt.Errorf("OverwriteRequestURL cannot be empty")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("Received a request: %s %s, options: %v", request.Method, request.URL.String(), agentOptions)
agentRequest, err := http.NewRequestWithContext(request.Context(), request.Method, agentOptions.OverwriteRequestURL, request.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print(err)
return
}
// 转发全部的 header
for requestHeaderKey, requestHeaderValues := range request.Header {
for _, requestHeaderValue := range requestHeaderValues {
agentRequest.Header.Add(requestHeaderKey, requestHeaderValue)
}
}
// 处理 OverwriteRequestHeader
for overwriteKey, overwriteValues := range agentOptions.OverwriteRequestHeader {
agentRequest.Header.Del(overwriteKey)
for _, overwriteValue := range overwriteValues {
agentRequest.Header.Add(overwriteKey, overwriteValue)
}
}
// 发送请求
client := &http.Client{}
agentResponse, err := client.Do(agentRequest)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print(err)
return
}
// 处理 ExposeResponseHeader
for _, exposeKey := range agentOptions.ExposeResponseHeader {
exposedKey := ExposedResponseHeader + exposeKey
exposedValues := agentResponse.Header.Values(exposeKey)
for _, exposedValue := range exposedValues {
agentResponse.Header.Add(exposedKey, exposedValue)
}
}
// 处理 OverwriteResponseHeader
for overwriteKey, overwriteValues := range agentOptions.OverwriteResponseHeader {
agentResponse.Header.Del(overwriteKey)
for _, overwriteValue := range overwriteValues {
agentResponse.Header.Add(overwriteKey, overwriteValue)
}
}
// 读取响应体内容
body, err := io.ReadAll(agentResponse.Body)
if err != nil {
log.Printf("Failed to read response body:", err)
return
}
// 设置响应头
for key, values := range agentResponse.Header {
for _, value := range values {
// 将响应头写入到 ResponseWriter
// 这里假设你已经有一个 http.ResponseWriter 对象,命名为 "w"
w.Header().Add(key, value)
}
}
// 设置响应状态码
if agentOptions.OverwriteResponseStatusCode != nil {
w.WriteHeader(*agentOptions.OverwriteResponseStatusCode)
} else {
w.WriteHeader(agentResponse.StatusCode)
}
// 将响应体写入到 ResponseWriter
if _, err = w.Write(body); err != nil {
log.Printf("Failed to read response body:", err)
}
}
func main() {
// 注册请求处理函数
http.HandleFunc("/", AutoProxy)
// 启动 HTTP 服务,监听在端口808
log.Println("start the server on port 8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Failed to start the server:", err)
}
}