aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2015-04-01 21:48:10 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2015-04-01 21:48:10 (GMT)
commit0603cb5b6ab05a8faab8847419548937d581f02b (patch)
tree35776763337364af632572dfaed0c792b9190c70
parentb1cef26b845c3845d916ef2215254c15dff103a4 (diff)
downloadopentracker-manage-0603cb5b6ab05a8faab8847419548937d581f02b.zip
opentracker-manage-0603cb5b6ab05a8faab8847419548937d581f02b.tar.gz
- Tabs -> Spaces
- uc()'d all incoming hashes. - added note to now defunct hup_server() and error message to --huponly
-rwxr-xr-xmanage.pl206
1 files changed, 100 insertions, 106 deletions
diff --git a/manage.pl b/manage.pl
index f7bbd31..622b3e3 100755
--- a/manage.pl
+++ b/manage.pl
@@ -6,158 +6,152 @@ use Getopt::Long;
6use Data::Dumper; 6use Data::Dumper;
7use Pod::Usage; 7use Pod::Usage;
8 8
9my $VERSION = '0.1'; 9my $VERSION = '0.2';
10my %infohashes; 10my %infohashes;;
11#my $whitelist = '~/tracker/bin/whitelist.txt';
12my %opts = ('whitelist' => '/var/www/tracker/whitelist.txt'); 11my %opts = ('whitelist' => '/var/www/tracker/whitelist.txt');
13 12
14sub verbose { 13sub verbose {
15 return unless($opts{'verbose'}); 14 return unless($opts{'verbose'});
16 my $msg = shift; 15 my $msg = shift;
17 print "[VERBOSE] $msg\n"; 16 print "[VERBOSE] $msg\n";
18} 17}
19 18
20sub load_whitelist { 19sub load_whitelist {
21 verbose('Cleaning hash list.'); 20 verbose('Cleaning hash list.');
22 delete($infohashes{$_}) foreach (keys %infohashes); 21 delete($infohashes{$_}) foreach (keys %infohashes);
23 22
24 verbose('Loading whitelist: '. $opts{'whitelist'}); 23 verbose('Loading whitelist: '. $opts{'whitelist'});
25 open IN, '<', $opts{'whitelist'}; 24 open IN, '<', $opts{'whitelist'};
26 while(<IN>) { 25 while(<IN>) {
27 chomp; 26 chomp;
28 27
29 s/;.+\n//g; ## Remove stuff that hasn't been removed from this script. 28 s/;.+\n//g; ## Remove stuff that hasn't been removed from this script.
30 29
31 if( /^\s*$/ or length($_) == 0 ) { 30 if( /^\s*$/ or length($_) == 0 ) {
32 verbose('Found an empty line; skipping.'); 31 verbose('Found an empty line; skipping.');
33 next; 32 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 } 33 }
45 close IN;
46}
47 34
48sub save_whitelist { 35 ## Found a good hash
49 verbose('Saving whitelist: '. $opts{'whitelist'}); 36 if(/[0-9a-f]{20}/i) {
50 open OUT, '>', $opts{'whitelist'}; 37 ## This removes duplicates
51 foreach my $hash ( keys %infohashes ) { 38 $infohashes{$_} = 1;
52 print OUT $hash, "\n"; 39 verbose('Found hash: '. $_);
40 } else {
41 print "Found a non-hash: '$_'\n";
53 } 42 }
54 close OUT; 43 }
44 close IN;
45}
55 46
56 ## Send a SIGHUP to the tracker processes 47sub save_whitelist {
57 hup_server(); 48 verbose('Saving whitelist: '. $opts{'whitelist'});
49 open OUT, '>', $opts{'whitelist'};
50 foreach my $hash ( keys %infohashes ) {
51 print OUT $hash, "\n";
52 }
53 close OUT;
54
55 ## Send a SIGHUP to the tracker processes
56 hup_server();
58} 57}
59 58
60sub remove_hash { 59sub remove_hash {
61 load_whitelist(); 60 load_whitelist();
62 my @remHash = @_; 61 my @remHash = @_;
63 62
64 for my $h ( @{$remHash[0]} ) { 63 for my $h ( @{$remHash[0]} ) {
65 $h = uc $h; 64 $h = uc $h;
66 verbose('Attempting to remove '. $h. ' from the whitelist.'); 65 verbose('Attempting to remove '. $h. ' from the whitelist.');
67 66
68 unless($h =~ /[0-9a-f]{20}/i) { 67 unless($h =~ /[0-9a-f]{20}/i) {
69 verbose('Was not a hash; skipping.'); 68 verbose('Was not a hash; skipping.');
70 return; 69 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 } 70 }
80 save_whitelist(); 71
72 if( $infohashes{$h} ) {
73 delete $infohashes{$h};
74 verbose('Removal successfull.');
75 } else {
76 print "Hash not found.\n";
77 }
78 }
79 save_whitelist();
81} 80}
82 81
83sub add_hash { 82sub add_hash {
84 load_whitelist(); 83 load_whitelist();
85 my @addHash = @_; 84 my @addHash = @_;
86 85
87 for my $h (@{$addHash[0]}) { 86 for my $h (@{$addHash[0]}) {
88 verbose('Attempting to add '. $h. ' to the whitelist'); 87 $h = uc $h;
89 unless($h =~ /[0-9a-z]{20}/i) { 88 verbose('Attempting to add '. $h. ' to the whitelist');
90 print "Invalid hash: '$h'\n"; 89 unless($h =~ /[0-9a-z]{20}/i) {
91 return; 90 print "Invalid hash: '$h'\n";
92 } 91 return;
93
94 $infohashes{$h} = 1;
95 verbose('Addition successfull.');
96 } 92 }
97 save_whitelist(); 93
94 $infohashes{$h} = 1;
95 verbose('Addition successfull.');
96 }
97 save_whitelist();
98} 98}
99 99
100sub hup_server { 100sub hup_server {
101 ## Sending a SIGHUP to both processes tells them to reload the whitelist file. 101 ## NOTE: I've removed this from my fork of opentracker. I've moved to
102 #verbose('Sending a SIGHUP to all \'opentracker\' processes.'); 102 ## using inotify instead of sending SIGHUP signals as it would crash my
103 #system('pkill', '-1', 'opentracker'); 103 ## server. Uncomment the system call here to restore this
104 verbose('ignoring hup'); 104 ## functionality.
105
106 ## Sending a SIGHUP to both processes tells them to reload the whitelist file.
107 #verbose('Sending a SIGHUP to all \'opentracker\' processes.');
108 #system('pkill', '-1', 'opentracker');
109 verbose('ignoring hup');
105} 110}
106 111
107sub list_hashes { 112sub list_hashes {
108 load_whitelist(); 113 load_whitelist();
109 my @hashes = keys %infohashes; 114 my @hashes = keys %infohashes;
110 foreach my $h ( @hashes ) { 115 foreach my $h ( @hashes ) {
111 print "$h\n"; 116 print "$h\n";
112 } 117 }
113 118
114 print "\n". ($#hashes + 1). " hashes found.\n"; 119 print "\n". ($#hashes + 1). " hashes found.\n";
115} 120}
116 121
117Getopt::Long::Configure("bundling"); 122Getopt::Long::Configure("bundling");
118GetOptions(\%opts, 'verbose|v', 'add|a=s@', 'remove|r=s@', 'list|l', 'huponly|h', 'whitelist|w=s'); 123GetOptions(\%opts, 'verbose|v', 'add|a=s@', 'remove|r=s@', 'list|l', 'huponly|h', 'whitelist|w=s');
119 124
120if(defined($opts{'huponly'})) { 125if(defined($opts{'huponly'})) {
121 hup_server(); 126 print("Server uses inotify. Sending SIGHUP is disabled.\n");
122 exit(0); 127 exit(1);
123} 128}
124 129
125my $didsomething = 0; 130my $didsomething = 0;
126 131
127if($opts{'whitelist'} ne '/home/nick/tracker/bin/whitelist.txt') { 132verbose('Using whitelist: '. $opts{'whitelist'});
128 verbose('Using non-default whitelist: '. $opts{'whitelist'});
129}
130 133
131if(defined($opts{'add'})) { 134if(defined($opts{'add'})) {
132 #print Dumper($opts{'add'}); 135 add_hash($opts{'add'});
133 add_hash($opts{'add'}); 136 $didsomething = 1;
134 $didsomething = 1;
135} 137}
136 138
137if(defined($opts{'remove'})) { 139if(defined($opts{'remove'})) {
138 remove_hash($opts{'remove'}); 140 remove_hash($opts{'remove'});
139 $didsomething = 1; 141 $didsomething = 1;
140} 142}
141 143
142if(defined($opts{'list'})) { 144if(defined($opts{'list'})) {
143 list_hashes(); 145 list_hashes();
144 $didsomething = 1; 146 $didsomething = 1;
145} 147}
146 148
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) { 149unless($didsomething == 1) {
156 pod2usage( 150 pod2usage(
157 -message => "======\n== Error: No action performed!\n======\n", 151 -message => "======\n== Error: No action performed!\n======\n",
158 -verbose => 1, 152 -verbose => 1,
159 -noperldoc => 1, 153 -noperldoc => 1,
160 ); 154 );
161} 155}
162 156
163 157