Make api more rich
* Implement help * Add simple dns resolve commands * Add punycode <-> unicode conversions * Add time info
This commit is contained in:
57
lib/utils.pm
57
lib/utils.pm
@@ -1,6 +1,8 @@
|
||||
package utils;
|
||||
|
||||
use strict;
|
||||
use Socket; # for name resolves
|
||||
use Net::LibIDN ':all';
|
||||
use vars qw/$VERSION/;
|
||||
|
||||
$VERSION = "1.0";
|
||||
@@ -21,4 +23,59 @@ sub ip ($) {
|
||||
return ('200', 'text/plain', $msg);
|
||||
}
|
||||
|
||||
sub getaddrbyname ($) {
|
||||
my $name = shift;
|
||||
return ('400', 'text/plain', "Supply hostname\n") unless (defined($name));
|
||||
my @addresses = gethostbyname($name);
|
||||
|
||||
if (@addresses > 0) {
|
||||
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
|
||||
|
||||
if (@addresses > 1) {
|
||||
return ('200', 'text/plain', join("\n", @addresses) . "\n");
|
||||
} else {
|
||||
return ('200', 'text/plin', "$addresses[0]\n");
|
||||
}
|
||||
} else {
|
||||
return ('404', 'text/plain', "Not found\n");
|
||||
}
|
||||
}
|
||||
|
||||
sub getnamebyaddr ($) {
|
||||
my $addr = shift;
|
||||
chomp($addr);
|
||||
return ('400', 'text/plain', "Supply address\n") unless (defined($addr));
|
||||
my $name = gethostbyaddr(inet_aton($addr), AF_INET);
|
||||
|
||||
if (defined($name)) {
|
||||
return ('200', 'text/plain', "$name\n");
|
||||
} else {
|
||||
return ('404', 'text/plain', "Not found\n");
|
||||
}
|
||||
}
|
||||
|
||||
sub punycoder ($) {
|
||||
my $str = shift;
|
||||
$str = Net::LibIDN::idn_to_ascii($str, 'utf-8');
|
||||
if (defined($str)) {
|
||||
return ('200', 'text/plain', "$str\n");
|
||||
} else {
|
||||
return ('400', 'text/plain', "Bad string\n")
|
||||
}
|
||||
}
|
||||
|
||||
sub punydecoder ($) {
|
||||
my $str = shift;
|
||||
$str = Net::LibIDN::idn_to_unicode($str, 'utf-8');
|
||||
if (defined($str)) {
|
||||
return ('200', 'text/plain', "$str\n");
|
||||
} else {
|
||||
return ('400', 'text/plain', "Bad string\n")
|
||||
}
|
||||
}
|
||||
|
||||
sub mytime () {
|
||||
return ('200', 'text/plain', time() . "\n");
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user