2016-07-08 1 views
1

Я пытаюсь изменить хост подключения в зависимости от пути, проблема в context, хотя он создан при создании потока, но он делится между запросами. Так я потерял здесь, это то, что я пыталсяmitmproxy change server host по пути

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"); 

Важное примечание:

Мне нужно не менять HOST: в заголовки запроса HTTP.

ответ

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");