Skip to content

Commit cf35f57

Browse files
committed
[enhancement] Logic for updating stale WHOIS records #1058
Added required checks for ensuring WHOIS records older than a certain Threshold (configurable) will be updated along with the coordinates. Formatted existing code to be consistent with current coding standards. Closes #1058 Signed-off-by: DragnEmperor <[email protected]>
1 parent 141a1b1 commit cf35f57

File tree

17 files changed

+520
-239
lines changed

17 files changed

+520
-239
lines changed

docs/user/estimated-location.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Estimated Location
1616
Overview
1717
--------
1818

19-
The Estimated Location feature automatically creates or updates a device’s
20-
location based on latitude and longitude information retrieved from the
21-
WHOIS Lookup feature.
19+
This feature automatically creates or updates a device’s location based on
20+
latitude and longitude information retrieved from the WHOIS Lookup
21+
feature.
2222

2323
Trigger Conditions
2424
------------------
2525

26-
Estimated Location is triggered when:
26+
This feature is triggered when:
2727

2828
- A **fresh WHOIS lookup** is performed for a device.
2929
- Or when a WHOIS record already exists for the device’s IP **and**:
@@ -78,3 +78,10 @@ In REST API, the field will be visible in the :ref:`Device Location
7878
<location_geojson_estimated>` if the feature is **enabled**. The field can
7979
also be used for filtering in the location list (including geojson)
8080
endpoints and in the :ref:`Device List <device_list_estimated_filters>`.
81+
82+
Managing Older Estimated Locations
83+
----------------------------------
84+
85+
Whenever location related fields in WHOIS records are updated as per
86+
:ref:`Managing WHOIS Older Records <whois_older_records>`; the location
87+
will also be updated automatically.

