[2/3] general-functions.pl: Use new downloader for FetchPublicIp function.

Message ID 20250322145724.4593-2-stefan.schantl@ipfire.org
State New
Headers
Series [1/3] general-functions.pl: Add LWP-based flexible downloader function. |

Commit Message

Stefan Schantl March 22, 2025, 2:57 p.m. UTC
  This helps to drop the Net::SSLeay module and to remove a lot of legacy
code.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/cfgroot/general-functions.pl | 32 +++++++++++++----------------
 1 file changed, 14 insertions(+), 18 deletions(-)
  

Comments

Michael Tremer March 23, 2025, 12:08 p.m. UTC | #1
This is much better and cleaner code than before. Well done!

> On 22 Mar 2025, at 14:57, Stefan Schantl <stefan.schantl@ipfire.org> wrote:
> 
> This helps to drop the Net::SSLeay module and to remove a lot of legacy
> code.
> 
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
> config/cfgroot/general-functions.pl | 32 +++++++++++++----------------
> 1 file changed, 14 insertions(+), 18 deletions(-)
> 
> diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
> index cb8df69c6..a132cf315 100644
> --- a/config/cfgroot/general-functions.pl
> +++ b/config/cfgroot/general-functions.pl
> @@ -17,7 +17,6 @@ package General;
> use strict;
> use Socket;
> use IO::Socket;
> -use Net::SSLeay;
> use Net::IPv4Addr qw(:all);
> 
> # Load module to move files.
> @@ -979,23 +978,20 @@ sub findhasharraykey {
> }
> 
> sub FetchPublicIp {
> -    my %proxysettings;
> -    &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
> -    if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
> -        my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
> -        Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
> -    }
> -    my $user_agent = &MakeUserAgent();
> -    my ($out, $response) = Net::SSLeay::get_http(  'checkip4.dns.lightningwirelabs.com',
> -                    80,
> -            "/",
> -    Net::SSLeay::make_headers('User-Agent' => $user_agent )
> - );
> -    if ($response =~ m%HTTP/1\.. 200 OK%) {
> - $out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
> - return $1;
> -    }
> -    return '';
> + # URL to grab the public IP.
> + my $url = "https://checkip4.dns.lightningwirelabs.com";
> +
> + # Call downloader to fetch the public IP.
> + my $response = &downloader("URL" => $url);
> +
> + # Omit the address from the resonse message.
> + if ($response =~ /Your IP address is: (\d+.\d+.\d+.\d+)/) {
> + # Return the address.
> + return $1;
> + }
> +
> + # Unable to grab the address - Return nothing.
> + return;
> }
> 
> #
> -- 
> 2.47.2
> 
>
  

Patch

diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
index cb8df69c6..a132cf315 100644
--- a/config/cfgroot/general-functions.pl
+++ b/config/cfgroot/general-functions.pl
@@ -17,7 +17,6 @@  package General;
 use strict;
 use Socket;
 use IO::Socket;
-use Net::SSLeay;
 use Net::IPv4Addr qw(:all);
 
 # Load module to move files.
@@ -979,23 +978,20 @@  sub findhasharraykey {
 }
 
 sub FetchPublicIp {
-    my %proxysettings;
-    &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
-    if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
-        my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
-        Net::SSLeay::set_proxy($peer,$peerport,$proxysettings{'UPSTREAM_USER'},$proxysettings{'UPSTREAM_PASSWORD'} );
-    }
-    my $user_agent = &MakeUserAgent();
-    my ($out, $response) = Net::SSLeay::get_http(  'checkip4.dns.lightningwirelabs.com',
-                				    80,
-        					    "/",
-						    Net::SSLeay::make_headers('User-Agent' => $user_agent )
-						);
-    if ($response =~ m%HTTP/1\.. 200 OK%) {
-	$out =~ /Your IP address is: (\d+.\d+.\d+.\d+)/;
-	return $1;
-    }
-    return '';
+	# URL to grab the public IP.
+	my $url = "https://checkip4.dns.lightningwirelabs.com";
+
+	# Call downloader to fetch the public IP.
+	my $response = &downloader("URL" => $url);
+
+	# Omit the address from the resonse message.
+	if ($response =~ /Your IP address is: (\d+.\d+.\d+.\d+)/) {
+		# Return the address.
+		return $1;
+	}
+
+	# Unable to grab the address - Return nothing.
+	return;
 }
 
 #