diff --git a/studio/auth.py b/studio/auth.py
index 7803ddf..3bcfde6 100644
--- a/studio/auth.py
+++ b/studio/auth.py
@@ -187,7 +187,7 @@ class AuthService:
raise ValueError("请输入学信网账号和密码")
if self.browser_cancel:
self.browser_cancel.set()
- with self.check_lock:
+ with self.check_lock, self.transport.router.hold_route():
response = self.transport.request("GET", LOGIN_URL)
soup = BeautifulSoup(response.text, "html.parser")
form = {node.get("name"): node.get("value", "") for node in soup.select("input[type=hidden][name]")}
diff --git a/studio/connectors.py b/studio/connectors.py
index 76a2377..52368b1 100644
--- a/studio/connectors.py
+++ b/studio/connectors.py
@@ -72,10 +72,11 @@ class CatalogClient:
raise
# A CAS cookie may still be valid while the catalogue session has expired.
# Refresh once, then retry exactly the same page; never skip its records.
- status = self.auth.check()
- if not status["valid"]:
- raise LoginRequired(status["message"], reason="captcha" if status["state"] == "captcha_required" else "login") from None
- return self._fetch_page(endpoint, payload)
+ with self.transport.router.hold_route():
+ status = self.auth.check()
+ if not status["valid"]:
+ raise LoginRequired(status["message"], reason="captcha" if status["state"] == "captcha_required" else "login") from None
+ return self._fetch_page(endpoint, payload)
def pages(self, endpoint, payload, page_size=10):
seen, start = set(), 0
diff --git a/studio/migrate.py b/studio/migrate.py
index 9cc7b3e..b70db3d 100644
--- a/studio/migrate.py
+++ b/studio/migrate.py
@@ -67,7 +67,7 @@ def main():
parser.add_argument('--config', type=Path, required=True)
args = parser.parse_args()
config = json.loads(args.config.read_text())
- print(json.dumps(migrate_sqlite(args.snapshot, MySQLDatabase(config)), ensure_ascii=False))
+ print(json.dumps(migrate_sqlite(args.snapshot, MySQLDatabase(config, recover_jobs=False)), ensure_ascii=False))
if __name__ == '__main__':
diff --git a/studio/mysql_database.py b/studio/mysql_database.py
index 0201444..c57df4a 100644
--- a/studio/mysql_database.py
+++ b/studio/mysql_database.py
@@ -96,7 +96,7 @@ class Connection:
class MySQLDatabase(Database):
- def __init__(self, config):
+ def __init__(self, config, recover_jobs=True):
self.config = {key: value for key, value in config.items() if key in ('host', 'port', 'user', 'password', 'database', 'unix_socket')}
if not self.config.get('database'):
raise ValueError('数据库配置缺少 database')
@@ -106,9 +106,10 @@ class MySQLDatabase(Database):
conn.execute(statement + ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin')
if not conn.execute("SHOW COLUMNS FROM jobs LIKE 'resume_pending'").fetchone():
conn.execute('ALTER TABLE jobs ADD COLUMN resume_pending TINYINT DEFAULT 0')
- recovery = "status IN ('running','queued') OR (status='paused' AND resume_pending=1)"
- self.recovered_job_ids = [row['id'] for row in conn.execute('SELECT id FROM jobs WHERE ' + recovery)]
- conn.execute("UPDATE jobs SET status='paused',resume_pending=0,message='服务重启,任务可从断点恢复' WHERE " + recovery)
+ if recover_jobs:
+ recovery = "status IN ('running','queued') OR (status='paused' AND resume_pending=1)"
+ self.recovered_job_ids = [row['id'] for row in conn.execute('SELECT id FROM jobs WHERE ' + recovery)]
+ conn.execute("UPDATE jobs SET status='paused',resume_pending=0,message='服务重启,任务可从断点恢复' WHERE " + recovery)
@contextmanager
def connection(self):
diff --git a/tests/test_mysql.py b/tests/test_mysql.py
index 8865338..54800fb 100644
--- a/tests/test_mysql.py
+++ b/tests/test_mysql.py
@@ -97,3 +97,14 @@ def test_migration_rolls_back_every_table_on_bad_source(mysql, tmp_path):
migrate_sqlite(tmp_path / 'bad.db', mysql)
assert mysql.overview()['records'] == 0
assert mysql.overview()['scores'] == 0
+
+
+def test_rejected_migration_cli_does_not_pause_destination_jobs(mysql, tmp_path):
+ import subprocess
+ import sys
+ source = Database(tmp_path / 'source.db')
+ mysql.job_create('existing-running-job', {'year': 2027})
+ mysql.job_update('existing-running-job', status='running')
+ result = subprocess.run([sys.executable, '-m', 'studio.migrate', str(source.path), '--config', os.environ['STUDIO_MYSQL_TEST_CONFIG']], capture_output=True, text=True)
+ assert result.returncode != 0
+ assert mysql.job_get('existing-running-job')['status'] == 'running'
diff --git a/tests/test_server_features.py b/tests/test_server_features.py
index faa481f..3b7f902 100644
--- a/tests/test_server_features.py
+++ b/tests/test_server_features.py
@@ -3,8 +3,10 @@ import random
import os
import pytest
-from studio.auth import CookieVault
-from tests.test_network import setup_network
+from studio.auth import CookieVault, AuthService, LOGIN_URL, CATALOG_ENTRY
+from studio.connectors import CatalogClient
+from studio.network import RemoteUnavailable
+from tests.test_network import setup_network, Remote, Response
def test_requests_sample_a_fresh_delay_inside_configured_bounds(tmp_path, monkeypatch):
@@ -45,6 +47,47 @@ def test_route_pin_defers_rotation_until_login_proof_finishes(tmp_path):
assert transport.request('GET', 'https://yz.chsi.com.cn/zsml/').json()['routed_through'] == 'http://5.6.7.8:9090'
+class RouteBoundSession(Remote):
+ def __init__(self):
+ super().__init__()
+ self.bound = set()
+ self.form_route = None
+
+ def request(self, method, url, **kwargs):
+ route = kwargs.get('proxies', {}).get('https')
+ if url == LOGIN_URL and method == 'GET':
+ self.form_route = route
+ return Response({}, text='')
+ if url == LOGIN_URL and method == 'POST':
+ if route != self.form_route:
+ raise RemoteUnavailable('login form and credentials used different routes')
+ return Response({})
+ if url == CATALOG_ENTRY:
+ self.bound = {route}
+ return Response({})
+ if url.endswith('/yjfxs.do'):
+ return Response({'flag': True, 'msg': {'list': [{'direction': 'confirmed'}]}}) if route in self.bound else Response({'flag': False, 'msg': '请登录'})
+ return super().request(method, url, **kwargs)
+
+
+@pytest.mark.parametrize('threshold', [1, 2])
+def test_auth_recovery_preserves_proven_route_through_catalog_retry(tmp_path, threshold):
+ settings, _, transport = setup_network(tmp_path, remote=RouteBoundSession())
+ settings.update({'proxy_rotate_every': threshold})
+ auth = AuthService(transport, CookieVault(tmp_path))
+ assert auth.check()['valid']
+ payload = CatalogClient(transport, auth).fetch_page('yjfxs', {'curPage': 2, 'start': 10, 'pageSize': 10})
+ assert payload['list'][0]['direction'] == 'confirmed'
+
+
+def test_credential_login_pins_form_submission_and_verification(tmp_path):
+ settings, _, transport = setup_network(tmp_path, remote=RouteBoundSession())
+ settings.update({'proxy_rotate_every': 2})
+ transport.request('GET', 'https://yz.chsi.com.cn/zsml/')
+ auth = AuthService(transport, CookieVault(tmp_path))
+ assert auth.login('test-user', 'test-password')['valid']
+
+
def test_linux_cookie_vault_encrypts_and_preserves_domain_cookies(tmp_path, monkeypatch):
monkeypatch.setattr(platform, 'system', lambda: 'Linux')
cookies = [{'name': 'JSESSIONID', 'value': 'private-account-cookie', 'domain': 'account.chsi.com.cn'},