1

Make api more rich

* Implement help
* Add simple dns resolve commands
* Add punycode <-> unicode conversions
* Add time info
This commit is contained in:
eleksir
2019-09-01 10:33:10 +03:00
parent 3500798f77
commit 5b17cfaa74
2 changed files with 97 additions and 16 deletions

View File

@@ -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;