parent
8de901aa05
commit
b643a43c99
4 changed files with 147 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
BaiduProxy/pkg |
||||||
|
BaiduProxy/bin |
||||||
|
BaiduProxy/src/github.com |
||||||
|
BaiduProxy/src/golang.org |
||||||
|
BaiduProxy/src/h12.io |
||||||
|
BaiduProxy/antiy |
||||||
|
BaiduProxy/.idea |
@ -0,0 +1,5 @@ |
|||||||
|
module antiy |
||||||
|
|
||||||
|
go 1.16 |
||||||
|
|
||||||
|
require h12.io/socks v1.0.2 |
@ -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= |
@ -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 := ` |
||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
||||||
|
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> |
||||||
|
<style type="text/css"> |
||||||
|
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";} |
||||||
|
</style> |
||||||
|
<script type="text/javascript" src="/api.map.baidu.com/api?v=2.0&ak=ADfpGTm3mDV4sZRiA2uDOKv3SbvcPtzG"></script> |
||||||
|
<title>地图展示</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<div id="allmap"></div> |
||||||
|
</body> |
||||||
|
</html> |
||||||
|
<script type="text/javascript"> |
||||||
|
// 百度地图API功能
|
||||||
|
var map = new BMap.Map("allmap"); // 创建Map实例
|
||||||
|
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
|
||||||
|
//添加地图类型控件
|
||||||
|
map.addControl(new BMap.MapTypeControl({ |
||||||
|
mapTypes:[ |
||||||
|
BMAP_NORMAL_MAP, |
||||||
|
BMAP_HYBRID_MAP |
||||||
|
]})); |
||||||
|
map.setCurrentCity("武汉"); // 设置地图显示的城市 此项是必须设置的
|
||||||
|
map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
|
||||||
|
</script> |
||||||
|
` |
||||||
|
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() |
||||||
|
} |
Loading…
Reference in new issue