better output, fixed install
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 17 Feb 2010 22:49:26 +0000 (23:49 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 17 Feb 2010 22:49:26 +0000 (23:49 +0100)
server.pl

index ebfdb31..b6194e2 100755 (executable)
--- a/server.pl
+++ b/server.pl
@@ -7,7 +7,15 @@ only depenency) to ad-hoc remote server
 
 Install on client with:
 
-  echo install | nc 10.60.0.92 9001 > bak ; chmod 755 bak
+  echo install | nc 127.0.0.1 9001 > bak ; chmod 755 bak
+
+Start server with:
+
+  ./server.pl /path/to/backup 127.0.0.1
+
+You will want to add following to C<~/.ssh/config>
+
+  RemoteForward 9001 localhost:9001
 
 =cut
 
@@ -17,30 +25,46 @@ use autodie;
 use IO::Socket::INET;
 use File::Path;
 
-my $dir = shift @ARGV || die "usage: $0 /backup/directory\n";
+my ( $dir, $server_ip ) = @ARGV;
+die "usage: $0 /backup/directory\n" unless $dir;
+$server_ip ||= '127.0.0.1';
+
+my $shell_client = <<__SHELL_CLIENT__;
+#!/bin/sh
+echo `pwd` \$* | nc $server_ip 9001
+__SHELL_CLIENT__
 
 chdir $dir;
 system 'git init' unless -e '.git';
 
 my $server = IO::Socket::INET->new(
        Proto     => 'tcp',
+       LocalAddr => $server_ip,
        LocalPort => 9001,
        Listen    => SOMAXCONN,
        Reuse     => 1
 ) || die $!;
 
+
+warn "dir: $dir listen: $server_ip:9001\n"
+       , "remote-host> echo install | nc $server_ip 9001 > bak ; chmod 755 bak\n"
+       , $shell_client
+;
+
 while (my $client = $server->accept()) {
        my ($pwd,$command,$path,$message) = split(/\s+/,<$client>,4);
        my $ip = $client->peerhost;
 
-       $message ||= '';
-       $path = "$pwd/$path" unless $path =~ m{^/};
-
-       if ( $command eq 'install' ) {
-               print $client '#!/bin/sh',$/,'echo `pwd` $* | nc 10.60.0.92 9001',$/;
+       if ( $pwd eq 'install' ) {
+               warn "install on $ip\n";
+               print $client $shell_client;
+               close($client);
                next;
        }
 
+       $message ||= '';
+       $path = "$pwd/$path" unless $path =~ m{^/};
+
        warn "$ip [$command] $path | $message\n";
 
        if ( ! -d $ip ) {