Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ profile.pprof

# Windows 特定
Thumbs.db
python/__pycache__/
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Repo Guidelines

- Run `go vet ./...` before committing Go code.
- After changes, run `go fmt ./...`.
- For Python code, run `python -m py_compile` on modified files.
- To build the Camoufox container and login, follow these steps:
1. Run `scripts/bootstrap_login.sh` with `TARGET_URL` env set to login and generate `data/cookie.json`.
2. Use `docker-compose up -d` to start the `camoufox` browser and Go server.
1 change: 1 addition & 0 deletions data/cookie.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.9"
services:
camoufox:
image: camoufox-runner
build:
context: .
dockerfile: docker/Dockerfile.camoufox
environment:
- TARGET_URL=${TARGET_URL}
- COOKIE_FILE=/data/cookie.json
- HTTP_PROXY=${HTTP_PROXY-}
- HEADLESS=true
volumes:
- ./data:/data
shm_size: "2gb"

server:
build: .
depends_on:
- camoufox
environment:
- AUTH_TOKEN=${AUTH_TOKEN}
- PORT=5345
ports:
- "5345:5345"
21 changes: 21 additions & 0 deletions docker/Dockerfile.camoufox
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.11-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
xvfb libgtk-3-0 libdbus-glib-1-2 libasound2 \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY python/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && camoufox fetch

COPY python/runner.py ./runner.py
COPY scripts/docker-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENV COOKIE_FILE=/data/cookie.json
ENV TARGET_URL=""
ENV HTTP_PROXY=""
ENV HEADLESS=true

VOLUME ["/data"]
ENTRYPOINT ["/entrypoint.sh"]
2 changes: 2 additions & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
camoufox[geoip]>=0.9.3
pyyaml>=6.0
47 changes: 47 additions & 0 deletions python/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import argparse, json, os, signal, sys, time
from camoufox import launch


def parse():
p = argparse.ArgumentParser()
p.add_argument("--url", required=True, help="page to keep in browser")
p.add_argument("--cookie", default="./data/cookie.json")
p.add_argument("--bootstrap", action="store_true",
help="open GUI, wait manual login, save cookie & exit")
p.add_argument("--proxy", help="http(s) proxy, e.g. http://user:pass@ip:port")
p.add_argument("--headless", action="store_true", default=False)
return p.parse_args()


def main():
a = parse()
opts = {
"headless": a.headless,
"proxy": {"server": a.proxy} if a.proxy else None,
"virtual_display": not a.headless,
}
browser = launch(**opts)
ctx = browser.new_context()

if os.path.exists(a.cookie):
with open(a.cookie) as f:
ctx.add_cookies(json.load(f))
page = ctx.new_page()
page.goto(a.url)

if a.bootstrap:
print(">> \u624b\u52a8\u5b8c\u6210 Google \u767b\u5f55\u540e\u6309 Ctrl-C \u7ed3\u675f")
try:
while True:
time.sleep(1)
finally:
with open(a.cookie, "w") as f:
json.dump(ctx.cookies(), f)
print(f">> cookie \u4fdd\u5b58\u5230 {a.cookie}")
else:
signal.pause()


if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions scripts/bootstrap_login.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
COOKIE=${COOKIE_FILE:-./data/cookie.json}
URL=${TARGET_URL:?need url}
python python/runner.py --url "$URL" --cookie "$COOKIE" --bootstrap
6 changes: 6 additions & 0 deletions scripts/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -e
ARGS=("--url" "$TARGET_URL" "--cookie" "$COOKIE_FILE")
[ -n "$HTTP_PROXY" ] && ARGS+=("--proxy" "$HTTP_PROXY")
[ "$HEADLESS" != "false" ] && ARGS+=("--headless")
exec python /app/runner.py "${ARGS[@]}"