Skip to content

Commit

Permalink
Allow multiple clients to proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
craig8 committed Jun 7, 2024
1 parent 4a7b3d4 commit 5fd6706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 21 additions & 5 deletions ieee_2030_5/basic_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ssl
from dataclasses import dataclass
from http.client import HTTPSConnection
from http.server import BaseHTTPRequestHandler, HTTPServer
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Tuple
from urllib.parse import urlparse
Expand All @@ -26,6 +26,7 @@ class ContextWithPaths:
certpath: str
keypath: str

connections: dict[str, HTTPSConnection] = {}

class RequestForwarder(BaseHTTPRequestHandler):

Expand Down Expand Up @@ -61,9 +62,14 @@ def get_context_cert_pair(self) -> ContextWithPaths:
def __start_request__(self) -> HTTPSConnection:
ccp = self.get_context_cert_pair()

host, port = self.server.proxy_target
conn = HTTPSConnection(host=host, port=port, context=ccp.context)
conn.connect()
conn = connections.get(ccp.keypath)

if not conn:
host, port = self.server.proxy_target
conn = HTTPSConnection(host=host, port=port, context=ccp.context)
conn.connect()
connections[ccp.keypath] = conn

return conn

def __handle_response__(self, conn: HTTPSConnection):
Expand Down Expand Up @@ -144,7 +150,7 @@ def do_PUT(self):
_log.info(f"PUT {self.path} Content-Length: {self.headers.get('Content-Length')}, Response Status: {response.status}")


class ProxyServer(HTTPServer):
class ProxyServer(ThreadingHTTPServer):

def __init__(self, tls_repo: TLSRepository, proxy_target: Tuple[str, int], **kwargs):
super().__init__(**kwargs)
Expand All @@ -160,6 +166,16 @@ def tls_repo(self) -> TLSRepository:
return self._tls_repo


def finish_request(self, request, client_address):
super().finish_request(request, client_address)

def shutdown_request(self, request):
print("Connection Closing")
super().shutdown_request(request)
for conn in connections.values():
conn.close()


def start_proxy(server_address: Tuple[str, int], tls_repo: TLSRepository,
proxy_target: Tuple[str, int]):
logging.getLogger().info(f"Serving {server_address} proxied to {proxy_target}")
Expand Down
10 changes: 4 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gridappsd-2030_5"
version = "1.2.0"
version = "1.2.1a0"
description = ""
authors = ["C. Allwardt <[email protected]>"]
packages = [{ include = "ieee_2030_5" }, { include = "ieee_2030_5_gui" }]
Expand All @@ -14,7 +14,6 @@ documentation = "https://github.com/GRIDAPPSD/gridappsd-2030_5"

[tool.poetry.dependencies]
python = "^3.8.1,<4.0"
pvlib = "^0.9.0"
Flask = "^2.0.3"
pickleDB = "^0.9.2"
tzlocal = "^4.2"
Expand All @@ -29,14 +28,14 @@ flask-talisman = "^1.0.0"
blinker = "^1.5"
click = "^8.1.3"
flask-session = "^0.5.0"

pyyaml = "^6.0.1"
gridappsd-field-bus = {version = "^2024.4.1a0", allow-prereleases = true}
gridappsd-python = {version = "^2024.4.1a0", allow-prereleases = true}
flet = "^0.22.1"

xsdata = {extras = ["lxml"], version = "^22.5"}
cim-graph = {path = "deps/cim_graph-1.0.0.dev1-py3-none-any.whl"}
# Fixes issue with pypi.org not serving the correct version of docutils
docutils = "!=0.21"

[tool.poetry.group.dev.dependencies]
m2r2 = "^0.3.2"
pytest = "^7.1.3"
Expand All @@ -62,7 +61,6 @@ spaces_before_comment = 4
column_limit = 99
split_before_logical_operator = true


[tool.poetry.scripts]
2030_5_ctl = 'ieee_2030_5.control:_main'
2030_5_server = 'ieee_2030_5.__main__:_main'
Expand Down

0 comments on commit 5fd6706

Please sign in to comment.