Skip to content

Commit

Permalink
feat: Proxy requests only to chosen data sinks (#5)
Browse files Browse the repository at this point in the history
* feat: proxy requests only two chosen data sinks

* fix dangerous default values
  • Loading branch information
lhw authored Jun 6, 2024
1 parent def49e5 commit 6ed56e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 4 additions & 1 deletion aiocloudweather/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

from aiocloudweather import CloudWeatherListener, WeatherStation
from aiocloudweather.proxy import DataSink
from aiocloudweather.station import Sensor

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,7 +49,9 @@ def main() -> None:
sys.exit(1)

print(f"Firing up webserver to listen on port {sys.argv[1]}")
cloudweather_server = CloudWeatherListener(port=sys.argv[1], proxy_enabled=True)
cloudweather_server = CloudWeatherListener(
port=sys.argv[1], proxy_sinks=[DataSink.WUNDERGROUND]
)

cloudweather_server.new_dataset_cb.append(my_handler)
try:
Expand Down
13 changes: 10 additions & 3 deletions aiocloudweather/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class DataSink(Enum):
class CloudWeatherProxy:
"""Proxy for forwarding data to the CloudWeather API."""

def __init__(self, dns_servers: list[str]):
def __init__(self, proxied_sinks: list[DataSink], dns_servers: list[str]):
resolver = AsyncResolver(nameservers=dns_servers)
self.proxied_sinks = proxied_sinks
self.session = ClientSession(connector=TCPConnector(resolver=resolver))

async def forward_wunderground(self, request: web.Request) -> web.Response:
Expand All @@ -34,7 +35,13 @@ async def forward_weathercloud(self, request: web.Request) -> web.Response:

async def forward(self, sink: DataSink, request: web.Request) -> web.Response:
"""Forward data to the CloudWeather API."""
if sink == DataSink.WUNDERGROUND:
if (
sink == DataSink.WUNDERGROUND
and DataSink.WUNDERGROUND in self.proxied_sinks
):
return await self.forward_wunderground(request)
if sink == DataSink.WEATHERCLOUD:
if (
sink == DataSink.WEATHERCLOUD
and DataSink.WEATHERCLOUD in self.proxied_sinks
):
return await self.forward_weathercloud(request)
11 changes: 6 additions & 5 deletions aiocloudweather/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ class CloudWeatherListener:
"""CloudWeather Server API server."""

def __init__(
self, port: int = _CLOUDWEATHER_LISTEN_PORT, proxy_enabled=False, **kwargs: Any
self,
port: int = _CLOUDWEATHER_LISTEN_PORT,
proxy_sinks: list[DataSink] | None = None,
dns_servers: list[str] | None = None,
):
"""Initialize CloudWeather Server."""
# API Constants
self.port: int = port

# Proxy functionality
self.proxy_enabled: bool = proxy_enabled
self.proxy_enabled: bool = proxy_sinks and len(proxy_sinks) > 0
if self.proxy_enabled:
self.proxy = CloudWeatherProxy(
dns_servers=kwargs.get("dns_servers", ["9.9.9.9"])
)
self.proxy = CloudWeatherProxy(proxy_sinks, dns_servers or ["9.9.9.9"])

# webserver
self.server: None | web.Server = None
Expand Down

0 comments on commit 6ed56e8

Please sign in to comment.