aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2015-04-01 21:31:31 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2015-04-01 21:31:31 (GMT)
commitb1cef26b845c3845d916ef2215254c15dff103a4 (patch)
treef0bd5c3430d58159f7a96b30d6c9c984f95babc8
downloadopentracker-manage-b1cef26b845c3845d916ef2215254c15dff103a4.zip
opentracker-manage-b1cef26b845c3845d916ef2215254c15dff103a4.tar.gz
Initial commit
-rwxr-xr-xmanage.pl239
1 files changed, 239 insertions, 0 deletions
diff --git a/manage.pl b/manage.pl
new file mode 100755
index 0000000..f7bbd31
--- /dev/null
+++ b/manage.pl
@@ -0,0 +1,239 @@
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Data::Dumper;
7use Pod::Usage;
8
9my $VERSION = '0.1';
10my %infohashes;
11#my $whitelist = '~/tracker/bin/whitelist.txt';
12my %opts = ('whitelist' => '/var/www/tracker/whitelist.txt');
13
14sub verbose {
15 return unless($opts{'verbose'});
16 my $msg = shift;
17 print "[VERBOSE] $msg\n";
18}
19
20sub load_whitelist {
21 verbose('Cleaning hash list.');
22 delete($infohashes{$_}) foreach (keys %infohashes);
23
24 verbose('Loading whitelist: '. $opts{'whitelist'});
25 open IN, '<', $opts{'whitelist'};
26 while(<IN>) {
27 chomp;
28
29 s/;.+\n//g; ## Remove stuff that hasn't been removed from this script.
30
31 if( /^\s*$/ or length($_) == 0 ) {
32 verbose('Found an empty line; skipping.');
33 next;
34 }
35
36 ## Found a good hash
37 if(/[0-9a-f]{20}/i) {
38 ## This removes duplicates
39 $infohashes{$_} = 1;
40 verbose('Found hash: '. $_);
41 } else {
42 print "Found a non-hash: '$_'\n";
43 }
44 }
45 close IN;
46}
47
48sub save_whitelist {
49 verbose('Saving whitelist: '. $opts{'whitelist'});
50 open OUT, '>', $opts{'whitelist'};
51 foreach my $hash ( keys %infohashes ) {
52 print OUT $hash, "\n";
53 }
54 close OUT;
55
56 ## Send a SIGHUP to the tracker processes
57 hup_server();
58}
59
60sub remove_hash {
61 load_whitelist();
62 my @remHash = @_;
63
64 for my $h ( @{$remHash[0]} ) {
65 $h = uc $h;
66 verbose('Attempting to remove '. $h. ' from the whitelist.');
67
68 unless($h =~ /[0-9a-f]{20}/i) {
69 verbose('Was not a hash; skipping.');
70 return;
71 }
72
73 if( $infohashes{$h} == 1 ) {
74 delete $infohashes{$h};
75 verbose('Removal successfull.');
76 } else {
77 print "Hash not found.\n";
78 }
79 }
80 save_whitelist();
81}
82
83sub add_hash {
84 load_whitelist();
85 my @addHash = @_;
86
87 for my $h (@{$addHash[0]}) {
88 verbose('Attempting to add '. $h. ' to the whitelist');
89 unless($h =~ /[0-9a-z]{20}/i) {
90 print "Invalid hash: '$h'\n";
91 return;
92 }
93
94 $infohashes{$h} = 1;
95 verbose('Addition successfull.');
96 }
97 save_whitelist();
98}
99
100sub hup_server {
101 ## Sending a SIGHUP to both processes tells them to reload the whitelist file.
102 #verbose('Sending a SIGHUP to all \'opentracker\' processes.');
103 #system('pkill', '-1', 'opentracker');
104 verbose('ignoring hup');
105}
106
107sub list_hashes {
108 load_whitelist();
109 my @hashes = keys %infohashes;
110 foreach my $h ( @hashes ) {
111 print "$h\n";
112 }
113
114 print "\n". ($#hashes + 1). " hashes found.\n";
115}
116
117Getopt::Long::Configure("bundling");
118GetOptions(\%opts, 'verbose|v', 'add|a=s@', 'remove|r=s@', 'list|l', 'huponly|h', 'whitelist|w=s');
119
120if(defined($opts{'huponly'})) {
121 hup_server();
122 exit(0);
123}
124
125my $didsomething = 0;
126
127if($opts{'whitelist'} ne '/home/nick/tracker/bin/whitelist.txt') {
128 verbose('Using non-default whitelist: '. $opts{'whitelist'});
129}
130
131if(defined($opts{'add'})) {
132 #print Dumper($opts{'add'});
133 add_hash($opts{'add'});
134 $didsomething = 1;
135}
136
137if(defined($opts{'remove'})) {
138 remove_hash($opts{'remove'});
139 $didsomething = 1;
140}
141
142if(defined($opts{'list'})) {
143 list_hashes();
144 $didsomething = 1;
145}
146
147#load_whitelist();
148
149my @hashes = keys(%infohashes);
150my $count = $#hashes + 1;
151
152#print $_."\n" foreach @hashes;
153#print "$count hashes found.\n";
154
155unless($didsomething == 1) {
156 pod2usage(
157 -message => "======\n== Error: No action performed!\n======\n",
158 -verbose => 1,
159 -noperldoc => 1,
160 );
161}
162
163
164__END__
165
166=head1 NAME
167
168manual-add-remove.pl
169
170=head1 SYNOPSIS
171
172=over 4
173
174=item manual-add-remove.pl <options>
175
176Manually add and remove hashes from opentracker.
177
178=back
179
180=head1 OPTIONS
181
182=over 4
183
184=item --add <hash>
185
186=item -a <hash>
187
188Add the given hash to the whitelist. This can be used multiple times.
189
190=item --huponly
191
192=item -h
193
194Don't add or remove anything, just make the server refresh the whitelist. All other arguments are ignored if this is specified.
195
196=item --list
197
198=item -l
199
200List all the hashes currently served.
201
202=item --remove <hash>
203
204=item -r <hash>
205
206Remove the given hash from the whiletlist. This can be used multiple times.
207
208=item --verbose
209
210=item -v
211
212Print B<everything>. Chances are you don't want this.
213
214=item --whitelist <file>
215
216=item -w <file>
217
218By default 'whitelist.txt' is used in the current directory. Use this to change that.
219
220=back
221
222=head1 EXAMPLES
223
224=over 4
225
226=item Adding multiple hashes
227
228manual-add-remove.pl --add b4b56d63aed238d91377576876c9489b2f39ffef --add 5492ad2882059c73b7bb7ff8dfe27930c7d6ba39
229
230=item Removing multiple hashes
231
232manual-add-remove.pl --remove b4b56d63aed238d91377576876c9489b2f39ffef --remove 5492ad2882059c73b7bb7ff8dfe27930c7d6ba39
233
234=item Adding and removing hashes
235
236manual-add-remove.pl --add b4b56d63aed238d91377576876c9489b2f39ffef --remove 5492ad2882059c73b7bb7ff8dfe27930c7d6ba39
237
238=cut
239