Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.12 KB | None | 0 0
  1. use common::sense;
  2. use File::ChangeNotify;  #Dont have it?  run >ppm install File-ChangeNotify
  3. use Win32::API;
  4. use Win32::Process;
  5. use Config::General;     #Dont have it?  run >ppm install Config-General
  6. use Win32::PerlExe::Env; #Dont have it?  run >cpan Win32::PerlExe::Env
  7. use PerlTray;
  8.  
  9. our $watcher;
  10. my ($EXE_DIR, $EXE_FILE_NAME ) = ( Win32::GetFullPathName( $0 ) =~ /^(.*)\\([^\\]*)$/ );
  11. my $LOG_FILE_NAME = $EXE_FILE_NAME; $LOG_FILE_NAME =~ s/\..*?$/.log/;
  12. my $CFG_FILE_NAME = $EXE_FILE_NAME; $CFG_FILE_NAME =~ s/\..*?$/.cfg/;
  13. our $P2P_FILE_NAME = $EXE_FILE_NAME; $P2P_FILE_NAME =~ s/\..*?$/.p2p/;
  14. print "Reading config from $CFG_FILE_NAME\nLogging to $LOG_FILE_NAME\nWriting to $P2P_FILE_NAME\nWorking in $EXE_DIR\n";
  15. my $conf = new Config::General($CFG_FILE_NAME);
  16. our %config = $conf->getall;
  17. my $start = time();
  18. $| = 1;
  19. if( open( LOG, ">>$EXE_DIR\\$LOG_FILE_NAME" ) ) {
  20.     my $BackupHandle = select( LOG );
  21.     $| = 1;
  22.     select( $BackupHandle );
  23.     print LOG "# APP: $0\n# PID: $$\n";
  24. }
  25.  
  26. our $tmpdir  = get_tmpdir();
  27. Log( "Starting userland daemon /w temp folder $tmpdir" );
  28.  
  29. my @paths = ();
  30. foreach my $server (keys %{$config{Server}}) {
  31.     push(@paths,$server);
  32. }
  33.  
  34. $watcher = File::ChangeNotify->instantiate_watcher (
  35.     directories => \@paths,
  36.     filter      => qr/^server_console\.log$|ban/i,
  37. ); 
  38. Log("Waiting for work...");
  39.  
  40.  
  41. sub Loop {
  42.     if (my @events = $watcher->new_events()) {
  43.         foreach my $event (@events) {
  44.             if ($event->type() eq 'modify') {
  45.                 my $path = $event->path();
  46.                 $path =~ s/\/.*//;
  47.                 Work($config{Server}->{$path}->{Name}, $path);
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. sub Work {
  54.     my ($servername,$path) = @_;
  55.     Log("Work begun for $servername");
  56.     my $log = $path."\\server_console.log";
  57.     open(FILE,$log) or die "Adjust the log variable! $log above"; my @lines = <FILE>; close FILE;
  58.     Log("Reading console log...");
  59.     my %names = ();
  60.     my $cnxn1_re = q{(\d+):(\d+):(\d+) Player (.*?) connected \(id=(\d+)\).};
  61.     my $cnxn2_re = q{(\d+):(\d+):(\d+) BattlEye Server: Player #(\d+) (.*?) \((\d+).(\d+).(\d+).(\d+):(\d+)\) - GUID: (.*)};
  62.     foreach my $line (@lines) {
  63.         if ($line =~ /$cnxn1_re/) {
  64.             my $h = $1; my $m = $2; my $s = $3;
  65.             my $playname = $4; my $playid = $5;
  66.             #print qq{$h | $m | $s | $playname | $playid \n}; #debug
  67.             $names{$playname}{ID} = $playid;
  68.  
  69.         }
  70.         if ($line =~ /$cnxn2_re/) {
  71.             my $h = $1; my $m = $2; my $s = $3;
  72.             my $playnum = $4; my $playname = $5;
  73.             my $oct1 = $6; my $oct2 = $7; my $oct3 = $8; my $oct4 = $9; my $port = $10;
  74.             my $guid = $11;
  75.             #print qq{$h | $m | $s | $playnum | $playname | $oct1 | $oct2 | $oct3 | $port | $guid \n}; #debug
  76.             $names{$playname}{GUID} = $guid;
  77.             $names{$playname}{IP} = qq{$oct1.$oct2.$oct3.$oct4};
  78.  
  79.         }
  80.     }
  81.     my %bansV2 = ();
  82.     my $banlist_v2 = $path."\\bans.txt";
  83.     open(FILE,$banlist_v2) or die "Adjust the banlist_v2 variable! $banlist_v2 above"; my @lines = <FILE>; close FILE;
  84.     Log("Reading bansV2...");
  85.     my $ban1_re = q{(.*?) -1 (.*)};
  86.     foreach my $line (@lines) {
  87.         if ($line =~ /$ban1_re/) {
  88.             my $guid = $1;
  89.             my $playname_reason = $2;
  90.             #print qq{$guid | $playname_reason \n}; #debug 
  91.             $bansV2{$guid}{BAN} = $playname_reason;
  92.         }
  93.     }
  94.     my %bansV1 = ();
  95.     my $banlist_v1 = $path."\\ban.txt";
  96.     open(FILE,$banlist_v1) or die "Adjust the banlist_v1 variable! $banlist_v1 above"; my @lines = <FILE>; close FILE;
  97.     Log("Reading bansV1...");
  98.     my $ban2_re = q{(\d+)};
  99.     foreach my $line (@lines) {
  100.         if ($line =~ /$ban2_re/) {
  101.             my $playid = $1;
  102.             #print qq{$playid \n}; #debug  
  103.             $bansV1{$playid}{BAN} = $playid;
  104.         }
  105.     }
  106.     my @ips = ();
  107.     my $blocklist = $config{PeerBlock}->{Path}."\\lists\\".$P2P_FILE_NAME;
  108.     my @lines = ();
  109.     if (open(FILE,$blocklist)) { @lines = <FILE>; close FILE; }
  110.     Log("Reading blocklist...");
  111.     open(FILE,">>$blocklist") or die "Adjust the blocklist variable! $blocklist above";
  112.     Log("Writing blocklist...");
  113.     foreach my $line (@lines) {
  114.         my ($ban,$range) = split(':',$line);
  115.         my $ip = (split('-',$range))[0];
  116.         push(@ips,$ip);
  117.     }
  118.     my $before = scalar @ips;
  119.     my $log = "Server console modified on ". localtime()."\n";
  120.     foreach my $name (keys %names) {
  121.         next if !$names{$name}{IP} || $names{$name}{IP} eq '';
  122.         foreach my $guid (keys %bansV2) {
  123.             if ($names{$name}{GUID} && $names{$name}{GUID} eq $guid && !grep(/$names{$name}{IP}/, @ips)) {
  124.                 print FILE qq{[$name] $bansV2{$guid}{BAN}:$names{$name}{IP}-$names{$name}{IP}\n};
  125.                 push(@ips,$names{$name}{IP});
  126.                 $log .= qq{$name ($guid) banned, blocking $names{$name}{IP}\n};
  127.             }
  128.         }
  129.         foreach my $id (keys %bansV1) {
  130.             if ($names{$name}{ID} && $names{$name}{ID} == $id && !grep(/$names{$name}{IP}/, @ips)) {
  131.                 print FILE qq{[$name] $bansV1{$id}{BAN}:$names{$name}{IP}-$names{$name}{IP}\n};
  132.                 push(@ips,$names{$name}{IP});
  133.                 $log .= qq{$name ($id) banned, blocking $names{$name}{IP}\n};
  134.             }
  135.         }
  136.     }
  137.     close FILE;
  138.     my $after = scalar @ips;
  139.     $log .= "IP bans before, $before after, $after.";
  140.     Log($log);
  141.     Reload() if ($before != $after);
  142.     open(FILE,">arma2oaserver_p2p.eml") or die "Can't write to working folder! $!"; print FILE $log; close FILE;
  143.     Log("Sending email...");
  144.     my $sendmail = $EXE_DIR.$tmpdir."\\sendEmail.exe"; my $proc;
  145.     Win32::Process::Create($proc,$sendmail,qq{sendEmail -f tcgbans\@gmail.com -t tcgbans\@gmail.com -u "Log entries scanned for $servername" -s smtp.gmail.com:587 -xu tcgbans\@gmail.com -xp innocent1 -o message-file=arma2oaserver_p2p.eml -q},0,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,".") or die "Adjust the sendmail variable! $sendmail above";
  146.     Log("Work finished!");
  147. }
  148.  
  149. sub Reload {
  150.     Log("PeerBlock lists reloading....");
  151.     my $reg_window = Win32::API->new('user32', 'RegisterWindowMessage', 'P', 'I');
  152.     my $sendmsg = Win32::API->new( 'user32', 'SendMessage', 'NNNN', 'N' );
  153.     my $rw = $reg_window->Call("PeerBlockLoadLists");
  154.     my $res = $sendmsg->Call(-1,$rw,0,0);
  155.     Log("PeerBlock cache regenerated! $rw - $res");
  156. }
  157.  
  158. sub Log {my $s = shift; print LOG "[" . localtime() . "] ".$s."\n" if( fileno( LOG ) ); print "[" . localtime() . "] ".$s."\n";}
  159.  
  160. sub PopupMenu {
  161.     return [
  162.         ["Exit", "exit"],
  163.     ];
  164. }
  165.  
  166. SetTimer($config{Options}->{Sleep} * 1000, \&Loop);
  167.  
  168. sub ToolTip { "Arma2:OA Server PeerBlock Daemon"; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement