Skip to content

Commit de9e91d

Browse files
authored
fix(ns-plug): inventory, better public IP retrieval (#1344)
The ifconfig.co site sometimes returns an HTML page and this info is wrongly sent to my.nethesis.it New behavior: - send an HTTP request to ifconfig.co and parse the response, if the remote server returns a valid IP address, return it - otherwise, try to obtain the same info by executing a DNS query to OpenDNS - return an empty string if none of the above responses are valid IP addresses
1 parent a85f715 commit de9e91d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/ns-plug/files/inventory

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import json
1010
import subprocess
11+
import ipaddress
1112
from euci import EUci
1213
from struct import pack
1314
from nethsec import utils, inventory
@@ -71,6 +72,22 @@ def _get_gateway(interface):
7172
except:
7273
return ''
7374

75+
def _get_public_ip():
76+
# Try ifconfig.co: sometimes the site returns an HTML error page, ignore it
77+
address = _run("curl --fail --max-time 5 --retry 2 https://ifconfig.co")
78+
try:
79+
ipaddress.ip_address(address)
80+
return address
81+
except:
82+
# Fallback to DNS query
83+
address = _run("dig +short myip.opendns.com @resolver1.opendns.com")
84+
85+
try:
86+
ipaddress.ip_address(address)
87+
return address
88+
except:
89+
return ''
90+
7491
release = _run('grep VERSION= /etc/os-release | cut -d= -f 2 | tr -d \'"\'')
7592
cpu_info = _run_json('lscpu -J')['lscpu']
7693
binfo = _run_json("cat /etc/board.json")
@@ -152,7 +169,7 @@ data = {
152169
}
153170
},
154171
"rpms": { "nethserver-firewall-base-ui": _run("opkg status ns-ui | grep Version | awk '{print $2}'") },
155-
"public_ip": _run("curl https://ifconfig.co"),
172+
"public_ip": _get_public_ip(),
156173
"features": features
157174
}
158175

0 commit comments

Comments
 (0)