Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use common::sense;
- use File::ChangeNotify; #Dont have it? run >ppm install File-ChangeNotify
- use Win32::API;
- use Win32::Process;
- use Config::General; #Dont have it? run >ppm install Config-General
- use Win32::PerlExe::Env; #Dont have it? run >cpan Win32::PerlExe::Env
- use PerlTray;
- our $watcher;
- my ($EXE_DIR, $EXE_FILE_NAME ) = ( Win32::GetFullPathName( $0 ) =~ /^(.*)\\([^\\]*)$/ );
- my $LOG_FILE_NAME = $EXE_FILE_NAME; $LOG_FILE_NAME =~ s/\..*?$/.log/;
- my $CFG_FILE_NAME = $EXE_FILE_NAME; $CFG_FILE_NAME =~ s/\..*?$/.cfg/;
- our $P2P_FILE_NAME = $EXE_FILE_NAME; $P2P_FILE_NAME =~ s/\..*?$/.p2p/;
- print "Reading config from $CFG_FILE_NAME\nLogging to $LOG_FILE_NAME\nWriting to $P2P_FILE_NAME\nWorking in $EXE_DIR\n";
- my $conf = new Config::General($CFG_FILE_NAME);
- our %config = $conf->getall;
- my $start = time();
- $| = 1;
- if( open( LOG, ">>$EXE_DIR\\$LOG_FILE_NAME" ) ) {
- my $BackupHandle = select( LOG );
- $| = 1;
- select( $BackupHandle );
- print LOG "# APP: $0\n# PID: $$\n";
- }
- our $tmpdir = get_tmpdir();
- Log( "Starting userland daemon /w temp folder $tmpdir" );
- my @paths = ();
- foreach my $server (keys %{$config{Server}}) {
- push(@paths,$server);
- }
- $watcher = File::ChangeNotify->instantiate_watcher (
- directories => \@paths,
- filter => qr/^server_console\.log$|ban/i,
- );
- Log("Waiting for work...");
- sub Loop {
- if (my @events = $watcher->new_events()) {
- foreach my $event (@events) {
- if ($event->type() eq 'modify') {
- my $path = $event->path();
- $path =~ s/\/.*//;
- Work($config{Server}->{$path}->{Name}, $path);
- }
- }
- }
- }
- sub Work {
- my ($servername,$path) = @_;
- Log("Work begun for $servername");
- my $log = $path."\\server_console.log";
- open(FILE,$log) or die "Adjust the log variable! $log above"; my @lines = <FILE>; close FILE;
- Log("Reading console log...");
- my %names = ();
- my $cnxn1_re = q{(\d+):(\d+):(\d+) Player (.*?) connected \(id=(\d+)\).};
- my $cnxn2_re = q{(\d+):(\d+):(\d+) BattlEye Server: Player #(\d+) (.*?) \((\d+).(\d+).(\d+).(\d+):(\d+)\) - GUID: (.*)};
- foreach my $line (@lines) {
- if ($line =~ /$cnxn1_re/) {
- my $h = $1; my $m = $2; my $s = $3;
- my $playname = $4; my $playid = $5;
- #print qq{$h | $m | $s | $playname | $playid \n}; #debug
- $names{$playname}{ID} = $playid;
- }
- if ($line =~ /$cnxn2_re/) {
- my $h = $1; my $m = $2; my $s = $3;
- my $playnum = $4; my $playname = $5;
- my $oct1 = $6; my $oct2 = $7; my $oct3 = $8; my $oct4 = $9; my $port = $10;
- my $guid = $11;
- #print qq{$h | $m | $s | $playnum | $playname | $oct1 | $oct2 | $oct3 | $port | $guid \n}; #debug
- $names{$playname}{GUID} = $guid;
- $names{$playname}{IP} = qq{$oct1.$oct2.$oct3.$oct4};
- }
- }
- my %bansV2 = ();
- my $banlist_v2 = $path."\\bans.txt";
- open(FILE,$banlist_v2) or die "Adjust the banlist_v2 variable! $banlist_v2 above"; my @lines = <FILE>; close FILE;
- Log("Reading bansV2...");
- my $ban1_re = q{(.*?) -1 (.*)};
- foreach my $line (@lines) {
- if ($line =~ /$ban1_re/) {
- my $guid = $1;
- my $playname_reason = $2;
- #print qq{$guid | $playname_reason \n}; #debug
- $bansV2{$guid}{BAN} = $playname_reason;
- }
- }
- my %bansV1 = ();
- my $banlist_v1 = $path."\\ban.txt";
- open(FILE,$banlist_v1) or die "Adjust the banlist_v1 variable! $banlist_v1 above"; my @lines = <FILE>; close FILE;
- Log("Reading bansV1...");
- my $ban2_re = q{(\d+)};
- foreach my $line (@lines) {
- if ($line =~ /$ban2_re/) {
- my $playid = $1;
- #print qq{$playid \n}; #debug
- $bansV1{$playid}{BAN} = $playid;
- }
- }
- my @ips = ();
- my $blocklist = $config{PeerBlock}->{Path}."\\lists\\".$P2P_FILE_NAME;
- my @lines = ();
- if (open(FILE,$blocklist)) { @lines = <FILE>; close FILE; }
- Log("Reading blocklist...");
- open(FILE,">>$blocklist") or die "Adjust the blocklist variable! $blocklist above";
- Log("Writing blocklist...");
- foreach my $line (@lines) {
- my ($ban,$range) = split(':',$line);
- my $ip = (split('-',$range))[0];
- push(@ips,$ip);
- }
- my $before = scalar @ips;
- my $log = "Server console modified on ". localtime()."\n";
- foreach my $name (keys %names) {
- next if !$names{$name}{IP} || $names{$name}{IP} eq '';
- foreach my $guid (keys %bansV2) {
- if ($names{$name}{GUID} && $names{$name}{GUID} eq $guid && !grep(/$names{$name}{IP}/, @ips)) {
- print FILE qq{[$name] $bansV2{$guid}{BAN}:$names{$name}{IP}-$names{$name}{IP}\n};
- push(@ips,$names{$name}{IP});
- $log .= qq{$name ($guid) banned, blocking $names{$name}{IP}\n};
- }
- }
- foreach my $id (keys %bansV1) {
- if ($names{$name}{ID} && $names{$name}{ID} == $id && !grep(/$names{$name}{IP}/, @ips)) {
- print FILE qq{[$name] $bansV1{$id}{BAN}:$names{$name}{IP}-$names{$name}{IP}\n};
- push(@ips,$names{$name}{IP});
- $log .= qq{$name ($id) banned, blocking $names{$name}{IP}\n};
- }
- }
- }
- close FILE;
- my $after = scalar @ips;
- $log .= "IP bans before, $before after, $after.";
- Log($log);
- Reload() if ($before != $after);
- open(FILE,">arma2oaserver_p2p.eml") or die "Can't write to working folder! $!"; print FILE $log; close FILE;
- Log("Sending email...");
- my $sendmail = $EXE_DIR.$tmpdir."\\sendEmail.exe"; my $proc;
- 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";
- Log("Work finished!");
- }
- sub Reload {
- Log("PeerBlock lists reloading....");
- my $reg_window = Win32::API->new('user32', 'RegisterWindowMessage', 'P', 'I');
- my $sendmsg = Win32::API->new( 'user32', 'SendMessage', 'NNNN', 'N' );
- my $rw = $reg_window->Call("PeerBlockLoadLists");
- my $res = $sendmsg->Call(-1,$rw,0,0);
- Log("PeerBlock cache regenerated! $rw - $res");
- }
- sub Log {my $s = shift; print LOG "[" . localtime() . "] ".$s."\n" if( fileno( LOG ) ); print "[" . localtime() . "] ".$s."\n";}
- sub PopupMenu {
- return [
- ["Exit", "exit"],
- ];
- }
- SetTimer($config{Options}->{Sleep} * 1000, \&Loop);
- sub ToolTip { "Arma2:OA Server PeerBlock Daemon"; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement