-
Notifications
You must be signed in to change notification settings - Fork 20
/
dominator.pl
90 lines (71 loc) · 1.92 KB
/
dominator.pl
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
use LWP::UserAgent;
use JSON;
use Config::IniFiles;
use POSIX qw(strftime);
# required for --log-master
STDOUT->autoflush(1);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $cfg = Config::IniFiles->new( -file => "/etc/uwsgi/local.ini" );
my $base_url = 'https://'.$cfg->val('uwsgi', 'api_domain').'/api/private';
my $ssl_key = $cfg->val('uwsgi', 'api_client_key_file');
my $ssl_cert = $cfg->val('uwsgi', 'api_client_cert_file');
my $timeout = 30;
for(;;) {
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_key_file => $ssl_key,
SSL_cert_file => $ssl_cert,
);
$ua->timeout($timeout);
my $response = $ua->get($base_url.'/domains/rsa/');
if ($response->is_error or $response->code != 200 ) {
print date().' oops: '.$response->code.' '.$response->message."\n";
exit;
}
my $customers = decode_json($response->decoded_content);
my %available_domains = {};
foreach(@{$customers}) {
my $domains = $_->{domains};
foreach my $domain(@{$domains}) {
next unless valid_domain($domain->{name})."\n";
my $domain_pem = '/etc/uwsgi/domains/'.$domain->{name}.'.pem';
if (-f $domain_pem) {
my @st = stat($domain_pem);
if ($domain->{mtime} > $st[9]) {
write_rsa($domain_pem, $_->{rsa});
}
}
else {
write_rsa($domain_pem, $_->{rsa});
}
$available_domains{$domain->{name}.'.pem'} = 1;
}
}
opendir DIR,'/etc/uwsgi/domains';
@files = readdir(DIR);
closedir(DIR);
foreach(@files) {
next if /^\./;
next if $_ eq $cfg->val('uwsgi', 'api_domain').'.pem';
unless(exists($available_domains{$_})) {
unlink '/etc/uwsgi/domains/'.$_;
print date().' '.$_." removed\n";
}
}
sleep(30);
}
sub write_rsa {
my ($domain_file, $rsa) = @_;
open PEM,'>'.$domain_file;
print PEM $rsa;
close(PEM);
print date().' '.$domain_file." updated\n";
};
sub date {
return strftime "%Y-%m-%d %H:%M:%S", localtime;
}
sub valid_domain {
my ($name) = @_;
return 1 if $name =~ /[a-zA-Z0-9\.\-]/;
return 0;
}