[2/3] ovpnmain.cgi/index.cgi: Use OpenVPN::n2n_status()
Commit Message
Both pages now use the same check, so a working N2N tunnel is no longer
shown as DISCONNECTED after an incomplete session attempt.
Fixes: #13897
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
html/cgi-bin/index.cgi | 12 ++------
html/cgi-bin/ovpnmain.cgi | 58 ++-------------------------------------
2 files changed, 5 insertions(+), 65 deletions(-)
@@ -20,7 +20,6 @@
###############################################################################
use strict;
-use Net::Telnet;
use Sort::Naturally;
# enable only the following on debugging purpose
@@ -31,6 +30,7 @@ require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
require "${General::swroot}/wireguard-functions.pl";
+require "${General::swroot}/ovpn-functions.pl";
require "/opt/pakfire/lib/functions.pl";
my %cgiparams=();
@@ -504,17 +504,11 @@ END
$count++;
my $tport = $ovpnconfig{$dkey}[22];
next if ($tport eq '');
- my $tnet = new Net::Telnet ( Timeout=>5, Errmode=>'return', Port=>$tport);
- $tnet->open('127.0.0.1');
- my @output = $tnet->cmd(String => 'state', Prompt => '/(END.*\n|ERROR:.*\n)/');
- my @tustate = split(/\,/, $output[1]);
- my $display;
+ my $display = _('DISCONNECTED');
my $display_colour = $Header::colourred;
- if ( $tustate[1] eq 'CONNECTED' || ($tustate[1] eq 'WAIT')) {
+ if (&OpenVPN::n2n_status($tport) eq "CONNECTED") {
$display_colour = $Header::colourgreen;
$display = _('CONNECTED');
- } else {
- $display = $tustate[1];
}
if ($count %2){
$col = $color{'color22'};
@@ -31,7 +31,6 @@ use MIME::Base32;
use MIME::Base64;
use Net::DNS;
use Net::Ping;
-use Net::Telnet;
use Sort::Naturally;
use URI::Encode qw(uri_encode uri_decode);;
@@ -39,6 +38,7 @@ require '/var/ipfire/general-functions.pl';
require "${General::swroot}/header.pl";
require "${General::swroot}/countries.pl";
require "${General::swroot}/location-functions.pl";
+require "${General::swroot}/ovpn-functions.pl";
# enable only the following on debugging purpose
#use warnings;
@@ -1024,60 +1024,6 @@ sub writecollectdconf {
&General::system("/usr/local/bin/collectdctrl", "restart");
}
-sub openvpn_status($) {
- my $port = shift;
-
- # Create a new Telnet session
- my $telnet = new Net::Telnet(
- Port => $port,
- Timeout => 1,
- Errmode => "return",
- );
-
- # Connect
- $telnet->open("127.0.0.1");
-
- # Send a command
- my @output = $telnet->cmd(
- String => "state",
- Prompt => "/(END.*\n|ERROR:.*\n)/"
- );
-
- my ($time, $status) = split(/\,/, $output[1]);
-
- ###
- #CONNECTING -- OpenVPN's initial state.
- #WAIT -- (Client only) Waiting for initial response from server.
- #AUTH -- (Client only) Authenticating with server.
- #GET_CONFIG -- (Client only) Downloading configuration options from server.
- #ASSIGN_IP -- Assigning IP address to virtual network interface.
- #ADD_ROUTES -- Adding routes to system.
- #CONNECTED -- Initialization Sequence Completed.
- #RECONNECTING -- A restart has occurred.
- #EXITING -- A graceful exit is in progress.
- ####
-
- if ($status eq "CONNECTING") {
- return "DISCONNECTED";
- } elsif ($status eq "WAIT") {
- return "DISCONNECTED";
- } elsif ($status eq "AUTH") {
- return "DISCONNECTED";
- } elsif ($status eq "GET_CONFIG") {
- return "DISCONNECTED";
- } elsif ($status eq "ASSIGN_IP") {
- return "DISCONNECTED";
- } elsif ($status eq "ADD_ROUTES") {
- return "DISCONNECTED";
- } elsif ($status eq "RECONNECTING") {
- return "CONNECTED";
- } elsif ($status eq "EXITING") {
- return "DISCONNECTED";
- }
-
- return $status;
-}
-
# Hook to regenerate the configuration files
if ($ENV{"REMOTE_ADDR"} eq "") {
&writeserverconf();
@@ -5309,7 +5255,7 @@ END
my $port = $confighash{$key}[22];
if ($port ne "") {
- $connstatus = &openvpn_status($confighash{$key}[22]);
+ $connstatus = &OpenVPN::n2n_status($port);
}
}