Skip to content

Commit

Permalink
Merge pull request #24 from kuefmz/main
Browse files Browse the repository at this point in the history
Improve proxy
  • Loading branch information
JJ-Author authored Jun 28, 2024
2 parents c1af7ea + f73fd4b commit b881d73
Show file tree
Hide file tree
Showing 9 changed files with 2,376 additions and 111 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ jobs:
python -m pip install --upgrade pip
- name: Install requirements with poetry
run: poetry install
- name: Write key file
env:
CA_CERT: ${{ secrets.CA_CERT }}
CA_KEY: ${{ secrets.CA_KEY }}
CA_SIGNING_KEY: ${{ secrets.CA_SIGNING_KEY }}
run: |
echo "$CA_CERT" > ca-cert.pem
echo "$CA_KEY" > ca-key.pem
echo "$CA_SIGNING_KEY" > ca-signing-key.pem
- name: Test with pytest
run: |
poetry run pytest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
*.pem

# C extensions
*.so
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ RUN apt-get update && apt-get install -y \
COPY pyproject.toml .
COPY ontologytimemachine /ontologytimemachine
COPY README.md /README.md
COPY ca-cert.pem /ca-cert.pem
COPY ca-key.pem /ca-key.pem
COPY ca-signing-key.pem /ca-signing-key.pem

RUN python3 -m pip install --upgrade pip

Expand Down
161 changes: 159 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,163 @@
# ontology-time-machine


Docker command:
### Before building the docker file:

```
git clone https://github.com/abhinavsingh/proxy.py.git
cd proxy.py
make ca-certificates
cp ca-cert.pem ~/ontology-time-machine/ca-cert.pem
cp ca-key.pem ~/ontology-time-machine/ca-key.pem
cp ca-signing-key.pem ~/ontology-time-machine/ca-signing-key.pem
```


### Docker command:
- docker build -t ontology_time_machine:0.1 .
- docker run -d -e PORT=8899 -p 8182:8899 ontology_time_machine:0.1
- docker run -d -e PORT=8899 -p 8182:8899 ontology_time_machine:0.1

### Deployed at:
95.217.207.179:8182

### Curl tests:
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem http://www.google.com
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://ontologi.es/days#
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://linked-web-apis.fit.cvut.cz/ns/core
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://www.w3id.org/simulation/ontology/
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://www.w3.org/ns/ldt#
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://raw.githubusercontent.com/br0ast/simulationontology/main/Ontology/simulationontology.owl
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://bblfish.net/work/atom-owl/2006-06-06/
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://purl.org/makolab/caont/


### Not working:
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://vocab.eccenca.com/auth/




from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.parser import HttpParser, httpParserTypes
from proxy.common.utils import build_http_response
from proxy.http.methods import HttpMethods
from ontologytimemachine.utils.utils import proxy_logic_http, proxy_logic_https
from ontologytimemachine.utils.utils import check_if_archivo_ontology_requested
from ontologytimemachine.utils.utils import get_headers_and_expected_type
from requests.exceptions import SSLError, Timeout, ConnectionError, RequestException
from http.client import responses
import proxy
import sys
import requests
import logging


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class OntologyTimeMachinePlugin(HttpProxyBasePlugin):
#def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)


def before_upstream_connection(self, request: HttpParser):
logger.debug('Before upstream')
print(request.method)
scheme = 'https' if request.method == b'CONNECT' else 'http'
if scheme == 'https':
logger.debug('The request is HTTPS, forward as it is')
return request

ontology_request = check_if_archivo_ontology_requested(request)
if ontology_request:
logger.debug('The request is for an ontology')
try:
ontology_url = str(request._url)
headers, _ = get_headers_and_expected_type(request)
response = requests.get(ontology_url, headers=headers)
if response.status_code == 502:
logger.error('Received 502 Bad Gateway error')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
else:
logger.debug('The request is correct')
return request
except (SSLError, Timeout, ConnectionError, RequestException) as e:
logger.error(f'Network-related exception occurred {e}')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
return request


def handle_client_request(self, request: HttpParser):
logger.debug('HTTP call')
logger.debug(request._url)

ontology_request = check_if_archivo_ontology_requested(request)
if not ontology_request:
logger.info('No ontology is asked, forward original request')
return request
response = proxy_logic_http(request)
self.queue_response(response)

return None


def handle_upstream_chunk(self, chunk: memoryview):
logger.info('HTTPS call')

try:
# Parse the HTTP response to handle different cases
parser = HttpParser(httpParserTypes.RESPONSE_PARSER)
parser.parse(memoryview(chunk))
code = int(parser.code.decode('utf-8'))
if code >= 100 and code < 200:
return chunk
elif code >= 201 and code <= 204:
return chunk
elif code == 451:
return chunk
else:
response = proxy_logic_https(parser)
logger.debug('Queue response')
self.queue_response(response)
return None
except UnicodeDecodeError:
logger.warning('Received non-text chunk, cannot decode')
except Exception as e:
logger.error(f'Exception occurred while handling upstream chunk: {e}')
return chunk

def queue_response(self, response):
self.client.queue(
build_http_response(
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
)
)


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', '0.0.0.0',
'--port', '8899',
'--plugins', __name__ + '.OntologyTimeMachinePlugin'
]
logger.info("Starting OntologyTimeMachineProxy server...")
proxy.main()
Loading

0 comments on commit b881d73

Please sign in to comment.