mitmproxy按路径更改服务器主机

时间:2016-07-08 09:08:54

标签: mitmproxy

我尝试根据路径更改连接主机,context中的问题,虽然它是在创建流时创建的,但它在请求之间共享。所以我在这里失去了,这就是我试过的。

import re
import os
os.environ['PAGER'] = 'cat'
from libmproxy.models import HTTPResponse
from netlib.http import Headers
from netlib.tcp import Address
def request(context, flow):
# flow.request.path
        context.log("server %s " % repr(flow.server_conn) ,"info");
        if flow.request.host.endswith("google.com"):
                if re.match(flow.request.path, "/"):
                        context.address = "10.0.0.15";
                else:
                        context.address = "google.com"

def serverconnect(context, server_conn):
        if hasattr(context, 'address'):
                context.log("server changed from %s" % (repr(server_conn)) ,"info");
                server_conn.address = Address(address=(context.address, server_conn.address.port))
                context.log("server changed to %s" % (repr(server_conn)) ,"info");
        else:
                context.log("server NOT changed %s" % repr(server_conn),"info");

重要提示:

我不需要在http请求标头中更改HOST:

1 个答案:

答案 0 :(得分:1)

我自己找到了理由和解决方案,

原因是keepalive,您需要在切换主机时关闭连接,否则您将不会转到serverconnect例程并使用最后连接的主机:

import re
import os
os.environ['PAGER'] = 'cat'
from libmproxy.models import HTTPResponse
from netlib.http import Headers
from netlib.tcp import Address
def request(context, flow):
# flow.request.path
        context.log("server %s " % repr(flow.server_conn) ,"info");
        if flow.request.host.endswith("google.com"):
                if re.match(flow.request.path, "/"):
                        context.address = "10.0.0.15";
                else:
                        context.address = "google.com"
#here is solution
                if repr(server_conn.get_state().get('timestamp_start')) != 'None':
                        print(server_conn.get_state().get('timestamp_start'))
                        server_conn.close()


def serverconnect(context, server_conn):
        if hasattr(context, 'address'):
                context.log("server changed from %s" % (repr(server_conn)) ,"info");
                server_conn.address = Address(address=(context.address, server_conn.address.port))
                context.log("server changed to %s" % (repr(server_conn)) ,"info");
        else:
                context.log("server NOT changed %s" % repr(server_conn),"info");
相关问题