| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # proxy::ssh2tcp Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Proxy::Ssh2tcp; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::System::Process); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable ssh tcp socket netcat) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | hostname => [ qw(listen_hostname) ], | ||
| 20 | port => [ qw(listen_port) ], | ||
| 21 | username => [ qw(username) ], | ||
| 22 | ssh_hostname_port => [ qw(ssh_hostname_port) ], | ||
| 23 | remote_hostname_port => [ qw(remote_hostname_port) ], | ||
| 24 | st => [ qw(INTERNAL) ], | ||
| 25 | so => [ qw(INTERNAL) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | username => 'root', | ||
| 29 | hostname => '127.0.0.1', | ||
| 30 | port => 8888, | ||
| 31 | }, | ||
| 32 | commands => { | ||
| 33 | start => [ qw(ssh_hostname_port|OPTIONAL remote_hostname_port|OPTIONAL) ], | ||
| 34 | tunnel_loop => [ qw(remote_hostname_port) ], | ||
| 35 | background_tunnel_loop => [ qw(remote_hostname_port) ], | ||
| 36 | is_started => [ ], | ||
| 37 | stop => [ ], | ||
| 38 | }, | ||
| 39 | require_modules => { | ||
| 40 | 'Metabrik::Client::Openssh' => [ ], | ||
| 41 | 'Metabrik::Server::Tcp' => [ ], | ||
| 42 | }, | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _handle_sigint { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | my $restore = $SIG{INT}; | ||
| 50 | |||
| 51 | $SIG{INT} = sub { | ||
| 52 | $self->log->debug("brik_init: INT caught"); | ||
| 53 | $SIG{INT} = $restore; | ||
| 54 | $self->stop; | ||
| 55 | return 1; | ||
| 56 | }; | ||
| 57 | |||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub brik_init { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | $self->_handle_sigint; | ||
| 65 | |||
| 66 | return $self->SUPER::brik_init(@_); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub is_started { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | if (defined($self->st)) { | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub background_tunnel_loop { | ||
| 80 | my $self = shift; | ||
| 81 | my $args = \@_; | ||
| 82 | |||
| 83 | return $self->start(sub { $self->start(@$args) && $self->tunnel_loop(@$args) }); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub start { | ||
| 87 | my $self = shift; | ||
| 88 | my ($ssh_hostname_port, $remote_hostname_port, $username) = @_; | ||
| 89 | |||
| 90 | my $hostname = $self->hostname; | ||
| 91 | my $port = $self->port; | ||
| 92 | $ssh_hostname_port ||= $self->ssh_hostname_port; | ||
| 93 | $remote_hostname_port ||= $self->remote_hostname_port; | ||
| 94 | $username ||= $self->username; | ||
| 95 | $self->brik_help_run_undef_arg('start', $ssh_hostname_port) or return; | ||
| 96 | my $ref = $self->brik_help_run_invalid_arg('start', $ssh_hostname_port, 'ARRAY', 'SCALAR') | ||
| 97 | or return; | ||
| 98 | $self->brik_help_run_undef_arg('start', $remote_hostname_port) or return; | ||
| 99 | if ($remote_hostname_port !~ /^[^:]+:\d+$/) { | ||
| 100 | return $self->log->error("start: invalid format for remote_hostname_port [$remote_hostname_port], must be hostname:port"); | ||
| 101 | } | ||
| 102 | |||
| 103 | my $so; | ||
| 104 | # Only one hop | ||
| 105 | if ($ref eq 'SCALAR') { | ||
| 106 | if ($ssh_hostname_port !~ /^[^:]+:\d+$/) { | ||
| 107 | return $self->log->error("start: invalid format for ssh_hostname_port [$ssh_hostname_port], must be hostname:port"); | ||
| 108 | } | ||
| 109 | |||
| 110 | my ($ssh_hostname, $ssh_port) = split(':', $ssh_hostname_port); | ||
| 111 | |||
| 112 | $so = Metabrik::Client::Openssh->new_from_brik_init($self) or return; | ||
| 113 | $so->username($username); | ||
| 114 | $so->connect($ssh_hostname, $ssh_port) or return; | ||
| 115 | |||
| 116 | $self->log->verbose("start: connected to SSH [$ssh_hostname]:$ssh_port"); | ||
| 117 | } | ||
| 118 | # Multiple hops :) | ||
| 119 | elsif ($ref eq 'ARRAY') { | ||
| 120 | my @ok = (); | ||
| 121 | for my $this (@$ssh_hostname_port) { | ||
| 122 | if ($this !~ /^[^:]+:\d+$/) { | ||
| 123 | $self->log->verbose("start: invalid format for this [$this], must be hostname:port"); | ||
| 124 | next; | ||
| 125 | } | ||
| 126 | push @ok, $this; | ||
| 127 | } | ||
| 128 | |||
| 129 | if (@ok < 2) { | ||
| 130 | return $self->log->error("start: cannot chain with only one proxy"); | ||
| 131 | } | ||
| 132 | |||
| 133 | # Build hop chain | ||
| 134 | my $path = []; | ||
| 135 | my $hop = 1; | ||
| 136 | my $lport = $port+1; | ||
| 137 | my $target = $remote_hostname_port; | ||
| 138 | while (1) { | ||
| 139 | if ($hop == 1) { | ||
| 140 | my $this = shift @ok; | ||
| 141 | my $next = shift @ok; | ||
| 142 | last unless defined $next; | ||
| 143 | push @$path, { | ||
| 144 | from => $this, | ||
| 145 | to => $next, | ||
| 146 | host => 'localhost', | ||
| 147 | port => $lport++, | ||
| 148 | }; | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | my $next = shift @ok; | ||
| 152 | last unless defined $next; | ||
| 153 | push @$path, { | ||
| 154 | from => $path->[-1]->{host}.':'.$path->[-1]->{port}, | ||
| 155 | to => $next, | ||
| 156 | host => 'localhost', | ||
| 157 | port => $lport++, | ||
| 158 | }; | ||
| 159 | } | ||
| 160 | $hop++; | ||
| 161 | } | ||
| 162 | |||
| 163 | push @$path, { | ||
| 164 | from => $path->[-1]->{host}.':'.$path->[-1]->{port}, | ||
| 165 | to => $remote_hostname_port, | ||
| 166 | host => 'localhost', | ||
| 167 | port => $port, | ||
| 168 | }; | ||
| 169 | |||
| 170 | use Data::Dumper; | ||
| 171 | print Dumper($path)."\n"; | ||
| 172 | #return 1; | ||
| 173 | |||
| 174 | $hop = 1; | ||
| 175 | for my $this (@$path) { | ||
| 176 | $self->hostname($this->{host}); | ||
| 177 | $self->port($this->{port}); | ||
| 178 | $self->log->verbose("background_start: ". | ||
| 179 | "from: ".$this->{from}." to: ".$this->{to}. " listen: ".$this->{port} | ||
| 180 | ); | ||
| 181 | |||
| 182 | #$self->start($this->{from}, $this->{to}) or return; | ||
| 183 | $self->background_tunnel_loop($this->{from}, $this->{to}) or return; | ||
| 184 | |||
| 185 | $self->log->verbose("start: connected to SSH hop [$hop] [".$this->{to}."]"); | ||
| 186 | |||
| 187 | # XXX: do better | ||
| 188 | sleep(5); # Wait for tunnel to be established | ||
| 189 | |||
| 190 | $hop++; | ||
| 191 | } | ||
| 192 | |||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | my $st = Metabrik::Server::Tcp->new_from_brik_init($self) or return; | ||
| 197 | $st->hostname($self->hostname); | ||
| 198 | $st->port($self->port); | ||
| 199 | |||
| 200 | my $server = $st->start or return; | ||
| 201 | $self->st($st); | ||
| 202 | $self->so($so); | ||
| 203 | |||
| 204 | $self->tunnel_loop($remote_hostname_port) or return; | ||
| 205 | |||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub tunnel_loop { | ||
| 210 | my $self = shift; | ||
| 211 | my ($remote_hostname_port) = @_; | ||
| 212 | |||
| 213 | if (! $self->is_started) { | ||
| 214 | return $self->log->error($self->brik_help_run('start')); | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->brik_help_run_undef_arg('tunnel_loop', $remote_hostname_port) or return; | ||
| 218 | if ($remote_hostname_port !~ /^[^:]+:\d+$/) { | ||
| 219 | return $self->log->error("start: invalid format for remote_hostname_port [$remote_hostname_port], must be hostname:port"); | ||
| 220 | } | ||
| 221 | |||
| 222 | my ($remote_hostname, $remote_port) = split(':', $remote_hostname_port); | ||
| 223 | |||
| 224 | my $st = $self->st; | ||
| 225 | my $server = $st->socket; | ||
| 226 | my $select = $st->select; | ||
| 227 | my $clients = $st->clients; | ||
| 228 | |||
| 229 | my $so = $self->so; | ||
| 230 | |||
| 231 | while (1) { | ||
| 232 | last if ! $self->is_started; # Used to stop the process on SIGINT | ||
| 233 | |||
| 234 | if (my $ready = $st->wait_readable) { | ||
| 235 | for my $sock (@$ready) { | ||
| 236 | my ($id, $this_client, $this_tunnel) = $self->_get_tunnel_from_sock($clients, $sock); | ||
| 237 | if ($sock == $server) { | ||
| 238 | $self->log->verbose("start: server socket ready"); | ||
| 239 | my $client = $st->accept; | ||
| 240 | |||
| 241 | $self->log->verbose("start: new connection from [". | ||
| 242 | $client->{ipv4}."]:".$client->{port}); | ||
| 243 | |||
| 244 | my $tunnel = $so->open_tunnel($remote_hostname, $remote_port) or return; | ||
| 245 | $select->add($tunnel); | ||
| 246 | $client->{tunnel} = $tunnel; | ||
| 247 | |||
| 248 | $self->log->verbose("start: tunnel opened to [$remote_hostname]:$remote_port"); | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | if ($sock == $this_client) { # Client sent something | ||
| 252 | my $buf = $st->read($this_client); | ||
| 253 | if (! defined($buf)) { | ||
| 254 | $self->log->verbose("start: client disconnected"); | ||
| 255 | $select->remove($this_client); | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $self->log->verbose("start: read from client [".length($buf)."]"); | ||
| 259 | $self->log->verbose("start: write to tunnel [".length($buf)."]"); | ||
| 260 | $this_tunnel->syswrite($buf); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | elsif ($sock == $this_tunnel) { | ||
| 264 | my $buf = $st->read($this_tunnel); | ||
| 265 | if (! defined($buf)) { | ||
| 266 | # If tunnel is disconnected, we can wipe the full connecting client state. | ||
| 267 | # And only at that time. | ||
| 268 | $self->log->verbose("start: tunnel disconnected"); | ||
| 269 | $select->remove($this_tunnel); | ||
| 270 | close($this_tunnel); | ||
| 271 | $st->client_disconnected($id); | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | $self->log->verbose("start: read from tunnel [".length($buf)."]"); | ||
| 275 | $self->log->debug("start: read from tunnel [$buf]"); | ||
| 276 | $self->log->verbose("start: write to client [".length($buf)."]"); | ||
| 277 | $self->log->debug("start: write to client [$buf]"); | ||
| 278 | $this_client->syswrite($buf); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | return 1; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub stop { | ||
| 290 | my $self = shift; | ||
| 291 | |||
| 292 | if (! $self->is_started) { | ||
| 293 | return $self->log->verbose("stop: not started"); | ||
| 294 | } | ||
| 295 | |||
| 296 | my $st = $self->st; | ||
| 297 | |||
| 298 | # server::tcp know nothing about tunnels, we have to clean by ourselves | ||
| 299 | my $clients = $st->clients; | ||
| 300 | for my $this (keys %$clients) { | ||
| 301 | if (exists($clients->{$this}{tunnel})) { | ||
| 302 | close($clients->{$this}{tunnel}); | ||
| 303 | $self->log->verbose("stop: tunnel for client [$this] closed"); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | $st->stop; | ||
| 308 | |||
| 309 | $self->_handle_sigint; # Reharm the signal | ||
| 310 | |||
| 311 | $self->st(undef); | ||
| 312 | |||
| 313 | return 1; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub _get_tunnel_from_sock { | ||
| 317 | my $self = shift; | ||
| 318 | my ($clients, $sock) = @_; | ||
| 319 | |||
| 320 | my $client; | ||
| 321 | my $this_client; | ||
| 322 | my $this_tunnel; | ||
| 323 | for my $k (keys %$clients) { | ||
| 324 | if ($sock == $clients->{$k}{socket} || $sock == $clients->{$k}{tunnel}) { | ||
| 325 | $client = $k; | ||
| 326 | $this_client = $clients->{$k}{socket}; | ||
| 327 | $this_tunnel = $clients->{$k}{tunnel}; | ||
| 328 | last; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | return ( $client, $this_client, $this_tunnel ); | ||
| 333 | } | ||
| 334 | |||
| 335 | 1; | ||
| 336 | |||
| 337 | __END__ | ||