1 Star 0 Fork 0

摘葫芦的葫芦娃/ReverseProxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
handle.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
ilanyu 提交于 2018-04-16 19:47 . change dns server
package main
import (
"net/http"
"net/url"
"net/http/httputil"
"log"
"net"
"time"
"context"
"github.com/bogdanovich/dns_resolver"
"strings"
)
type handle struct {
reverseProxy string
}
func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr + " " + r.Method + " " + r.URL.String() + " " + r.Proto + " " + r.UserAgent())
remote, err := url.Parse(this.reverseProxy)
if err != nil {
log.Fatalln(err)
}
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
remote := strings.Split(addr, ":")
if cmd.ip == "" {
resolver := dns_resolver.New([]string{"114.114.114.114", "114.114.115.115", "119.29.29.29", "223.5.5.5", "8.8.8.8", "208.67.222.222", "208.67.220.220"})
resolver.RetryTimes = 5
ip, err := resolver.LookupHost(remote[0])
if err != nil {
log.Println(err)
}
cmd.ip = ip[0].String()
}
addr = cmd.ip + ":" + remote[1]
return dialer.DialContext(ctx, network, addr)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
r.Host = remote.Host
proxy.ServeHTTP(w, r)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lynnwuxincheng/ReverseProxy.git
git@gitee.com:lynnwuxincheng/ReverseProxy.git
lynnwuxincheng
ReverseProxy
ReverseProxy
master

搜索帮助