[Tails-dev] ***SPAM*** Re: ***SPAM*** Re: Proposed changes t…

Üzenet törlése

Válasz az üzenetre
Szerző: Cryptkiddy
Dátum:  
Címzett: intrigeri
CC: tails-dev
Régi témák: Re: [Tails-dev] ***SPAM*** Re: Proposed changes to tails-starter-i2p && my first contribution to OSS
Tárgy: [Tails-dev] ***SPAM*** Re: ***SPAM*** Re: Proposed changes totails-starter-i2p && my first contribution to OSS
Hi Intrigeri,

Am Dienstag, 6. September 2011, 16:41:12 schrieb intrigeri:
> BTW, such regexps are starting to be complicated enough to deserve the
> use of the extended RE syntax ("x" flag).
>
> I recommend using that extended syntax to write your regexps on
> several lines and document their components along the way like I
> showed above.



Ok, I have replaced it with an extended RegEx:
(Which, indeed, makes it look a lot more readable)

#############################
$client_config =~
    m{
    \n                      # (beginning of line)
        clientApp\.                     # (literal)
        $client_app_no                  # (obtained above)
        \.args=                         # (literal)


        ([0-9]{1,5})                    #this is the port we are looking for.
                                        #When only https is given it won't
                                        #be here but we don't handle that case 
anyway


        [[:space:]]                     # (delimiting space)


        ( [0-9s:,.-] | [[:space:]] )+   #we are eating up :
                                        #an ip4 and/or ip6 address 
(comma/space separated)
                                        #and if https is additionally given
                                        #we also eat "-s" followed again by:
                                        #(port, ip4, ip6, commas, spaces)


        [[:space:]] \.\/webapps\/       # (literal)
        [[:space:]]*                    # (arbitrary amount of tabs and/or 
spaces)
    \n                      # (end of line)
    }x;
########################################


Cheers,
    Cryptkiddy

diff --git a/config/chroot_local-includes/usr/local/bin/tails-start-i2p b/config/chroot_local-includes/usr/local/bin/tails-start-i2p
index 716d05d..8ae29d8 100755
--- a/config/chroot_local-includes/usr/local/bin/tails-start-i2p
+++ b/config/chroot_local-includes/usr/local/bin/tails-start-i2p
@@ -25,6 +25,8 @@ See https://tails.boum.org/.
use Desktop::Notify;
use Locale::gettext;
use POSIX;
+use Memoize;
+memoize('get_router_port');

### initialization
setlocale(LC_MESSAGES, "");
@@ -32,9 +34,70 @@ textdomain("tails-i2p-notify-user");

### helper subs

-# TODO: get router port (default 7657) from /etc/i2p/clients.config
 sub get_router_port {
-    return 7657;
+    # gets router port from /etc/i2p/clients.config . If this fails we return I2P default port (7657).
+    my $i2p_default_port = 7657;
+    my $router_port = 0;
+
+
+    my $conffile;
+    if(! open($conffile, '<', '/etc/i2p/clients.config')) {
+        warn "Can not open i2p config file '/etc/i2p/client.config'.".
+        " Using default port ".$i2p_default_port." for I2P webinterface instead.";
+        return $i2p_default_port;
+    }
+    my $client_config = join('', <$conffile>);
+    close($conffile);
+
+    my $client_app_no;
+    my $regex = '\nclientApp\.([0-9]+)\.main=net\.i2p\.router\.web\.RouterConsoleRunner\n';
+    #we are trying to get $NUMBER$ from >>clientApp.$NUMBER$.main=net.i2p.router.web.RouterConsoleRunner<<
+
+    if(! (($client_app_no) = ($client_config =~ m/$regex/))) {
+        warn "RegEx checking 'clientApp.[0-9].main=net.i2p.router.web.RouterConsoleRunner' didn't match.".
+        "Can not find port in '/etc/i2p/client.config'." .
+        " Using default port ".$i2p_default_port." for I2P webinterface instead.";
+        return $i2p_default_port;
+    }
+
+
+
+    #Below we match something like:
+    #      'clientApp.$NUMBER$.args=$PORT$ ::1,127.0.0.1 ./webapps/'
+    # or   'clientApp.$NUMBER$.args=$PORT$ ::1 ./webapps/
+    # or   'clientApp.$NUMBER$.args=$PORT$ ::1,127.0.0.1 -s 7667 ::1,127.0.0.1 ./webapps/
+    $client_config =~
+    m{
+    \n                      # (beginning of line)
+        clientApp\.         # (literal)
+        $client_app_no      # (obtained above)
+        \.args=             # (literal)
+        ([0-9]{1,5})        #this is the port we are looking for. When only https is given it won't
+                            #be here but we don't handle that case anyway
+
+        [[:space:]]         # (delimiting space)
+
+        ( [0-9s:,.-] | [[:space:]] )+   #we are eating up :
+                                        #an ip4 and/or ip6 address (comma/space separated)
+                                        #and if https is additionally given
+                                        #we also eat "-s" followed again by (port, ip4, ip6, commas, spaces)
+
+        [[:space:]] \.\/webapps\/       # (literal)
+        [[:space:]]*            # (arbitrary amount of tabs and/or spaces)
+    \n                       # (end of line)
+    }x;
+
+
+
+    $router_port=$1;
+
+    if (! $router_port){
+        warn "RegEx checking 'clientApp.".$client_app_no.".args= [...]' didn't match.".
+        "Can not find port in '/etc/i2p/client.config'." .
+        " Using default port ".$i2p_default_port." for I2P webinterface instead.";
+        return $i2p_default_port;
+    }
+    return $router_port;
 }


# TODO: more perlish way to do below?