-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (65 loc) · 2.21 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
package main
import (
"crypto/tls"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
)
// proxyHandler 根据请求类型选择处理方式
func proxyHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
handleTunneling(w, r) // 处理 HTTPS 隧道请求
} else {
handleHTTP(w, r) // 处理 HTTP 请求
}
}
// handleTunneling 处理 HTTPS 隧道请求
func handleTunneling(w http.ResponseWriter, r *http.Request) {
destConn, err := net.Dial("tcp", r.Host) // 连接目标服务器
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK) // 返回成功状态码
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
return
}
clientConn, _, err := hijacker.Hijack() // 获取客户端连接
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
go transfer(destConn, clientConn) // 开启协程进行数据转发
go transfer(clientConn, destConn) // 开启协程进行数据转发
}
// handleHTTP 处理 HTTP 请求
func handleHTTP(w http.ResponseWriter, r *http.Request) {
rp := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http" // 设置请求协议
req.URL.Host = r.Host // 设置请求主机
},
}
rp.ServeHTTP(w, r) // 反向代理处理请求
}
// transfer 负责在两个连接之间传输数据
func transfer(destination io.WriteCloser, source io.ReadCloser) {
defer destination.Close() // 关闭目标连接
defer source.Close() // 关闭源连接
io.Copy(destination, source) // 数据拷贝
}
func main() {
server := &http.Server{
Addr: ":8006", // 监听地址
Handler: http.HandlerFunc(proxyHandler), // 设置处理函数
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // 禁用 HTTP/2
}
log.Printf("HTTP proxy server is running on %s", server.Addr) // 输出监听地址
if err := server.ListenAndServe(); err != nil { // 启动服务器
log.Fatalf("Failed to start the server: %v", err) // 输出错误信息
}
}