Skip to content

Commit

Permalink
trying to fix the https requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenifer Tabita Ciuciu-Kiss committed Jun 19, 2024
1 parent 32513c9 commit 92f0a9f
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.proxy import HttpProxyBasePlugin, HttpProxyPlugin
from proxy.http.parser import HttpParser
from proxy.common.utils import build_http_response
from proxy.http.codes import httpStatusCodes
import requests
import sys
import proxy
Expand All @@ -24,6 +25,16 @@ def __init__(
super().__init__(*args, **kwargs)

def before_upstream_connection(self, request: HttpParser):
if request.method == b'CONNECT':
# Handle HTTPS connection establishment
host, port = request.host, request.port
self.client.queue(
build_http_response(
httpStatusCodes.SWITCHING_PROTOCOLS, reason=b'Connection established', headers={
b'Proxy-Agent': b'Python Proxy'
}
)
)
return None


Expand All @@ -38,12 +49,10 @@ def handle_client_request(self, request: HttpParser):
)
)
return None

# Process other requests using proxy logic
self.proxy_logic(request)
return None


def proxy_logic(self, request: HttpParser):
self.failover_mode(request)
self.time_based_mode(request)
Expand All @@ -52,17 +61,38 @@ def proxy_logic(self, request: HttpParser):

def failover_mode(self, request):
logging.info('Failover mode')
ontology = str(request._url)
logging.info(f'Method: {request.method}')
logging.info(f'URL: {request._url}')
scheme = 'https' if request.method == b'CONNECT' else 'http'
logging.info(f'Scheme: {scheme}')
if scheme == 'https':
host = request.host.decode()
logging.info(f'Host: {host}')
port = request.port if request.port else (443 if scheme == 'https' else 80)
logging.info(f'Port: {port}')
path = request.path.decode() if request.path else '/'
#logging.info(f'{request.url.path}')
logging.info(f'Path: {path}')
full_url = f'{scheme}://{host}:{port}{path}'

#logging.info(response.text)
logging.info(full_url)
ontology = full_url
else:
ontology = str(request._url)

logging.info(f'Ontology: {ontology}')

try:
response = requests.get(ontology)
#logging.info(f'{response.text}')
logging.info('Response received')
logging.info(f'Response status code: {response.status_code}')
content_type = response.headers.get('Content-Type', '')
logging.info(f'Content type: {content_type}')

if response.status_code == 200 and content_type in ['text/turtle']:
logging.info(f'Content type: {content_type}')
logging.info(f'Ontology: {ontology}')
if response.status_code == 200 and (content_type in ['text/turtle', 'text/html; charset=utf-8'] or 'text/html' in content_type):
logging.info(f'Send answer')
self.client.queue(
build_http_response(
200, reason=b'OK', headers={
Expand Down Expand Up @@ -130,9 +160,14 @@ def on_client_connection_close(self):


if __name__ == '__main__':
sys.argv += [
'--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',
'--plugins', __name__ + '.OntologyTimeMachinePlugin'
]
proxy.main()

0 comments on commit 92f0a9f

Please sign in to comment.