ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/tpb/common.pl
Revision: 1.4
Committed: Sun Jun 24 15:58:19 2018 UTC (8 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +16 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     use common::sense;
4     use Compress::LZF;
5     use EV;
6     use AnyEvent::HTTP;
7     use BDB;
8     use Coro;
9     use Coro::BDB;
10     use AnyEvent::DNS;
11     use Set::IntSpan;
12     use CBOR::XS;
13     use Compress::Zlib ();
14     use Compress::Raw::Zlib ();
15     use AnyEvent::TLS;
16    
17     BDB::min_parallel 8;
18    
19 root 1.4 #our @TPB = qw(thepiratebay.org);
20     #our @TPB = qw(pirateproxy.ist);
21     #our @TPB = qw(thepiratebay.futbol);
22     our @TPB = qw(tpbproxy.club);
23 root 1.1 our $TPB = $TPB[0];
24    
25     our $TODAY = int time / 86400;
26    
27     #our $TPB_SWITCHER = AE::timer 1, 0.02, sub {
28     # state $i;
29     # $TPB = $TPB[++$i % @TPB];
30     #};
31    
32     #############################################################################
33    
34     $AnyEvent::HTTP::MAX_PER_HOST =
35     $AnyEvent::HTTP::MAX_PERSISTENT_PER_HOST = 400;
36    
37     our %stat;
38    
39     our $statprint = AE::timer 1, 1, sub {
40     syswrite STDOUT, "\r" . (join " ", map "$_=$stat{$_}", sort keys %stat) . " "
41     if $ENV{VERBOSE};
42     };
43    
44     our @DICTIONARY = (do {
45     open my $fh, "<:perlio", "dictionary0"
46     or die "dictionary0: $!";
47     local $/;
48     <$fh>
49     });
50    
51     #############################################################################
52    
53     our $db_env = db_env_create;
54    
55     $db_env->set_flags (BDB::AUTO_COMMIT | BDB::TXN_WRITE_NOSYNC, 1);
56     $db_env->log_set_config (BDB::LOG_AUTO_REMOVE);
57 root 1.4 #$db_env->set_cachesize (0, 8 * 1024 * 1024);
58     $db_env->set_cachesize (0, 64 * 1024);
59 root 1.1 $db_env->set_lk_max_lockers (20480);
60     $db_env->set_lk_max_locks (20480);
61     $db_env->set_lk_max_objects (20480);
62    
63     mkdir "db", 0777;
64     db_env_open
65     $db_env,
66     "db",
67     BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL
68     | BDB::INIT_TXN | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
69     0666;
70    
71     our %DB;
72    
73     sub table($) {
74     my $db = $DB{$_[0]} = db_create $db_env;
75     db_open $db, undef, $_[0], undef, BDB::BTREE, BDB::AUTO_COMMIT | BDB::CREATE, 0666;
76     die if $!;
77     $db
78     }
79    
80     our $db_state = table "state"; # global state
81     our $db_info = table "info"; # per torrent state
82    
83     our $TXN;
84    
85     sub get($$) {
86     db_get $_[0], $TXN, $_[1], my $data, 0;
87     $data # will be undef on error
88     }
89    
90     sub put($$$) {
91     db_put $_[0], $TXN, $_[1], $_[2], 0;
92     }
93    
94     sub del($$) {
95     db_del $_[0], $TXN, $_[1], 0, sub { };
96     }
97    
98     sub sget($) {
99     get $db_state, $_[0]
100     }
101    
102     sub sput($$) {
103     put $db_state, $_[0], $_[1];
104     }
105    
106     sub iget($) {
107     my $data = get $db_info, $_[0];
108     length $data ? decode_cbor $data : []
109     }
110    
111     sub iput($$) {
112     put $db_info, $_[0], encode_cbor $_[1];
113     }
114    
115 root 1.4 our $syncer = AE::timer 5, 60, sub {
116 root 1.1 db_env_txn_checkpoint $db_env, 0, 0, 0, sub { };
117     };
118    
119     sub dbsync() {
120     db_env_txn_checkpoint $db_env;
121     }
122    
123     sub dbflush {
124     return if $::dbflush_called++;
125     db_env_txn_checkpoint $db_env, sub { };
126     BDB::set_sync_prepare undef;
127     db_close $_ for values %DB;
128     db_env_txn_checkpoint $db_env;
129     db_env_close $db_env;
130     }
131    
132     END {
133     dbflush;
134     }
135    
136     sub quit {
137     EV::unloop;
138     #exit 1;
139     };
140    
141     our $sigint = AE::signal INT => \&quit;
142     our $sigterm = AE::signal TERM => \&quit;
143    
144     sub dic_compress($$) {
145     my $d = new Compress::Raw::Zlib::Deflate
146     -Level => 9,
147     -WindowBits => -15,
148     -Dictionary => $DICTIONARY[$_[0]],
149     -AppendOutput => 1,
150     ;
151    
152     my $o;
153    
154     $d->deflate ($_[1], $o);
155     $d->flush ($o);
156    
157     $o
158     }
159    
160     sub dic_decompress($$) {
161     my $i = new Compress::Raw::Zlib::Inflate
162     -WindowBits => -15,
163     -Dictionary => $DICTIONARY[$_[0]],
164     -AppendOutput => 1,
165     -ConsumeInput => 0,
166     ;
167    
168     $i->inflate ($_[1], my $o, 1);
169    
170     $o
171     }
172    
173     #############################################################################
174    
175     #############################################################################
176    
177     sub GET {
178     my ($data, $hdr);
179    
180 root 1.4 for (1..50) {
181 root 1.1 http_get $_[0],
182     proxy => undef,
183     recurse => 0,
184     headers => {
185 root 1.4 "user-agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0 Iceweasel/20.0",
186 root 1.1 "referer" => undef,
187     },
188     Coro::rouse_cb;
189     ($data, $hdr) = Coro::rouse_wait;
190    
191 root 1.4 $hdr->{Status} = 404
192     if $data =~ /You're looking for something that does not, has not, will not, might not or must not exist/;
193    
194 root 1.1 $hdr->{Status} = 580
195 root 1.4 if $data =~ /^(?:Upgrading software|Could not connect to caching server|Database maintenance, please check back in 10 minutes)/;
196 root 1.1
197     $hdr->{Status} = 581
198     unless length $data;
199    
200     $data = Compress::Zlib::memGunzip $data
201     if $hdr->{"content-encoding"} eq "gzip";
202    
203     last
204     if $hdr->{Status} < 500;
205    
206 root 1.4 Coro::AnyEvent::sleep 1.5 if $hdr->{Satus} == 503;
207     Coro::AnyEvent::sleep 9.5 if $hdr->{Satus} == 580;
208    
209 root 1.1 Coro::AnyEvent::sleep 0.5;
210     ++$stat{"retry-$hdr->{Status}"};
211 root 1.3
212     # use Data::Dump; ddx $hdr;#d#
213 root 1.1 }
214    
215     ++$stat{$hdr->{Status}};
216     ($data, $hdr)
217     }
218    
219     1
220