Skip to content

Commit

Permalink
Save X-Forwarded-For host information in case of use behind reverse p…
Browse files Browse the repository at this point in the history
…roxy
  • Loading branch information
zwimer committed Oct 17, 2024
1 parent 7bfa846 commit 6e321c3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 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.0.8" # Must be "<major>.<minor>.<patch>", all numbers
__version__: str = "9.1.0" # Must be "<major>.<minor>.<patch>", all numbers
4 changes: 2 additions & 2 deletions rpipe/server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
from flask import Response, request

from ..shared import AdminMessage, AdminStats, AdminEC, Version
from ..shared import AdminMessage, AdminStats, AdminEC, Version, remote_addr
from .util import plaintext, json_response

if TYPE_CHECKING:
Expand Down Expand Up @@ -195,7 +195,7 @@ def _wrap(self, func: Callable) -> Callable:
def _verify(state: State) -> Response:
try:
# Log the request and verify initialization
stat = AdminStats(host=request.remote_addr, command=func.__name__[len(self._UNSAFE) :])
stat = AdminStats(host=remote_addr(), command=func.__name__[len(self._UNSAFE) :])
with state as s:
s.stats.admin.append(stat)
if not self._init:
Expand Down
4 changes: 2 additions & 2 deletions rpipe/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flask import Response, Flask, send_file, request
import waitress

from ..shared import restrict_umask, log, __version__
from ..shared import restrict_umask, remote_addr, log, __version__
from .util import MAX_SIZE_HARD, MIN_VERSION, json_response, plaintext
from .channel import handler, query
from .server import Server
Expand All @@ -33,7 +33,7 @@ def wrapper(*args, **kwargs):
if (fp := request.full_path).endswith("?"):
fp = fp[:-1]
lvl = DEBUG if (ret.status_code < 300 or ret.status_code in (410, 425)) else INFO
args = (request.remote_addr, request.method, fp, ret.status_code)
args = (remote_addr(), request.method, fp, ret.status_code)
getLogger(_LOG).log(lvl, '%s - "%s %s" %d', *args)
return ret

Expand Down
2 changes: 1 addition & 1 deletion rpipe/shared/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
AdminMessage,
)
from .error_code import UploadEC, DownloadEC, QueryEC, AdminEC
from .util import restrict_umask, total_len
from .util import restrict_umask, remote_addr, total_len
from .stats import AdminStats, Stats
from .log import TRACE, LFS
4 changes: 2 additions & 2 deletions rpipe/shared/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING
from datetime import datetime

from flask import request
from .util import remote_addr

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -49,5 +49,5 @@ def delete(self, channel: str) -> None:
self._update(channel, "deletes")

def _update(self, channel: str, name: str) -> None:
getattr(self.channels[channel], name)[str(request.remote_addr)] += 1
getattr(self.channels[channel], name)[remote_addr()] += 1
self.channels[channel].natime = datetime.now()
8 changes: 8 additions & 0 deletions rpipe/shared/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
from typing import TYPE_CHECKING
from os import umask

from flask import request

if TYPE_CHECKING:
from collections.abc import Sequence


def remote_addr() -> str:
addr = request.remote_addr
xf = request.headers.get("X-Forwarded-For")
return f"{addr} / X-Forwarded-For: {xf}" if xf else str(addr)


def total_len(x: Sequence[bytes]) -> int:
return sum(len(i) for i in x)

Expand Down

0 comments on commit 6e321c3

Please sign in to comment.