Project

General

Profile

Download (1.37 KB) Statistics
| Branch: | Revision:
1
#!/usr/bin/perl
2

    
3
use CGI qw(:standard);
4
use JSON;
5
use Tie::DBI;
6
use Data::Dumper;
7
use ConfigReader::Simple;
8
use Hash::Merge qw( merge );
9

    
10
$config = ConfigReader::Simple->new("/etc/stabile/config.cfg",
11
    [qw(
12
        DBI_PASSWD
13
        DBI_USER
14
    )]);
15
my $dbiuser =  $config->get('DBI_USER') || "irigo";
16
my $dbipasswd = $config->get('DBI_PASSWD') || "";
17
$dbopts = {db=>'mysql:steamregister', key=>'uuid', autocommit=>0, CLOBBER=>2, user=>$dbiuser, password=>$dbipasswd};
18

    
19
print "Content-type: application/json\n\n";
20

    
21
unless ( tie(%intreg,'Tie::DBI', Hash::Merge::merge({table=>'networks', key=>'internalip'}, $dbopts)) ) {print "Unable to access network register"};
22
unless ( tie(%extreg,'Tie::DBI', Hash::Merge::merge({table=>'networks', key=>'externalip'}, $dbopts)) ) {print "Unable to access network register"};
23

    
24
my $remoteip = $ENV{REMOTE_ADDR};
25
my $internalip = '';
26
my $externalip = '';
27
my $gw = '';
28
my $obj;
29

    
30
if ($remoteip =~ /^10\./) {
31
    $internalip = $remoteip;
32
    $obj = $intreg{$internalip};
33
    $externalip = $obj->{'externalip'};
34
} else {
35
    $externalip = $remoteip;
36
    $obj = $extreg{$externalip};
37
    $internalip = $obj->{'internalip'};
38
}
39
if ($internalip && $internalip ne '--') {
40
    $gw = "$1.1" if ($internalip =~ /(\d+\.\d+\.\d+)\.\d+/);
41
}
42

    
43
print qq|{"internalip": "$internalip", "externalip": "$externalip", "gw": "$gw", "remoteip": "$remoteip"}|;
44

    
45
untie %intreg;
46
untie %extreg;
    (1-1/1)