Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http to https #6

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.mypy_cache
/.ruff_cache
/.cache
/config.yaml
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ PROXY_RUN = $(DOCKER_COMPOSE) run --rm proxy
# Container management
# --------------------

.PHONY: config
config: .env config.yaml

# Create .env file to set the UID/GID for the docker containers to run as to the current user
.env:
echo "DOCKER_LOCAL_USER=$(shell id -u):$(shell id -g)" >> .env

# Create config file from config_dist_dev.yaml if it does not exist yet
config.yaml:
cp config_dist_dev.yaml config.yaml

.PHONY: docker-up
docker-up: docker-build
$(DOCKER_COMPOSE) up

.PHONY: docker-down
docker-down: .env
docker-down: config
derhuerst marked this conversation as resolved.
Show resolved Hide resolved
$(DOCKER_COMPOSE) down --remove-orphans

.PHONY: docker-purge
docker-purge: .env
docker-purge: config
$(DOCKER_COMPOSE) down --remove-orphans --volumes

.PHONY: docker-build
docker-build: .env
docker-build: config
$(DOCKER_COMPOSE) build proxy


Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ You can also use the interactive mode by
mitmproxy -s addons.py
```

If you want to have access to a config, you have to create a config.yaml. You can use the template in
the-infinity marked this conversation as resolved.
Show resolved Hide resolved
`config_dist_dev.yaml`:
the-infinity marked this conversation as resolved.
Show resolved Hide resolved

Using docker, you just have to use `make` to start the `mitmdump` service.
```shell
cp config_dist_dev.yaml config.yaml
```

Using docker, you just have to use `make` to start the `mitmdump` service. The `config.yaml` is created with default
values automatically.

The HTTP proxy will be available at `http://localhost:8080`.

Expand Down
8 changes: 8 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
from mitmproxy.http import HTTPFlow

from app.base_converter import BaseConverter
from app.config_helper import ConfigHelper


class App:
json_converters: Dict[str, List[BaseConverter]]

def __init__(self):
self.config_helper = ConfigHelper()

self.json_converters = {}

# the following code is a converter autoloader. It dynamically adds all converters in ./converters.
Expand All @@ -41,6 +44,11 @@ def __init__(self):
self.json_converters[hostname] = []
self.json_converters[hostname].append(obj)

def request(self, flow: HTTPFlow):
if flow.request.host in self.config_helper.get('HTTP_TO_HTTPS_HOSTS', []):
flow.request.scheme = 'https'
flow.request.port = 443

def response(self, flow: HTTPFlow):
# if there is no converter for the requested host, don't do anything
if flow.request.host not in self.json_converters:
Expand Down
26 changes: 26 additions & 0 deletions app/config_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
MobiData BW Proxy
Copyright (c) 2023, binary butterfly GmbH
All rights reserved.
"""

from pathlib import Path
from typing import Any

from yaml import safe_load


class ConfigHelper:
_config: dict

def __init__(self):
config_path = Path(Path(__file__).parents[1], 'config.yaml')

if config_path.exists():
with open(config_path) as config_file:
self._config = safe_load(config_file)
else:
self._config = {}

def get(self, key: str, default: Any) -> Any:
return self._config.get(key, default)
30 changes: 30 additions & 0 deletions app/converters/gbfs_https_to_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
MobiData BW Proxy
Copyright (c) 2023, binary butterfly GmbH
All rights reserved.
"""

from typing import Union

from app.base_converter import BaseConverter


class GbfsHttpsToHttpConverter(BaseConverter):
hostnames = ['gbfs.nextbike.net']

def convert(self, data: Union[dict, list], path: str) -> Union[dict, list]:
if not isinstance(data, dict) and not path.endswith('/gbfs.json'):
derhuerst marked this conversation as resolved.
Show resolved Hide resolved
return data

if not isinstance(data, dict) or 'data' not in data or not isinstance(data['data'], dict):
return data

for language in data['data']:
if 'feeds' not in data['data'][language] or not isinstance(data['data'][language]['feeds'], list):
continue
for feed in data['data'][language]['feeds']:
if not isinstance(feed, dict) or 'url' not in feed or not isinstance(feed['url'], str):
continue
feed['url'] = f'http{feed["url"][5:]}'

return data
3 changes: 3 additions & 0 deletions config_dist_dev.yaml
derhuerst marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

HTTP_TO_HTTPS_HOSTS:
- gbfs.nextbike.net
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ruff~=0.0.282
black~=23.7.0
mypy~=1.4.1
types-pyyaml~=6.0.12.11
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mitmproxy~=10.0.0
mitmproxy~=10.0.0