|
|
@ -49,6 +49,9 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tr := &http.Transport{
|
|
|
|
tr := &http.Transport{
|
|
|
|
DialContext: localDialer,
|
|
|
|
DialContext: localDialer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("%s %s", r.Method, r.RequestURI)
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := tr.RoundTrip(r)
|
|
|
|
resp, err := tr.RoundTrip(r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
log.Errorln(err)
|
|
|
@ -70,22 +73,30 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func mydail(uri string) (net.Conn, error) {
|
|
|
|
|
|
|
|
addr := &net.TCPAddr{}
|
|
|
|
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
|
|
|
|
LocalAddr: addr,
|
|
|
|
|
|
|
|
Timeout: 3 * time.Second,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
ip := getLocalIP()
|
|
|
|
ip := getLocalIP()
|
|
|
|
|
|
|
|
addr.IP = net.ParseIP(ip)
|
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("connect to %s, use local ip %s", r.RequestURI, ip)
|
|
|
|
log.Debugf("connect to %s, use local ip %s", uri, ip)
|
|
|
|
|
|
|
|
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
conn, err := dialer.Dial("tcp", uri)
|
|
|
|
IP: net.ParseIP(ip),
|
|
|
|
if err == nil {
|
|
|
|
Port: 0,
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("connect to %s, %s, retrying", uri, err)
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
}
|
|
|
|
LocalAddr: addr,
|
|
|
|
return nil, fmt.Errorf("connect to %s, max retries reached, give up", uri)
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := dialer.Dial("tcp", r.RequestURI)
|
|
|
|
func handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
conn, err := mydail(r.RequestURI)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
log.Errorln(err)
|
|
|
|
http.Error(w, "connection failed", http.StatusBadGateway)
|
|
|
|
http.Error(w, "connection failed", http.StatusBadGateway)
|
|
|
|