-
Notifications
You must be signed in to change notification settings - Fork 2
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 #1 from kuefmz/fixing_pr_comments
Fixing comments from @JJ-Author
- Loading branch information
Showing
16 changed files
with
1,000 additions
and
698 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 |
---|---|---|
@@ -1,116 +1,132 @@ | ||
from proxy.http.proxy import HttpProxyBasePlugin | ||
from proxy.http.parser import HttpParser | ||
from proxy.common.utils import build_http_response | ||
from ontologytimemachine.utils.utils import parse_arguments | ||
from ontologytimemachine.utils.mock_responses import mock_response_403 | ||
from ontologytimemachine.proxy_wrapper import HttpRequestWrapper | ||
from ontologytimemachine.utils.proxy_logic import proxy_logic, is_ontology_request_only_ontology | ||
from ontologytimemachine.utils.proxy_logic import is_archivo_ontology_request | ||
from ontologytimemachine.utils.proxy_logic import if_intercept_host | ||
from ontologytimemachine.utils.proxy_logic import ( | ||
get_response_from_request, | ||
if_intercept_host, | ||
is_archivo_ontology_request, | ||
) | ||
from ontologytimemachine.utils.config import Config, parse_arguments | ||
from http.client import responses | ||
import proxy | ||
import sys | ||
import logging | ||
|
||
|
||
IP = '0.0.0.0' | ||
PORT = '8899' | ||
IP = "0.0.0.0" | ||
PORT = "8899" | ||
|
||
config = ({'format': 'turtle', 'precedence': 'enforcedPriority', 'patchAcceptUpstream': False}, 'originalFailoverLiveLatest', False, 'all', False, True, None, None) | ||
config = None | ||
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | ||
logging.basicConfig( | ||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OntologyTimeMachinePlugin(HttpProxyBasePlugin): | ||
def __init__(self, *args, **kwargs): | ||
logger.info('Init') | ||
logger.info("Init") | ||
super().__init__(*args, **kwargs) | ||
(self.ontoFormat, self.ontoVersion, self.restrictedAccess, | ||
self.httpsInterception, self.disableRemovingRedirects, | ||
self.forward_headers, self.timestamp, self.manifest) = config | ||
self.config = config | ||
|
||
def before_upstream_connection(self, request: HttpParser): | ||
logger.info('Before upstream connection hook') | ||
logger.info(f'Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}') | ||
def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: | ||
print(config) | ||
logger.info("Before upstream connection hook") | ||
logger.info( | ||
f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}" | ||
) | ||
wrapped_request = HttpRequestWrapper(request) | ||
|
||
if wrapped_request.is_connect_request(): | ||
logger.info(f'HTTPS interception mode: {self.httpsInterception}') | ||
logger.info(f"HTTPS interception mode: {self.config.httpsInterception}") | ||
|
||
# Only intercept if interception is enabled | ||
# Move this to the utils | ||
if if_intercept_host(self.httpsInterception): | ||
logger.info('HTTPS interception is on, forwardig the request') | ||
if if_intercept_host(self.config): | ||
logger.info("HTTPS interception is on, forwardig the request") | ||
return request | ||
else: | ||
logger.info('HTTPS interception is turned off') | ||
logger.info("HTTPS interception is blocked") | ||
return None | ||
|
||
# If only ontology mode, return None in all other cases | ||
if is_ontology_request_only_ontology(wrapped_request, self.restrictedAccess): | ||
logger.warning('Request denied: not an ontology request and only ontologies mode is enabled') | ||
self.queue_response(mock_response_403) | ||
return None | ||
|
||
if is_archivo_ontology_request(wrapped_request): | ||
logger.debug('The request is for an ontology') | ||
response = proxy_logic(wrapped_request, self.ontoFormat, self.ontoVersion, self.disableRemovingRedirects, self.timestamp, self.manifest) | ||
# # If only ontology mode, return None in all other cases | ||
logger.info(f"Config: {self.config}") | ||
response = get_response_from_request(wrapped_request, self.config) | ||
if response: | ||
self.queue_response(response) | ||
return None | ||
return request | ||
|
||
def handle_client_request(self, request: HttpParser): | ||
logger.info('Handle client request hook') | ||
logger.info(f'Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}') | ||
return request | ||
|
||
wrapped_request = HttpRequestWrapper(request) | ||
if wrapped_request.is_connect_request(): | ||
return request | ||
def do_intercept(self, _request: HttpParser) -> bool: | ||
wrapped_request = HttpRequestWrapper(_request) | ||
if self.config.httpsInterception in ["all"]: | ||
return True | ||
elif self.config.httpsInterception in ["none"]: | ||
return False | ||
elif self.config.httpsInterception in ["archivo"]: | ||
if is_archivo_ontology_request(wrapped_request): | ||
return True | ||
return False | ||
else: | ||
logger.info( | ||
f"httpsInterception: {self.config.httpsInterception} option is not allowed." | ||
) | ||
return False | ||
|
||
is_ontology_request = is_archivo_ontology_request(wrapped_request) | ||
if not is_ontology_request: | ||
logger.info('The requested IRI is not part of DBpedia Archivo') | ||
return request | ||
def handle_client_request(self, request: HttpParser) -> HttpParser: | ||
logger.info("Handle client request hook") | ||
logger.info( | ||
f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}" | ||
) | ||
|
||
response = proxy_logic(wrapped_request, self.ontoFormat, self.ontoVersion, self.disableRemovingRedirects, self.timestamp, self.manifest) | ||
self.queue_response(response) | ||
return request | ||
|
||
return None | ||
|
||
def handle_upstream_chunk(self, chunk: memoryview): | ||
return chunk | ||
|
||
def queue_response(self, response): | ||
self.client.queue( | ||
build_http_response( | ||
response.status_code, | ||
reason=bytes(responses[response.status_code], 'utf-8'), | ||
response.status_code, | ||
reason=bytes(responses[response.status_code], "utf-8"), | ||
headers={ | ||
b'Content-Type': bytes(response.headers.get('Content-Type'), 'utf-8') | ||
}, | ||
body=response.content | ||
b"Content-Type": bytes( | ||
response.headers.get("Content-Type"), "utf-8" | ||
) | ||
}, | ||
body=response.content, | ||
) | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
|
||
config = parse_arguments() | ||
|
||
sys.argv = [sys.argv[0]] | ||
|
||
# check it https interception is enabled | ||
if config[3] != 'none': | ||
if config.httpsInterception != "none": | ||
sys.argv += [ | ||
'--ca-key-file', 'ca-key.pem', | ||
'--ca-cert-file', 'ca-cert.pem', | ||
'--ca-signing-key-file', 'ca-signing-key.pem', | ||
"--ca-key-file", | ||
"ca-key.pem", | ||
"--ca-cert-file", | ||
"ca-cert.pem", | ||
"--ca-signing-key-file", | ||
"ca-signing-key.pem", | ||
] | ||
|
||
sys.argv += [ | ||
'--hostname', IP, | ||
'--port', PORT, | ||
'--plugins', __name__ + '.OntologyTimeMachinePlugin' | ||
"--hostname", | ||
IP, | ||
"--port", | ||
PORT, | ||
"--plugins", | ||
__name__ + ".OntologyTimeMachinePlugin", | ||
] | ||
|
||
logger.info("Starting OntologyTimeMachineProxy server...") | ||
proxy.main() | ||
proxy.main() |
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
Oops, something went wrong.