fix: 代理探测只看学习站,手填地址只允许公网
This commit is contained in:
@@ -712,7 +712,7 @@ def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
json={
|
||||
"employee_ids": ["p1-user", "p2-user"],
|
||||
"scheme": "socks5",
|
||||
"host": "127.0.0.1",
|
||||
"host": "8.8.8.8",
|
||||
"port": 1080,
|
||||
"username": "u",
|
||||
"password": "secret",
|
||||
@@ -723,7 +723,7 @@ def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "p1-user")
|
||||
assert one["has_proxy"] is True
|
||||
assert one["proxy"] == "SOCKS5 127.0.0.1:1080"
|
||||
assert one["proxy"] == "SOCKS5 8.8.8.8:1080"
|
||||
detail = client.get("/api/admin/users/p1-user/proxy")
|
||||
assert detail.status_code == 200
|
||||
assert detail.json()["username"] == "u"
|
||||
@@ -741,7 +741,7 @@ def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
json={
|
||||
"employee_ids": ["p1-user"],
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.1",
|
||||
"host": "1.1.1.1",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"password": "",
|
||||
@@ -750,7 +750,7 @@ def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
assert no_auth.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "p1-user")
|
||||
assert one["proxy"] == "HTTP 10.0.0.1:8080"
|
||||
assert one["proxy"] == "HTTP 1.1.1.1:8080"
|
||||
detail = client.get("/api/admin/users/p1-user/proxy")
|
||||
assert detail.json()["has_password"] is False
|
||||
assert detail.json()["username"] == ""
|
||||
@@ -833,7 +833,7 @@ def test_admin_can_turn_off_proxy_with_scheme() -> None:
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
client.put(
|
||||
"/api/admin/users/proxy",
|
||||
json={"employee_ids": ["off-user"], "scheme": "http", "host": "10.0.0.1", "port": 8080},
|
||||
json={"employee_ids": ["off-user"], "scheme": "http", "host": "1.1.1.1", "port": 8080},
|
||||
)
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "off-user")
|
||||
@@ -893,7 +893,7 @@ def test_admin_can_save_study_settings_in_one_request() -> None:
|
||||
"second": 11,
|
||||
"workers": 2,
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.8",
|
||||
"host": "9.9.9.9",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"password": "",
|
||||
@@ -905,7 +905,7 @@ def test_admin_can_save_study_settings_in_one_request() -> None:
|
||||
assert one["monthly_target"] == 120
|
||||
assert one["study_start"] == "每月8日 09:10:11"
|
||||
assert one["course_workers"] == 2
|
||||
assert one["proxy"] == "HTTP 10.0.0.8:8080"
|
||||
assert one["proxy"] == "HTTP 9.9.9.9:8080"
|
||||
|
||||
|
||||
def test_admin_can_switch_hours_source() -> None:
|
||||
|
||||
@@ -545,27 +545,27 @@ def test_user_can_set_proxy_with_or_without_password() -> None:
|
||||
assert "password" not in empty.json()
|
||||
saved = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "http", "host": "10.0.0.1", "port": 8080, "username": "", "password": ""},
|
||||
json={"scheme": "http", "host": "1.1.1.1", "port": 8080, "username": "", "password": ""},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
assert saved.json() == {
|
||||
"enabled": True,
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.1",
|
||||
"host": "1.1.1.1",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"has_password": False,
|
||||
}
|
||||
with_auth = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "socks5", "host": "127.0.0.1", "port": 1080, "username": "u", "password": "p@ss"},
|
||||
json={"scheme": "socks5", "host": "8.8.8.8", "port": 1080, "username": "u", "password": "p@ss"},
|
||||
)
|
||||
assert with_auth.status_code == 200
|
||||
assert with_auth.json()["has_password"] is True
|
||||
assert "password" not in with_auth.json()
|
||||
kept = client.put(
|
||||
"/api/settings/proxy",
|
||||
json={"scheme": "socks5", "host": "127.0.0.1", "port": 1080, "username": "u", "password": None},
|
||||
json={"scheme": "socks5", "host": "8.8.8.8", "port": 1080, "username": "u", "password": None},
|
||||
)
|
||||
assert kept.status_code == 200
|
||||
assert kept.json()["has_password"] is True
|
||||
|
||||
@@ -705,7 +705,7 @@ def test_proxy_pool_scheme_enables_auto() -> None:
|
||||
assert view["enabled"] is True
|
||||
assert view["scheme"] == "pool"
|
||||
assert account.use_proxy_pool is True
|
||||
apply_proxy(account, "http", "10.0.0.1", 8080)
|
||||
apply_proxy(account, "http", "1.1.1.1", 8080)
|
||||
assert account.use_proxy_pool is False
|
||||
assert proxy_view(account)["scheme"] == "http"
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ from euniv.db import SessionLocal
|
||||
from euniv.models import Account, ProxyNode
|
||||
from euniv.proxy import apply as apply_proxy
|
||||
from euniv.proxy import bind_url, is_proxy_failure, view_of
|
||||
from euniv.proxy_pool import PROBE_URLS, acquire, mark_failed, page, probe, refresh_once, release
|
||||
from euniv.proxy_pool import PROBE_URL, acquire, mark_failed, page, probe, refresh_once, release
|
||||
from euniv.proxy_pool import _holds
|
||||
|
||||
INITIAL_PASSWORD = os.environ["EUNIV_ADMIN_PASSWORD"]
|
||||
@@ -127,13 +127,13 @@ def test_bind_url_uses_pool_or_manual() -> None:
|
||||
manual_user = db.get(Account, "manual-user")
|
||||
assert pool_user is not None and manual_user is not None
|
||||
apply_proxy(pool_user, "pool")
|
||||
apply_proxy(manual_user, "http", "127.0.0.1", 7890)
|
||||
apply_proxy(manual_user, "http", "1.1.1.1", 7890)
|
||||
db.commit()
|
||||
db.refresh(pool_user)
|
||||
db.refresh(manual_user)
|
||||
assert view_of(pool_user)["scheme"] == "pool"
|
||||
assert bind_url(pool_user) == "http://10.0.0.4:8004"
|
||||
assert bind_url(manual_user) == "http://127.0.0.1:7890"
|
||||
assert bind_url(manual_user) == "http://1.1.1.1:7890"
|
||||
apply_proxy(pool_user, "off")
|
||||
db.commit()
|
||||
db.refresh(pool_user)
|
||||
@@ -147,6 +147,7 @@ def test_refresh_marks_slow_and_keeps_fast(monkeypatch) -> None:
|
||||
lambda: [
|
||||
ProxyCandidate("http", "1.1.1.1", 80, "cn", "elite", "proxyhub"),
|
||||
ProxyCandidate("http", "2.2.2.2", 80, "cn", "elite", "proxyhub"),
|
||||
ProxyCandidate("http", "4.4.4.4", 80, "cn", "elite", "proxyhub"),
|
||||
ProxyCandidate("http", "3.3.3.3", 80, "us", "elite", "proxyhub"),
|
||||
],
|
||||
)
|
||||
@@ -159,7 +160,8 @@ def test_refresh_marks_slow_and_keeps_fast(monkeypatch) -> None:
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", fake_probe)
|
||||
payload = refresh_once()
|
||||
assert payload["alive"] == 1
|
||||
assert payload["dead"] == 1
|
||||
assert payload["dead"] == 2
|
||||
assert payload["message"] == ""
|
||||
with SessionLocal() as db:
|
||||
listed = page(db)
|
||||
hosts = {item["host"]: item for item in listed["items"]}
|
||||
@@ -240,13 +242,13 @@ def test_probe_averages_euniv_related_sites(monkeypatch) -> None:
|
||||
seen.append(url)
|
||||
return FakeResponse()
|
||||
|
||||
ticks = iter([0.0, 0.12, 0.12, 0.34, 0.34, 0.64])
|
||||
ticks = iter([0.0, 0.12])
|
||||
monkeypatch.setattr("euniv.proxy_pool.sync_httpx_client", FakeClient)
|
||||
monkeypatch.setattr("euniv.proxy_pool.time.perf_counter", lambda: next(ticks))
|
||||
ok, latency, reason = probe("http", "10.0.0.9", 8080)
|
||||
assert seen == list(PROBE_URLS)
|
||||
ok, latency, reason = probe("http", "1.1.1.1", 8080)
|
||||
assert seen == [PROBE_URL]
|
||||
assert ok is True
|
||||
assert latency == 213
|
||||
assert latency == 120
|
||||
assert reason == ""
|
||||
|
||||
|
||||
@@ -264,7 +266,7 @@ def test_probe_instant_connect_error_keeps_elapsed_latency(monkeypatch) -> None:
|
||||
def get(self, _url: str):
|
||||
raise httpx.ConnectError("boom")
|
||||
|
||||
ticks = iter([0.0, 0.012, 0.012, 0.018, 0.018, 0.025])
|
||||
ticks = iter([0.0, 0.008])
|
||||
monkeypatch.setattr("euniv.proxy_pool.sync_httpx_client", FakeClient)
|
||||
monkeypatch.setattr("euniv.proxy_pool.time.perf_counter", lambda: next(ticks))
|
||||
ok, latency, reason = probe("socks4", "10.0.0.9", 1080)
|
||||
|
||||
Reference in New Issue
Block a user