#!/usr/bin/env perl
=About
This is a perlized "Update Server" script which should run out of the box
on most machines. If not you need to install HTTP::Tiny
sudo cpan
>install HTTP::Tiny

by virii
=cut
use HTTP::Tiny;
use IO::Socket;
######## [defaults] ########
$data_dir = '/var/lib/tor';
$pidfile  = '/var/run/tor/tor.pid';
######## [ennstatus URL] ########
open ennstatus, "<", "/etc/ennstatus_updater.conf" || die "Ennstatus config file not found!\n";
    while (<ennstatus>) {
        $ennstatus_url = $1 if /^ennstatus_url = (.+)/i;
    }
close ennstatus;
######## [loop through configs] ########
@configs = qw(torrc);                                                   # every config file! like: torrc torrc0 torrc1 
for (1..scalar @configs) {
    open config, "<", "/etc/tor/" . $configs[$_ -1] || die "Cannot open $configs[$_ -1]\n";
        while (<config>) {
            $server_name = $1       if /^Nickname (\w+)/i;
            $server_type = 'Exit'   if /^ExitPolicy accept *:*/i;
            $server_type = 'Relay'  if /^ExitPolicy reject *:*/i;
            $server_type = 'Bridge' if /^BridgeRelay 1/i;
            $data_dir    = $1       if /^DataDirectory (.*)/i;
            $pidfile     = $1       if /^PidFile (.*)/i;
            $obfs        = 'True'   if /^ServerTransportPlugin obfs2,obfs3/i;
            $ip          = $1       if /^OutboundBindAddress (\d+\.\d+\.\d+\.\d+)/i;
        }
    close config;
######## [obfs check] ########  
    $obfs = 'False' if ($obfs ne "True" and $server_type == 'Bridge');
######## [ip check] ########    
    if ($ip == "") {
        $socket = new IO::Socket::INET ( PeerAddr => "google.com",
                                         PeerPort => 80,
                                         Proto => 'tcp');
        $ip = $socket->sockhost;
        close $socket;
    }
######## [fingerprint] ######## 
    open fingerprint, "<", $data_dir . '/fingerprint';
        $read_in = <fingerprint>;
        $fingerprint = $1 if $read_in =~ /^\w+ (\w{40})/i;
    close fingerprint;
######## [pid check] ########   
    open pidfile, "<", $pidfile;
        $pid = <pidfile>;
    close pidfile;
    $psaux = kill 0, $pid;
    if ($psaux) {
        $tor_status = 'Online';
    } else {
        $tor_status = 'Offline';
    }
######## [end of loop] ########
}
######## [create json string] ########    
if ($server_type eq "Bridge") {
$json_string = '{"fingerprint": "' . $fingerprint .
                '", "ip": "' . $ip .
                '", "server_name": "' . $server_name .
                '", "server_type": "' . $server_type .
                '", "obfs": "' . $obfs .
                '", "tor_status": "' . $tor_status .
                '"}';
} else {
$json_string = '{"fingerprint": "' . $fingerprint .
                '", "ip": "' . $ip .
                '", "server_name": "' . $server_name .
                '", "server_type": "' . $server_type . 
                '", "tor_status": "' . $tor_status .
                '"}';
}
######## [Send to server] ########
$request = HTTP::Tiny->new->request('POST', $ennstatus_url . '/api/update',
                    {
                      content => $json_string,
                      headers => { 'content-type' => 'application/json' }
                    }
                                    );

print $request->{content};

