| 1 | package MYDan::Agent::KeyUpdate; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Agent::KeyUpdate | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Agent::KeyUpdate; | ||
| 10 | |||
| 11 | MYDan::Agent::KeyUpdate->new( auth => /path/, interval => 3600, url => 'https://xxx' )->run(); | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | use YAML::XS; | ||
| 20 | use File::Basename; | ||
| 21 | use LWP::UserAgent; | ||
| 22 | use File::Temp; | ||
| 23 | use Digest::MD5; | ||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my ( $class, %self ) = @_; | ||
| 28 | |||
| 29 | map{ die "$_ undef" unless $self{$_} }qw( url auth ); | ||
| 30 | |||
| 31 | die "noauth" unless -e $self{auth}; | ||
| 32 | |||
| 33 | bless \%self, ref $class || $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub run | ||
| 37 | { | ||
| 38 | my $this = shift; | ||
| 39 | |||
| 40 | $this->update(); | ||
| 41 | |||
| 42 | return unless my $interval = $this->{interval}; | ||
| 43 | |||
| 44 | sleep int 3 + rand $interval; | ||
| 45 | |||
| 46 | while(1) | ||
| 47 | { | ||
| 48 | $this->update(); | ||
| 49 | sleep $interval; | ||
| 50 | } | ||
| 51 | |||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub update | ||
| 56 | { | ||
| 57 | my $this = shift; | ||
| 58 | |||
| 59 | print "update ...\n"; | ||
| 60 | |||
| 61 | my ( $url, $auth ) = @$this{qw( url auth )}; | ||
| 62 | return unless my $key = uaget( $url ); | ||
| 63 | |||
| 64 | my ( %d, %dd, $ddd ); | ||
| 65 | for( split /\n/, $key ) | ||
| 66 | { | ||
| 67 | my ( $md5, $url, $name ); | ||
| 68 | |||
| 69 | unless( ( $md5, $url ) = $_ =~ /^([a-zA-Z0-9]{32}):(http.*\.pub)$/ ) | ||
| 70 | { | ||
| 71 | $ddd = 1; next; | ||
| 72 | } | ||
| 73 | |||
| 74 | $dd{ $name = basename $url } = 1; | ||
| 75 | |||
| 76 | next if -e "$auth/$name"; | ||
| 77 | next unless my $c = uaget( $url ); | ||
| 78 | |||
| 79 | my $TEMP = File::Temp->new(); | ||
| 80 | print $TEMP $c; | ||
| 81 | seek $TEMP, 0, 0; | ||
| 82 | my $tmd5 = Digest::MD5->new()->addfile( $TEMP )->hexdigest(); | ||
| 83 | |||
| 84 | next unless lc( $md5 ) eq lc( $tmd5 ); | ||
| 85 | system "mv '$TEMP->filename' '$auth/$name'"; | ||
| 86 | } | ||
| 87 | |||
| 88 | return if $ddd; | ||
| 89 | |||
| 90 | map{ $d{ basename $_ } = 1 }glob "$auth/*.pub"; | ||
| 91 | map{ delete $dd{$_} if delete $d{$_} }keys %dd; | ||
| 92 | return if keys %dd; | ||
| 93 | map{ unlink "$auth/$_" }keys %d; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | sub uaget | ||
| 98 | { | ||
| 99 | my $url = shift; | ||
| 100 | |||
| 101 | my $ua = LWP::UserAgent->new(); | ||
| 102 | $ua->agent('Mozilla/9 [en] (Centos; Linux)'); | ||
| 103 | $ua->timeout( 10 ); | ||
| 104 | |||
| 105 | my $res = $ua->get( $url ); | ||
| 106 | my $code = $res->code(); | ||
| 107 | warn "get $url code: $code\n" unless $code == 200; | ||
| 108 | return $res->is_success ? $res->content : undef; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |