File: ftpd

package info (click to toggle)
libalien-build-perl 2.84-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,116 kB
  • sloc: perl: 10,350; ansic: 134; sh: 66; makefile: 2
file content (90 lines) | stat: -rwxr-xr-x 1,850 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;
use URI;
use AnyEvent; ## no critic
use AnyEvent::FTP::Server;
use JSON::PP qw( encode_json decode_json );
use Path::Tiny qw( path );
use Getopt::Long qw( GetOptions );

my $daemon = 0;
my $kill   = 0;
my $host   = 'localhost';

GetOptions(
  "d"       => \$daemon,
  "k"       => \$kill,
  "hosth=s" => \$host,
);

my $bindir    = path(__FILE__)->parent->absolute;
my $distdir   = $bindir->parent->parent;

my $config_file = $bindir->child('ftpd.json');

if(-r $config_file)
{
  my $config = decode_json($config_file->slurp);
  my $pid = $config->{pid};
  if(defined $pid)
  {
    kill 'KILL', $pid;
  }
}

exit if $kill;

if($daemon)
{
  require Proc::Daemon;
  my $daemon = Proc::Daemon->new(
    child_STDOUT => $bindir->child('ftpd.log')->stringify,
    child_STDERR => $bindir->child('ftpd.log')->stringify,
  );
  $daemon->Init;
}

my $server = AnyEvent::FTP::Server->new(
  host            => $host,
  port            => 0,
  default_context => 'AnyEvent::FTP::Server::Context::FSRO',
);

my %config = (
  user => join('', map { chr(ord('a') + int rand(26)) } (1..10)),
  pass => join('', map { chr(ord('a') + int rand(26)) } (1..10)),
  root => $distdir->child('corpus/dist')->stringify,
  pid             => $$,
);

my $url = URI->new("ftp://localhost");
$url->host($host);
$url->path($config{root});
$url->user($config{user});
$url->password($config{pass});

$server->on_bind(sub {
  my $port = shift;
  $url->port($port);
});

$server->on_connect(sub {
  my $con = shift;
  $con->context->authenticator(sub {
    my($user, $pass) = @_;
    $user eq $config{user} && $pass eq $config{pass} ? 1 : 0;
  });
  $con->context->bad_authentication_delay(0);
});


$server->start;

$config{url} = $url->as_string;
$config_file->spew(encode_json(\%config));

print "$url\n";

AnyEvent->condvar->recv;