Skip to content

Instantly share code, notes, and snippets.

@emorozov
Created October 24, 2022 05:45
Show Gist options
  • Save emorozov/e791894ebd55f6569086da345b11bf23 to your computer and use it in GitHub Desktop.
Save emorozov/e791894ebd55f6569086da345b11bf23 to your computer and use it in GitHub Desktop.
Decode BitTorrent udp tracker announce
import sys
import struct
data = open(sys.argv[1], 'rb').read()
start = 0
client_id, action, transaction_id, infohash, peer_id, downloaded, left, uploaded, event, ip, key, num_want, port, extensions, s_len = (
struct.unpack('!QII20s20sQQQIIIiHHB', data)
)
print(f'{client_id=:x}, {action=}, {transaction_id=}, infohash={infohash.hex()}, peer_id={peer_id.decode("ascii")}')
print(f'{downloaded=}, {uploaded=}, {left=}, {event=}, {ip=}, {num_want=}, {port=}, extensions={bin(extensions)}')
print(f'{s_len=}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment