-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
95 lines (83 loc) · 1.83 KB
/
server.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
package webproxy
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net"
"net/http"
"strings"
)
func Start() {
gin.DefaultWriter = &nullWriter{}
router := gin.Default()
//fromHost := fmt.Sprintf("http%s%s", "://", "baidu.localhost.com")
toHost := "https://www.baidu.com"
router.MaxMultipartMemory = (1024 * 50) << 20 // 设置最大上传大小为50G
routerGroup := router.Group("/")
routerGroup.GET("*path", func(c *gin.Context) {
path := c.Params.ByName("path")
fmt.Println("path:", path)
fmt.Println("path:", c.Params)
client := &http.Client{}
url := toHost + path
fmt.Println("url:", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
for k, v := range c.Request.Header {
if strings.EqualFold(k, "Host") {
req.Header[k] = []string{toHost}
} else if strings.EqualFold(k, "Referer") {
req.Header[k] = []string{url}
} else {
req.Header[k] = v
}
}
for _, cook := range c.Request.Cookies() {
req.AddCookie(cook)
}
fmt.Println("req.Header:", req.Header)
res, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Println("res.Header:", res.Header)
for k, vs := range res.Header {
for _, v := range vs {
c.Header(k, v)
}
}
for _, cook := range res.Cookies() {
c.SetCookie(cook.Name, cook.Value, cook.MaxAge, cook.Path, cook.Domain, cook.Secure, cook.HttpOnly)
}
bs, err := io.ReadAll(res.Body)
if err == io.EOF {
err = nil
}
if err != nil {
panic(err)
}
text := string(bs)
fmt.Println(text)
c.Writer.Write(bs)
})
s := &http.Server{
Addr: "0.0.0.0:8008",
Handler: router,
}
ln, err := net.Listen("tcp", s.Addr)
if err != nil {
return
}
go func() {
err = s.Serve(ln)
if err != nil {
panic(err)
}
}()
}
type nullWriter struct{}
func (*nullWriter) Write(bs []byte) (int, error) {
return 0, nil
}