kioubit-dn42-v6-canvas-send/gen1.py
2023-03-10 16:10:49 +08:00

108 lines
3 KiB
Python

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
import pathlib
import json
if pathlib.Path('1.png').exists():
from PIL import Image
img = Image.open('1.png')
img = img.resize((X, Y), Image.Resampling.LANCZOS)
img.save('out.png')
img = img.convert('RGBA')
imgd = []
for x in range(X):
imgd.append([])
for y in range(Y):
r, g, b, a = img.getpixel((x, y))
imgd[-1].append((r, g, b, a))
# with open('1.json', 'w') as f:
# json.dump(imgd, f)
# input('pil')
else:
print('use 1.json')
with open('1.json', 'r') as f:
imgd = json.load(f)
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
skip = int(sys.argv[1]) if sys.argv[1:] else 0
slp = float(sys.argv[2]) if sys.argv[2:] else 0.0001
for x in range(X):
if x < skip:
continue
print(f"{x=}")
for y in range(Y):
r, g, b, a = imgd[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)