ovpn correction
authorKilian Saffran <ksaffran@dks.lu>
Fri, 27 Apr 2018 11:14:45 +0000 (13:14 +0200)
committerKilian Saffran <ksaffran@dks.lu>
Fri, 27 Apr 2018 11:14:45 +0000 (13:14 +0200)
Tools/ovpn.pl [new file with mode: 0644]

diff --git a/Tools/ovpn.pl b/Tools/ovpn.pl
new file mode 100644 (file)
index 0000000..3c3ea91
--- /dev/null
@@ -0,0 +1,153 @@
+#!C:\Stawberry\Perl\bin\perl.exe
+
+use strict;
+use Getopt::Long;
+use File::Find::Rule;
+use File::Basename;
+my $connect =0;
+my $disconnect=0;
+GetOptions("connect" => \$connect,"disconnect" => \$disconnect);
+my $conn = "";
+print "Starting vpn!\n";
+if (! -d $ENV{USERPROFILE}.'/OpenVPN/config' ){
+       print "no default VPN Configuration found missing: ".$ENV{USERPROFILE}.'/OpenVPN/config'."\n";
+       exit(1);
+}
+if (-e $ENV{APPDATA}.'/Creorga/Profiles/vpncfg.txt'){
+       
+       open(CFG,$ENV{APPDATA}.'/Creorga/Profiles/vpncfg.txt');
+       while (my $l = <CFG>){
+               if ($l =~ /^OPENVPNNAME=/){
+                       ($conn) = $l =~ m/OPENVPNNAME=(.+)$/;
+                       chomp($conn); 
+               }
+       } 
+       close(CFG);
+}
+if ($conn eq "" && $disconnect != 1){
+       print "No configuration, no disconnect forced!Nothing to do!\n";
+       exit(0);
+}
+if ($connect == $disconnect){
+       if (-e  $ENV{USERPROFILE}.'/OpenVPN/config/'.$conn.'.ovpn'){
+               my $status = &vpnstatus();
+               if (exists($status->{active})){
+                       &vpndisconnect();
+               } else {
+                       &vpnconnect();
+               }
+       } else {
+               &vpndisconnect();
+       }
+}else{
+       if ($connect == 1){
+               &vpnconnect();
+       } else {
+               &vpndisconnect();
+       }
+}      
+
+sub vpnconnect(){
+       
+       my $uprofile = "";
+       #is gui or vpn running
+       
+       if (-e  $ENV{USERPROFILE}.'/OpenVPN/config/'.$conn.'.ovpn'){
+               my $status = &vpnstatus();
+               if (!exists($status->{active}->{$conn})){
+                       if ($^O eq "MSWin32"){
+                               if (exists($status->{gui})){
+                                       system('taskkill.exe /F /IM openvpn.exe');
+                                       system('taskkill.exe /F /IM openvpn-gui.exe');
+                                       sleep(1);
+                               }
+                               my $st = system('start /b "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect '.$conn.'.ovpn');
+                               if ($st == 0){
+                                       
+                                       my $bconn = 0;
+                                       my $i = 30;
+                                       while ($bconn == 0 || $i > 0){
+                                               $status = &vpnstatus();
+                                               if (exists($status->{active}->{$conn})){
+                                                       
+                                                       $bconn = 1;
+                                               }
+                                               $i--;
+                                               sleep(1);
+                                       }
+                               }
+                       }       
+               } 
+       }
+
+}
+
+sub vpnstatus(){
+       my $status = ();
+       if ($^O eq "MSWin32"){
+               
+               my $tasklist = `tasklist`;
+               my @task = split("\n",$tasklist);
+               my @ovpntasks = grep(/openvpn-gui\.exe/,@task);
+               if (scalar(@ovpntasks) > 0){
+                       $status->{gui} = "running";
+               }
+               @ovpntasks = grep(/openvpn.exe/,@task);
+               #$status->{active_connections} = scalar(@ovpntasks);
+               if (scalar(@ovpntasks) > 0){
+                       my $ff = File::Find::Rule->new();
+                       $ff->file;
+                       $ff->name('*.log');
+                       my @loglist =$ff->in($ENV{USERPROFILE}.'/OpenVPN/log');
+                       foreach my $c (@loglist){
+                               open(CLOG,$c);
+                               my @data = <CLOG>;
+                               close(CLOG);
+                               my $laststate=$data[scalar(@data)-1];
+                               chomp($laststate);
+                               if ($laststate =~ /CONNECTED/){
+                                       my ($time,$ip,$server,$port) = $laststate =~ /.+MANAGEMENT:\s>STATE:(\d+),CONNECTED,SUCCESS,(.+),(.+),(.+),,$/;
+                                       if (!exists($status->{connection}->{$ip})){
+                                               $status->{connection}->{$ip}->{config} = substr(basename($c),0,-4);;
+                                               $status->{connection}->{$ip}->{server} = $server; 
+                                               $status->{connection}->{$ip}->{port} = $port;
+                                               $status->{connection}->{$ip}->{connected_since} = $time;
+                                       }else {
+                                               if ($time >= $status->{connection}->{$ip}->{connected_since}){
+                                                       $status->{connection}->{$ip}->{config} = substr(basename($c),0,-4);
+                                                       $status->{connection}->{$ip}->{server}= $server; 
+                                                       $status->{connection}->{$ip}->{port} = $port;
+                                                       $status->{connection}->{$ip}->{connected_since} = $time;
+                                               }
+                                       }
+                               }
+                       }
+                       my @notactive = ();
+                       my $active = ();
+                       foreach my $c (keys(%{$status->{connection}})){
+                               my $routeslist = `route print -4`;      
+                               my @routes = split("\n",$routeslist);
+                               my @activetest = grep(/$c/,@routes);
+                               if (scalar(@activetest) == 0){
+                                       push @notactive,$c;
+                               } else {
+                                       $active->{$status->{connection}->{$c}->{config}} = $c;
+                               }
+                       }
+                       foreach my $na (@notactive){
+                               delete $status->{connection}->{$na};
+                       }
+                       $status->{active} = $active;
+               }
+       }
+       return $status;
+}
+
+sub vpndisconnect(){
+       
+       if ($^O eq "MSWin32"){
+               system('taskkill.exe /F /IM openvpn.exe');
+               system('taskkill.exe /F /IM openvpn-gui.exe');
+       }
+
+}
\ No newline at end of file