Skip to content

Commit

Permalink
Bump Version. Use own run_app to prevent the aiohttp cleanup fails du…
Browse files Browse the repository at this point in the history
…ring tests
  • Loading branch information
fkantelberg committed Dec 14, 2023
1 parent f64a2cf commit b53dd8d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[run]
omit = */tunnel_gui.py
source = */socket_proxy/*
concurrency = multiprocessing
parallel = True
4 changes: 1 addition & 3 deletions src/socket_proxy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from .base import config
from .base import VERSION, config
from .connection import Connection
from .package import *
from .proxy import ProxyServer
from .tunnel import Tunnel
from .tunnel_client import TunnelClient
from .tunnel_server import TunnelServer

VERSION = "5.0.2"
40 changes: 32 additions & 8 deletions src/socket_proxy/api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import asyncio
import enum
import logging
import ssl
from typing import Any, Optional, Sequence

from . import base, utils

try:
from aiohttp import web
from aiohttp.web import Request, Response
from aiohttp.web import AppRunner, Request, Response, TCPSite
except ImportError:
web = Request = Response = None # type: ignore
web = AppRunner = Request = Response = TCPSite = None # type: ignore

_logger = logging.getLogger(__name__)

Expand All @@ -18,12 +20,35 @@ class APIType(enum.IntEnum):
Server = 0x02


async def run_app(
api,
host: Optional[str] = None,
port: Optional[int] = None,
ssl_context: Optional[ssl.SSLContext] = None,
) -> AppRunner:
app = AppRunner(
api,
access_log_format='%a "%r" %s %b "%{Referer}i" "%{User-Agent}i"',
)
await app.setup()

site = TCPSite(
app,
host=host,
port=port,
reuse_address=True,
reuse_port=True,
ssl_context=ssl_context,
)
await site.start()


class APIMixin:
"""Mixin to define the basic API implementations"""

def __init__(self, api_type: APIType):
self.api_type: APIType = api_type
self.api: Optional[web.Application] = None
self.api: Optional[AppRunner] = None
self.api_ssl: bool = False
self.api_host: Optional[str] = None
self.api_port: Optional[int] = None
Expand Down Expand Up @@ -96,13 +121,12 @@ async def start_api(self) -> None:
]
)

await web._run_app(
self.app = await run_app(
self.api,
host=self.api_host,
port=self.api_port,
access_log_format='%a "%r" %s %b "%{Referer}i" "%{User-Agent}i"',
reuse_address=True,
reuse_port=True,
print=lambda *_x: None,
ssl_context=self.sc if self.api_ssl else None,
)

while True:
await asyncio.sleep(60)
2 changes: 2 additions & 0 deletions src/socket_proxy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

_logger = logging.getLogger(__name__)

VERSION = "5.0.3"

CLIENT_NAME_SIZE = 8
EVENT_TIMEOUT = 0.5
INTERVAL_TIME = 1
Expand Down
4 changes: 2 additions & 2 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from aiohttp import web

from socket_proxy import event
from socket_proxy import api, event

from .common import unused_ports

Expand All @@ -25,7 +25,7 @@ async def receive(request: web.Request) -> web.Response:

app = web.Application()
app.add_routes([web.post("/", receive)])
asyncio.create_task(web._run_app(app, host="127.0.0.1", port=port))
asyncio.create_task(api.run_app(app, host="127.0.0.1", port=port))
await asyncio.sleep(0.1)

system = event.EventSystem(event.EventType.Server, url=None, token=None)
Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ deps =
pytest-asyncio
pytest-cov
pytest-timeout
pytest-xdist
aiohttp
coverage
depends:
py3: clean
report: py3
commands =
pytest --cov {envsitepackagesdir}/socket_proxy --asyncio-mode=auto --cov-append --timeout 5 --log-level INFO
pytest --cov {envsitepackagesdir}/socket_proxy --asyncio-mode=auto --cov-append --timeout 5 --log-level info

[testenv:report]
skip_install = true
Expand Down

0 comments on commit b53dd8d

Please sign in to comment.