Haineko
view release on metacpan or search on metacpan
lib/Haineko/Root.pm view on Meta::CPAN
package Haineko::Root;
use feature ':5.10';
use strict;
use warnings;
sub index {
# GET /
use Haineko;
my $class = shift;
my $httpd = shift;
my $neko1 = { 'name' => $httpd->name, 'version' => $Haineko::VERSION };
return $httpd->res->json( 200, $neko1 );
}
sub info {
# GET /dump, /conf
my $class = shift;
my $httpd = shift;
my $xforwarded = [ split( ',', $httpd->req->header('X-Forwarded-For') || q() ) ];
my $remoteaddr = pop @$xforwarded || $httpd->req->address // undef;
my $ip4network = undef;
# Only 127.0.0.1 is permitted
use Net::CIDR::Lite;
$ip4network = Net::CIDR::Lite->new( '127.0.0.1/32' );
if( $ip4network->find( $remoteaddr ) ) {
my $requesturl = $httpd->req->path_info;
my $configfile = q();
my $configdata = {};
my $smtpconfig = undef;
if( $requesturl eq '/dump' ) {
# /dump
use Data::Dumper;
return $httpd->res->text( 200, Data::Dumper::Dumper $httpd );
} else {
# /conf
use Haineko::JSON;
use File::Basename;
if( defined $ENV{'HAINEKO_CONF'} ) {
if( -f -r -s $ENV{'HAINEKO_CONF'} ) {
# HAINEKO_CONF=/path/to/haineko.cf
$configfile = $ENV{'HAINEKO_CONF'};
} else {
$configfile = 'Haineko::Default->conf'
}
}
$configdata->{'haineko.cf'} = {
'path' => $configfile,
'data' => $httpd->conf,
};
$smtpconfig = $configdata->{'haineko.cf'}->{'data'}->{'smtpd'};
for my $e ( 'mailer', 'access' ) {
# mailer: auth, mail, rcpt
# access: conn, rcpt
for my $f ( keys %{ $smtpconfig->{ $e } } ) {
# Load mailertables, access configurations
my $g = $smtpconfig->{ $e }->{ $f };
my $h = File::Basename::basename $g; $h =~ s/[-]debug\z//;
$configdata->{ $h } = {
'path' => undef,
'data' => undef,
};
next unless -f -r -s $g;
$configdata->{ $h }->{'path'} = $g;
$configdata->{ $h }->{'data'} = Haineko::JSON->loadfile( $g );
next unless $h eq 'authinfo';
for my $i ( keys %{ $configdata->{'authinfo'}->{'data'} } ) {
( run in 1.260 second using v1.01-cache-2.11-cpan-354807fb38d )