-
Notifications
You must be signed in to change notification settings - Fork 181
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 #1112 from stratosphereips/alya/use-lru-cache
Optimize MAC OUIs and malicious domains lookups
- Loading branch information
Showing
5 changed files
with
143 additions
and
62 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
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,47 @@ | ||
from collections import defaultdict | ||
from typing import ( | ||
Dict, | ||
Tuple, | ||
Optional, | ||
) | ||
|
||
|
||
class TrieNode: | ||
def __init__(self): | ||
self.children = defaultdict(TrieNode) | ||
self.is_end_of_word = False | ||
self.domain_info = ( | ||
None # store associated domain information if needed | ||
) | ||
|
||
|
||
class Trie: | ||
def __init__(self): | ||
self.root = TrieNode() | ||
|
||
def insert(self, domain: str, domain_info: str): | ||
"""Insert a domain into the trie (using domain parts not chars).""" | ||
node = self.root | ||
parts = domain.split(".")[::-1] # reverse to handle subdomains | ||
for part in parts: | ||
node = node.children[part] | ||
node.is_end_of_word = True | ||
node.domain_info = domain_info | ||
|
||
def search(self, domain: str) -> Tuple[bool, Optional[Dict[str, str]]]: | ||
""" | ||
Check if a domain or its subdomain exists in the trie | ||
(using domain parts instead of characters). | ||
Returns a tuple (found, domain_info). | ||
""" | ||
node = self.root | ||
# reverse domain to handle subdomains | ||
parts = domain.split(".")[::-1] | ||
for part in parts: | ||
if part not in node.children: | ||
return False, None | ||
|
||
node = node.children[part] | ||
if node.is_end_of_word: | ||
return True, node.domain_info | ||
return False, None |
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