2016-07-08 54 views
1

我試圖根據路徑更改連接主機,context中的問題,雖然它是在創建流時創建的,但它在請求之間共享。所以我失去了這裏,這是我的嘗試mitmproxy更改服務器主機的路徑

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

我已經找到原因及解決辦法嘍,

原因是存活,你需要在你切換主機關閉連接,否則你不會去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");