kioubit-dn42-v6-canvas-send/gen2.py

163 lines
4.3 KiB
Python
Raw Normal View History

2023-03-10 16:10:49 +08:00
import struct
class ICMP6:
_ICMP_ECHO_REQUEST = 128
def __init__(self, destination, id, sequence, payload=None,
payload_size=56, ttl=64, traffic_class=0):
if payload is not None:
payload_size = len(payload)
else:
payload = b'\x00'*payload_size
self._destination = destination
self._id = id & 0xffff
self._sequence = sequence & 0xffff
self._payload = payload
self._payload_size = payload_size
self._ttl = ttl
self._traffic_class = traffic_class
self._time = 0
def _checksum(self, data):
'''
Compute the checksum of an ICMP packet. Checksums are used to
verify the integrity of packets.
'''
sum = 0
data += b'\x00'
for i in range(0, len(data) - 1, 2):
sum += (data[i] << 8) + data[i + 1]
sum = (sum & 0xffff) + (sum >> 16)
sum = ~sum & 0xffff
return sum
def _create_packet(self):
'''
Build an ICMP packet from an identifier, a sequence number and
a payload.
This method returns the newly created ICMP header concatenated
to the payload passed in parameters.
'''
id, sequence, payload = self._id, self._sequence, self._payload
checksum = 0
# Temporary ICMP header to compute the checksum
header = struct.pack('!2B3H', self._ICMP_ECHO_REQUEST, 0, checksum,
id, sequence)
checksum = self._checksum(header + payload)
# Definitive ICMP header
header = struct.pack('!2B3H', self._ICMP_ECHO_REQUEST, 0, checksum,
id, sequence)
return header + payload
X=512
Y=512
import socket, traceback, time
from PIL import Image, ImageOps
img = Image.open('1.png')
img = img.resize((X, Y), Image.Resampling.LANCZOS)
img.save('out.png')
img = img.convert('RGBA')
imgdvd = Image.open('dvd.png')
imgdvd = imgdvd.convert('RGBA')
def send_icmp(addr):
try:
with socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_ICMPV6) as s:
p = ICMP6(None, 0xad, 0, b'')._create_packet()
s.sendto(p, (addr, 0))
except Exception:
traceback.print_exc()
import sys, random
skip = int(sys.argv[1]) if sys.argv[1:] else 0
slp = float(sys.argv[2]) if sys.argv[2:] else 0.0001
slp1 = float(sys.argv[3]) if sys.argv[3:] else 0.5
draw_img = int(sys.argv[4]) if sys.argv[4:] else 0
Dx, Dy = imgdvd.size
sx = random.randint(0, X-1-Dx)
sy = random.randint(0, Y-1-Dy)
fx = random.choice((1, -1))
fy = random.choice((1, -1))
def draw(img):
for x in range(X):
if x < skip:
continue
print(f"{x=}")
for y in range(Y):
r, g, b, a = img.getpixel((x, y))
if a:
send_icmp(f"fdcf:8538:9ad5:3333:{x:x}:{y:x}:11{r:02x}:{g:02x}{b:02x}")
#print(f"fdcf:8538:9ad5:3333:{x:x}:{y:x}:11{r:02x}:{g:02x}{b:02x}")
time.sleep(slp)
D_RGB = [0, 0, 0]
imgdvdc = None
def update_drgb():
global imgdvdc
D_RGB.clear()
D_RGB.extend([random.randint(50, 250), random.randint(50, 250), random.randint(50, 250)])
imgdvdc = imgdvd.copy()
for x in range(Dx):
for y in range(Dy):
_, _, _, a = imgdvd.getpixel((x, y))
imgdvdc.putpixel((x, y), (*D_RGB, a))
update_drgb()
old1 = Image.new(mode="RGBA", size=(Dx, Dy))
oldsxy = [0, 0]
def blend():
global old1
new = img.crop((sx, sy, sx+Dx, sy+Dy))
old1_old = old1.copy()
#old1 = Image.new(mode="RGBA", size=(X, Y))
old1 = new.copy()
#old1.paste(old, (sx, sy))
new.paste(imgdvdc, (0, 0), imgdvdc)
new1 = Image.new(mode="RGBA", size=(X, Y))
#new1 = img.copy()
new1.paste(old1_old, oldsxy.copy(), old1_old)
new1.paste(new, (sx, sy), new)
oldsxy[0], oldsxy[1] = sx, sy
#new1.save('/tmp/2.png')
#time.sleep(0.5)
return new1
STEP=5
if draw_img:
draw(img)
while True:
imgf = blend()
draw(imgf)
if fx > 0:
if sx + Dx >= X:
fx = -fx
update_drgb()
else:
if sx <= 0:
fx = -fx
update_drgb()
if fy > 0:
if sy + Dy >= Y:
fy = -fy
update_drgb()
else:
if sy <= 0:
fy = -fy
update_drgb()
sx += STEP * fx
sy += STEP * fy
time.sleep(slp1)