-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwc.php
executable file
·3282 lines (3280 loc) · 131 KB
/
gwc.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class settings {
const CACHE_NAME = 'Beacon Cache II';
const CACHE_VERSION = '0.8.0.1';
const CACHE_VENDOR = 'BCII';
/////////////////////////////////////////////BEGIN Settings
const STORAGE = 'flat'; //Storage system type - either 'mysql', ''mysqli', or 'flat' (flat is just regular .dat files).
const FOLDER = 'beacon_data'; //Folder name for the GWC data.
//////If you're using SQL instead of flat-file:
const DBA_ADDRESS = 'localhost'; //SQL server address.
const DBA_USERNAME = ''; //SQL server username.
const DBA_PASSWORD = ''; //SQL server password.
const DBA_NAME = 'beacon'; //SQL server database for Beacon.
const STATS_CLIENT_FLUSH = 5; //Multiple of STATS_THROTTLE for which to clear the clients table.
//////END SQL Settings
const CACHE_ADDRESS = ''; //If included, this address will be enforced (If not, then multiple addresses of your cache are possible).
const HOSTS_IN = 30; //How many hosts to store.
const HOSTS_OUT = 25; //How many hosts to output.
const URLS_IN = 100; //How many URLs to store.
const URLS_OUT = 25; //How many URLs to output.
const BANNED_URLS_IN = 100; //How many temp. banned URLs to keep track of per network.
const ENABLE_V1 = true; //Shall the cache accept GWC specification 1 requests?
const ENABLE_V2 = true; //Shall the cache accept GWC specification 2 requests?
const ENABLE_V3 = true; //Shall the cache accept GWC XML requests?
const SHUFFLE_HOSTS = true; //Shall the cache shuffle hosts on output?
const SHUFFLE_URLS = true; //Shall the cache shuffle URLs on output?
const EXPIRE_HOSTS = 16; //At how many hours shall a host expire?
const EXPIRE_URLS = 24; //At how many hours shall a URL expire?
const EXPIRE_BANNED_URLS = 24; //At how many hours shall a temp. banned URL expire?
const DEFAULT_NET = 'gnutella'; //The defaulting network of this cache (This really should not be changed to prevent the leakage of Gnutella onto another network).
const FSOCKOPEN = true; //Can this cache use sockets?
const IP_ALT_CACHES = false; //Shall this cache accept IP addresses as URLs?
const NON_HTTP = true; //Can this cache connect on ports other than port 80 for sockets?
const RETRY_URLS = true; //Can the cache retry expired URLs?
const RETRY_BAD_URLS = false; //Can the cache retry expired bad URLs?
const TIMEOUT = 10; //Timeout for the sockets.
const WEB_FRONT = true; //Shall a web front be displayed?
const BLOCK_NO_USER_AGENT = true; //Shall the cache block clients with no user agent header from updating an IP?
const BLOCK_NO_USER_AGENT_ALL = false; //Shall the cache block clients with no user agent header completely?
const WEBFRONT_URL_ADDS = true; //Shall the cache accept URL adds through the web front?
const ENABLE_HIT_STATS = true; //Shall the cache keep track of per-hit (number tracking) statistics? (*Disabling it reduces the load by a lot!*)
const ENABLE_CLIENT_STATS = true; //Shall the cache keep track of per-client (name tracking) statistics? (*Disabling it reduces the load by a lot!*)
const STATS_THROTTLE = 1000; //How many clients shall be kept track of at any one time in the stats?
const ADD_URL_TO_SHAREAZA = true; //This adds the extra link to each URL to let the user have the URL auto-entered into Shareaza.
const CHECKS_ALLOWED = true; //This adds the extra link to each URL to let the user see a detailed debug of the selected cache.
const UPDATE_SELF_TO_REMOTE = true; //Shall the cache update self-url to other GWCs?
const LOG_UPDATE_WARNINGS = false; //Shall the cache log update warnings generated in the update class?
const LOG_BAN_WARNINGS = false; //Shall the cache log ban warnings generated in the client check class?
const LOG_SPLIT_SIZE = 256000; //The size of a log file in bytes at which a new one is generated.
public $NETWORKS = array( //Configure here to enable certain networks to bootstrap from this cache. (**Must be lower-case!**)
'gnutella',
'gnutella2'
);
public $BANNED = array( //Banned IPs configured here.
'38.107.160',
'38.107.161',
'38.107.162',
'38.107.163',
'38.107.164',
'38.107.165',
'38.107.166',
'38.107.167',
'72.172.87',
'72.172.88',
'72.172.89',
'72.172.90',
'72.172.91',
'129.47.128',
'174.136.255'
);
public $VENDOR_CODE_BAN = array( //Vendor code bans configured here.
'Self-Add', //Reserved for internal use.
'URL-ADD', //Reserved for internal use.
'MESH', //Has been marked as bad by others.
'RAZA1.0', //Shareaza 1.0 is a fake client (1.x was released, but never 1.0).
'BEAR6', //Has been marked as bad by others.
'BEAR7', //Has been marked as bad by others.
'BEAR8', //Has been marked as bad by others.
'BEAR9', //Has been marked as bad by others.
'limewire' //Fake client (A trojan trying to DDoS multiple caches) (Not the real limewire!!!)
);
public $VENDOR_CODE_IP_UPDATE_BAN = array( //Block certain vendor codes from updating IP addresses here.
'TEST',
'GCII',
'WURM',
'BAZK',
'BCON',
'BCII',
'SKLL',
'LIME4.18.8',
'LIME5.0.',
'LIME5.1.'
);
public $BANNED_URLS = array( //Used to ban bad URLs on a permanent basis...
'http://gwc.nickstallman.net/beta.php',
'http://websamba.com/doscache/',
'http://borednow.net/gwebcache/gcache.asp',
'http://www.engle-enterprises.com/gcache/gcache.php',
'http://www.pietroam.net/script/gnutella/phpgnucacheII/gwcii.php',
'http://web-1.bidz.com/3minute.html',
'http://gwc.the4400.tv/skulls/skulls.php',
'http://home.zk3d.com/gwebcache/gcache.php',
'http://www.thebobclan.com/gnetcache/gnetcache.php',
'http://server925.gisol.com/~enhance/search/files/cache/gcache.php',
'http://www.inet-one.com/gwebcache-0.7.5/gcache.php',
'http://60gp.ovh.net/erreur.html',
'http://gwc.snack-donation.com/skulls.php',
'http://invalidsubdomain.toddenrentieren.net/gcache.php',
'http://home.no.net/dforsb/gnet/index.php',
'http://e.sears.com/a/tA-lvD$AM$zVoAOVOWAAMHVT7Lx/sear63',
'http://t.az.is.teh.r0x0r.gtkg.de/',
'http://overbeer.ghostwhitecrab.de/',
'http://gwc1.nouiz.org/servlet/GWebCache/req',
'http://www.andi-benni-mexz-carly-pfunds-tobadill.dr.ag/',
'http://www.melissazone.com/gwebcache/gcache.php',
'http://www.ryannet.net/gwebcache/gcache.php',
'http://geocities.com/prae2rian/GNUTELLA.net',
'http://www.markus-wolf-home.de/GWebCache/gcache.php',
'http://dr-ag-dr-ag-dr-op.dr.ag/skulls.php',
'http://torrent.delvedown.com/cgi-bin/perlgcache.cgi',
'http://gwc2.mycroft.at/bazooka.php',
'http://www.gamerspage.com/lynn.asp',
'http://www.la-forza.com/gnucache/gcache.php',
'http://www.zadox.com/gwebcache',
'http://www.iwolfnet.com/gcache/gcache.asp',
'http://gwc.nickstallman.net/',
'http://gwc1.nickstallman.net/',
'http://gwc2.nickstallman.net/',
'http://gwc.nonexiste.net/',
'http://gwc2.nonexiste.net/',
'http://gwc3.nonexiste.net/',
'http://gwc4.nonexiste.net/'
);
public $USER_AGENT_BAN = array( //User agent bans configured here.
'Etomi',
'360Share',
'Shareaza PRO',
'Morpheus Music',
'WinMX Mp3',
'MP3Rocket',
'CruX',
'P2P Rocket',
'Bearshare MP3',
'Bearshare Music',
'K-Lite Pro',
'Fastload.TV',
'Gnutella Turbo',
'Trilix',
'Morpheus Games',
'Morpheus Premium'
);
public $BROWSER_DETECT = array( //Browser detection for IP banning configured here.
'opera',
'MSIE',
'gecko',
'windows',
'firefox',
'infopath',
'mozilla',
'Safari',
'KHTML',
'webkit',
'chrome'
);
public $NET_SPEC1 = array( //Isolate certain spec1 clients to prevent network leakage. (Client+Version=>Network)
'MUTE'=>'mute', //In order for mute support to be enabled, mute needs to be added to the $NETWORKS array and BLOCK_NO_USER_AGENT edited to false (to allow updates).
'mutekomm'=>'mute'
);
public $BAN_GDNA_CLONES = array( //Which vendor codes to block on GDNA clone-catch...
'BEAR',
'LIME',
'RAZA',
'RAZB',
'RZCB'
);
public $IP_EXCEPTION = array( //Which networks shall submitted IP addresses be ignored and replaced by server-detected IP addresses?
'mute'
);
public $NETWORKS_ALT = array( //Alternate names for the implemented networks. (Alternate Network=>Network)
'gnutella1'=>'gnutella',
'g1'=>'gnutella',
'g2'=>'gnutella2'
);
public $BANNED_PONGS = array( //Ban the pong responses of specific caches.
'FTWebCache/3.0',
'bootdesu 0.2 :P'
);
public $WHITELIST_USER_AGENT = array( //Create a whitelist to ban those who aren't on it. (Good User-Agent=>Network)
);
public $VENDOR_CODE_MATCHINGS = array( //Vendor code conversions for matching to client names and URLs.
//URLs are optional to add ino the conversions, just replace with a false statement before doing.
//(vendor code)=>array((Actual name), (URL or put a boolean false))
'ACQL'=>array('Acqlite','http://acqlite.sourceforge.net/'),
'ACQX'=>array('Acquisition','http://www.acquisitionx.com/'),
'AGIO'=>array('Adagio','http://sourceforge.net/projects/agio'),
'BAZK'=>array('Bazooka','http://www.bazookanetworks.com/'),
'BCON'=>array('Beacon Cache','http://sourceforge.net/projects/beaconcache/'),
'BEAR'=>array('BearShare','http://en.wikipedia.org/wiki/BearShare'),
'CABO'=>array('Cabos/LimeWire','http://cabos.sourceforge.jp/'),
'CANN'=>array('Cannon',false),
'CHTC'=>array('Cheater Cache',false),
'COCO'=>array('CocoGnut','http://www.alpha-programming.co.uk/software/cocognut/'),
'DNET'=>array('Deepnet Explorer','http://www.deepnetexplorer.com/'),
'Dianlei'=>array('Dianlei','http://www.dianlei.com/'),
'dianlei'=>array('Dianlei','http://www.dianlei.com/'),
'FLOX'=>array('Flox','http://sourceforge.net/projects/flox/'),
'FOXY'=>array('Foxy P2P','http://tw.gofoxy.net/'),
'FTWC'=>array('FTWebCache',false),
'GCII'=>array('PHPGnuCacheII','http://gwcii.sourceforge.net/'),
'GDNA'=>array('GnucDNA','http://www.gnucleus.com/GnucDNA/'),
'GIFT'=>array('giFT','http://gift.sourceforge.net/'),
'GNZL'=>array('Gnoozle',false),
'GNUC'=>array('Gnucleus','http://www.gnucleus.com/Gnucleus/'),
'GNUT'=>array('Gnut','http://www.mrob.com/gnut/'),
'GOLD'=>array('Ares Gold','http://www.aresgold.com/'),
'GTKG'=>array('GTK Gnutella','http://gtk-gnutella.sourceforge.net/'),
'GWCC'=>array('Cheater Cache',false),
'iswip'=>array('iSwipe','http://www.hillmanminx.net/iswipe/'),
'JGWC'=>array('Jums Web Cache','http://www1.mager.org/GWebCache/'),
'JTEL'=>array('JTella','http://jtella.sourceforge.net/'),
'KIWI'=>array('Kiwi Alpha','http://www.kiwialpha.com/'),
'KUPE'=>array('Kupe',false),
'LIME'=>array('LimeWire','http://limewire.com/'),
'MESH'=>array('iMesh','http://en.wikipedia.org/wiki/IMesh'),
'MLDK'=>array('MLDonkey','http://mldonkey.sourceforge.net/Main_Page'),
'MMMM'=>array('Morpheus','http://www.morpheus.com/'),
'MNAP'=>array('MyNapster','http://mynapster.sourceforge.net/'),
'MRPH'=>array('Morpheus','http://www.morpheus.com/'),
'MTLL'=>array('Mutella','http://mutella.sourceforge.net/'),
'mutekomm'=>array('Kommute','http://kommute.sourceforge.net/'),
'MUTEkomm'=>array('Kommute','http://kommute.sourceforge.net/'),
'MUTE'=>array('MUTE','http://mute-net.sourceforge.net/'),
'NOVA'=>array('Nova P2P','http://novap2p.sourceforge.net/'),
'PEER'=>array('Peer Project','http://sourceforge.net/projects/peerproject/'),
'PGII'=>array('Pocket G2',false),
'PHEX'=>array('Phex','http://www.phex.org/'),
'PNTH'=>array('Panthera Project','http://pantheraproject.sourceforge.net/'),
'QTEL'=>array('Qtella','http://qtella.sourceforge.net/'),
'RAZA'=>array('Shareaza','http://shareaza.sourceforge.net/'),
'RAZB'=>array('Shareaza Beta','http://shareaza.sourceforge.net/'),
'RAZL'=>array('ShareazaLite',false),
'RZCA'=>array('ShareazaPlus (Alpha)','http://shareazaplus.sourceforge.net/'),
'RZCB'=>array('ShareazaPlus (Beta)','http://shareazaplus.sourceforge.net/'),
'RZCC'=>array('ShareazaPlus','http://shareazaplus.sourceforge.net/'),
'Self-Add'=>array('Cache Retry System',false),
'Sharetastic'=>array('Sharetastic','http://goforsharing.com/index.php/home-mainmenu-1/sharetastic-mainmenu-126'),
'SHLN-DEV'=>array('Sharelin','http://sharelin.sourceforge.net/'),
'SHLN'=>array('Sharelin','http://sharelin.sourceforge.net/'),
'SKLL'=>array('Skulls','http://sourceforge.net/projects/skulls/'),
'SNOW'=>array('Frostwire','http://www.frostwire.com/'),
'SWAP'=>array('Swapper','http://www.revolutionarystuff.com/swapper/'),
'TESTBazooka'=>array('Bazooka GWC','http://www.bazookanetworks.com/'),
'TESTBCII'=>array('Beacon Cache II','http://sourceforge.net/projects/beaconcache/'),
'TESTBCON'=>array('Beacon Cache','http://sourceforge.net/projects/beaconcache/'),
'TESTCachechu'=>array('Cachechu','http://code.google.com/p/cachechu/'),
'TESTCrab-'=>array('GhostWhiteCrab','http://wiki.limewire.org/index.php?title=GhostWhiteCrab'),
'TESTGWCSCANNER'=>array('Multi-Network GWC Scan','http://gcachescan.grantgalitz.com/'),
'TESTPGDBScan'=>array('Jon Atkins GWC Scan','http://gcachescan.jonatkins.com/'),
'TESTSKLL'=>array('Skulls','http://sourceforge.net/projects/skulls/'),
'TFLS'=>array('TrustyFiles','http://www.trustyfiles.com/'),
'URL-ADD'=>array('URL-ADD',false),
'WURM'=>array('Wurm GWC Scanner','http://kevogod.trillinux.org/')
);
}
/////////////////////////////////////////////END Settings
abstract class data_system {
public $contents; //Raw unedited output.
public $output; //Edited and 'prepped' output.
protected $runtime; //Container for passed variables.
protected $file; //The unique physical file-name that carries the requested data.
protected $name; //The unique physical name that carries the requested data (For MySQL).
protected $refresh; //Used by the stats to prevent a stat-write for read only.
protected $expire; //Time (in hours) for which a host cannot be older than.
protected $maximum; //Maximum count stored.
function __construct($runtime) {
$this->runtime = $runtime; //Container for passed variables
$this->name = $this->runtime->ID.$this->runtime->network;
$this->file = settings::FOLDER.'/'.$this->name.'.dat';
if ($this->runtime->ID == 'log' || strtolower(settings::STORAGE) == 'flat') {
if (!file_exists(settings::FOLDER)) {
mkdir(settings::FOLDER); //Create the folder
}
}
if ($this->runtime->ID != 'log' && strtolower(settings::STORAGE) == 'flat') { $this->check_exists($this->file); }
$this->refresh = false;
switch ($this->runtime->ID) { ////Run consistency checks
case 'ip': //IP consistency check
$this->expire = settings::EXPIRE_HOSTS;
$this->maximum = settings::HOSTS_IN;
break;
case 'bad_url': //banned-URL consistency check
$this->expire = settings::EXPIRE_BANNED_URLS;
$this->maximum = settings::BANNED_URLS_IN;
break;
case 'retry': //Retry-URL consistency check
case 'url': //URL consistency check
$this->expire = settings::EXPIRE_URLS;
$this->maximum = settings::URLS_IN;
}
switch ($this->runtime->flow) {
case 'up': //An update
files::update();
break;
case 'down': //Data retrieval
files::request();
}
}
protected function request() {
switch ($this->runtime->ID) { //Decide what data to retrieve.
case 'ip':
$this->ip_request();
break;
case 'retry':
$this->url_retry();
break;
case 'bad_url':
case 'url':
$this->url_request();
break;
case 'stats_total_requests':
case 'stats_total_updates':
case 'stats_client':
$this->contents = $this->grab();
break;
case 'stats_requests':
case 'stats_updates':
$this->refresh = true;
$this->update_stats();
$this->refresh = false;
$this->contents = $this->grab();
}
}
protected function update() {
switch ($this->runtime->ID) { //Decide what data to update.
case 'ip':
$this->ip_update();
break;
case 'bad_url':
case 'url':
$this->url_update();
break;
case 'log':
$this->log_update();
break;
case 'stats_total_requests':
case 'stats_total_updates':
case 'stats_client':
case 'stats_requests':
case 'stats_updates':
$this->update_stats();
}
}
protected function ip_request() {
$this->all_requests(settings::SHUFFLE_HOSTS, settings::HOSTS_OUT);
}
protected function ip_update() {
$this->add();
}
protected function url_request() {
$this->all_requests(settings::SHUFFLE_URLS, settings::URLS_OUT);
}
protected function url_update() {
$this->add();
}
protected function check_exists($name) {
if (!file_exists($name)) {
$file = fopen($name, 'wb');
fclose($file);
}
}
protected function log_update() {
$log_marker = 0;
$this->check_exists(settings::FOLDER.'/log_'.$log_marker.'.dat');
while (settings::LOG_SPLIT_SIZE <= filesize(settings::FOLDER.'/log_'.$log_marker.'.dat')) { $log_marker++; $this->check_exists(settings::FOLDER.'/log_'.$log_marker.'.dat'); }
$file = fopen(settings::FOLDER.'/log_'.$log_marker.'.dat', 'ab');
flock($file, LOCK_EX);
fwrite($file, '######################################'."\r\n".'--'.gmdate('Y/m/d H:i')."--\r\n");
fwrite($file, '@@@@@@[ENVIRONMENT VARIABLES]@@@@@@'."\r\n");
foreach ($this->runtime->runtime->env_variables as $key=>$value) {
if ($key !== false && $value !== false) {
fwrite($file, '['.$key.']'.'('.(string)$value.")\r\n");
}
}
fwrite($file, '@@@@@@[GWC VARIABLES]@@@@@@'."\r\n");
foreach ($this->runtime->runtime->gwc_variables as $key=>$value) {
if ($key !== false && $value !== false) {
fwrite($file, '['.$key.']'.'('.(string)$value.")\r\n");
}
}
fwrite($file, '@@@@@@[WARNINGS]@@@@@@'."\r\n");
if (!empty($this->runtime->info['warnings'])) {
foreach ($this->runtime->info['warnings'] as $key=>$value) {
if ($key !== false && $value !== false) {
fwrite($file, '['.$key.']'.'('.(string)$value.")\r\n");
}
}
}
flock($file, LOCK_UN);
fclose($file);
}
protected function compile_saves() {
return ((($this->runtime->ID == 'ip') ? $this->runtime->runtime->final_ip : $this->runtime->runtime->gwc_variables[$this->runtime->ID]).'|'.
time().'|'.
$this->runtime->runtime->gwc_variables['client'].(($this->runtime->runtime->gwc_variables['version'] !== false) ? $this->runtime->runtime->gwc_variables['version'] : '').'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->gwc_variables['cluster'] !== false) ? $this->runtime->runtime->gwc_variables['cluster'] : '') : ((isset($this->runtime->runtime->pong)) ? $this->runtime->runtime->pong : '')).'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->gwc_variables['x_leaves'] !== false) ? $this->runtime->runtime->gwc_variables['x_leaves'] : '') : ((isset($this->runtime->runtime->host_count)) ? $this->runtime->runtime->host_count : '0')).'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->gwc_variables['uptime'] !== false) ? $this->runtime->runtime->gwc_variables['uptime'] : '') : ((isset($this->runtime->runtime->url_count)) ? $this->runtime->runtime->url_count : '0')).'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->gwc_variables['x_max'] !== false) ? $this->runtime->runtime->gwc_variables['x_max'] : '') : ((isset($this->runtime->runtime->v1_cache)) ? (int)$this->runtime->runtime->v1_cache : '0')).'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->gwc_variables['vendor_only'] !== false) ? $this->runtime->runtime->gwc_variables['vendor_only'] : '') : ((isset($this->runtime->runtime->v2_cache)) ? (int)$this->runtime->runtime->v2_cache : '0')).'|'.
(($this->runtime->ID == 'ip') ? (($this->runtime->runtime->ipv6) ? '6' : '4') : ((strlen($this->runtime->runtime->url_ip)) ? $this->runtime->runtime->url_ip : '')).'|');
}
protected function compile_client_stats() {
return ($this->runtime->runtime->gwc_variables['client'].
(($this->runtime->runtime->gwc_variables['version'] !== false) ? $this->runtime->runtime->gwc_variables['version'] : '').'|'.
(($this->runtime->runtime->gwc_variables['ip'] !== false || $this->runtime->runtime->gwc_variables['ipv6'] !== false) ? '1' : '0').'|'.
(($this->runtime->runtime->gwc_variables['url'] !== false) ? '1' : '0').'|'.
(($this->runtime->runtime->gwc_variables['ping']) ? '1' : '0').'|'.
(($this->runtime->runtime->gwc_variables['get'] || $this->runtime->runtime->gwc_variables['hostfile'] || $this->runtime->runtime->gwc_variables['urlfile']) ? '1' : '0').'|'.
(($this->runtime->runtime->gwc_variables['info'] || $this->runtime->runtime->gwc_variables['support'] || $this->runtime->runtime->gwc_variables['statfile']) ? '1' : '0'));
}
protected function compile_retries($host, $bad_count=0) {
return ($host.'|'.time().'|'.'Self-Add'.'|'.((string)$bad_count).'|');
}
protected function filter_bad($data) {
$hosts = array();
$filtered = array(
'good'=>array(),
'bad'=>array(),
'expired'=>array()
);
if (!empty($data)) {
while (count($data) > $this->maximum) {
$filtered['bad'][] = array_shift($data);
}
foreach ($data as $line) {
$host = $timestamp = '';
list($host, $timestamp) = explode('|', $line);
if (strlen($host) === 0 || strlen($timestamp) === 0 || in_array($host, $hosts)) {
$filtered['bad'][] = $line;
}
elseif ((time() - (int)$timestamp) >= ($this->expire * 3600)) {
$filtered['bad'][] = $line;
$filtered['expired'][] = $host;
}
else {
$filtered['good'][] = $line;
}
$hosts[] = $host;
}
}
if (!empty($filtered['good'])) {
$filtered['good'] = array_unique($filtered['good']);
}
if (!empty($filtered['bad'])) {
$this->remove_bad(array_unique($filtered['bad']));
}
if (!empty($filtered['expired']) && (($this->runtime->ID == 'url' && settings::RETRY_URLS) or ($this->runtime->ID == 'bad_url' && settings::RETRY_BAD_URLS))) {
$this->add_retry(array_unique($filtered['expired']));
}
return $filtered['good'];
}
protected function all_requests($shuffle, $number_out) {
$contents = $this->contents = array_reverse($this->filter_bad($this->grab()));
if (!empty($contents)) {
($shuffle) ? shuffle($contents) : $contents = $contents;
foreach ($contents as $key=>$value) {
if (!$this->runtime->runtime->gwc_variables['getv6'] && $this->runtime->ID == 'ip') { //Filter out IPv6 addresses when not requested...
if ($value[0] == '[') {
unset($contents[$key]);
}
}
}
if (count($contents) > $number_out) {
while (count($contents) > $number_out) {
array_shift($contents);
}
}
}
$this->output = $contents;
}
protected abstract function grab();
protected abstract function update_stats();
protected abstract function add_retry($retries);
protected abstract function remove_bad($bad_lines);
protected abstract function add();
protected abstract function url_retry();
}
class files extends data_system {
function __construct($runtime) {
parent::__construct($runtime);
}
protected function file($handle) {
$data = array();
while(!feof($handle)) {
if ($data_found = fgets($handle)) {
if (strlen($data_found) >= 1) {
$data_found = trim($data_found);
if (strlen($data_found) >= 1) {
$data[] = $data_found;
}
}
}
}
@rewind($handle); //We have to rewind, as there's a file-system bug in Unix that places a NULL character on data append for next operation.
return $data;
}
protected function grab() {
$file = @fopen($this->file, 'rb');
$this->file_handle_check($file);
flock($file, LOCK_SH);
$contents = $this->file($file);
flock($file, LOCK_UN);
fclose($file);
return $contents;
}
protected function file_handle_check($file) {
if ($file === false) { throw new Exception('Failed to open file '.$this->file); }
}
protected function update_stats() {
$file = @fopen($this->file, 'r+b');
$this->file_handle_check($file);
flock($file, LOCK_EX);
if ($this->runtime->ID == 'stats_total_requests' || $this->runtime->ID == 'stats_total_updates') {
$data = $this->file($file);
ftruncate($file, 0);
fwrite($file, (!empty($data) ? ($data[0] + 1) : '1'));
}
elseif ($this->runtime->ID == 'stats_client') {
$data = $this->file($file); $data2 = '';
if (!empty($data) && count($data) >= settings::STATS_THROTTLE) {
ftruncate($file, 0);
while (count($data) >= settings::STATS_THROTTLE) {
array_shift($data);
}
fwrite($file, implode("\r\n", $data)."\r\n".$this->compile_client_stats()."\r\n");
}
else {
fseek($file, 0, SEEK_END);
fwrite($file, $this->compile_client_stats()."\r\n");
}
}
elseif ($this->runtime->ID == 'stats_requests' || $this->runtime->ID == 'stats_updates') {
$outdated = false;
$data = $this->file($file); $data2 = '';
if (!empty($data)) {
foreach ($data as $time) {
if ((time() - ((int)$time)) < 3600) {
$data2 .= $time."\r\n";
}
else {
$outdated = true;
}
}
if ($outdated) {
ftruncate($file, 0);
if (!empty($data2)) {
fwrite($file, $data2);
}
}
}
if (!$this->refresh) {
if (!$outdated) {
fseek($file, 0, SEEK_END);
}
fwrite($file, time()."\r\n");
}
}
flock($file, LOCK_UN);
fclose($file);
}
protected function add_retry($retries) {
$file = @fopen(settings::FOLDER.'/retry'.$this->runtime->network.'.dat', 'ab');
$this->file_handle_check($file);
flock($file, LOCK_EX);
foreach ($retries as $host) {
fwrite($file, $this->compile_retries($host)."\r\n");
}
flock($file, LOCK_UN);
fclose($file);
}
protected function remove_bad($bad_lines) {
$file = @fopen($this->file, 'r+b');
$this->file_handle_check($file);
flock($file, LOCK_EX);
$data = $this->file($file);
ftruncate($file, 0);
foreach ($data as $line) {
if (!in_array($line, $bad_lines)) {
fwrite($file, $line."\r\n");
}
}
flock($file, LOCK_UN);
fclose($file);
}
protected function add() {
$file = @fopen($this->file, 'ab');
$this->file_handle_check($file);
flock($file, LOCK_EX);
fwrite($file, $this->compile_saves()."\r\n");
flock($file, LOCK_UN);
fclose($file);
}
protected function url_retry() {
$file = @fopen($this->file, 'r+b');
$this->file_handle_check($file);
flock($file, LOCK_EX);
$this->contents = $this->file($file);
if (!empty($this->contents)) {
$urls_to_retry = '';
if (count($this->contents) > 1) {
array_shift($this->contents);
$urls_to_retry = implode("\r\n", $this->contents);
}
ftruncate($file, 0);
fwrite($file, $urls_to_retry);
}
flock($file, LOCK_UN);
fclose($file);
}
}
abstract class mysql_routine_control extends data_system {
protected static $connection = false;
function __construct($runtime) {
//Linking contructor call of lower classes with the data_system constructor:
parent::__construct($runtime);
}
protected function grab() {
$link = $this->query('SELECT data FROM '.$this->clean($this->name).' ORDER BY id;');
$contents = array();
if (!$this->error($link)) {
while($row_returned = $this->mysql_fetch_row($link)) {
$contents[] = $row_returned[0];
}
}
return ((!empty($contents)) ? array_map('trim', $contents) : $contents);
}
protected function update_stats() {
if ($this->runtime->ID == 'stats_total_requests' || $this->runtime->ID == 'stats_total_updates') {
$data = array();
$link = $this->query('SELECT data FROM '.$this->clean($this->name).' ORDER BY id;');
if (!$this->error($link)) {
while($row_returned = $this->mysql_fetch_row($link)) {
$data[] = $row_returned[0];
}
}
if (empty($data)) {
$this->query('INSERT INTO '.$this->clean($this->name).'( data ) VALUES( \'1\' );');
}
else {
$this->query('UPDATE '.$this->clean($this->name).' SET data = \''.($data[0] + 1).'\';');
}
}
elseif ($this->runtime->ID == 'stats_client') {
$link = $this->query('SELECT COUNT(data) as num_clients FROM '.$this->clean($this->name).' ORDER BY id;');
if (!$this->error($link)) {
$count = 0;
if ($row_returned = $this->mysql_fetch_row($link)) {
$count = (int)$row_returned[0];
}
if ($count > 0) {
if ($count > (settings::STATS_THROTTLE * ((int)settings::STATS_CLIENT_FLUSH))) {
$this->query('TRUNCATE TABLE '.$this->clean($this->name).';');
}
}
}
$this->query('INSERT INTO '.$this->clean($this->name).'( data ) VALUES( \''.$this->clean($this->compile_client_stats()).'\' );');
}
elseif ($this->runtime->ID == 'stats_requests' || $this->runtime->ID == 'stats_updates') {
$this->query('DELETE FROM '.$this->clean($this->name).' WHERE data < '.(time() - 3600).';');
if (!$this->refresh) { $this->query('INSERT INTO '.$this->clean($this->name).'( data ) VALUES( \''.time().'\' );'); }
}
}
protected function add_retry($retries) {
foreach ($retries as $host) {
$this->query('INSERT INTO retry'.$this->runtime->network.'( data ) VALUES( \''.$this->compile_retries($host).'\' );');
}
}
protected function remove_bad($bad_lines) {
$lines = array();
foreach ($bad_lines as $line) {
$lines[] = 'data = \''.$this->clean($line).'\'';
}
$this->query('DELETE FROM '.$this->clean($this->name).' WHERE '.implode(' OR ', $lines).';');
}
protected function add() {
$this->query('INSERT INTO '.$this->clean($this->name).'( data ) VALUES( \''.$this->clean($this->compile_saves()).'\' );');
}
protected function url_retry() {
$link = $this->query('SELECT data, id FROM '.$this->clean($this->name).' ORDER BY id;');
if (!$this->error($link)) {
if ($row_returned = $this->mysql_fetch_row($link)) {
$this->contents[0] = $row_returned[0];
$id = $row_returned[1];
}
if (isset($id)) {
$this->query('DELETE FROM '.$this->clean($this->name).' WHERE id = \''.$this->clean($id).'\';');
}
}
}
///Below are the functions not executed by class data_system, but used instead by the child mysql handler classes:
protected function generate_table() {
return $this->direct_query('CREATE TABLE '.$this->clean($this->name).' ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY( id ), data TEXT( 500 ) );');
}
protected function query($query) {
$result = $this->direct_query($query);
if ($this->error($result)) {
$error_message = $this->obtain_error();
if (!$this->error($this->generate_table())) {
$result = $this->direct_query($query);
}
else {
throw new Exception('MySQL: '.$error_message);
}
}
return $result;
}
public abstract function clean($data);
public abstract function mysql_fetch_row($link);
public abstract function direct_query($query);
public abstract function error($link);
public abstract function obtain_error();
}
class mysql extends mysql_routine_control {
public $fallback;
function __construct($runtime) {
if (self::$connection !== false) {
parent::__construct($runtime);
}
else if ((self::$connection = @mysql_pconnect(settings::DBA_ADDRESS, settings::DBA_USERNAME, settings::DBA_PASSWORD)) === false) {
$this->fallback = true;
}
else {
if (@mysql_select_db(settings::DBA_NAME, self::$connection) !== false) {
parent::__construct($runtime);
}
else {
$this->fallback = true;
}
}
}
///Functions are public, as they are used by the class mysql_routine_control:
public function clean($data) {
return mysql_real_escape_string($data);
}
public function mysql_fetch_row($link) {
return mysql_fetch_row($link);
}
public function direct_query($query) {
return mysql_query($query, self::$connection);
}
public function error($link) {
return (($link === false) ? true : false);
}
public function obtain_error() {
return mysql_error();
}
}
class mysql_adv extends mysql_routine_control {
public $fallback;
function __construct($runtime) {
//Persistent connections are ignored in mysqli still, so static connection variable should return false.
self::$connection = (self::$connection) ? self::$connection : new mysqli(settings::DBA_ADDRESS, settings::DBA_USERNAME, settings::DBA_PASSWORD, settings::DBA_NAME);
(self::$connection->error || !self::$connection) ? $this->fallback = true : parent::__construct($runtime);
}
///Functions are public, as they are used by the class mysql_routine_control:
public function clean($data) {
return self::$connection->real_escape_string($data);
}
public function mysql_fetch_row($result) {
return $result->fetch_row();
}
public function direct_query($query) {
return self::$connection->query($query);
}
public function error($result) {
return ((self::$connection->error !== '' || $result === false) ? true : false);
}
public function obtain_error() {
return self::$connection->error;
}
function __destruct() {
//Connection in mysqli NOT persistent, so must close.
self::$connection->close();
}
}
class data_manage {
public $data;
public $flow;
public $ID;
public $runtime;
public $info;
function __construct($runtime, $flow, $ID, $network='', $info=array()) {
$this->ID = $ID;
$this->flow = $flow;
$this->network = strtolower((strlen($network) >= 1) ? '_'.$network : '');
$this->runtime = $runtime;
$this->info = $info;
$this->decide();
}
public function decide() {
switch (strtolower(settings::STORAGE)) {
case 'mysql':
$this->data = new mysql($this);
break;
case 'mysqli':
$this->data = new mysql_adv($this);
break;
case 'flat':
$this->data = new files($this);
break;
default:
throw new Exception('Improper configuration/Invalid STORAGE value ( \''.settings::STORAGE.'\' )');
}
$this->data = (isset($this->data->fallback)) ? new files($this) : $this->data;
}
}
class statistics {
public $runtime;
public $type;
public $network;
function __construct($runtime, $type, $network='') {
$this->runtime = $runtime;
$this->type = $type;
$this->network = (string)$network;
}
public function output() {
if (settings::ENABLE_HIT_STATS) {
$stats_get = new data_manage($this->runtime, 'down', 'stats_total_requests', $this->network);
(!empty($stats_get->data->contents)) ? $stats[0] = $stats_get->data->contents[0] : $stats[0] = 0;
$stats_get = new data_manage($this->runtime, 'down', 'stats_total_updates', $this->network);
(!empty($stats_get->data->contents)) ? $stats[0] += $stats_get->data->contents[0] : $stats[0] = $stats[0];
$stats_get = new data_manage($this->runtime, 'down', 'stats_requests', $this->network);
(!empty($stats_get->data->contents)) ? $stats[1] = count($stats_get->data->contents) : $stats[1] = '0';
$stats_get = new data_manage($this->runtime, 'down', 'stats_updates', $this->network);
(!empty($stats_get->data->contents)) ? $stats[2] = count($stats_get->data->contents) : $stats[2] = '0';
$stat = implode("\r\n", $stats);
return $stat."\r\n";
}
else {
return '0'."\r\n".'0'."\r\n".'0'."\r\n";
}
}
public function update() {
if ($this->type == 'request' && settings::ENABLE_HIT_STATS) {
$stats_up = new data_manage($this->runtime, 'up', 'stats_total_requests', $this->network);
$stats_up = new data_manage($this->runtime, 'up', 'stats_requests', $this->network);
}
elseif ($this->type == 'update' && settings::ENABLE_HIT_STATS) {
$stats_up = new data_manage($this->runtime, 'up', 'stats_total_updates', $this->network);
$stats_up = new data_manage($this->runtime, 'up', 'stats_updates', $this->network);
}
elseif ($this->type == 'client' && settings::WEB_FRONT && settings::ENABLE_CLIENT_STATS) {
$stats_up = new data_manage($this->runtime, 'up', 'stats_client', $this->network);
}
}
}
class client_check {
public $runtime;
function __construct($runtime) {
$this->runtime = $runtime;
}
public function bans($warns=array()) {
$warnings = array();
if (!empty($this->runtime->settings->BANNED)) {
foreach ($this->runtime->settings->BANNED as $bad_ip) {
if ($bad_ip == substr($this->runtime->env_variables['client_ip'], 0, strlen($bad_ip))) {
$warnings[] = 'Banned IP Address';
break;
}
}
}
if (!empty($this->runtime->settings->VENDOR_CODE_BAN) && $this->runtime->gwc_variables['client'] !== false) {
$client_modified = ($this->runtime->gwc_variables['version'] !== false) ? $this->runtime->gwc_variables['client'].$this->runtime->gwc_variables['version'] : $this->runtime->gwc_variables['client'];
foreach ($this->runtime->settings->VENDOR_CODE_BAN as $bad_vendor) {
if (stripos($client_modified, $bad_vendor) === 0) {
$warnings[] = 'Banned Vendor Code';
break;
}
}
}
if ($this->runtime->gwc_variables['ip'] !== false || $this->runtime->gwc_variables['ipv6'] !== false) {
if (!empty($this->runtime->settings->VENDOR_CODE_IP_UPDATE_BAN) && $this->runtime->gwc_variables['client'] !== false) {
$client_vendor_update = ($this->runtime->gwc_variables['version'] !== false) ? $this->runtime->gwc_variables['client'].$this->runtime->gwc_variables['version'] : $this->runtime->gwc_variables['client'];
foreach ($this->runtime->settings->VENDOR_CODE_IP_UPDATE_BAN as $bad_update_vendor) {
if (stripos($client_vendor_update, $bad_update_vendor) === 0) {
$warnings[] = 'Vendor Code banned from updating an IP address';
}
}
}
if ($this->runtime->gwc_variables['cache']) {
$warnings[] = 'IP updates from web caches not allowed';
}
}
if ($this->runtime->env_variables['user_agent']) {
if (!empty($this->runtime->settings->USER_AGENT_BAN)) {
foreach ($this->runtime->settings->USER_AGENT_BAN as $bad_user_agent) {
if (stripos($this->runtime->env_variables['user_agent'], $bad_user_agent) !== false) {
$warnings[] = 'Banned User Agent';
break;
}
}
}
if (!empty($this->runtime->settings->WHITELIST_USER_AGENT)) {
$network_found = $whitelisted = false;
foreach ($this->runtime->settings->WHITELIST_USER_AGENT as $good_user_agent=>$network) {
if ($network == $this->runtime->gwc_variables['net']) {
$network_found = true;
if (stripos($this->runtime->env_variables['user_agent'], $good_user_agent) === 0) {
$whitelisted = true;
break;
}
}
}
if (!$whitelisted && $network_found) {
$warnings[] = 'Banned by user agent whitelist';
}
}
if ($this->runtime->env_variables['user_agent'] != 'Mozilla/4.0') {
if (!empty($this->runtime->settings->BROWSER_DETECT) && ($this->runtime->gwc_variables['ip'] !== false || $this->runtime->gwc_variables['ipv6'] !== false)) {
foreach ($this->runtime->settings->BROWSER_DETECT as $browser_user_agent) {
if (stripos($this->runtime->env_variables['user_agent'], $browser_user_agent) !== false) {
$warnings[] = 'Browser banned from updating an IP address';
break;
}
}
}
}
else {
if ($this->runtime->gwc_variables['client'] !== false && !empty($this->runtime->settings->BAN_GDNA_CLONES)) {
foreach ($this->runtime->settings->BAN_GDNA_CLONES as $banned_gdna_clone) {
if (stripos($this->runtime->gwc_variables['client'], $banned_gdna_clone) !== false) {
$warnings[] = 'GnucDNA clone banned from the cache';
break;
}
}
}
}
}
else {
if (($this->runtime->gwc_variables['ip'] !== false || $this->runtime->gwc_variables['ipv6'] !== false) && settings::BLOCK_NO_USER_AGENT) {
$warnings[] = 'Clients with no user-agent banned from updating IP addresses';
}
elseif (settings::BLOCK_NO_USER_AGENT_ALL) {
$warnings[] = 'Clients with no user-agent banned';
}
}
if (!empty($warnings)) {
header('HTTP/1.1 404 Not Found');
if ($warnings && settings::LOG_BAN_WARNINGS) {
$log = new data_manage($this->runtime, 'up', 'log', '', array('warnings'=>$warnings));
}
}
return array_merge($warnings, $warns);
}
}
class update {
public $info;
public $success;
public $warnings;
private $already_submitted;
private $url_port;
private $domain;
private $folder;
private $unsafe_folder;
private $domain_without_port;
private $ip_v1_found_in_cache;
private $ip_v2_found_in_cache;
private $url_v1_found_in_cache;
private $url_v2_found_in_cache;
private $v2_h_block_found;
private $v2_u_block_found;
private $v2_i_block_found;
private $response_data;
function __construct($info, $type, $network='') {
$this->info = $info;
$this->info->ip = '';
$this->info->final_ip = '';
$this->info->ipv6 = false;
$this->info->url_ip = '';
$this->success = false;
$this->warnings = array();
$this->info->host_count = 0;
$this->info->url_count = 0;
$this->info->pong = '';
$this->info->v1_cache = false;
$this->info->v2_cache = false;
$this->already_submitted = false;
$this->url_port = 80;
$this->ip_v1_found_in_cache = false;
$this->ip_v2_found_in_cache = false;
$this->url_v1_found_in_cache = false;
$this->url_v2_found_in_cache = false;
$this->v2_h_block_found = false;
$this->v2_u_block_found = false;
$this->v2_i_block_found = false;
$this->network = (string)$network;
switch ($type) {
case 'ip':
$this->ip_validate();
break;