From b643a43c994207eaec98895d21a45f415427d1b0 Mon Sep 17 00:00:00 2001 From: ZhangYueqian Date: Wed, 14 Apr 2021 17:20:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0xz=E9=A1=B9=E7=9B=AE=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E7=99=BE=E5=BA=A6=E5=9C=B0=E5=9B=BE=E5=8F=8D=E4=BB=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 ++ BaiduProxy/src/antiy/go.mod | 5 ++ BaiduProxy/src/antiy/go.sum | 6 ++ BaiduProxy/src/antiy/main.go | 129 +++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 BaiduProxy/src/antiy/go.mod create mode 100644 BaiduProxy/src/antiy/go.sum create mode 100644 BaiduProxy/src/antiy/main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f135b96 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +BaiduProxy/pkg +BaiduProxy/bin +BaiduProxy/src/github.com +BaiduProxy/src/golang.org +BaiduProxy/src/h12.io +BaiduProxy/antiy +BaiduProxy/.idea diff --git a/BaiduProxy/src/antiy/go.mod b/BaiduProxy/src/antiy/go.mod new file mode 100644 index 0000000..bd2b473 --- /dev/null +++ b/BaiduProxy/src/antiy/go.mod @@ -0,0 +1,5 @@ +module antiy + +go 1.16 + +require h12.io/socks v1.0.2 diff --git a/BaiduProxy/src/antiy/go.sum b/BaiduProxy/src/antiy/go.sum new file mode 100644 index 0000000..17ccb89 --- /dev/null +++ b/BaiduProxy/src/antiy/go.sum @@ -0,0 +1,6 @@ +github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364 h1:5XxdakFhqd9dnXoAZy1Mb2R/DZ6D1e+0bGC/JhucGYI= +github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364/go.mod h1:eDJQioIyy4Yn3MVivT7rv/39gAJTrA7lgmYr8EW950c= +github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc= +github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= +h12.io/socks v1.0.2 h1:cZhhbV8+DE0Y1kotwhr1a3RC3kFO7AtuZ4GLr3qKSc8= +h12.io/socks v1.0.2/go.mod h1:AIhxy1jOId/XCz9BO+EIgNL2rQiPTBNnOfnVnQ+3Eck= diff --git a/BaiduProxy/src/antiy/main.go b/BaiduProxy/src/antiy/main.go new file mode 100644 index 0000000..a500560 --- /dev/null +++ b/BaiduProxy/src/antiy/main.go @@ -0,0 +1,129 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/url" + "os" + "strings" + "time" +) + +var domains = []string{ + "api.map.baidu.com", + "api0.map.bdimg.com", + "api1.map.bdimg.com", + "api2.map.bdimg.com", + "api3.map.bdimg.com", + "maponline0.bdimg.com", + "maponline1.bdimg.com", + "maponline2.bdimg.com", + "maponline3.bdimg.com", + "dlswbr.baidu.com", + "shangetu0.map.bdimg.com", + "shangetu1.map.bdimg.com", + "shangetu2.map.bdimg.com", + "shangetu3.map.bdimg.com", + "miao.baidu.com", +} + +func ReplaceHost(contentString, hostname string) string { + for _, domain := range domains { + contentString = strings.Replace(contentString, domain, hostname+"/"+domain, -1) + } + contentString = strings.Replace(contentString, "https://", "http://", -1) + return contentString +} + +type MyHandler struct { +} + +func (myHandler *MyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + hostname := req.Host + getUrl := url.URL{ + Scheme: "http", + } + if !strings.HasPrefix(req.URL.Path, "/") { + req.URL.Path = "/" + req.URL.Path + } + + for _, domain := range domains { + if strings.HasPrefix(req.URL.Path, "/"+domain) { + getUrl.Host = domain + getUrl.Path = strings.Replace(req.URL.Path, domain+"/", "", 1) + getUrl.RawQuery = req.URL.RawQuery + baiduResp, _ := http.Get(getUrl.String()) + fmt.Printf("fmt: %s", baiduResp.Header.Get("Content-Type")) + fmt.Printf("Get response from %s\n", getUrl.String()) + content, _ := ioutil.ReadAll(baiduResp.Body) + contentString := string(content) + if strings.Contains(baiduResp.Header.Get("Content-Type"), "javascript") { + contentString = ReplaceHost(contentString, hostname) + } + resp.Header().Set("Access-Control-Allow-Origin", "*") + resp.Header().Set("Access-Control-Allow-Headers", "Origin") + resp.Header().Set("Content-Type", baiduResp.Header.Get("Content-Type")) + fmt.Fprintf(resp, "%v\n", contentString) + return + } + } + + //fp, _ := os.Open("demo.html") + //pageContent, _ := io.ReadAll(fp) + pageContent := ` + + + + + + + + 地图展示 + + +
+ + + +` + fmt.Fprint(resp, string(pageContent)) +} + +func (myHandler *MyHandler) Handler() { + +} + +func main() { + var port string + if len(os.Args) > 1 { + port = os.Args[1] + } else { + port = "127.0.0.1:8080" + } + m := MyHandler{} + + s := &http.Server{ + Addr: port, + Handler: &m, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + + s.ListenAndServe() +}