Thread

Posted on Tue Mar 21 11:25:33 2006 by martinw
Callback for filtering capabilities
Hi Phillipe, Hi Perl users,

I want to implement a little imap proxy with filtering capabilities.
Therefore put a small piece of code into Proxy.pm to do the filtering in a
general manner.

I've appended a patch to Proxy.pm at the end of the posting. Maybe helpful for the further
development.

Example usage

#!/usr/bin/perl # use strict; use Net::Proxy; my $proxy = Net::Proxy->new( { in => { type => 'tcp', port => 143, hook => \&readData }, out => { type => 'tcp', host => 'imap.lw-systems.de', port => '143'}, }); $proxy->register(); Net::Proxy->mainloop(); ### CALLBACK ### sub readData { my ($direction,$data) = @_; # direction may be 'in' or 'out' if ($direction eq 'in') { # incoming connection my ($tag,$cmd,$args) = split /\s+/, $data, 3; print STDERR "IMAP command: $cmd\n"; } return $data; }



Patch to Proxy.pm

--- Proxy.pm_org 2006-03-21 10:03:11.489872434 +0100 +++ Proxy.pm 2006-03-21 10:01:06.200640759 +0100 @@ -19,6 +19,11 @@ in => {}, out => {}, ); +my %CALLBACK = ( + in => {}, + out => {}, +); + my $VERBOSITY = 0; # be silent by default # @@ -57,6 +62,11 @@ $args->{$conn}{_proxy_} = $self; $CONNECTOR{$conn}{ refaddr $self} = $class->new( $args->{$conn} ); $CONNECTOR{$conn}{ refaddr $self}->set_proxy($self); + + if (ref ($args->{$conn}->{hook}) =~ /CODE/) { + $CALLBACK{$conn}{$CONNECTOR{$conn}{ refaddr $self}} = + $args->{$conn}->{hook}; + } } return $self; @@ -202,8 +212,11 @@ my $data = $conn->read_from($sock); next SOCKET if !defined $data; - # TODO filtering by the proxy - + # Callback to a custom filter function + my $direction = 'in'; $direction = 'out' if $conn->is_out() ; + if ($CALLBACK{$direction}{$conn}) { + $data = $CALLBACK{$direction}{$conn}($direction,$data); + } Net::Proxy->get_connector($peer)->write_to( $peer, $data ); } }
Direct Responses: 1993 | Write a response
Posted on Tue Mar 21 12:42:01 2006 by book in response to 1991
Re: Callback for filtering capabilities

As the TODO line shows, I knew I would have to add this at some point. :-) But I didn't want to implement the kind of complex stack of filters that HTTP::Proxy provides.

I thought about providing some kind of hook for a single callback, just like your patch. Then again, being able to handle a list of callbacks to call in order could be nice.

I like the "hook" key you added and will provide this (or something very similar using references for passing the data around) in the next release. Net::Proxy will probably be able to manage a reference to an array of callbacks as well.

Thanks a lot for your input and patch.

Write a response