import socket
import json

# Server configuration
SERVER_HOST = '127.0.0.1'
SERVER_PORT = 65432

# Function to send DNS lookup requests
def dns_lookup(domain):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
        client_socket.connect((SERVER_HOST, SERVER_PORT))
        client_socket.sendall(domain.encode())
        response = client_socket.recv(1024)
        return response.decode()

# Function to send DNS update requests
def update_dns_record(domain, record_type, record_data):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
        client_socket.connect((SERVER_HOST, SERVER_PORT))
        request_data = {
            "action": "update",
            "domain": domain,
            "type": record_type,
            "data": record_data
        }
        client_socket.sendall(json.dumps(request_data).encode())
        response = client_socket.recv(1024)
        return response.decode()

# Main client function
def run_client():
    while True:
        print("\n1. DNS Lookup")
        print("2. Update DNS Record")
        print("3. Quit")
        choice = input("Enter your choice: ")

        if choice == '1':
            domain = input("Enter domain name: ")
            result = dns_lookup(domain)
            print(f"DNS lookup result for {domain}:\n{result}")
        elif choice == '2':
            domain = input("Enter domain name: ")
            record_type = input("Enter record type (A/AAAA/MX/NS/SRV/TXT/CNAME): ")
            record_data = input("Enter record data: ")
            result = update_dns_record(domain, record_type, record_data)
            print(f"Response from server: {result}")
        elif choice == '3':
            break
        else:
            print("Invalid choice")

if __name__ == "__main__":
    run_client()

