From 276ce11f80858ca4942eecd6832896ac70a46114 Mon Sep 17 00:00:00 2001 From: "Arnaud G. GIBERT" Date: Thu, 18 Sep 2025 00:14:05 +0200 Subject: [PATCH] - Support and report resolver failure by using default IP addresses, - Simple code refactoring. --- ReadMe.txt | 3 +- ReleaseNotes.txt | 9 +++ {SPEC => SPECS}/myip.spec | 24 +++++-- bin/myip | 130 ++++++++++++++++++++++++++++++++------ var/www/cgi-bin/myip.cgi | 28 +++++++- 5 files changed, 166 insertions(+), 28 deletions(-) rename {SPEC => SPECS}/myip.spec (54%) diff --git a/ReadMe.txt b/ReadMe.txt index 6ede2a1..96fa770 100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -2,7 +2,8 @@ Welcome to Rx3 MyIP! -This is a small tool to return the extern public IP address and optionnally the internal private IP, +This is a small tool to return the extern public IP address and optionnally the internal private IP. +Support missing name resolver by using default IP addresses. The internal IP service is also included. diff --git a/ReleaseNotes.txt b/ReleaseNotes.txt index 1d05e9b..4b40b7c 100644 --- a/ReleaseNotes.txt +++ b/ReleaseNotes.txt @@ -1,3 +1,12 @@ +------------------------------------------------------------------------------------------------------------------------------------ +MyIP V 1.2.0 - A. GIBERT - 2025/09/18 +------------------------------------------------------------------------------------------------------------------------------------ + +- Support and report resolver failure by using default IP addresses, +- Simple code refactoring. + + + ------------------------------------------------------------------------------------------------------------------------------------ MyIP V 1.1.0 - A. GIBERT - 2025/09/09 ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/SPEC/myip.spec b/SPECS/myip.spec similarity index 54% rename from SPEC/myip.spec rename to SPECS/myip.spec index 3a0462b..93397a4 100644 --- a/SPEC/myip.spec +++ b/SPECS/myip.spec @@ -1,17 +1,22 @@ %define name myip -%define version 1.1.0 +%define version 1.2.0 %define release %mkrel 1rx3 %global debug_package %{nil} +%global _webdir %{_localstatedir}/www +%global _webcgi %{_webdir}/cgi-bin + + + Name: %{name} Version: %{version} Release: %{release} -Summary: Simple script to get your public IP +Summary: Simple script to get your public and private IP addresses License: GPL URL: https://git.rx3.org/gitea/rx3/myip -Source0: myip +Source0: %{name}-%{version}.tar.bz2 Distribution: Rx3 Free Software Vendor: Rx3 @@ -22,12 +27,12 @@ Requires: bind-utils %description -Simple script to get your public IP. +Simple script to get your public and private IP. %prep -cp %{SOURCE0} . +%setup -q -n %{name} @@ -36,7 +41,8 @@ cp %{SOURCE0} . %install -install -D -pm 755 %{name} $RPM_BUILD_ROOT%{_bindir}/%{name} +install -D -pm 755 bin/%{name} %{buildroot}%{_bindir}/%{name} +install -D -pm 755 var/www/cgi-bin/%{name}.cgi %{buildroot}%{_webcgi}/%{name}.cgi @@ -45,11 +51,17 @@ install -D -pm 755 %{name} $RPM_BUILD_ROOT%{_bindir}/%{name} %files +%doc ReadMe.txt ReleaseNotes.txt +%license GNU_GPL-3.0.txt GNU_LGPL-3.0.txt GNU_FDL-1.3.txt %{_bindir}/%{name} +%{_webcgi}/%{name}.cgi %changelog +* Thu Sep 18 2025 Arnaud G. GIBERT - 1.2.0-1rx3.mga9 +- Update to 1.2.0 + * Tue Sep 9 2025 Arnaud G. GIBERT - 1.1.0-1rx3.mga9 - Update to 1.1.0 diff --git a/bin/myip b/bin/myip index 8d18fd7..200e931 100755 --- a/bin/myip +++ b/bin/myip @@ -1,4 +1,79 @@ #!/bin/bash +#----------------------------------------------------------------------------------------------------------------------- +# +# MyIP +# Copyright (C) 2025 Arnaud G. GIBERT +# mailto:arnaud@rx3.net +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +#----------------------------------------------------------------------------------------------------------------------- + +WEB_SERVER_NAME="www.rx3" + WEB_SERVER_IP="10.0.0.65" + +RESOLVER_NAME="resolver1.opendns.com" + RESOLVER_IP="208.67.222.222" + +declare -g RESOLV_STATUS="OK" + + + + + +#----------------------------------------------------------------------------------------------------------------------- +# ip_filter +#----------------------------------------------------------------------------------------------------------------------- + +ip_filter() +{ + grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' +} + + + + + +#----------------------------------------------------------------------------------------------------------------------- +# resolv +#----------------------------------------------------------------------------------------------------------------------- + +resolv() +{ + local host_name="$1" + local host_ip="$2" + + + resolv_ip="$( dig +short "${host_name}" | ip_filter)" + + if [[ "${resolv_ip}" == "" ]] + then + RESOLV_STATUS="KO" + resolv_ip="${host_ip=}" + else + RESOLV_STATUS="OK" + fi +} + + + + + +#----------------------------------------------------------------------------------------------------------------------- +# main +#----------------------------------------------------------------------------------------------------------------------- if [[ "$1" == "-h" ]] then @@ -9,44 +84,61 @@ then else if [[ "$1" == "-a" ]] then - INT_FLAG="TRUE" - EXT_FLAG="TRUE" - VERBOSE_FLAG="TRUE" + int_flag="TRUE" + ext_flag="TRUE" + verbose_flag="TRUE" else - VERBOSE_FLAG="FALSE" + verbose_flag="FALSE" if [[ "$1" == "-i" ]] then - INT_FLAG="TRUE" - EXT_FLAG="FALSE" + int_flag="TRUE" + ext_flag="FALSE" else - INT_FLAG="FALSE" - EXT_FLAG="TRUE" + int_flag="FALSE" + ext_flag="TRUE" fi fi fi -if [[ ${VERBOSE_FLAG} == "TRUE" ]] + +if [[ ${verbose_flag} == "TRUE" ]] then - INT_LABEL="internal_ip: " - EXT_LABEL="external_ip: " + int_label="Internal IP: " + ext_label="External IP: " else - INT_LABEL="" - EXT_LABEL="" + int_label="" + ext_label="" fi -if [[ ${INT_FLAG} == "TRUE" ]] +if [[ ${int_flag} == "TRUE" ]] then - INT_IP="$( curl -s http://www.rx3/cgi-bin/myip.cgi | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$')" + resolv "${WEB_SERVER_NAME}" "${WEB_SERVER_IP}" + web_server_ip="${resolv_ip}" + + int_ip="$( curl --resolve ${WEB_SERVER_NAME}:80:${web_server_ip} -s http://www.rx3/cgi-bin/myip.cgi | ip_filter)" - echo "${INT_LABEL}${INT_IP}" + if [[ ( "${int_label}" != "") && ( "${RESOLV_STATUS}" != "OK") ]] + then + echo "Warning: resolver failed for [${WEB_SERVER_NAME}], using default address: [${WEB_SERVER_IP}]" + fi + + echo "${int_label}${int_ip}" fi -if [[ ${EXT_FLAG} == "TRUE" ]] +if [[ ${ext_flag} == "TRUE" ]] then - EXT_IP="$( dig +short myip.opendns.com @resolver1.opendns.com | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$')" + resolv "${RESOLVER_NAME}" "${RESOLVER_IP}" + resolver_ip="${resolv_ip}" + + ext_ip="$( dig +short myip.opendns.com @${resolver_ip} | ip_filter)" - echo "${EXT_LABEL}${EXT_IP}" + if [[ ( "${ext_label}" != "") && ( "${RESOLV_STATUS}" != "OK") ]] + then + echo "Warning: resolver failed for [${RESOLVER_NAME}], using default address: [${RESOLVER_IP}]" + fi + + echo "${ext_label}${ext_ip}" fi diff --git a/var/www/cgi-bin/myip.cgi b/var/www/cgi-bin/myip.cgi index ac3d336..b4caf8f 100755 --- a/var/www/cgi-bin/myip.cgi +++ b/var/www/cgi-bin/myip.cgi @@ -1,5 +1,29 @@ -#!/bin/sh +#!/bin/bash +#----------------------------------------------------------------------------------------------------------------------- +# +# MyIP.CGI +# Copyright (C) 2025 Arnaud G. GIBERT +# mailto:arnaud@rx3.net +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +#----------------------------------------------------------------------------------------------------------------------- + + + echo "Content-type: text/plain" echo "" -echo "$REMOTE_ADDR" +echo "${REMOTE_ADDR}"