Twisted - extending portforward (simple example)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Moore

    Twisted - extending portforward (simple example)

    I hit a problem yesterday with my mail connection. In a desparate
    attempt to understand what was going on, I wanted to log the
    connection traffic. After a bit of searching, I found a post on c.l.p
    from Andrew Bennetts explaining how to run a port forwarder in 2 lines
    using Twisted.

    $ mktap portforward -p 8000 -h remote -d 20
    $ twistd -f portforward.tap

    This looked brilliant - all I needed to do was add logging. A quick
    look (I was *very* short of time, so I couldn't spend long) showed me
    no obvious way of adding a hook (at least, not one which wouldn't turn
    the above 2-liner into something more complex, that I didn't have time
    to work out...) So I gave in, and just added a print statement in the
    twisted code.

    This gave me the results I wanted, but left me wondering (and not
    getting far in finding out!) how I *should* have done this.

    Ideally, I was expecting to be able to write a small .py file,
    importing and minimally overriding portforward.py, and then be able to
    do the mktap/twistd thing. But I couldn't see how.

    Can anyone give me a simple example of how I should have done this?
    The logging portforwarder example seems like a nice simple starting
    point, from which I can go on and learn how to do clever stuff :-)

    Thanks,
    Paul.

    PS Many thanks to the twisted crew - even if I didn't do things the
    "right" way, I got my problem diagnosed in no time at all, when I'd
    previously struggled with it for days!
  • Andrew Bennetts

    #2
    Re: Twisted - extending portforward (simple example)

    On Fri, Jun 27, 2003 at 03:42:20AM -0700, Paul Moore wrote:[color=blue]
    > I hit a problem yesterday with my mail connection. In a desparate
    > attempt to understand what was going on, I wanted to log the
    > connection traffic. After a bit of searching, I found a post on c.l.p
    > from Andrew Bennetts explaining how to run a port forwarder in 2 lines
    > using Twisted.
    >
    > $ mktap portforward -p 8000 -h remote -d 20
    > $ twistd -f portforward.tap
    >
    > This looked brilliant - all I needed to do was add logging. A quick
    > look (I was *very* short of time, so I couldn't spend long) showed me
    > no obvious way of adding a hook (at least, not one which wouldn't turn
    > the above 2-liner into something more complex, that I didn't have time
    > to work out...) So I gave in, and just added a print statement in the
    > twisted code.
    >
    > This gave me the results I wanted, but left me wondering (and not
    > getting far in finding out!) how I *should* have done this.
    >
    > Ideally, I was expecting to be able to write a small .py file,
    > importing and minimally overriding portforward.py, and then be able to
    > do the mktap/twistd thing. But I couldn't see how.[/color]

    The easiest way is probably to do this:

    --- loggingpf.py ---

    from twisted.interne t import app
    from twisted.python import log
    from twisted.protoco ls import portforward
    from twisted.tap.por tforward import Options

    class LoggingProxyCli ent(portforward .ProxyClient):
    def dataReceived(se lf, data):
    log.msg('server sent: ' + repr(data))
    portforward.Pro xyClient.dataRe ceived(self, data)

    class LoggingProxyCli entFactory(port forward.ProxyCl ientFactory):
    protocol = LoggingProxyCli ent

    class LoggingProxySer ver(portforward .ProxyServer):
    clientProtocolF actory = LoggingProxyCli entFactory

    def dataReceived(se lf, data):
    log.msg('client sent: ' + repr(data))
    portforward.Pro xyServer.dataRe ceived(self, data)

    class LoggingProxyFac tory(portforwar d.ProxyFactory) :
    protocol = LoggingProxySer ver

    application = app.Application ('LoggingPortFo rward')
    application.lis tenTCP(8000, LoggingProxyFac tory('localhost ', 80))

    --- end --

    Then you can use

    $ twistd -n -o -y loggingpf.py

    You could easily adapt this to become a mktap plugin; see



    (For instance, the normal portforward plugin is defined in
    twisted/plugins.tml and twisted/tap/portforward.py)

    An even lazier approach would be to add an "application.ru n(save=0)" to the
    end of loggingpf.py -- then you'd be able to run it directly as

    $ python loggingpf.py

    (but using twistd gives you the ability to daemonise, use log files, etc).
    [color=blue]
    > Can anyone give me a simple example of how I should have done this?
    > The logging portforwarder example seems like a nice simple starting
    > point, from which I can go on and learn how to do clever stuff :-)[/color]

    Unfortunately, it's not very simple, as you can see -- although you only
    need to override two methods (ProxyClient.da taReceived and
    ProxyServer.dat aReceived), you need to make four subclasses to tie it all
    together.

    -Andrew.


    Comment

    • Lee Harr

      #3
      Re: Twisted - extending portforward (simple example)

      In article <mailman.105677 0626.1534.pytho [email protected] >, Andrew Bennetts:[color=blue]
      > On Fri, Jun 27, 2003 at 03:42:20AM -0700, Paul Moore wrote:[color=green]
      >> I hit a problem yesterday with my mail connection. In a desparate
      >> attempt to understand what was going on, I wanted to log the
      >> connection traffic. After a bit of searching, I found a post on c.l.p
      >> from Andrew Bennetts explaining how to run a port forwarder in 2 lines
      >> using Twisted.
      >>
      >> $ mktap portforward -p 8000 -h remote -d 20
      >> $ twistd -f portforward.tap
      >>
      >> This looked brilliant - all I needed to do was add logging. A quick
      >> look (I was *very* short of time, so I couldn't spend long) showed me
      >> no obvious way of adding a hook (at least, not one which wouldn't turn
      >> the above 2-liner into something more complex, that I didn't have time
      >> to work out...) So I gave in, and just added a print statement in the
      >> twisted code.
      >>
      >> This gave me the results I wanted, but left me wondering (and not
      >> getting far in finding out!) how I *should* have done this.
      >>
      >> Ideally, I was expecting to be able to write a small .py file,
      >> importing and minimally overriding portforward.py, and then be able to
      >> do the mktap/twistd thing. But I couldn't see how.[/color]
      >
      > The easiest way is probably to do this:
      >
      > --- loggingpf.py ---[/color]
      [color=blue]
      >
      > Unfortunately, it's not very simple, as you can see -- although you only
      > need to override two methods (ProxyClient.da taReceived and
      > ProxyServer.dat aReceived), you need to make four subclasses to tie it all
      > together.
      >[/color]


      I was thinking about this too, and I was wondering if it might be good to
      add a "verbosity" switch to either mktap or twistd, along the lines of
      what netcat or ssh does. (ie, include 2 or 3 -v switches for additional
      verbosity). It seems like it would definitely be helpful in certain
      situations.

      Comment

      • Markus Wankus

        #4
        Re: Twisted - extending portforward (simple example)

        On 27 Jun 2003 03:42:20 -0700, Paul Moore <paul.moore@ato sorigin.com>
        wrote:
        [color=blue]
        > I hit a problem yesterday with my mail connection.[/color]

        Well, if you are unlucky enough to be a Bell Sympatico customer (like me)
        on the 26th the blocked all traffic on port 25 without telling anybody. It
        was really rather nice of them...

        Gee, let's first raise the rates for the same service, then put a bandwidth
        cap on it, and now block port 25...and see how many people get pissed.

        --
        Markus

        Comment

        • Steve Holden

          #5
          Re: Twisted - extending portforward (simple example)

          "Markus Wankus" <markus_wankus@ hotmail.com> wrote in message
          news:oprrhiyfcd [email protected] .sympatico.ca.. .[color=blue]
          > On 27 Jun 2003 03:42:20 -0700, Paul Moore <paul.moore@ato sorigin.com>
          > wrote:
          >[color=green]
          > > I hit a problem yesterday with my mail connection.[/color]
          >
          > Well, if you are unlucky enough to be a Bell Sympatico customer (like me)
          > on the 26th the blocked all traffic on port 25 without telling anybody.[/color]
          It[color=blue]
          > was really rather nice of them...
          >
          > Gee, let's first raise the rates for the same service, then put a[/color]
          bandwidth[color=blue]
          > cap on it, and now block port 25...and see how many people get pissed.
          >[/color]

          Yeah, Cox recently installed a block on outgoing port 25 connections. Their
          justification is "it helps us to reduce the amount of spam coming out of our
          domain". At least they will let me use any email address I like as long as I
          connect to their SMTP servers from inside their network, but unfotunately
          they don't appear to have any way to let me do the same thing when I'm
          somewhere else (like a customer location), so they've effectively forced me
          to use a dual setup for my mail.

          All done without any advance notice, of course. Internet "access" takes on a
          whole new meaning in these guys' hands :-)


          regards
          --
          Steve Holden http://www.holdenweb.com/
          Python Web Programming http://pydish.holdenweb.com/pwp/



          Comment

          Working...