-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
52 lines (46 loc) · 1.04 KB
/
gin.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
package main
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
g := gin.New()
g.POST("/", func(context *gin.Context) {
body,err := HttpProxyDo(context.Request,"127.0.0.1:81","www.baidu.com")
if err != nil{
context.String(200,err.Error())
return
}
context.String(200,string(body))
})
g.Run(":81")
}
func HttpProxyDo(req *http.Request, from,to string) (body []byte,err error) {
log.Printf("%+v",req)
log.Println()
//log.Println(req.Host)
//req.URL.Scheme = "http"
req.URL.Host = req.Host
req.URL.Scheme = "http"
log.Printf("%+v",req.URL.Scheme)
log.Printf("origin:%s,to %s, find %s",req.URL.Host, to, strings.Replace(req.URL.Host,from, to,1))
req.URL.Host = strings.Replace(req.URL.Host,from, to,1)
req.Host = strings.Replace(req.Host, from, to,1)
req.RequestURI = ""
log.Printf("%+v",req)
var c = &http.Client{}
resp, err := c.Do(req)
if err != nil {
log.Println(err)
return
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
resp.Body.Close()
return
}