aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Core/Util.pm
blob: eb4d50914811087f24638a56b4cc9db1d6824c52 (plain)
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

package MasterServer::Core::Util;

use strict;
use warnings;
use IP::Country::Fast;
use POSIX qw/strftime/;
use Exporter 'import';

our @EXPORT = qw| valid_address ip2country |;

################################################################################
## return the abbreviated country based on IP
################################################################################
sub ip2country {
my ($self, $ip) = @_;
  my $reg = IP::Country::Fast->new();
  return $reg->inet_atocc($ip);
}

################################################################################
## Verify whether a given domain name or IP address and port are valid.
## returns 1/0 if valid/invalid ip + port
################################################################################
sub valid_address {
  my ($self, $a, $p) = @_;
  
  # check if ip and port are in valid range
  my $val_addr = ($a =~ '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b');
  my $val_port = (0 < $p && $p <= 65535);
  
  # exclude addresses where we don't want people sniffing
  for (qw|192.168.(.\d*).(.\d*) 127.0.(.\d*).(.\d*) 10.0.(.\d*).(.\d*)|){$val_addr = 0 if ($a =~ m/$_/)}
  
  # only return true if both are valid  
  return ($val_addr && $val_port);
}

1;