main.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import os
  2. import os.path
  3. import sys
  4. import time
  5. import queue
  6. import argparse
  7. import tempfile
  8. import threading
  9. import subprocess
  10. from itertools import groupby
  11. import cv2
  12. import requests
  13. import bitstring
  14. from loguru import logger
  15. from Crypto.Hash import BLAKE2b
  16. try:
  17. import rdrand
  18. except ImportError:
  19. logger.warning("RdRand is not available.")
  20. try:
  21. from linuxpy.video.device import Device, VideoCapture
  22. except ImportError:
  23. logger.warning("linuxpy is not available.")
  24. tmp_dir = None
  25. push_timeout = 10
  26. class TemporaryFile:
  27. def __init__(self, name, io, delete):
  28. self.name = name
  29. self.__io = io
  30. self.__delete = delete
  31. def __getattr__(self, k):
  32. return getattr(self.__io, k)
  33. def __del__(self):
  34. if self.__delete:
  35. try:
  36. os.unlink(self.name)
  37. except FileNotFoundError:
  38. pass
  39. def NamedTemporaryFile(
  40. mode="w+b", bufsize=-1, suffix="", prefix="tmp", dir=None, delete=True
  41. ):
  42. if not dir:
  43. dir = tempfile.gettempdir()
  44. name = os.path.join(dir, prefix + os.urandom(32).hex() + suffix)
  45. if mode is None:
  46. return TemporaryFile(name, None, delete)
  47. fh = open(name, "w+b", bufsize)
  48. if mode != "w+b":
  49. fh.close()
  50. fh = open(name, mode)
  51. return TemporaryFile(name, fh, delete)
  52. def chunks(lst, n):
  53. for i in range(0, len(lst), n):
  54. yield lst[i : i + n]
  55. def run_check(cmd):
  56. logger.info(f"Executing '{cmd}'.")
  57. subprocess.check_output(
  58. cmd,
  59. stderr=subprocess.STDOUT,
  60. timeout=100
  61. )
  62. def extract_image(path):
  63. logger.info(f"Extract image '{path}'.")
  64. im = cv2.imread(path)
  65. data = []
  66. rows, cols, _ = im.shape
  67. for i in range(rows):
  68. for j in range(cols):
  69. r, g, b = im[i, j]
  70. data.extend((r, g, b))
  71. return bytes(data)
  72. def extract_wav(path):
  73. logger.info(f"Extract audio: '{path}'.")
  74. data = []
  75. with open(path, "rb") as f:
  76. for sample in chunks(f.read()[44:], 2):
  77. data.append(sample[0])
  78. return bytes(data)
  79. def extract_video(path):
  80. logger.info(f"Extract video: '{path}'.")
  81. with tempfile.TemporaryDirectory(dir=tmp_dir) as tmpd:
  82. run_check(
  83. f"ffmpeg -hide_banner -loglevel error -y -i {path} -vf mpdecimate -r 1/1 {tmpd}/%d.bmp"
  84. )
  85. data = b""
  86. for filename in sorted(
  87. os.listdir(tmpd), key=lambda filename: int(filename.split(".")[0])
  88. ):
  89. data += extract_image(os.path.join(tmpd, filename))
  90. return data
  91. def extract_lsbs(data):
  92. logger.info("Extract LSBs.")
  93. buffer = []
  94. data = [k for k, _ in groupby(data)]
  95. data = chunks(data, 8)
  96. for chunk in data:
  97. if len(chunk) != 8:
  98. break
  99. ba = bitstring.BitArray(8)
  100. for i, byte in zip(range(len(chunk)), chunk):
  101. ba[i] = byte & 1
  102. buffer.append(ba.u)
  103. return bytes(buffer)
  104. def whiten(data):
  105. logger.info("Whitening.")
  106. buffer = b""
  107. for chunk in chunks(data, 32):
  108. if len(chunk) < 32:
  109. break
  110. buffer += BLAKE2b.new(data=chunk, digest_bits=256).digest()
  111. return buffer
  112. def read_video(source, duration=60):
  113. tmpf = NamedTemporaryFile(suffix=".mkv", mode=None, dir=tmp_dir)
  114. run_check(
  115. f"ffmpeg -hide_banner -loglevel error -y -i {source} -t {duration} -acodec copy -vcodec copy {tmpf.name}"
  116. )
  117. return extract_video(tmpf.name)
  118. def read_audio(source, duration=60):
  119. tmpf = NamedTemporaryFile(suffix=".wav", mode=None, dir=tmp_dir)
  120. run_check(
  121. f"ffmpeg -hide_banner -loglevel error -y -f alsa -i {source} -t {duration} -ar 44100 -f s16le -acodec pcm_s16le {tmpf.name}"
  122. )
  123. return extract_wav(tmpf.name)
  124. def read_audio_video(source, duration=60):
  125. tmpf = NamedTemporaryFile(suffix=".mkv", mode=None, dir=tmp_dir)
  126. run_check(
  127. f"ffmpeg -hide_banner -loglevel error -y -i {source} -t {duration} -acodec copy -vcodec copy {tmpf.name}"
  128. )
  129. data_a = extract_video(tmpf.name)
  130. tmpf2 = NamedTemporaryFile(suffix=".wav", mode=None, dir=tmp_dir)
  131. run_check(
  132. f"ffmpeg -hide_banner -loglevel error -y -i {tmpf.name} -vn -ar 44100 -f s16le -acodec pcm_s16le {tmpf2.name}"
  133. )
  134. data_b = extract_wav(tmpf2.name)
  135. return bytes(a ^ b for a, b in zip(data_a, data_b))
  136. def read_rdseed(_, amount=16):
  137. data = rdrand.rdseed_get_bytes(amount)
  138. if len(data) != amount or data.count(0) == amount:
  139. raise ValueError("bad data")
  140. return data
  141. def sample(source, source_type, multiplier=1):
  142. match source_type:
  143. case "video":
  144. sampler = read_video
  145. multiplier *= 60
  146. case "audio":
  147. sampler = read_audio
  148. multiplier *= 60
  149. case "video+audio":
  150. sampler = read_audio_video
  151. multiplier *= 60
  152. case "rdseed":
  153. sampler = read_rdseed
  154. case _:
  155. raise ValueError(source_type)
  156. multiplier = int(multiplier)
  157. if multiplier < 1:
  158. raise ValueError(multiplier)
  159. logger.info("Sampling...")
  160. data = sampler(source, multiplier)
  161. logger.info(f"Sample ready: {len(data)}b.")
  162. if source_type != "rdseed":
  163. data = extract_lsbs(data)
  164. data = whiten(data)
  165. return data
  166. def video2_extractor(q, q2):
  167. while True:
  168. data = q2.get()
  169. data = chunks(data, 8)
  170. newdata = b""
  171. for chunk in data:
  172. newdata += bytes(a ^ b for a, b in zip(chunk[:4], chunk[4:]))
  173. data = extract_lsbs(newdata)
  174. data = whiten(data)
  175. logger.info(f"Sample ready: {len(data)}b.")
  176. q.put(data)
  177. def video2_sampler(q, q2, source):
  178. with Device.from_id(abs(int(source))) as device:
  179. device.set_format(
  180. 1,
  181. device.info.frame_sizes[0].width,
  182. device.info.frame_sizes[0].height,
  183. pixel_format="YUYV",
  184. )
  185. last = 0
  186. for frame in device:
  187. new = time.monotonic()
  188. if new - last > 10:
  189. q2.put(bytes(frame))
  190. last = new
  191. def push(pool_url, data, secret):
  192. logger.info(f"Pushing {len(data)}b.")
  193. resp = requests.post(
  194. f"{pool_url}/api/pool",
  195. data=data,
  196. headers={"X-Secret": secret},
  197. timeout=(push_timeout, push_timeout),
  198. )
  199. (logger.success if resp.status_code == 200 else logger.error)(
  200. f"{resp.status_code}: {resp.text}"
  201. )
  202. def puller(queue, source, source_type, multiplier, sample_interval):
  203. while True:
  204. try:
  205. data = sample(source, source_type, multiplier)
  206. except KeyboardInterrupt:
  207. logger.info("Interrupted by user.")
  208. sys.exit(0)
  209. except Exception as e:
  210. logger.error(f"Pull exception: {e}")
  211. if sample_interval:
  212. time.sleep(sample_interval)
  213. continue
  214. for piece in chunks(data, 1024 * 500):
  215. queue.put(piece)
  216. if sample_interval:
  217. time.sleep(sample_interval)
  218. def pusher(queue, pool_url, secret, cooldown=0):
  219. while True:
  220. piece = queue.get()
  221. try:
  222. push(pool_url, piece, secret)
  223. except KeyboardInterrupt:
  224. logger.info("Interrupted by user.")
  225. sys.exit(0)
  226. except Exception as e:
  227. logger.error(f"Push exception: {e}")
  228. queue.put(piece)
  229. if cooldown:
  230. time.sleep(cooldown)
  231. if __name__ == "__main__":
  232. parser = argparse.ArgumentParser()
  233. parser.add_argument("--source", type=str, required=True)
  234. parser.add_argument("--source-type", type=str, default="video+audio")
  235. parser.add_argument("--multiplier", type=float, default=1)
  236. parser.add_argument("--secret-file", type=str, default="./.secret")
  237. parser.add_argument("--cooldown", type=int, default=0)
  238. parser.add_argument("--sample-interval", type=int, default=0)
  239. parser.add_argument("--pool-url", type=str, default="https://yebi.su")
  240. parser.add_argument("--push-timeout", type=int, default=10)
  241. parser.add_argument("--tmp-dir", type=str)
  242. args = parser.parse_args()
  243. if args.tmp_dir and os.path.isdir(args.tmp_dir):
  244. tmp_dir = args.tmp_dir
  245. logger.info(f"Changed temp-dir: '{tmp_dir}'")
  246. push_timeout = max(args.push_timeout, 1)
  247. with open(args.secret_file, "r") as f:
  248. lines = f.read().strip().split("\n")
  249. ident = lines[0].strip()
  250. secret = lines[1].strip()
  251. secret = f"{ident} {secret}"
  252. q = queue.Queue()
  253. pusher_th = threading.Thread(
  254. target=pusher, args=(q, args.pool_url, secret, args.cooldown)
  255. )
  256. if args.source_type == "linuxvideo":
  257. q2 = queue.Queue()
  258. threading.Thread(target=video2_sampler, args=(q, q2, args.source)).start()
  259. threading.Thread(target=video2_extractor, args=(q, q2)).start()
  260. else:
  261. threading.Thread(
  262. target=puller, args=(q, args.source, args.source_type, args.multiplier, args.sample_interval)
  263. ).start()
  264. pusher_th = threading.Thread(
  265. target=pusher, args=(q, args.pool_url, secret, args.cooldown)
  266. )
  267. pusher_th.start()
  268. pusher_th.join()