| 1 | package Net::SSH::Perl::Proxy; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Net::SSH::Perl'; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Net::SSH::Perl::Proxy - Use TCP proxy to connect to a host via SSH | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | my $ssh = Net::SSH::Perl::Proxy->new('myhost', | ||
| 13 | proxy => { | ||
| 14 | host => 'proxyhost', | ||
| 15 | port => 1080, | ||
| 16 | } | ||
| 17 | ); | ||
| 18 | |||
| 19 | $ssh->login(); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This class extends C<Net::SSH::Perl> to allow connections through | ||
| 24 | a SOCKS proxy. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | use Carp; | ||
| 29 | use IO::Socket::Socks; | ||
| 30 | |||
| 31 | sub _init { | ||
| 32 | my $self = shift; | ||
| 33 | my %parm = @_; | ||
| 34 | if ($parm{proxy}) { | ||
| 35 | $self->{Proxy} = { | ||
| 36 | ProxyAddr => $parm{proxy}{host}, | ||
| 37 | ProxyPort => $parm{proxy}{port}, | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | $self->SUPER::_init(%parm); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _connect { | ||
| 44 | my $ssh = shift; | ||
| 45 | return $ssh->SUPER::_connect(@_) unless $ssh->{Proxy}; | ||
| 46 | |||
| 47 | my $rport = $ssh->{config}->get('port') || 22; | ||
| 48 | $ssh->debug("Connecting to $ssh->{host}:$rport"); | ||
| 49 | my $sock = IO::Socket::Socks->new( | ||
| 50 | ConnectAddr => $ssh->{host}, | ||
| 51 | ConnectPort => $rport, | ||
| 52 | %{$ssh->{Proxy}}, | ||
| 53 | Timeout => 5, | ||
| 54 | ) or die "Can't connect to $ssh->{host}:$rport : $!"; | ||
| 55 | |||
| 56 | select((select($sock), $|=1)[0]); | ||
| 57 | |||
| 58 | $ssh->{session}{sock} = $sock; | ||
| 59 | $ssh->_exchange_identification; | ||
| 60 | |||
| 61 | defined( $sock->blocking(0) ) or die "Can't set non-blocking: $!"; | ||
| 62 | $ssh->debug("Connection established."); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | __END__ |