refactor: extract decryption auto-detection into detect.py#41
Merged
Conversation
Move the AES-CTR counter-source / AES-128-EAX period-exponent auto-detect and EID-type classification logic out of cli.py into a new, CLI-free hubblenetwork.detect module. - detect.py: detect_eid_type() plus per-scan CtrCounterModeDetector and EaxExponentDetector stateful objects. Detection returns (result, label); the module imports no click and never prints. The CLI supplies the packet-bound decrypt_fn and emits the [INFO] Detected: line when a label is set, keeping all presentation in cli.py. - cli.py: 4 call sites (ble detect, ble scan, sat scan, ble validate) updated to construct/use detectors; ~160 lines removed. - tests: new tests/test_detect.py with direct unit coverage of the detect/cache/label logic plus an AST decoupling guard; relocate the detect_eid_type unit tests out of test_ble_validate.py and retarget its decrypt patches to hubblenetwork.detect. No user-facing behavior change.
6465f8d to
3b3ac7d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
cli.pyhad grown to ~3500 lines, with a cluster of decryption-strategyhelpers ("given a key and a packet, figure out which counter mode / EAX period
exponent decrypts it, cache that, and decrypt") fused with presentation
(
click.secho("[INFO] Detected: …")). That's domain logic, not commandplumbing, and it had no direct test coverage — only indirect coverage through
full CLI scan invocations.
This extracts that logic into a new
src/hubblenetwork/detect.pythat isfully decoupled from the CLI: it imports no
clickand never prints. Detectionreturns what happened (
Detection(result, label)); the CLI decides whetherand how to present it.
No user-facing behavior change.
What's in
detect.pydetect_eid_type(key, pkts)— pure EID-rotation classifier (UNIX_TIME /DEVICE_UPTIME / AMBIGUOUS), renamed public.
CtrCounterModeDetector/EaxExponentDetector— per-scan stateful objectsthat own the detect/sweep loop + cache. Their
decrypt()takes acaller-supplied packet-bound
decrypt_fnand returnsDetection;labelisset only on the first successful detection of a scan.
cache_key(Nonedisables caching for EID-lesspackets); satellite omits it and shares one per-stream slot via a private
_SINGLE_STREAMsentinel — no magic strings leak into the CLI.The CLI builds the
decrypt_fn(referencingcli.decrypt/cli.decrypt_eax,so existing test mocks still apply) and emits the
[INFO] Detected:line via asmall
_announce_detection()helper.cli.pyshrinks by ~160 lines.Tests
tests/test_detect.py: direct unit coverage of the detect/cache/labellogic (sweep order, per-EID caching, single-stream caching, label-once,
wrong-key,
cache_key=None, zero-byte-payload-is-success, non-auto bypass),the
detect_eid_typeclassifications, an AST decoupling guard assertingno
clickimport, and theDetectiondefault.detect_eid_typeunit tests relocated out oftest_ble_validate.py; thatfile's
decryptpatches retargeted tohubblenetwork.detect.test_cli_ctr_autodetect.py,test_sat_decrypt.py, andtest_aes_eax.pyassertions unchanged.Verification
ruff check src— cleanpytest -m "not integration and not ble and not docker"— 229 passedgrep -rn "click" src/hubblenetwork/detect.py— empty