Skip to content

Commit 61bad09

Browse files
committed
Lint and bug fixes
1 parent 47d5896 commit 61bad09

File tree

10 files changed

+49
-28
lines changed

10 files changed

+49
-28
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
pull_request:
77
branches: [main]
88

9+
permissions:
10+
contents: read
11+
912
jobs:
1013
test:
1114
runs-on: ${{ matrix.os }}
@@ -24,7 +27,8 @@ jobs:
2427

2528
- uses: actions/setup-go@v5
2629
with:
27-
go-version: '1.25'
30+
go-version: '1.25.7'
31+
cache-dependency-path: pygofastproxy/go/go.sum
2832

2933
- name: Install package with test dependencies
3034
run: pip install -e ".[test]"

.github/workflows/lint.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
pull_request:
77
branches: [main]
88

9+
permissions:
10+
contents: read
11+
912
jobs:
1013
python-lint:
1114
runs-on: ubuntu-latest
@@ -32,7 +35,7 @@ jobs:
3235

3336
- uses: actions/setup-go@v5
3437
with:
35-
go-version: '1.25'
38+
go-version: '1.25.7'
3639

3740
- name: golangci-lint
3841
uses: golangci/golangci-lint-action@v6

.github/workflows/security.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ on:
66
pull_request:
77
branches: [main]
88
schedule:
9-
- cron: '0 8 * * 1' # Weekly on Monday
9+
- cron: '0 8 * * 1'
10+
11+
permissions:
12+
contents: read
1013

1114
jobs:
1215
go-vulncheck:
@@ -16,7 +19,7 @@ jobs:
1619

1720
- uses: actions/setup-go@v5
1821
with:
19-
go-version: '1.25'
22+
go-version: '1.25.7'
2023

2124
- name: Install govulncheck
2225
run: go install golang.org/x/vuln/cmd/govulncheck@latest

benchmarks/benchmark.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ def run_benchmark(total_requests: int, concurrency: int):
6161
url = f"http://127.0.0.1:{proxy_port}/bench"
6262

6363
# Warmup
64-
print(f"Warming up with 200 requests...")
64+
print("Warming up with 200 requests...")
6565
for _ in range(200):
6666
try:
6767
urllib.request.urlopen(url, timeout=5)
6868
except Exception:
6969
pass
7070

7171
# Benchmark
72-
print(
73-
f"Benchmarking: {total_requests} requests, {concurrency} concurrent..."
74-
)
72+
print(f"Benchmarking: {total_requests} requests, {concurrency} concurrent...")
7573
successes = 0
7674
errors = 0
7775

@@ -116,10 +114,18 @@ def make_request():
116114
def main():
117115
parser = argparse.ArgumentParser(description="Benchmark pygofastproxy")
118116
parser.add_argument(
119-
"-n", "--requests", type=int, default=10000, help="Total requests (default: 10000)"
117+
"-n",
118+
"--requests",
119+
type=int,
120+
default=10000,
121+
help="Total requests (default: 10000)",
120122
)
121123
parser.add_argument(
122-
"-c", "--concurrency", type=int, default=50, help="Concurrent workers (default: 50)"
124+
"-c",
125+
"--concurrency",
126+
type=int,
127+
default=50,
128+
help="Concurrent workers (default: 50)",
123129
)
124130
args = parser.parse_args()
125131
run_benchmark(args.requests, args.concurrency)

pygofastproxy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .runner import run_proxy
22

3-
__all__ = ["run_proxy"]
3+
__all__ = ["run_proxy"]

