[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Proxy script using IO::Async won't loop.

Asked by Wibble_ in Perl Programming Language, CGI Scripting, Web Authoring

Tags: perl, proxy, script, IO::Async

I'm trying to write a proxy script that looks at the incoming traffic, and either identifies it as XMLRPC, or not, and proxies it to either an XMLRPC server or to another server.

The problem I have is that I can't seem to identify the traffic before i have to make the second outgoing connection.

I'm sure that I'm missing something obvious, but i can't find it.

If you take out the if ( defined $connect_host{$p} ) loop it works, but it connects to the outgoing server before identifying the traffic (which is no good, as we need to identify before we connect, as the services operate on different ports.)

I've been trying to get this working for about 12 hours now, and it's driving me around the bend.

Any pointers would be hugely appreciated. :-)

W.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
#!/usr/bin/perl
use strict;
use warnings;
 
use IO::Async::Loop;
use IO::Async::Stream;
 
our $LOGLEVEL = 4; # 1-Error, 2-Warn, 3-info, 4-debug, 5-silly
 
my $LISTEN_PORT = 3389;
my $CONNECT_HOST = "10.0.0.64"; #- to be replaced
my $CONNECT_PORT = 1080; #- to be replaced 
 
my %stream1 = ();
my %stream2 = ();
my %connect_port = (); 
my %connect_host = ();
 
my $loop = IO::Async::Loop->new;
 
 
$loop->listen
(
	service	=> $LISTEN_PORT,
	socktype => 'stream',
 
	on_accept => sub 
	{
		my ( $socket1 ) = @_;
 
		# $socket is just an IO::Socket reference
		my $p = $socket1->peerhost . ":" . $socket1->peerport;
		
		wlog(3,"New connection from $p");
 
		##############################################
		$stream1{$p} = IO::Async::Stream->new
		(
			handle => $socket1,
 
			on_read => sub 
			{
				my ( $self, $buffref, $closed ) = @_;
				if( $$buffref =~ /RPC2/ ) 
				{
					wlog (3,"XMLRPC detected");
					($connect_host{$p},$connect_port{$p}) = xmlrpc($$buffref);
					wlog(4,"host:" . $connect_host{$p} . " port:" . $connect_port{$p});
				}
				# Just copy all the data
				if (defined $stream2{$p})
				{
					$stream2{$p}->write( $$buffref ); $$buffref = "";
				}
				return 0;
			},
			on_closed => sub 
			{
				$stream2{$p}->close_when_empty;
				delete $stream2{$p};
				wlog(3,"Connection from $p closed");
			},
		);
		$loop->add( $stream1{$p} );
		##############################################
		wlog(4,"MARK: if defined connect_host");
		if ( defined $connect_host{$p} )
		{
			wlog(4,"MARK: Start of loop->connect");
			wlog(4,"connect_host set to". $connect_host{$p}) if defined $connect_host{$p};
			$loop->connect(
				host	=> $CONNECT_HOST, #should be $connect_host{$p}
				service => $CONNECT_PORT, #should be $connect_port{$p}
	
				on_connected => sub 
				{
					my ( $socket2 ) = @_;
	
					$stream2{$p} = IO::Async::Stream->new
					(
						handle => $socket2,
	
						on_read => sub 
						{
							my ( $self, $buffref, $closed ) = @_;
							# Just copy all the data
							$stream1{$p}->write( $$buffref ); $$buffref = "";
							return 0;
						},
						on_closed => sub 
						{
							$stream1{$p}->close_when_empty;
							wlog(3,"Connection to $CONNECT_HOST:$CONNECT_PORT closed");
						},
					);
	
					#$loop->add( $stream1{$p} );
					$loop->add( $stream2{$p} );
				},
	
				on_resolve_error => sub { wlog(1,"Cannot resolve - $_[0]"); },
				on_connect_error => sub { wlog(1,"Cannot connect"); },
			);
		}
		#############################################
	},
 
	on_resolve_error => sub { wlog(1,"Cannot resolve - $_[0]"); die;},
	on_listen_error  => sub { wlog(1,"Cannot listen"); die; },
);
 
$loop->loop_forever;
 
sub wlog
{
	my ($level,$msg) = @_;
	if ($LOGLEVEL >= $level)
	{
		#for now print to sderror.
		print STDERR $msg . "\n";
	}
}
 
sub xmlrpc
{
	my $input = @_;
	#magic goes here
	return ("10.0.0.64", 1080);
}
[+][-]06/18/09 03:05 PM, ID: 24662176Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Perl Programming Language, CGI Scripting, Web Authoring
Tags: perl, proxy, script, IO::Async
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 1
Solution Grade: B
 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625