Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Exclude env files at any depth (**/*.env) — Docker patterns without '/' are
# root-anchored (unlike gitignore) and miss subdirectories.
# config_sample.env is re-included below; order matters, last match wins.
# Without this file, COPY . . bakes env files and virtualenvs into the image.
.git
.gitattributes
**/*.env
!config_sample.env
**/*.session
**/*.session-journal
**/*.session-shm
**/*.session-wal
Thunder/logs
.venv
venv
__pycache__
**/__pycache__
**/*.pyc
.pytest_cache
.ruff_cache
.coverage
.coverage.*
.coverage.json
tests
requirements-dev.txt
pyproject.toml
.github
.gitignore
.python-version
Dockerfile
.dockerignore
README.md
AGENTS.md
VPS.md
PLAN.md

8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
# Actions are pinned to commit SHAs, so they need updates to move.
# Application dependencies are deliberately left floating.
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
26 changes: 0 additions & 26 deletions .github/workflows/dockerize.yml

This file was deleted.

111 changes: 102 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,119 @@
name: Tests
name: CI

on:
push:
branches: [main]
pull_request:
workflow_dispatch:
schedule:
# Dependencies float on purpose, so a weekly run is the only thing that
# notices a bad upstream release before a user does.
- cron: "30 4 * * 1"

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
# Only PR runs race. Cancelling a publish to Docker Hub midway is worse than
# letting it finish.
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
pytest:
test:
# Runs on the upstream repository, not in every fork.
if: github.repository == 'fyaz05/FileToLink'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# Windows earns its place: the uvloop marker and update.py's chmod
# fallback never execute on Linux.
os: [ubuntu-latest, windows-latest]
python-version: ["3.13"]
include:
# The image pins 3.13, but the project documents 3.13+ support and the
# dependencies float, so this is what proves the promise still holds.
- os: ubuntu-latest
python-version: "3.14"
runs-on: ${{ matrix.os }}
timeout-minutes: 20
steps:
- uses: actions/checkout@v5

- uses: actions/setup-python@v5
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.13"

python-version: ${{ matrix.python-version }}
- name: Install
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt

pip check
- name: Test
run: python -m pytest -q
- name: Lint
run: python -m ruff check .

build:
# Waiting for the tests is what stops a red main from publishing.
needs: [test]
if: github.repository == 'fyaz05/FileToLink'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Set up Buildx
# The default docker driver writes to the daemon's image store, and the
# classic store cannot hold an image index, so an explicit provenance
# attestation fails there. A container-driver builder supports them
# regardless, and also keeps the result addressable for the smoke test.
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Log in to Docker Hub
# Fork pull requests never receive secrets, so logging in there would
# only fail. The image still builds without credentials.
if: github.event_name == 'push'
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
username: fyaz05
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push
id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
# Loaded, not pushed, and without attestations: this build exists to be
# tested, and the publish step below rebuilds from the same cache. An
# attestation cannot be loaded into the daemon's image store, so asking
# for one here would fail before the test ever ran. Passing no tag keeps
# this image out of the registry entirely.
load: true
tags: fyaz05/thunder:test
- name: Smoke test the built image
# Building is not the same as working: a missing dependency or a broken
# import still produces a valid image that fails the moment it starts.
# This runs BEFORE anything is published, so a broken image is never the
# one tagged latest. Importing the entry point is the strongest check that
# needs no real credentials -- Thunder.vars validates its configuration at
# import, so placeholders stand in for a deployment's own values. That
# also means this proves the dependencies and the code, not the config.
run: |
docker run --rm \
-e API_ID=123456 \
-e API_HASH=00000000000000000000000000000000 \
-e BOT_TOKEN=123456:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
-e BIN_CHANNEL=-1001234567890 \
-e OWNER_ID=1 \
-e DATABASE_URL=mongodb://localhost:27017/smoke \
fyaz05/thunder:test python -c \
"import Thunder, Thunder.__main__, uvloop, tgcrypto, aiohttp; print('ok', Thunder.__version__)"
- name: Publish
# Only reached once the smoke test passed, and only for a push event: a
# pull request never receives secrets. Rebuilds from the cache the tested
# build populated, so this is the same image, now with attestations.
if: github.event_name == 'push'
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
# Public and private repositories default differently, so it is set
# explicitly.
provenance: mode=max
push: true
tags: fyaz05/thunder:latest
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
*$py.class
*.so
.venv/
venv/
.Python
config.env
# Exclude env files (a stray `git add -A` must not pick them up); track only the template.
*.env
!config_sample.env
# Local operational files containing credentials or scratch notes.
VPS.md
PLAN.md
log.text
.vscode/
**/__pycache__/
Expand All @@ -28,5 +34,9 @@ wheels/
.installed.cfg
*.egg
.pytest_cache/
.ruff_cache/
.coverage
.coverage.*
.coverage.json
__pycache__/
*.pyc
68 changes: 58 additions & 10 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ Python 3.13+ Telegram bot converting files to direct HTTP links. Uses Pyrofork,
python -m Thunder # Primary entry point
bash thunder.sh # Updates from UPSTREAM_REPO, then runs the bot
python3 update.py # Update only (rolls back if the new tree is unusable)
# update.py exits 0 when it cannot apply: no UPSTREAM_REPO, or no git/.git in a container
```

## Dependencies

```bash
pip install -r requirements.txt
# aiohttp, cloudscraper, Jinja2, pyrofork, pymongo, psutil, python-dotenv, speedtest-cli, tgcrypto
# uvloop==0.21.0 only on non-Windows/Cygwin (requirements.txt marker); asyncio loop elsewhere
# aiohttp, cloudscraper, Jinja2, pyrofork, pymongo, psutil, python-dotenv, speedtest-cli, tgcrypto-pyrofork
# uvloop (non-Windows/Cygwin only, requirements.txt marker); asyncio loop elsewhere
```

## Project Structure
Expand All @@ -29,7 +30,7 @@ Thunder/
│ ├── __init__.py # StreamBot client, multi_clients, work_loads
│ ├── clients.py # Multi-client management
│ └── plugins/
│ ├── admin.py # Owner commands: /users /broadcast /status /stats /restart /log /authorize /deauthorize /ban /unban /shell /speedtest
│ ├── admin.py # Owner commands: /users /broadcast /status /stats /restart /log /authorize /deauthorize /listauth /ban /unban /shell /speedtest
│ ├── callbacks.py # Inline keyboard handlers
│ ├── common.py # User commands: /start /help /about /dc /ping
│ └── stream.py # /link (groups), private/channel media handlers
Expand All @@ -38,9 +39,26 @@ Thunder/
│ ├── stream_routes.py # HTTP streaming endpoints
│ └── exceptions.py # Custom HTTP exceptions
├── utils/ # 20 modules — see imports below
└── template/ # dl.html, req.html (Jinja2)
└── template/ # req.html (Jinja2)
```

## Docker

The image runs as unprivileged user `10001` and installs no build toolchain (all
dependencies provide wheels). `.dockerignore` excludes env files, session files,
virtualenvs, dev tools, and tests; supply `config.env` at runtime.

Two gotchas:
- A mounted `config.env` must be readable by uid `10001` (`chmod 644`; a
root-only `600` fails). Do not `chown` it — that hands the file to a host-unknown
uid you can no longer edit.
- Docker's `--env-file` passes values verbatim, so inline comments and quotes in
`config_sample.env` corrupt variables (`PORT` becomes `8080 # Web server port`).
Generate the file with `python-dotenv` outside the repo (see README), or mount `config.env`.

Self-update is skipped inside the image: there is no `git` and no `.git`, so
`UPSTREAM_REPO` has no effect there. Update a container by building a new image.

## Key Imports

```python
Expand Down Expand Up @@ -77,16 +95,46 @@ Copy `config_sample.env` → `config.env`. Required vars: `API_ID`, `API_HASH`,
## Testing

```bash
pip install pytest pytest-asyncio
python -m pytest -q # ~60s; no network or Telegram credentials needed
pip install -r requirements-dev.txt
python -m pytest -q # ~20s on Linux, ~45s on Windows; no credentials needed
python -m ruff check . # config lives in pyproject.toml
```

Covers request parsing, slot conservation and truncation on the streaming
routes, the import cycle, database naming, /status before startup, and
update.py preconditions and rollback.
routes, the import cycle, database naming, /status before startup,
update.py preconditions and rollback, plugin handler registration, the CI
workflow's invariants, and .gitignore hygiene.

`pytest` and `ruff` both read `pyproject.toml`; there is no `pytest.ini`.

### With uv

`uv` is faster than pip for creating environments and installing, and it is used
only as a tool — there is deliberately **no `uv.lock`**: pinning every package is
the opposite of what this project wants, because `requirements.txt` floats so that
CI notices a bad upstream release. Every command below was run to confirm it works
and leaves no lock file:

```bash
uv venv .venv --python 3.13 # ~0.15s
uv pip install --python .venv/bin/python -r requirements-dev.txt # ~3s first time
uvx ruff check . # ~0.1s, no install needed
```

On Windows the interpreter is `.venv\Scripts\python.exe`. `uv pip install` runs in
well under a second once anything is installed; the first install after creating
the environment takes a few seconds. Re-running it is safe.

One caveat if you deploy from this checkout: `uv venv` creates an environment
**without pip**, and `update.py` installs dependencies on a self-update with
`python -m pip`. It falls back to `uv pip` and warns if neither works, but the
cleanest result comes from seeding pip in the first place:

```bash
uv venv --seed .venv --python 3.13 # includes pip
```

## Debugging

- Logs: Thunder/logs/bot.txt
- Health check: admin /status command, or GET /status on the web port
- No linting/formatting tools configured - follow conventions manually
- Health check: admin /status command, or GET /status on the web port
23 changes: 10 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ ENV PYTHONUNBUFFERED=1 \

WORKDIR /app

RUN apt-get update && \
apt-get install -y --no-install-recommends \
git \
build-essential \
libssl-dev \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*

# No build toolchain needed: dependencies provide prebuilt wheels (see requirements.txt).
COPY requirements.txt .

RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Owned by unprivileged user so Thunder/logs and session file can be written at runtime.
COPY --chown=10001:10001 . .

USER 10001:10001

COPY . .
EXPOSE 8080

# Update from UPSTREAM_REPO when it is configured, then start. A failed or
# skipped update never blocks startup.
# Start. thunder.sh tries update.py first; with no git or .git in the image that
# is reported as not applicable and the bot starts on the code baked in here.
CMD ["bash", "thunder.sh"]
Loading
Loading