-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from nolancarougepro/lan_access_control
Lan access control
- Loading branch information
Showing
7 changed files
with
224 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"LAN": [ | ||
"10.0.0.0/8", | ||
"172.16.0.0/12", | ||
"192.168.0.0/16", | ||
"127.0.0.0/8", | ||
"::1", | ||
"fc00::/7" | ||
], | ||
"MULTICAST": [ | ||
"224.0.0.0/4", | ||
"ff00::/8" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"LAN": [ | ||
"10.0.0.0/8", | ||
"172.16.0.0/12", | ||
"192.168.0.0/16", | ||
"127.0.0.0/8", | ||
"::1", | ||
"fc00::/7" | ||
], | ||
"MULTICAST": [ | ||
"224.0.0.0/4", | ||
"ff00::/8" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
import ipaddress | ||
import os | ||
|
||
class NetworkAliases: | ||
ALIASES = {} | ||
|
||
@staticmethod | ||
def load_aliases(): | ||
# Define the path to the network_aliases.json file | ||
script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
filename = os.path.join(script_dir, 'network_aliases.json') | ||
|
||
# Check if the file exists before attempting to load it | ||
if not os.path.exists(filename): | ||
raise FileNotFoundError(f"The file '{filename}' does not exist.") | ||
|
||
# Load the JSON file | ||
with open(filename, 'r') as f: | ||
NetworkAliases.ALIASES = json.load(f) | ||
print(f"Loaded network aliases from {filename}") # Confirmation message | ||
|
||
@staticmethod | ||
def get_alias(ip): | ||
try: | ||
ip_obj = ipaddress.ip_address(ip) | ||
for alias, networks in NetworkAliases.ALIASES.items(): | ||
for network in networks: | ||
net_obj = ipaddress.ip_network(network) | ||
if ip_obj in net_obj: | ||
return alias | ||
except ValueError: | ||
pass | ||
return None | ||
|
||
@staticmethod | ||
def get_networks_for_alias(alias): | ||
return NetworkAliases.ALIASES.get(alias, []) | ||
|
||
@staticmethod | ||
def get_alias_all(): | ||
# Return a list of all alias names | ||
return list(NetworkAliases.ALIASES.keys()) | ||
|
||
# Load aliases at startup | ||
try: | ||
NetworkAliases.load_aliases() | ||
except FileNotFoundError as e: | ||
print(e) |