Reputation: 8059
I trying to change connection host depending on path, the problem in context
, I though it is created when flow created, but it shared between requests. So i lost here, this is what i tried
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");
Important note:
I need to not change HOST:
in http request headers.
Upvotes: 1
Views: 685
Reputation: 8059
I have found reason and solution myself,
reason is keepalive, you need to close connections when you switch hosts, because otherwise you will not be go to serverconnect
routine and use last connected 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"
#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");
Upvotes: 1