CH-J Tools Mobile透過 SSH 管理伺服器
選單
已發布的原始碼

CH-J Tools Mobile

瀏覽特定應用程式版本的目錄與檔案。

下載原始碼 ZIP
CH-J / CH-J Tools Mobile 175 個檔案
sm-mobile-20261016T185235Z-8e93ecc1b59a 新增時間: · 建置時間:
test_openssh.py 新增時間: 未記錄日期 3.3 KB · 54 行
程式碼 下載檔案
1#!/usr/bin/env python3
2"""Real, unprivileged loopback-only OpenSSH fixture on macOS/Linux. No Docker required."""
3import argparse,base64,getpass,json,os,pathlib,shutil,socket,subprocess,tempfile,time
4root=pathlib.Path(__file__).resolve().parents[1]
5p=argparse.ArgumentParser()
6p.add_argument('--device',help='Flutter device id; omitted runs host network tests')
7p.add_argument('--android',action='store_true',help='Use emulator host address 10.0.2.2')
8p.add_argument('--integration',default='integration_test/device_test.dart',help='Integration test file for --device')
9p.add_argument('--define',action='append',default=[],help='Extra NAME=VALUE dart define for --device')
10args=p.parse_args()
11sshd=shutil.which('sshd') or '/usr/sbin/sshd'
12# StrictModes requires a non-world-writable parent, unlike /tmp on macOS.
13cache=pathlib.Path.home()/'.cache/sm-mobile-tests';cache.mkdir(parents=True,exist_ok=True);cache.chmod(0o700)
14with tempfile.TemporaryDirectory(prefix='openssh-',dir=cache) as temporary:
15 fixture=pathlib.Path(temporary)
16 for name,phrase in [('host_key',''),('client_key','isolated-test-passphrase')]:
17 subprocess.run(['ssh-keygen','-q','-t','ed25519','-N',phrase,'-f',str(fixture/name)],check=True)
18 shutil.copy(fixture/'client_key.pub',fixture/'authorized_keys')
19 (fixture/'authorized_keys').chmod(0o600)
20 (fixture/'remote').mkdir()
21 (fixture/'sshd_config').write_text(f'''Port 22229
22ListenAddress 127.0.0.1
23HostKey {fixture}/host_key
24PidFile {fixture}/pid
25AuthorizedKeysFile {fixture}/authorized_keys
26StrictModes yes
27PubkeyAuthentication yes
28PasswordAuthentication no
29KbdInteractiveAuthentication no
30UsePAM no
31AllowUsers {getpass.getuser()}
32Subsystem sftp internal-sftp
33LogLevel ERROR
34''')
35 with (fixture/'server.log').open('w') as log:
36 server=subprocess.Popen([sshd,'-D','-e','-f',str(fixture/'sshd_config')],stdout=log,stderr=log)
37 try:
38 time.sleep(.5)
39 if server.poll() is not None: raise SystemExit((fixture/'server.log').read_text())
40 with socket.create_connection(('127.0.0.1',22229),timeout=3): pass
41 env={**os.environ,'SM_OPENSSH_TEST':'1','SM_TEST_KEY':str(fixture/'client_key'),'SM_TEST_REMOTE_DIR':str(fixture/'remote')}
42 if args.device:
43 fingerprint=subprocess.check_output(['ssh-keygen','-lf',str(fixture/'host_key.pub'),'-E','sha256'],text=True).split()[1]
44 defines={'SM_TEST_KEY_B64':base64.b64encode((fixture/'client_key').read_bytes()).decode(),
45 'SM_TEST_FINGERPRINT':fingerprint,'SM_TEST_USER':getpass.getuser(),'SM_TEST_HOST':'10.0.2.2' if args.android else '127.0.0.1'}
46 defines.update(d.split('=',1) for d in args.define)
47 (fixture/'defines.json').write_text(json.dumps(defines));(fixture/'defines.json').chmod(0o600)
48 command=['flutter','test',args.integration,'-d',args.device,'--dart-define-from-file='+str(fixture/'defines.json')]
49 else: command=['flutter','test','test/network/openssh_test.dart','test/network/sftp_editor_test.dart','test/network/adaptive_session_test.dart','--reporter','expanded']
50 subprocess.run(command,cwd=root,env=env,check=True)
51 finally:
52 server.terminate()
53 try: server.wait(timeout=5)
54 except subprocess.TimeoutExpired: server.kill();server.wait()
SHA-256cdad20ba3f5d99081d8eaa45d6f531813715cb5d0a39f368c4d6386352bc4065

CH-J Proprietary Software License 1.14 · 原始碼依 CH-J Proprietary Software License 1.14 提供。公開原始碼不會改變授權條款,也不會授予額外的權利。

壓縮檔 SHA-25646e427a43e87af091004f36145590bda8fe90086dfb0f631afeb5029fb0b5655
檔案