-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related: #505
- Loading branch information
Showing
7 changed files
with
136 additions
and
6 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
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,84 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
from datetime import date | ||
from typing import Any, Dict, Optional | ||
|
||
import requests | ||
|
||
from har2tree import CrawledTree | ||
|
||
from ..default import ConfigError, get_homedir | ||
from ..helpers import get_cache_directory | ||
|
||
|
||
class URLhaus(): | ||
|
||
def __init__(self, config: Dict[str, Any]): | ||
if not config.get('enabled'): | ||
self.available = False | ||
return | ||
|
||
self.available = True | ||
self.allow_auto_trigger = False | ||
self.url = config.get('url') | ||
if config.get('allow_auto_trigger'): | ||
self.allow_auto_trigger = True | ||
|
||
self.storage_dir_uh = get_homedir() / 'urlhaus' | ||
self.storage_dir_uh.mkdir(parents=True, exist_ok=True) | ||
|
||
def get_url_lookup(self, url: str) -> Optional[Dict[str, Any]]: | ||
url_storage_dir = get_cache_directory(self.storage_dir_uh, url, 'url') | ||
if not url_storage_dir.exists(): | ||
return None | ||
cached_entries = sorted(url_storage_dir.glob('*'), reverse=True) | ||
if not cached_entries: | ||
return None | ||
|
||
with cached_entries[0].open() as f: | ||
return json.load(f) | ||
|
||
def __url_result(self, url: str) -> Dict: | ||
data = {'url': url} | ||
response = requests.post(f'{self.url}/url/', data) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
def capture_default_trigger(self, crawled_tree: CrawledTree, /, *, auto_trigger: bool=False) -> Dict: | ||
'''Run the module on all the nodes up to the final redirect''' | ||
if not self.available: | ||
return {'error': 'Module not available'} | ||
if auto_trigger and not self.allow_auto_trigger: | ||
return {'error': 'Auto trigger not allowed on module'} | ||
|
||
# Check URLs up to the redirect | ||
if crawled_tree.redirects: | ||
for redirect in crawled_tree.redirects: | ||
self.url_lookup(redirect) | ||
else: | ||
self.url_lookup(crawled_tree.root_hartree.har.root_url) | ||
|
||
return {'success': 'Module triggered'} | ||
|
||
def url_lookup(self, url: str) -> None: | ||
'''Lookup an URL on URL haus | ||
Note: It will trigger a request to URL haus every time *until* there is a hit (it's cheap), then once a day. | ||
''' | ||
if not self.available: | ||
raise ConfigError('URL haus not available, probably not enabled.') | ||
|
||
url_storage_dir = get_cache_directory(self.storage_dir_uh, url, 'url') | ||
url_storage_dir.mkdir(parents=True, exist_ok=True) | ||
uh_file = url_storage_dir / date.today().isoformat() | ||
|
||
if uh_file.exists(): | ||
return | ||
|
||
url_information = self.__url_result(url) | ||
if not url_information: | ||
url_storage_dir.rmdir() | ||
return | ||
|
||
with uh_file.open('w') as _f: | ||
json.dump(url_information, _f) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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