| 1 | # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | package SMB::Proxy; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB::Server'; | ||
| 22 | |||
| 23 | use SMB::Client; | ||
| 24 | |||
| 25 | use IO::Socket; | ||
| 26 | |||
| 27 | sub new ($%) { | ||
| 28 | my $class = shift; | ||
| 29 | my %options = @_; | ||
| 30 | |||
| 31 | my $quiet = $options{quiet} ||= 0; | ||
| 32 | my $verbose = $options{verbose} ||= 0; | ||
| 33 | |||
| 34 | my %client_options = map { $_ => delete $options{$_} } | ||
| 35 | qw(server_addr server_username server_password quiet verbose); | ||
| 36 | |||
| 37 | my $self = $class->SUPER::new( | ||
| 38 | %options, | ||
| 39 | quiet => $quiet, | ||
| 40 | verbose => $verbose, | ||
| 41 | share_roots => '-', | ||
| 42 | client_options => \%client_options, | ||
| 43 | ); | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | # on connection from client, create connection to server | ||
| 49 | sub on_connect ($$) { | ||
| 50 | my $self = shift; | ||
| 51 | my $connection = shift; | ||
| 52 | |||
| 53 | my %options = %{$self->{client_options}}; | ||
| 54 | my $client = SMB::Client->new( | ||
| 55 | $options{server_addr}, | ||
| 56 | quiet => $options{quiet}, | ||
| 57 | verbose => $options{verbose}, | ||
| 58 | username => $options{server_username}, | ||
| 59 | password => $options{server_password}, | ||
| 60 | just_socket => 1, | ||
| 61 | ); | ||
| 62 | |||
| 63 | my $connection2 = $self->add_connection($client->socket, -$self->client_id); | ||
| 64 | |||
| 65 | $connection->{connection2} = $connection2; | ||
| 66 | $connection2->{connection2} = $connection; | ||
| 67 | } | ||
| 68 | |||
| 69 | # on disconnection from client or server, disconnect the other end | ||
| 70 | sub on_disconnect ($$) { | ||
| 71 | my $self = shift; | ||
| 72 | my $connection = shift; | ||
| 73 | |||
| 74 | $self->delete_connection($connection->connection2); | ||
| 75 | } | ||
| 76 | |||
| 77 | # just forward packet to the other end, ignore the actual command semantics | ||
| 78 | sub recv_command ($$) { | ||
| 79 | my $self = shift; | ||
| 80 | my $connection = shift; | ||
| 81 | |||
| 82 | $connection->recv_nbss or return; | ||
| 83 | $connection->connection2->send_nbss($connection->parser->data); | ||
| 84 | |||
| 85 | return "dummy"; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub on_command ($$$) { | ||
| 89 | my $self = shift; | ||
| 90 | my $connection = shift; | ||
| 91 | my $command = shift; | ||
| 92 | |||
| 93 | # ignore a dummy command | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; |