86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
import socket
|
|
import time
|
|
|
|
LISTEN_IP = "0.0.0.0"
|
|
PORT = 4444
|
|
|
|
CLIENT_CALLSIGN = "N0CALL-7"
|
|
|
|
configured = False
|
|
lease_expiration = None
|
|
|
|
def parse_message(data):
|
|
parts = data.split('|')
|
|
return parts
|
|
|
|
def build_request(network_name):
|
|
return f"0.1|CRAP_REQUEST|{CLIENT_CALLSIGN}|{network_name}"
|
|
|
|
def apply_network_config(assigned_ip, gateway, dns, lease_time):
|
|
global configured, lease_expiration
|
|
print("\n[Network Configuration]")
|
|
print(f" Bringing up interface: ax0")
|
|
print(f" Assigned IP address: {assigned_ip}")
|
|
print(f" Default Gateway: {gateway}")
|
|
print(f" DNS Server: {dns}")
|
|
print(f" Lease Time: {lease_time} seconds")
|
|
print(" Interface ax0 configured successfully! 🚀")
|
|
print("----------------------------------------")
|
|
|
|
configured = True
|
|
lease_expiration = time.time() + int(lease_time)
|
|
|
|
def main():
|
|
global configured, lease_expiration
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
sock.bind((LISTEN_IP, PORT))
|
|
|
|
print(f"Client {CLIENT_CALLSIGN} listening for CRAPRNIAC beacons...")
|
|
|
|
while True:
|
|
# Check lease expiration
|
|
if configured and lease_expiration and time.time() >= lease_expiration:
|
|
print("\n[Lease Expired]")
|
|
print(" Tearing down interface ax0...")
|
|
print(" Ready to rejoin network.")
|
|
print("----------------------------------------")
|
|
configured = False
|
|
lease_expiration = None
|
|
|
|
data, addr = sock.recvfrom(1024)
|
|
decoded = data.decode('utf-8')
|
|
parts = parse_message(decoded)
|
|
|
|
if len(parts) < 2:
|
|
continue
|
|
|
|
if parts[1] == "CRAP_BEACON":
|
|
if not configured:
|
|
base_callsign = parts[2]
|
|
network_name = parts[3]
|
|
print(f"\nHeard beacon from {base_callsign} ({addr}):")
|
|
print(f" Network: {network_name}")
|
|
print("Sending join request...")
|
|
request = build_request(network_name).encode('utf-8')
|
|
sock.sendto(request, addr)
|
|
|
|
elif parts[1] == "CRAP_ACCEPT":
|
|
client_callsign = parts[2]
|
|
network_name = parts[3]
|
|
assigned_ip = parts[4]
|
|
gateway = parts[5]
|
|
dns = parts[6]
|
|
lease_time = parts[7]
|
|
print(f"\nReceived CRAP_ACCEPT:")
|
|
print(f" Assigned IP: {assigned_ip}")
|
|
print(f" Gateway: {gateway}")
|
|
print(f" DNS Server: {dns}")
|
|
print(f" Lease Time: {lease_time} seconds")
|
|
|
|
apply_network_config(assigned_ip, gateway, dns, lease_time)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|