130 lines
8.5 KiB
Python
130 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Verify/publish the sensor-only APK sharing production diagnostics; no device use."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import urllib.request
|
|
import zipfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
TOOLS = Path('/home/ubuntu/android-build/sdk/build-tools/34.0.0')
|
|
CERT = 'b9608336bc1ed1de3269bb4594f9fe718daaa5439511c5187aa385d2b799dcf1'
|
|
SHARED = (
|
|
'CameraDiagnosticsActivity.kt', 'ImmersiveMode.kt', 'expression/CameraProbeController.kt',
|
|
'expression/CameraProbeState.kt', 'expression/CameraPreviewSurface.kt',
|
|
'expression/FaceDiagnostics.kt', 'expression/FaceCueRules.kt',
|
|
'expression/ExpressionRules.kt', 'expression/ExpressionState.kt',
|
|
'expression/PresenceRules.kt', 'dialogue/ReplyEmotion.kt',
|
|
'diag/DiagnosticLog.kt', 'diag/DiagnosticReportUpload.kt',
|
|
)
|
|
|
|
def require(ok, reason):
|
|
if not ok:
|
|
raise RuntimeError(reason)
|
|
|
|
def digest(path):
|
|
with path.open('rb') as stream:
|
|
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
|
|
|
def verify(apk):
|
|
badge = subprocess.check_output([str(TOOLS/'aapt'), 'dump', 'badging', str(apk)], text=True)
|
|
for value in ("name='com.gorios.lanvoice.cameraprobe'", "versionCode='6'",
|
|
"versionName='camera-probe-0.1.5'", "sdkVersion:'26'", "targetSdkVersion:'34'"):
|
|
require(value in badge, 'APK metadata: '+value)
|
|
permissions = set(re.findall(r"^uses-permission: name='([^']+)'", badge, re.M))
|
|
require(permissions == {'android.permission.CAMERA', 'android.permission.INTERNET',
|
|
'com.gorios.lanvoice.cameraprobe.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION'},
|
|
'unexpected permissions')
|
|
native = re.search(r'^native-code: (.+)$', badge, re.M)
|
|
require(native is not None and native[1].strip() == "'arm64-v8a'", 'unexpected native ABI')
|
|
manifest = subprocess.check_output([str(TOOLS/'aapt'), 'dump', 'xmltree', str(apk), 'AndroidManifest.xml'], text=True)
|
|
services = re.findall(r'^ E: service .*?(?=^ E:|\Z)', manifest, re.M | re.S)
|
|
worker = next((s for s in services if '"com.gorios.lanvoice.CameraProbeWorkerService"' in s), '')
|
|
require('android:exported(0x01010010)=(type 0x12)0x0' in worker and
|
|
'android:process(0x01010011)=":camera_probe_worker"' in worker, 'worker not privately process-isolated in actual APK')
|
|
cert = subprocess.check_output([str(TOOLS/'apksigner'), 'verify', '--verbose', '--print-certs', str(apk)], text=True)
|
|
require(re.search(r'Signer #1 certificate SHA-256 digest: (\w+)', cert)[1] == CERT, 'debug certificate drift')
|
|
with zipfile.ZipFile(apk) as archive:
|
|
paths = archive.namelist()
|
|
require(not any(n.startswith('assets/') for n in paths), 'camera APK must contain no avatar/model assets')
|
|
libraries = {n for n in paths if n.startswith('lib/')}
|
|
require(libraries == {'lib/arm64-v8a/libandroidx.graphics.path.so',
|
|
'lib/arm64-v8a/libimage_processing_util_jni.so',
|
|
'lib/arm64-v8a/libsurface_util_jni.so'}, 'unexpected native dependency')
|
|
dex = b''.join(archive.read(n) for n in paths if re.fullmatch(r'classes\d*\.dex', n))
|
|
for mode in ('CAMERA1_SYSTEM_1080_PREVIEW','CAMERA1_SYSTEM_1080_CALLBACK'):
|
|
require(mode.encode() in dex, 'system-parameter mode missing: '+mode)
|
|
for klass in ('MainActivity', 'VoiceSession', 'ExpressionCameraManager',
|
|
'ContinuousBodyRenderer', 'EmotionActionAvatarView'):
|
|
require(('Lcom/gorios/lanvoice/'+klass+';').encode() not in dex and
|
|
('Lcom/gorios/lanvoice/avatar/'+klass+';').encode() not in dex and
|
|
('Lcom/gorios/lanvoice/expression/'+klass+';').encode() not in dex,
|
|
'unexpected app component: '+klass)
|
|
require(b'Lcom/google/mediapipe/' not in dex, 'face model dependency included')
|
|
for klass in ('Lcom/gorios/lanvoice/CameraDiagnosticsActivity;',
|
|
'Lcom/gorios/lanvoice/CameraProbeLauncherActivity;',
|
|
'Lcom/gorios/lanvoice/IsolatedCameraDiagnosticsActivity;',
|
|
'Lcom/gorios/lanvoice/CameraProbeWorkerService;',
|
|
'Lcom/gorios/lanvoice/IsolatedProbeSuite;',
|
|
'Lcom/gorios/lanvoice/ProbeAvailabilityMonitor;',
|
|
'Lcom/gorios/lanvoice/expression/CameraProbeController;',
|
|
'Lcom/gorios/lanvoice/diag/DiagnosticReportUpload;'):
|
|
require(klass.encode() in dex, 'missing production diagnostic component: '+klass)
|
|
staged = ROOT/'camera-probe/build/generated/sharedCameraSource'
|
|
require({str(p.relative_to(staged)) for p in staged.rglob('*.kt')} == set(SHARED), 'shared source whitelist drift')
|
|
sources = {}
|
|
for name in SHARED:
|
|
source = ROOT/'app/src/main/java/com/gorios/lanvoice'/name
|
|
require((staged/name).read_bytes() == source.read_bytes(), 'shared source drift: '+name)
|
|
sources[name] = digest(source)
|
|
controller = (ROOT/'app/src/main/java/com/gorios/lanvoice/expression/CameraProbeController.kt').read_text()
|
|
require(not re.search(r'\bpreviewFrameRate\s*=', controller), 'deprecated FPS setter must not coexist with range API')
|
|
trust = ROOT/'camera-probe/build/generated/cameraTrust/res/raw/gateway_cert.pem'
|
|
require(trust.read_bytes() == (ROOT/'app/src/main/res/raw/gateway_cert.pem').read_bytes(), 'gateway trust drift')
|
|
camera_sources = {str(p.relative_to(ROOT)): digest(p) for p in sorted((ROOT/'camera-probe/src').rglob('*')) if p.is_file()}
|
|
return {'package': 'com.gorios.lanvoice.cameraprobe', 'version_name': 'camera-probe-0.1.5',
|
|
'version_code': 6, 'min_sdk': 26, 'target_sdk': 34, 'certificate_sha256': CERT,
|
|
'abi': 'arm64-v8a', 'assets': 0, 'microphone_permission': False,
|
|
'shared_production_sources': sources, 'camera_application_sources': camera_sources,
|
|
'camera_tests': 2, 'planned_modes': ['camera1_system_1080_preview','camera1_system_1080_callback'], 'requested_preview': {'width':1920,'height':1080,'format':'nv21','fps_min_milli':30000,'fps_max_milli':30000}, 'fps_api': 'preview_fps_range_only', 'process_isolated': True,
|
|
'run_limit_ms': 5000, 'close_limit_ms': 2000, 'process_exit_limit_ms': 4000, 'availability_limit_ms': 15000, 'surface_resize_handshake': True,
|
|
'availability_is_streaming_proof': False, 'system_three_streams_reproduced': False, 'universal_third_party_restriction_confirmed': False, 'device': 'not_run'}
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--output', type=Path, required=True, help='Fresh ignored delivery JSON path')
|
|
parser.add_argument('--build', action='store_true')
|
|
args = parser.parse_args()
|
|
output = args.output.resolve()
|
|
require(not output.exists() and output.is_relative_to(ROOT/'logs'), 'fresh ignored evidence path required')
|
|
if args.build:
|
|
subprocess.run([str(ROOT/'scripts/build.sh'), ':camera-probe:assembleDebug'], cwd=ROOT, check=True)
|
|
built = ROOT/'camera-probe/build/outputs/apk/debug/camera-probe-debug.apk'
|
|
report = verify(built)
|
|
artifact = ROOT/'artifacts/lan-voice-camera-probe-v015-code6-debug.apk'
|
|
require(not artifact.exists() and not artifact.with_suffix('.apk.sha256').exists(), 'published camera version occupied')
|
|
with artifact.open('xb') as target, built.open('rb') as source:
|
|
shutil.copyfileobj(source, target)
|
|
sha = digest(artifact)
|
|
checksum = f'{sha} {artifact.name}\n'.encode()
|
|
with artifact.with_suffix('.apk.sha256').open('xb') as target:
|
|
target.write(checksum)
|
|
url = 'http://192.168.31.124:8080/'+artifact.name
|
|
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
with opener.open(url, timeout=30) as response:
|
|
require(response.status == 200 and hashlib.sha256(response.read()).hexdigest() == sha, 'HTTP APK bytes differ')
|
|
with opener.open(url+'.sha256', timeout=15) as response:
|
|
require(response.status == 200 and response.read() == checksum, 'HTTP checksum differs')
|
|
report.update(status='camera_diagnostic_only', url=url, sha256=sha, bytes=artifact.stat().st_size,
|
|
http_bytes_verified=True, paid_calls=0, model_calls=0)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(json.dumps(report, indent=2)+'\n')
|
|
print(json.dumps({k:v for k,v in report.items() if k not in ('shared_production_sources','camera_application_sources')}, indent=2))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|