✨ New util API command: address
Allows to test a provided or the client's address whether it is a LAN address, and if so returns the subnet address in CIDR notation.
This commit is contained in:
1 parent
c44e90302d
commit
89f3edcb27
4 files changed
+206
-59
No files matched your search
@@ -398,6 +398,7 @@ def utilTest():
|
||||
"url": ["url"],
|
||||
"server": ["host", "port"],
|
||||
"resolution": ["name"],
|
||||
"address": [],
|
||||
}
|
||||
|
||||
command, data, response = get_json_command_from_request(request, valid_commands)
|
||||
@@ -412,6 +413,8 @@ def utilTest():
|
||||
return _test_server(data)
|
||||
elif command == "resolution":
|
||||
return _test_resolution(data)
|
||||
elif command == "address":
|
||||
return _test_address(data)
|
||||
|
||||
|
||||
def _test_path(data):
|
||||
@@ -743,3 +746,34 @@ def _test_resolution(data):
|
||||
result = {"name": name, "result": resolvable}
|
||||
|
||||
return jsonify(**result)
|
||||
|
||||
|
||||
def _test_address(data):
|
||||
import netaddr
|
||||
|
||||
from octoprint.util.net import get_lan_ranges, sanitize_address
|
||||
|
||||
remote_addr = data.get("address")
|
||||
if not remote_addr:
|
||||
remote_addr = get_remote_address(request)
|
||||
|
||||
remote_addr = sanitize_address(remote_addr)
|
||||
ip = netaddr.IPAddress(remote_addr)
|
||||
|
||||
lan_subnets = get_lan_ranges()
|
||||
|
||||
detected_subnet = None
|
||||
for subnet in lan_subnets:
|
||||
if ip in subnet:
|
||||
detected_subnet = subnet
|
||||
break
|
||||
|
||||
result = {
|
||||
"is_lan_address": detected_subnet is not None,
|
||||
"address": remote_addr,
|
||||
}
|
||||
|
||||
if detected_subnet is not None:
|
||||
result["subnet"] = str(detected_subnet)
|
||||
|
||||
return jsonify(**result)
|
||||
Reference in new issue
Block a user