mirror of
https://github.com/Astatin3/ip-hilbert.git
synced 2026-06-09 00:18:01 -06:00
commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import sys
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.colors as colors
|
||||
|
||||
def hilbert_xy(ip_int, order=16):
|
||||
"""Convert IP integer to (x,y) on a Hilbert curve of given order."""
|
||||
x = y = 0
|
||||
for i in range(order):
|
||||
xi = (ip_int >> (2 * i)) & 1
|
||||
yi = (ip_int >> (2 * i + 1)) & 1
|
||||
|
||||
if yi == 0:
|
||||
if xi == 1:
|
||||
x = (1 << order) - 1 - x
|
||||
y = (1 << order) - 1 - y
|
||||
x, y = y, x
|
||||
|
||||
x += xi << i
|
||||
y += yi << i
|
||||
|
||||
return x, y
|
||||
|
||||
def ip_to_int(ip):
|
||||
return int(''.join([bin(int(x) + 256)[3:] for x in ip.split('.')]), 2)
|
||||
|
||||
def create_hilbert_points(ip_list, order=16):
|
||||
points = []
|
||||
for ip in ip_list:
|
||||
ip_int = ip_to_int(ip)
|
||||
x, y = hilbert_xy(ip_int, order)
|
||||
points.append((x, y))
|
||||
return points
|
||||
|
||||
def visualize_hilbert_points(ip_list, order=16, grid_size=32):
|
||||
points = create_hilbert_points(ip_list, order)
|
||||
x, y = zip(*points)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 12))
|
||||
|
||||
# Create density grid
|
||||
H, xedges, yedges = np.histogram2d(x, y, bins=grid_size, range=[[0, 2**order], [0, 2**order]])
|
||||
|
||||
# Plot heatmap
|
||||
im = ax.imshow(H.T, cmap='YlOrRd', extent=[0, 2**order, 0, 2**order], origin='lower',
|
||||
norm=colors.LogNorm(vmin=1, vmax=H.max()))
|
||||
|
||||
# Plot points
|
||||
ax.scatter(x, y, color='blue', s=2, alpha=0.5)
|
||||
|
||||
ax.set_xlim(0, 2 ** order)
|
||||
ax.set_ylim(0, 2 ** order)
|
||||
|
||||
plt.title(f"IP Addresses on Hilbert Curve (order {order}) with Density Heatmap")
|
||||
plt.colorbar(im, label='Number of IPs')
|
||||
plt.axis('off')
|
||||
plt.show()
|
||||
|
||||
if __name__ == '__main__':
|
||||
ip_list = open(sys.argv[1]).read().splitlines()
|
||||
visualize_hilbert_points(ip_list)
|
||||
@@ -0,0 +1,37 @@
|
||||
def inverse_hilbert_xy(x, y, order=16):
|
||||
"""Convert (x, y) on a Hilbert curve of given order to IP integer."""
|
||||
ip_int = 0
|
||||
for i in range(order - 1, -1, -1):
|
||||
xi = (x >> i) & 1
|
||||
yi = (y >> i) & 1
|
||||
|
||||
if yi == 0:
|
||||
if xi == 1:
|
||||
x, y = (1 << order) - 1 - y, (1 << order) - 1 - x
|
||||
x, y = y, x
|
||||
|
||||
ip_int |= (xi << (2 * i)) | (yi << (2 * i + 1))
|
||||
|
||||
return ip_int
|
||||
|
||||
|
||||
def ip_int_to_ip(ip_int):
|
||||
"""Convert an integer IP address to a string IP address."""
|
||||
return ".".join(str((ip_int >> i) & 0xFF) for i in [24, 16, 8, 0])
|
||||
|
||||
def generate_ip_ranges(size=16, order=16):
|
||||
# for x in range(size):
|
||||
# for y in range(size):
|
||||
# x_pos = int((x/size)*(2 ** order)/2)
|
||||
# y_pos = int((y/size)*(2 ** order)/2)
|
||||
# ip = inverse_hilbert_xy(x_pos, y_pos, order=order)
|
||||
# print(ip_int_to_ip(ip))
|
||||
|
||||
for x in range(256):
|
||||
for y in range(256):
|
||||
print(str(x)+"."+str(y)+".255.255")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
generate_ip_ranges()
|
||||
@@ -0,0 +1,4 @@
|
||||
with open("./ips_1.txt") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
print(line.split("/")[1].split(":")[0].rstrip())
|
||||
Reference in New Issue
Block a user