Changeset - f0c4f1476771
[Not reviewed]
default
0 1 0
virii - 11 years ago 2013-12-18 20:58:42
virii@enn.lu
multiple instances now fully supported
1 file changed with 16 insertions and 11 deletions:
0 comments (0 inline, 0 general)
Scripts/update_server.pl
Show inline comments
 
#!/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;
 
@configs = qw(torrc torrc2);					 # every config file! like: torrc torrc0 torrc1 
 
$data_dir = '/var/lib/tor';					 # default data dir
 
$pidfile  = '/var/run/tor/tor.pid';				 # default pid file
 
$API_url = 'http://enn.lu';					 # will be ignored if /etc/ennstatus_updater.conf is present
 
######## [ennstatus URL - config file] ######
 
if (-e '/etc/ennstatus_updater.conf') {
 
	open ennstatus, "<", "/etc/ennstatus_updater.conf" || die "Cannot open 'ennstatus_updater.conf'!\n";
 
    		while (<ennstatus>) {
 
        		$ennstatus_url = $1 if /^ennstatus_url = (.+)/i;
 
    		}
 
	close ennstatus;
 
} else {
 
	$ennstatus_url = $API_url;
 
}
 
######## [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;
 
        }
 
@@ -49,26 +54,24 @@ for (1..scalar @configs) {
 
        $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 .
 
@@ -77,13 +80,15 @@ if ($server_type eq "Bridge") {
 
                '", "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};
 
######## [end of loop] ########
 
}
 

	
0 comments (0 inline, 0 general)