pygofastproxy/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""CLI entry point for pygofastproxy."""
2+
23
import os
34
import sys
5+
46
from .runner import run_proxy
57

68

pygofastproxy/runner.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
from pathlib import Path
55
from urllib.parse import urlparse
66

7+
78
# Check if running on Windows.
89
def _is_windows() -> bool:
910
return os.name == "nt"
1011

12+
1113
# Return executable name, appending .exe on Windows.
1214
def _bin_name(base: str) -> str:
1315
return f"{base}.exe" if _is_windows() else base
1416

17+
1518
# Check if Go is installed and available in PATH.
1619
def check_go_installed():
1720
try:
@@ -24,32 +27,33 @@ def check_go_installed():
2427
except Exception as e:
2528
raise RuntimeError("Go is not installed or not found in PATH.") from e
2629

30+
2731
# Build the Go proxy binary from source.
2832
def build_proxy(go_dir: Path, binary_path: Path):
2933
print("Building Go proxy ...")
3034
if not (go_dir / "go.mod").exists():
3135
raise FileNotFoundError(f"Missing go.mod in {go_dir}")
3236

3337
cmd = [
34-
"go", "build",
38+
"go",
39+
"build",
3540
"-trimpath",
36-
"-ldflags", "-s -w",
37-
"-o", binary_path.name,
41+
"-ldflags",
42+
"-s -w",
43+
"-o",
44+
binary_path.name,
3845
".",
3946
]
4047
result = subprocess.run(
41-
cmd,
42-
cwd=go_dir,
43-
stdout=subprocess.PIPE,
44-
stderr=subprocess.STDOUT,
45-
text=True
48+
cmd, cwd=go_dir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
4649
)
4750
if result.returncode != 0:
4851
raise RuntimeError(f"Failed to build Go proxy:\n{result.stdout}")
4952

5053
if not _is_windows():
5154
binary_path.chmod(0o755)
5255

56+
5357
# Check if the Go binary is missing or older than source files.
5458
def is_rebuild_needed(go_dir: Path, binary_file: Path) -> bool:
5559
if not binary_file.exists():
@@ -65,12 +69,13 @@ def is_rebuild_needed(go_dir: Path, binary_file: Path) -> bool:
6569
return True
6670
return False
6771

72+
6873
# Run the Go proxy, rebuilding if needed.
6974
def run_proxy(target="http://localhost:4000", port=8080, **kwargs):
7075
# Validate target URL
7176
try:
7277
parsed = urlparse(target)
73-
if parsed.scheme not in ('http', 'https'):
78+
if parsed.scheme not in ("http", "https"):
7479
raise ValueError(f"Target must use http or https scheme, got: {target}")
7580
if not parsed.netloc:
7681
raise ValueError(f"Invalid target URL (missing host): {target}")
@@ -116,7 +121,8 @@ def run_proxy(target="http://localhost:4000", port=8080, **kwargs):
116121
if "tls_key_file" in kwargs:
117122
env["PROXY_TLS_KEY_FILE"] = str(kwargs["tls_key_file"])
118123
if "cors_allow_credentials" in kwargs:
119-
env["PROXY_CORS_ALLOW_CREDENTIALS"] = str(kwargs["cors_allow_credentials"]).lower()
124+
val = str(kwargs["cors_allow_credentials"]).lower()
125+
env["PROXY_CORS_ALLOW_CREDENTIALS"] = val
120126

121127
print(f"Starting Go proxy at http://localhost:{port} -> {target}")
122128

@@ -127,7 +133,7 @@ def run_proxy(target="http://localhost:4000", port=8080, **kwargs):
127133
env=env,
128134
stdout=subprocess.PIPE,
129135
stderr=subprocess.STDOUT,
130-
text=True
136+
text=True,
131137
)
132138

133139
# Log output from the proxy process in a background thread.

test_functionality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_basic_proxy():
7676
# Test health endpoint
7777
health = requests.get(f"{base}/health", timeout=2)
7878
assert health.status_code == 200
79-
79+
8080
# Test security headers
8181
assert "X-Content-Type-Options" in res.headers
8282
assert "X-Frame-Options" in res.headers

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def do_GET(self):
2121
self.send_response(200)
2222
self.send_header("Content-Type", "application/json")
2323
self.end_headers()
24-
self.wfile.write(
25-
b'{"method":"GET","path":"' + self.path.encode() + b'"}'
26-
)
24+
self.wfile.write(b'{"method":"GET","path":"' + self.path.encode() + b'"}')
2725

2826
def do_POST(self):
2927
length = int(self.headers.get("Content-Length", 0))

tests/test_proxy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from pygofastproxy.runner import run_proxy
77

8-
98
# ---------------------------------------------------------------------------
109
# Basic proxy forwarding
1110
# ---------------------------------------------------------------------------
@@ -218,4 +217,4 @@ def test_port_too_high_raises(self):
218217

219218
def test_port_not_int_raises(self):
220219
with pytest.raises(ValueError, match="Port"):
221-
run_proxy(target="http://localhost:4000", port="abc")
220+
run_proxy(target="http://localhost:4000", port="abc") # type: ignore[arg-type]

0 commit comments

Comments
 (0)