Skip to content

Commit

Permalink
Merge pull request #18 from dbluhm/chore/updates
Browse files Browse the repository at this point in the history
Update dependencies and project tooling
  • Loading branch information
dbluhm authored May 7, 2024
2 parents f7f2af4 + 6464f51 commit 2c4cb4b
Show file tree
Hide file tree
Showing 15 changed files with 989 additions and 1,259 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/code-quality-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Code Quality Check

"on":
push:
branches:
- main
pull_request:
branches:
- main

jobs:
format:
name: Format and Lint Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
with:
version: "0.4.3"
args: 'format --check'
- uses: chartboost/ruff-action@v1
with:
version: "0.4.3"
30 changes: 7 additions & 23 deletions .github/workflows/publish-image.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Publish Image
run-name: Publish SocketDock ${{ inputs.tag || github.event.release.tag_name }} Image

on:
on:
release:
types: [published]

Expand All @@ -27,28 +27,20 @@ env:
PLATFORMS: ${{ inputs.platforms || 'linux/amd64' }}

jobs:
publish_to_ghcr:
name: Publish image to ghcr.io
publish-image:
name: Publish Image
runs-on: ubuntu-latest
steps:
- name: Checkout
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref || '' }}

- name: Gather image info
- name: Image info
id: info
run: |
echo "repo-owner=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_OUTPUT
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Expand Down Expand Up @@ -76,14 +68,6 @@ jobs:
file: Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: ${{ env.PLATFORMS }}

# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
16 changes: 7 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
---
repos:
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v2.2.0
rev: v9.16.0
hooks:
- id: commitlint
stages: [commit-msg]
args: ["--config", ".commitlint.config.js"]
additional_dependencies: ['@commitlint/config-conventional']
- repo: https://github.com/psf/black
rev: 23.3.0
additional_dependencies: ["@commitlint/config-conventional"]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.3
hooks:
- id: black
- id: ruff
stages: [commit]
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
stages: [commit]
2 changes: 1 addition & 1 deletion docker-compose-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ services:
# parser.add_argument('--disconnect_uri')

tunnel:
image: dbluhm/agent-tunnel
image: ghcr.io/indicio-tech/agent-tunnel:latest
command: -s websocket-gateway:8765 -p 4040 -h ${AGENT_TUNNEL_HOST}
12 changes: 2 additions & 10 deletions locust/locustClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

import time
import inspect
import json

import fcntl
import os
import requests
import signal

from websocket import create_connection
import websocket

import sys
import gevent

gevent.monkey.patch_all()
Expand Down Expand Up @@ -64,7 +57,6 @@ class InboundHTTP(metaclass=Singleton):
_self = None

def __init__(self):

app = Flask(__name__)

@app.route("/")
Expand Down Expand Up @@ -98,7 +90,7 @@ def __init__(self, host):
def startup(self):
# print("startup", file=sys.stderr)

server = InboundHTTP()
InboundHTTP()

# Give a sec for the inbound server to start
time.sleep(5) # Magic Number
Expand Down Expand Up @@ -134,7 +126,7 @@ def shutdown(self):
@stopwatch
def msg_client(self):
i = ""
if self.connected == False:
if self.connected is False:
raise Exception("Not Connected!")
self.ws.send(f"Ping! ({i})")
pass
2,069 changes: 900 additions & 1,169 deletions poetry.lock

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@ description = "Websocket relay service for use with clustered mediators"
authors = [
"Sam Curren <telegramsam@gmail.com>",
"Colton Wolkins (Indicio work address) <colton@indicio.tech>",
"Micah Peltier <micah@indicio.tech>",
"Daniel Bluhm <dbluhm@pm.me>",
"Micah Peltier <micah@indicio.tech>",
"Daniel Bluhm <dbluhm@pm.me>",
]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8.1"
aiohttp = "^3.8.4"
aiohttp = "^3.9.4"
sanic = "^22.12.0"

[tool.poetry.group.dev.dependencies]
websocket = "^0.2.1"
black = "^22"
flake8 = "^5"
ruff = "^0.4.3"
pre-commit = "^3.2.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 90
extend-exclude = ["locust"]

[tool.ruff.lint]
select = ["E", "F", "C", "D"]
ignore = [
# Google Python Doc Style
"D203", "D204", "D213", "D215", "D400", "D401", "D404", "D406", "D407",
"D408", "D409", "D413",
]
per-file-ignores = {"**/{tests}/*" = ["F841", "D", "E501"]}

[tool.ruff.format]
docstring-code-format = true
14 changes: 0 additions & 14 deletions setup.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions socket_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Simple test client."""

import asyncio
import websockets


async def hello():
"""Connect and say hello."""
async with websockets.connect("ws://localhost:8765/ws") as websocket:
for i in range(5):
print(f"> Hello world! ({i})", flush=True)
Expand Down
1 change: 1 addition & 0 deletions socketdock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""SocketDock."""
2 changes: 0 additions & 2 deletions socketdock/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

