aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Core/Util.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MasterServer/Core/Util.pm')
-rwxr-xr-xlib/MasterServer/Core/Util.pm35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/MasterServer/Core/Util.pm b/lib/MasterServer/Core/Util.pm
new file mode 100755
index 0000000..001137d
--- /dev/null
+++ b/lib/MasterServer/Core/Util.pm
@@ -0,0 +1,35 @@
+
+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 true/false if valid 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;