docs/user/settings.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,16 @@ Allows enabling the optional :doc:`Estimated Location feature
812812
.. image:: https://raw.githubusercontent.com/openwisp/openwisp-controller/docs/docs/1.3/estimated-location-setting.png
813813
:target: https://raw.githubusercontent.com/openwisp/openwisp-controller/docs/docs/1.3/estimated-location-setting.png
814814
:alt: Estimated Location setting
815+
816+
.. _openwisp_controller_whois_refresh_threshold_days:
817+
818+
``OPENWISP_CONTROLLER_WHOIS_REFRESH_THRESHOLD_DAYS``
819+
----------------------------------------------------
820+
821+
============ =======
822+
**type**: ``int``
823+
**default**: ``14``
824+
============ =======
825+
826+
Specifies the number of days after which the WHOIS information for a
827+
device is considered stale and eligible for refresh.

docs/user/whois.rst

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ WHOIS Lookup
1515
Overview
1616
--------
1717

18-
The WHOIS Lookup feature displays information about the public IP address
19-
used by devices to communicate with OpenWISP (via the ``last_ip`` field).
20-
It helps identify the geographic location and ISP associated with the IP
21-
address, which can be useful for troubleshooting network issues.
18+
This feature displays information about the public IP address used by
19+
devices to communicate with OpenWISP (via the ``last_ip`` field). It helps
20+
identify the geographic location and ISP associated with the IP address,
21+
which can be useful for troubleshooting network issues.
2222

2323
The retrieved information pertains to the Autonomous System (ASN)
2424
associated with the device's public IP address and includes:
@@ -30,12 +30,15 @@ associated with the device's public IP address and includes:
3030
- Timezone of the ASN's registered location
3131
- Coordinates (Latitude and Longitude)
3232

33+
.. _whois_trigger_conditions:
34+
3335
Trigger Conditions
3436
------------------
3537

3638
A WHOIS lookup is triggered automatically when:
3739

3840
- A new device is registered.
41+
- An existing device's last IP address is changed.
3942
- A device fetches its checksum.
4043

4144
However, the lookup will only run if **all** the following conditions are
@@ -114,3 +117,15 @@ retrieved details can be viewed in the following locations:
114117
- **Device REST API**: See WHOIS details in the :ref:`Device List
115118
<device_list_whois>` and :ref:`Device Detail <device_detail_whois>`
116119
responses.
120+
121+
.. _whois_older_records:
122+
123+
Managing Older WHOIS Records
124+
----------------------------
125+
126+
If a record is older than :ref:`Threshold
127+
<openwisp_controller_whois_refresh_threshold_days>`, it will be refreshed
128+
automatically.
129+
130+
The update mechanism will be triggered whenever a device is registered or
131+
its last IP changes or fetches its checksum.

openwisp_controller/config/base/device.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,6 @@ def _check_last_ip(self, creating=False):
519519
return
520520
if creating or self.last_ip != self._initial_last_ip:
521521
self.whois_service.process_ip_data_and_location()
522+
else:
523+
self.whois_service.update_whois_info()
522524
self._initial_last_ip = self.last_ip

openwisp_controller/config/controller/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ def get(self, request, pk):
153153
# updates cache if ip addresses changed
154154
if updated:
155155
self.update_device_cache(device)
156+
# check if WHOIS Info of device requires update
157+
else:
158+
device.whois_service.update_whois_info()
156159
checksum_requested.send(
157160
sender=device.__class__, instance=device, request=request
158161
)

openwisp_controller/config/management/commands/clear_last_ip.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55

66
class Command(BaseCommand):
7-
help = "Clear the last IP address, if set, of active devices of all organizations."
7+
help = (
8+
"Clears the last IP address (if set) for every active device"
9+
" across all organizations."
10+
)
811

912
def add_arguments(self, parser):
1013
parser.add_argument(
@@ -29,11 +32,11 @@ def handle(self, *args, **options):
2932
"Are you sure you want to do this?\n\n"
3033
"Type 'yes' to continue, or 'no' to cancel: "
3134
)
32-
if input("".join(message)) != "yes":
35+
if input("".join(message)).lower() != "yes":
3336
raise CommandError("Operation cancelled by user.")
3437

3538
devices = Device.objects.filter(_is_deactivated=False).only("last_ip")
36-
# Filter devices that have no WHOIS information for their last IP
39+
# Filter out devices that have WHOIS information for their last IP
3740
devices = devices.exclude(last_ip=None).exclude(
3841
last_ip__in=Subquery(
3942
WHOISInfo.objects.filter(ip_address=OuterRef("last_ip")).values(

openwisp_controller/config/migrations/0061_config_checksum_db.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Generated by Django for issue #1113 optimization
22

33
from django.db import migrations, models
4-
from swapper import load_model
54

65

76
def populate_checksum_db(apps, schema_editor):
@@ -13,7 +12,7 @@ def populate_checksum_db(apps, schema_editor):
1312
hence we use Config.objects.bulk_update() instead of
1413
Config.update_status_if_checksum_changed().
1514
"""
16-
Config = load_model("config", "Config")
15+
Config = apps.get_model("config", "Config")
1716
chunk_size = 100
1817
updated_configs = []
1918
qs = (

openwisp_controller/config/migrations/0061_whoisinfo.py renamed to openwisp_controller/config/migrations/0062_whoisinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Migration(migrations.Migration):
1212
dependencies = [
13-
("config", "0060_cleanup_api_task_notification_types"),
13+
("config", "0061_config_checksum_db"),
1414
]
1515

1616
operations = [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Migration(migrations.Migration):
1010

1111
dependencies = [
12-
("config", "0061_whoisinfo"),
12+
("config", "0062_whoisinfo"),
1313
]
1414

1515
operations = [

openwisp_controller/config/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def get_setting(option, default):
6868
"API_TASK_RETRY_OPTIONS",
6969
dict(max_retries=5, retry_backoff=True, retry_backoff_max=600, retry_jitter=True),
7070
)
71+
WHOIS_REFRESH_THRESHOLD_DAYS = get_setting("WHOIS_REFRESH_THRESHOLD_DAYS", 14)
7172
WHOIS_GEOIP_ACCOUNT = get_setting("WHOIS_GEOIP_ACCOUNT", None)
7273
WHOIS_GEOIP_KEY = get_setting("WHOIS_GEOIP_KEY", None)
7374
WHOIS_ENABLED = get_setting("WHOIS_ENABLED", False)

0 commit comments

Comments
 (0)