#!/usr/bin/perl -W # # torbandwctl.pl - A script which sets the given bandwidths to the Tor server at the # specified times. # # Copyright (C) 2006 Bogdan 'bogdro' Drozdowski # (bogdandr AT op.pl, bogdro AT rudy.mif.pg.gda.pl) # # Licence: GNU General Public Licence v2 # # Last modified : 2006-10-12 # # Syntax: # ./torbandwctl.pl [-p|--port NNNN] hour1:min1 "bandwidth1" [hour2:min2 "bandwidth2" ...] # # PLEASE PUT THE BANDWIDTH IN QUOTATION MARKS ("), SO THEY WON'T BE # INTERPRETED BY THE SHELL AND PASSED AS ONE ARGUMENT. # # Exit codes: 1 - arguments missing # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foudation: # Free Software Foundation # 51 Franklin Street, Fifth Floor # Boston, MA 02110-1301 # USA use strict; use warnings; use IO::Socket::INET; if ( @ARGV < 2 ) { print "Syntax: $0 [-p|--port NNNN] hour1:min1 \"bandwidth1\" [hour2:min2 \"bandwidth2\" ...]\n". "PLEASE PUT THE BANDWIDTH IN QUOTATION MARKS (\").\n". "Default port value is 9051.\n"; exit 1; } my (@times, @bands, $port, $sock); $port = 9051; for ( my $i=0; $i <= $#ARGV; $i++ ) { if ( $ARGV[$i] =~ /(\d+):(\d+)/ ) { push @times, $1*100+$2; } elsif ( $ARGV[$i] eq "-p" || $ARGV[$i] eq "--port" ) { $port = $ARGV[++$i]; } else { push @bands, $ARGV[$i]; } } @times = sort @times; while (1) { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); for ( my $i=0; $i <= $#times; $i++ ) { if ( $hour*100+$min == $times[$i] ) { &tor_conn($port); &tor_send("AUTHENTICATE"); &tor_send("SETCONF bandwidthrate=\"".$bands[$i]."\""); &tor_send("SAVECONF"); &tor_disconn; } } sleep 60; } exit 0; sub tor_conn { $sock = new IO::Socket::INET ( PeerAddr => '127.0.0.1', PeerPort => shift, Proto => 'tcp' ); } sub tor_send { $sock->send((shift)."\r\n"); } sub tor_disconn { $sock->close(); }