From: Kilian Saffran Date: Thu, 7 Mar 2019 07:35:35 +0000 (+0100) Subject: ovpn X-Git-Tag: 3.28.2^0 X-Git-Url: http://cloud.dks.lu/git/?a=commitdiff_plain;h=5accc4cd7456ee461b535bcc414caaa4aa75b2f3;p=creorga.git ovpn --- diff --git a/Tools/ovpn.pl b/Tools/ovpn.pl index 50ce6f8..4c09702 100644 --- a/Tools/ovpn.pl +++ b/Tools/ovpn.pl @@ -1,158 +1,158 @@ -#!C:\Stawberry\Perl\bin\perl.exe - -use strict; -use Getopt::Long; -use File::Find::Rule; -use File::Basename; -use Win32::HideConsole; - -hide_console; -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 = ){ - 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(); - } -} -print "Normal End!\n"; -exit(0); - -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 = ; - 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'); - } - +#!C:\Stawberry\Perl\bin\perl.exe + +use strict; +use Getopt::Long; +use File::Find::Rule; +use File::Basename; +use Win32::HideConsole; + +hide_console; +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 = ){ + 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(); + } +} +print "Normal End!\n"; +exit(0); + +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 = ; + 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