import os
import re
import sys
import glob

def extract_strings(filename):
    with open(filename, 'rb') as f:
        data = f.read()
        
    # Protobuf data often has strings prefixed by length and tag, but we can just use regex 
    # to find human-readable text (messages) and URLs (media)
    
    # Extract URLs (photos, videos, voice notes)
    urls = re.findall(rb'https?://[^\s\x00-\x1f"\']+', data)
    
    # Extract possible text messages (sequences of printable characters)
    # This is a bit noisy, but filters out short garbage
    text_matches = re.findall(rb'[a-zA-Z0-9.,!?\'" ]{10,}', data)
    
    print(f"=== Analysis of {filename} ===")
    
    if urls:
        print("\n[Found Media/Links]:")
        for u in set(urls):
            print(f"  - {u.decode('utf-8', errors='ignore')}")
            
    if text_matches:
        print("\n[Possible Text Messages]:")
        for t in set(text_matches):
            text = t.decode('utf-8', errors='ignore').strip()
            # Filter out some common protobuf garbage and keys
            if len(text) > 5 and not text.startswith("https://") and not all(c in "0123456789abcdef-" for c in text):
                print(f"  - {text}")
                
    print("\n" + "="*40 + "\n")

if __name__ == "__main__":
    target = sys.argv[1] if len(sys.argv) > 1 else "intercept_grpc_*.bin"
    files = glob.glob(target)
    
    if not files:
        print(f"No files found matching {target}")
    else:
        for f in files:
            extract_strings(f)
