Skip to content

Commit

Permalink
Block improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zwimer committed Feb 1, 2025
1 parent 738d1f1 commit 37f5bcf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rpipe/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__: str = "9.6.1" # Must be "<major>.<minor>.<patch>", all numbers
__version__: str = "9.6.2" # Must be "<major>.<minor>.<patch>", all numbers
8 changes: 7 additions & 1 deletion rpipe/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def cli() -> None:
help="Get information on the given channel",
)
priority_mode.add_argument("-A", "--admin", action="store_true", help="Allow use of admin commands")
priority_mode.add_argument(
"-B", "--blocked", action="store_true", help="Determine if the client is blocked from the server"
)
# Admin commands
admin = parser.add_subparsers(
title="Admin Commands",
Expand All @@ -178,7 +181,10 @@ def cli() -> None:
admin.add_parser("lock", help="Lock the channel")
admin.add_parser("unlock", help="Unlock the channel")
ip_p = admin.add_parser("ip", help="Block / unblock ip addresses, or get a list of blocked addresses")
m_g = ip_p.add_mutually_exclusive_group(required=False)
m_g = ip_p.add_argument_group(
"Block / Unblock a given IP",
"If none of these are passed, the command will return the list of banned IP addresses",
).add_mutually_exclusive_group(required=False)
m_g.add_argument("--block", help="Block a given IP address")
m_g.add_argument("--unblock", help="Unblock a given IP address")
argcomplete.autocomplete(parser) # Tab completion
Expand Down
21 changes: 15 additions & 6 deletions rpipe/client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from typing import TYPE_CHECKING
from logging import getLogger
from json import dumps
from sys import stderr
import sys

from ...shared import TRACE, QueryEC, Version, version
from .errors import UsageError, VersionError
from .errors import UsageError, VersionError, BlockedError
from .delete import delete
from .util import request
from .recv import recv
Expand Down Expand Up @@ -78,9 +78,18 @@ def _priority_actions(conf: Config, mode: Mode, config_file: Path) -> None:
# Remaining priority modes
if mode.outdated:
_check_outdated(conf)
if mode.server_version:
if mode.server_version or mode.blocked:
log.info("Mode: Server Version")
r = request("GET", f"{conf.url}/version", timeout=conf.timeout)
try:
r = request("GET", f"{conf.url}/version", timeout=conf.timeout)
except BlockedError:
if mode.blocked:
print("Server is blocking this IP address")
sys.exit(1)
raise
if mode.blocked:
print("Server is not blocking this IP address")
return
if not r.ok:
raise RuntimeError(f"Failed to get version: {r}")
print(f"rpipe_server {r.text}")
Expand Down Expand Up @@ -115,6 +124,6 @@ def rpipe(conf: Config, mode: Mode, config_file: Path) -> None:
return
# Print results
if rv.checksum is not None:
print(f"Blake2s: {rv.checksum.hexdigest()}", file=stderr)
print(f"Blake2s: {rv.checksum.hexdigest()}", file=sys.stderr)
if rv.total is not None:
print(f"Total bytes {'sent' if mode.write else 'received'}: {rv.total}", file=stderr)
print(f"Total bytes {'sent' if mode.write else 'received'}: {rv.total}", file=sys.stderr)
10 changes: 9 additions & 1 deletion rpipe/client/client/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Mode:
save_config: bool
outdated: bool
server_version: bool
blocked: bool
query: bool
# Read/Write/Delete modes
read: bool
Expand All @@ -150,6 +151,13 @@ def keys(cls) -> list[str]:
return [i.name for i in fields(cls)]

def priority(self) -> bool:
c = (self.print_config, self.save_config, self.outdated, self.server_version, self.query).count(True)
c = (
self.print_config,
self.save_config,
self.outdated,
self.server_version,
self.blocked,
self.query,
).count(True)
assert c <= 1, "Sanity check on priority mode count failed"
return c > 0
2 changes: 2 additions & 0 deletions rpipe/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
if self._objs.blocked():
if lg.isEnabledFor(DEBUG):
lg.debug("Blocked request by %s to: %s", remote_addr(), request.path)
return Response(status=BLOCKED_EC)
ret = func(*args, self._objs, **kwargs) if objs else func(*args, **kwargs)
if not logged or self._objs.server.state.debug:
Expand Down

0 comments on commit 37f5bcf

Please sign in to comment.