Server/Tor.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/Tcp.pm ↑ Summary Shell/Command.pm »
1 #
2 # $Id$
3 #
4 # server::tor Brik
5 #
6 package Metabrik::Server::Tor;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 tor_port => [ qw(port) ],
21 tor_listen => [ qw(address) ],
22 dns_port => [ qw(port) ],
23 dns_listen => [ qw(address) ],
24 virtual_network => [ qw(subnet) ],
25 user => [ qw(user) ],
26 conf => [ qw(file) ],
27 pidfile => [ qw(file) ],
28 },
29 attributes_default => {
30 tor_port => 9051,
31 tor_listen => '127.0.0.1',
32 dns_port => 9061,
33 dns_listen => '127.0.0.1',
34 virtual_network => '10.20.0.0/255.255.0.0',
35 conf => 'torrc',
36 pidfile => 'tor.pid',
37 },
38 commands => {
39 install => [ ], # Inherited
40 generate_conf => [ qw(file|OPTIONAL) ],
41 start => [ qw(conf_file|OPTIONAL) ],
42 stop => [ ],
43 status => [ ],
44 list_exit_nodes => [ ],
45 },
46 require_modules => {
47 'Metabrik::File::Text' => [ ],
48 'Metabrik::System::File' => [ ],
49 'Metabrik::System::Process' => [ ],
50 },
51 require_binaries => {
52 tor => [ ],
53 },
54 need_packages => {
55 ubuntu => [ qw(tor) ],
56 debian => [ qw(tor) ],
57 kali => [ qw(tor) ],
58 },
59 };
60 }
61  
62 sub brik_use_properties {
63 my $self = shift;
64  
65 return {
66 attributes_default => {
67 user => defined($self->global) && $self->global->username || 'username',
68 },
69 };
70 }
71  
72 #
73 # Inspired by https://github.com/HeitorG/nipe/blob/master/nipe.pl
74 #
75 sub generate_conf {
76 my $self = shift;
77 my ($conf) = @_;
78  
79 $conf ||= $self->conf;
80 my $datadir = $self->datadir;
81 # If it does not start with a /, we put it in $datadir
82 if ($conf !~ m{^/}) {
83 $conf = $datadir.'/'.$conf;
84 }
85  
86 $self->brik_help_run_undef_arg('generate_conf', $conf) or return;
87  
88 my $user = $self->user;
89 my $pidfile = $self->pidfile;
90 my $tor_port = $self->tor_port;
91 my $tor_listen = $self->tor_listen;
92 my $dns_port = $self->dns_port;
93 my $dns_listen = $self->dns_listen;
94 my $virtual_network = $self->virtual_network;
95  
96 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
97 $sf->remove($conf);
98  
99 my $data =<<EOF
100 DataDirectory $datadir
101 PidFile $datadir/$pidfile
102 RunAsDaemon 1
103 ClientOnly 1
104 User $user
105  
106 ControlSocket $datadir/control
107 ControlSocketsGroupWritable 1
108  
109 CookieAuthentication 1
110 CookieAuthFileGroupReadable 1
111 CookieAuthFile $datadir/control.authcookie
112  
113 Log notice file $datadir/tor.log
114  
115 TransPort $tor_port
116 TransListenAddress $tor_listen
117 DNSPort $dns_port
118 DNSListenAddress $dns_listen
119  
120 VirtualAddrNetwork $virtual_network
121 AutomapHostsOnResolve 1
122 EOF
123 ;
124  
125 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
126 $ft->write($data, $conf) or return;
127 $ft->close;
128  
129 $sf->chmod($datadir, "0700") or return;
130  
131 return $conf;
132 }
133  
134 sub start {
135 my $self = shift;
136 my ($conf) = @_;
137  
138 $conf ||= $self->conf;
139 $self->brik_help_run_undef_arg('start', $conf) or return;
140  
141 my $datadir = $self->datadir;
142  
143 # If it does not start with a /, we put it in $datadir
144 if ($conf !~ m{^/}) {
145 $conf = $datadir.'/'.$conf;
146 }
147  
148 my $cmd = "tor -f \"$conf\"";
149  
150 return $self->sudo_system($cmd);
151 }
152  
153 sub stop {
154 my $self = shift;
155  
156 my $datadir = $self->datadir;
157 my $pidfile = $self->pidfile;
158  
159 if ($pidfile !~ m{^/}) {
160 $pidfile = $datadir.'/'.$pidfile;
161 }
162  
163 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
164 $sp->kill_from_pidfile($pidfile) or return;
165  
166 return 1;
167 }
168  
169 sub status {
170 my $self = shift;
171  
172 my $datadir = $self->datadir;
173 my $pidfile = $self->pidfile;
174  
175 if ($pidfile !~ m{^/}) {
176 $pidfile = $datadir.'/'.$pidfile;
177 }
178  
179 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
180 return $sp->is_running_from_pidfile($pidfile);
181 }
182  
183 #
184 # Alternatives URLs:
185 # https://www.dan.me.uk/torlist/
186 # https://check.torproject.org/exit-addresses
187 # https://www.dan.me.uk/torcheck?ip=2.100.184.78
188 # https://globe.torproject.org/
189 # https://atlas.torproject.org/
190 #
191 sub list_exit_nodes {
192 my $self = shift;
193  
194 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
195 my $get = $cw->get('http://torstatus.blutmagie.de/ip_list_exit.php/Tor_ip_list_EXIT.csv')
196 or return;
197  
198 my $content = $get->{content};
199 my @ip_list = split(/\n/, $content);
200  
201 return \@ip_list;
202 }
203  
204 1;
205  
206 __END__
« Server/Tcp.pm ↑ Summary Shell/Command.pm »