lyyyuna / mitm

HTTPS man in the middle
39 stars 21 forks source link

如何设置上游代理 #1

Open Totti0135 opened 6 years ago

Totti0135 commented 6 years ago

我想把接收到的请求再转发到别的代理上,请问这个应该怎么做? 我在DumpHTTPAndHTTPS方法中

    var connOut net.Conn
    if !handler.https {
        if !matched {
            host += ":80"
        }
        connOut, err = net.DialTimeout("tcp", "127.0.0.1:8001", time.Second * 10)
        if err != nil {
            logger.Println("Dial to", host, "error:", err)
            return
        }
    } else {
        if !matched {
            host += ":443"
        }
        connOut, err = tls.Dial("tcp", "127.0.0.1:8001", handler.tlsConfig.ServerTLSConfig)
        if err != nil {
            logger.Println("Dial to", host, "error:", err)
            return
        }
    }

net.DialTimeout的第二个参数是本地的另外一个代理的话,没有问题,但是设置远程的代理的时候就会报错 tls.Dial设置本地也不能成功

Status:  400 Bad Request
GET www.sina.com.cn /
Remote Addr:  
    User-Agent: [Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0]
    Accept: [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
    Accept-Encoding: [gzip, deflate]
    Accept-Language: [zh-CN,zh;q=0.8,en;q=0.5,en-US;q=0.3]
    Connection: [Keep-Alive]
    Cookie: [UOR=www.baidu.com,sports.sina.com.cn,; ULV=1528881736495:12:6:2:112.64.131.101_1528881722.810080:1528881722630; SINAGLOBAL=112.64.131.100_1527476728.8437; lxlrttp=1528341939; SUB=_2AkMsV_01f8NxqwJRmP4Qz2Lqbo9yzAnEieKaCwzuJRMyHRl-yD9kqlYBtRB6B9fT2jFRJ0CbQv4pTmQKv4aw73g8oFz2; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWeOB8M3mreMLgu709WfAe1; U_TRS1=00000064.8bb7660b.5b0b721b.8e61825b; SGUID=1527654306662_62106522; CNZZDATA1271230489=1398476948-1527653943-%7C1528878397; lxlrtst=1528341939_o; UM_distinctid=163f8741da29-04723aa712c084-17357940-100200-163f8741da344b]
    If-Modified-Since: [Wed, 13 Jun 2018 09:20:02 GMT]
    Upgrade-Insecure-Requests: [1]
    X-Test: [Y-Test]
Response:
    Server: [mitmproxy 2.0.2]
    Content-Length: [306]
    Content-Type: [text/html]
lyyyuna commented 6 years ago

@Totti0135 因为两个代理之间应该走 HTTP 的代理协议,所以应该完整实现一套 HTTP 代理规范,而不是直接把客户端发过来的流量直接转发(因为是个 mitm的缘故,https 变成了明文,还需再转成 CONNECT 请求给上游。