def config() -> argparse.Namespace:
"""Parse command line arguments."""

parser = argparse.ArgumentParser(
prog="SocketDock", description="Socket Gateway for configurable backends"
)
Expand All @@ -31,7 +30,6 @@ def config() -> argparse.Namespace:

def main():
"""Run the SocketDock server."""

args = config()
if args.backend == "loopback":
from .testbackend import TestBackend
Expand Down
19 changes: 12 additions & 7 deletions socketdock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ async def socket_send(request: Request, connectionid: str):
return text("FAIL", status=500)

socket = active_connections[connectionid]
if request.headers["content-type"] == 'text/plain':
if request.headers["content-type"] == "text/plain":
await socket.send(request.body.decode())
else:
await socket.send(request.body)
return text("OK")


@api.post("/socket/<connectionid>/disconnect")
async def socket_disconnect(request: Request, connectionid: str):
"""Disconnect a socket."""
Expand All @@ -70,12 +71,16 @@ async def socket_disconnect(request: Request, connectionid: str):
await socket.close()
return text("OK")


@api.websocket("/ws")
async def socket_handler(request: Request, websocket: Websocket):
"""Handle a new websocket connection."""
global lifetime_connections
backend = backend_var.get()
socket_id = None
endpoint = endpoint_var.get()
send = f"{endpoint}/socket/{socket_id}/send"
disconnect = f"{endpoint_var.get()}/socket/{socket_id}/disconnect"
try:
# register user
LOGGER.info("new client connected")
Expand All @@ -87,21 +92,21 @@ async def socket_handler(request: Request, websocket: Websocket):
LOGGER.info("Request headers: %s", dict(request.headers.items()))

await backend.socket_connected(
{
{
"connection_id": socket_id,
"headers": dict(request.headers.items()),
"send": f"{endpoint_var.get()}/socket/{socket_id}/send",
"disconnect": f"{endpoint_var.get()}/socket/{socket_id}/disconnect",
},
"send": send,
"disconnect": disconnect,
},
)

async for message in websocket:
if message:
await backend.inbound_socket_message(
{
"connection_id": socket_id,
"send": f"{endpoint_var.get()}/socket/{socket_id}/send",
"disconnect": f"{endpoint_var.get()}/socket/{socket_id}/disconnect",
"send": send,
"disconnect": disconnect,
},
message,
)
Expand Down
4 changes: 1 addition & 3 deletions socketdock/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Backend(ABC):
"""Backend interface for SocketDock."""

@abstractmethod
async def socket_connected(
self, callback_uris: dict
):
async def socket_connected(self, callback_uris: dict):
"""Handle new socket connections, with calback provided."""
raise NotImplementedError()

Expand Down
11 changes: 2 additions & 9 deletions socketdock/httpbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ def __init__(self, connect_uri: str, message_uri: str, disconnect_uri: str):
self._message_uri = message_uri
self._disconnect_uri = disconnect_uri

async def socket_connected(
self, callback_uris: dict
):
async def socket_connected(self, callback_uris: dict):
"""Handle inbound socket message, with calback provided."""

http_body = {
"meta": callback_uris,
}
Expand All @@ -43,12 +40,9 @@ async def inbound_socket_message(
self, callback_uris: dict, message: Union[str, bytes]
):
"""Handle inbound socket message, with calback provided."""

http_body = {
"meta": callback_uris,
"message": message.decode("utf-8")
if isinstance(message, bytes)
else message,
"message": message.decode("utf-8") if isinstance(message, bytes) else message,
}

async with aiohttp.ClientSession() as session:
Expand All @@ -62,7 +56,6 @@ async def inbound_socket_message(

async def socket_disconnected(self, bundle: dict):
"""Handle socket disconnected."""

async with aiohttp.ClientSession() as session:
LOGGER.info("Notifying of disconnect: %s %s", self._disconnect_uri, bundle)
async with session.post(self._disconnect_uri, json=bundle) as resp:
Expand Down
17 changes: 10 additions & 7 deletions socketdock/testbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
class TestBackend(Backend):
"""Test backend for SocketDock."""

async def socket_connected(
self, callback_uris: dict
):
# This test method doesn't care, but can be useful to clean up state.
pass
async def socket_connected(self, callback_uris: dict):
"""Socket connected.
This test backend doesn't care, but can be useful to clean up state.
"""

async def inbound_socket_message(
self, callback_uris: dict, message: Union[str, bytes]
):
"""Receive socket message."""
# send three backend messages in response
# TODO: send response message via callback URI for sending a message
send_uri = callback_uris["send"]
Expand All @@ -29,5 +30,7 @@ async def inbound_socket_message(
# response = requests.post(send_uri, data="Hello yourself!")

async def socket_disconnected(self, bundle: dict):
# This test method doesn't care, but can be useful to clean up state.
pass
"""Socket disconnected.
This test backend doesn't care, but can be useful to clean up state.
"""

0 comments on commit 2c4cb4b

Please sign in to comment.