3

Hello! I have this code:

from twisted.web import proxy, http
from twisted.internet import reactor

class akaProxy(proxy.Proxy):
    """
    Local proxy = bridge between browser and web application
    """

    def dataReceived(self, data):

        print "Received data..."

        headers = data.split("\n")
        request = headers[0].split(" ")

        method = request[0].lower()
        action = request[1]
        print action
        print "ended content manipulation"  
        return proxy.Proxy.dataReceived(self, data)

class ProxyFactory(http.HTTPFactory):
    protocol = akaProxy

def intercept(port):
    print "Intercept"
    try:                
        factory = ProxyFactory()
        reactor.listenTCP(port, factory)
        reactor.run()
    except Exception as excp:
        print str(excp)

intercept(1337)

I use above code to intercept everything between browser and web site. When using above, I configure my browser settings: to IP: 127.0.0.1 and Port: 1337. I put this script in remote server to act my remote server as proxy server. But when I change browser proxy IP settings to my server's it does not work. What I do wrong? What else I need to configure?

torayeff
  • 9,296
  • 19
  • 69
  • 103

2 Answers2

2

Presumably your dataReceived is raising an exception during its attempts to parse the data passed to it. Try enabling logging so you can see more of what's going on:

from twisted.python.log import startLogging
from sys import stdout
startLogging(stdout)

The reason your parser is likely to raise exceptions is that dataReceived is not called only with a complete request. It is called with whatever bytes are read from the TCP connection. This may be a complete request, a partial request, or even two requests (if pipelining is in use).

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
0

dataReceived in the Proxy context is handling "translation of rawData into lines", so it may be too early for trying your manipulation code. You can try overriding allContentReceived instead and you will have access to the complete headers and content. Here is an example that I believe does what you are after:

#!/usr/bin/env python
from twisted.web import proxy, http

class SnifferProxy(proxy.Proxy):
    """
    Local proxy = bridge between browser and web application
    """

    def allContentReceived(self):
        print "Received data..."
        print "method = %s" % self._command
        print "action = %s" % self._path
        print "ended content manipulation\n\n"
        return proxy.Proxy.allContentReceived(self)


class ProxyFactory(http.HTTPFactory):

    protocol = SnifferProxy

if __name__ == "__main__":
    from twisted.internet import reactor
    reactor.listenTCP(8080, ProxyFactory())
    reactor.run()
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Braudel
  • 11
  • 3
  • 1
    I have copied and placed your script on my remote server and run it while connection with server keeping open. Then I changed my proxy ip of browser to my server's to access http://www.ifconfig.me/ip, but I could not, browser displayed "The connection has timed out" and neither open python script on server displayed any information. It stayed as empty as it was. – torayeff Aug 30 '12 at 18:16
  • 1
    In addition to changing your browser proxy settings to your server's ip, and also need to set the proxy port to 8080. – Braudel Aug 30 '12 at 23:27
  • 1
    Try first a direct connection http://your-server-ip:8080 (with no proxy settings). If this works then ensure that in addition to changing your browser proxy settings to your server's ip, you also set the proxy port to 8080. If the direct connection did not work, check that your server firewall allows port 8080 and that your LAN does not requires a proxy to get to your server's ip – Braudel Aug 30 '12 at 23:35