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