- 增加信令访问令牌、HTTPS 校验、限流及跨网段中继连接修复 - 同步三端媒体 v2 协议,实现 AES-GCM、防重放和乱序分片重组,升级 Rust ABI 6 - 修复文件鉴权、权限协商及剪贴板越权,完善上传校验和串流超时处理 - 增加跨端回归测试及部署文档,在 AGENTS.md 记录真机与真实跨网段待验收项目
159 lines
7.6 KiB
Python
159 lines
7.6 KiB
Python
"""Real Go server + Rust FFI auth/ECDH/encrypted-control smoke test.
|
|
|
|
python signaling_server/tests/relay_smoke.py --server PATH --core PATH
|
|
Uses only Python's standard library; does not require Android or open any UI.
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import ctypes as c
|
|
import json
|
|
import os
|
|
import secrets
|
|
import socket
|
|
import subprocess
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--server', required=True)
|
|
parser.add_argument('--core', required=True)
|
|
args = parser.parse_args()
|
|
with socket.socket() as probe:
|
|
probe.bind(('127.0.0.1', 0))
|
|
port = probe.getsockname()[1]
|
|
token = secrets.token_hex(32)
|
|
base = f'http://127.0.0.1:{port}'
|
|
env = dict(os.environ, SIGNALING_TOKEN=token, LISTEN_ADDR=f'127.0.0.1:{port}', TLS_CERT_FILE='', TLS_KEY_FILE='')
|
|
process = subprocess.Popen([os.path.abspath(args.server)], env=env, stdout=subprocess.DEVNULL,
|
|
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
|
lib = None
|
|
channel = 0
|
|
def request(path, body=None, authenticated=True):
|
|
data = None if body is None else json.dumps(body).encode()
|
|
headers = {'Content-Type': 'application/json'}
|
|
if authenticated:
|
|
headers['Authorization'] = f'Bearer {token}'
|
|
req = urllib.request.Request(base + path, data=data, headers=headers)
|
|
# Ignore the developer machine's outbound HTTP proxy for loopback tests.
|
|
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=3) as response:
|
|
return json.load(response)
|
|
try:
|
|
for _ in range(50):
|
|
try:
|
|
request('/healthz', authenticated=False)
|
|
break
|
|
except (OSError, urllib.error.URLError):
|
|
time.sleep(.1)
|
|
try:
|
|
request('/list', authenticated=False)
|
|
raise AssertionError('anonymous access accepted')
|
|
except urllib.error.HTTPError as error:
|
|
assert error.code == 401
|
|
request('/register', {'deviceId': 'smoke-receiver', 'name': 'smoke', 'ip': '127.0.0.1',
|
|
'controlPort': 41001, 'mediaPort': 41000, 'fp': 'test-fingerprint'})
|
|
lib = c.CDLL(os.path.abspath(args.core))
|
|
def bind(name, restype, *argtypes):
|
|
func = getattr(lib, name)
|
|
func.restype, func.argtypes = restype, argtypes
|
|
return func
|
|
ptr, size, i64 = c.c_void_p, c.c_size_t, c.c_int64
|
|
start = bind('ttstd_relay_start', c.c_int32, ptr, size, ptr, size, ptr, size)
|
|
status = bind('ttstd_demo_status', c.c_int32, ptr, size)
|
|
generate = bind('ttstd_ecdh_generate', i64, ptr, size)
|
|
agree = bind('ttstd_ecdh_agree', c.c_int32, i64, ptr, size, ptr, size)
|
|
derive = bind('ttstd_derive_subkey', c.c_int32, ptr, size, ptr, size, ptr, size)
|
|
new_channel = bind('ttstd_control_channel_new', i64, ptr)
|
|
ping = bind('ttstd_control_encode_ping', c.c_int32, i64, c.c_uint16, c.c_uint64, ptr, size)
|
|
decode = bind('ttstd_control_decode', c.c_int32, i64, ptr, size, ptr, size)
|
|
media_encode = bind('ttstd_media_encode', c.c_int32, c.c_uint64, c.c_uint8, ptr, ptr, size, ptr, size)
|
|
aes_encrypt = bind('ttstd_aes_encrypt', c.c_int32, ptr, ptr, ptr, size, ptr, size, ptr, size)
|
|
video_stats = bind('ttstd_video_stats', c.c_int32, ptr, size)
|
|
bind('ttstd_control_channel_free', None, i64)
|
|
bind('ttstd_relay_stop', None)
|
|
output = c.create_string_buffer(8192)
|
|
endpoint, receiver = f'{base}#token={token}'.encode(), b'smoke-receiver'
|
|
count = start(endpoint, len(endpoint), receiver, len(receiver), output, len(output))
|
|
assert json.loads(output.raw[:count])['ok']
|
|
hello = None
|
|
for _ in range(100):
|
|
for message in request('/relay/poll?deviceId=smoke-receiver')['messages']:
|
|
if message['payload']['kind'] == 'hs':
|
|
hello = message['payload']
|
|
if hello:
|
|
break
|
|
time.sleep(.05)
|
|
assert hello, 'Rust hello did not reach registered receiver'
|
|
public = c.create_string_buffer(65)
|
|
handle = generate(public, 65)
|
|
assert handle > 0
|
|
remote = base64.b64decode(hello['pub'])
|
|
secret = c.create_string_buffer(32)
|
|
assert agree(handle, remote, len(remote), secret, 32) == 32
|
|
key = c.create_string_buffer(32)
|
|
label = b'lancast/control/v1'
|
|
assert derive(secret, 32, label, len(label), key, 32) == 32
|
|
channel = new_channel(key)
|
|
def send(payload):
|
|
return request('/relay', {'from': 'smoke-receiver', 'to': hello['from'], 'payload': payload})
|
|
send({'ttstd': 'relay/1', 'kind': 'hsAck', 'from': 'smoke-receiver',
|
|
'pub': base64.b64encode(public.raw).decode()})
|
|
for _ in range(100):
|
|
count = status(output, len(output))
|
|
if json.loads(output.raw[:count])['keyEstablished']:
|
|
break
|
|
time.sleep(.05)
|
|
else:
|
|
raise AssertionError('Rust did not establish relay keys')
|
|
count = ping(channel, 1, 123456, output, len(output))
|
|
assert count > 0
|
|
send({'ttstd': 'relay/1', 'kind': 'frame', 'from': 'smoke-receiver',
|
|
'data': base64.b64encode(output.raw[:count]).decode()})
|
|
for _ in range(100):
|
|
for message in request('/relay/poll?deviceId=smoke-receiver')['messages']:
|
|
payload = message['payload']
|
|
if payload['kind'] != 'frame':
|
|
continue
|
|
frame = base64.b64decode(payload['data'])
|
|
count = decode(channel, frame, len(frame), output, len(output))
|
|
if count > 0 and json.loads(output.raw[:count])['kind'] == 'pong':
|
|
video_key = c.create_string_buffer(32)
|
|
label = b'lancast/video/v1'
|
|
assert derive(secret, 32, label, len(label), video_key, 32) == 32
|
|
sample = b'\x00\x00\x00\x01\x65\x01\x02'
|
|
count = media_encode(123456, 1, video_key, sample, len(sample), output, len(output))
|
|
assert count > 0
|
|
packet = output.raw[:count]
|
|
nonce, aad = secrets.token_bytes(12), b'relay-media'
|
|
count = aes_encrypt(video_key, nonce, packet, len(packet), aad, len(aad), output, len(output))
|
|
assert count > 0
|
|
encrypted = nonce + output.raw[:count]
|
|
send({'ttstd': 'relay/1', 'kind': 'media', 'from': 'smoke-receiver',
|
|
'video': [base64.b64encode(encrypted).decode()], 'audio': []})
|
|
for _ in range(100):
|
|
count = video_stats(output, len(output))
|
|
if json.loads(output.raw[:count])['received'] >= 1:
|
|
print('PASS: anonymous rejected; authenticated registration, Rust/Go ECDH, encrypted ping/pong and AES-GCM relay media')
|
|
return
|
|
time.sleep(.05)
|
|
raise AssertionError('encrypted media did not reach Rust frame queue')
|
|
time.sleep(.05)
|
|
raise AssertionError('encrypted pong did not return to receiver')
|
|
finally:
|
|
if lib:
|
|
lib.ttstd_relay_stop()
|
|
if channel:
|
|
lib.ttstd_control_channel_free(channel)
|
|
process.terminate()
|
|
try:
|
|
process.wait(timeout=5)
|
|
except subprocess.TimeoutExpired:
|
|
process.kill()
|
|
process.wait()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|