Advertisement
Patriik

Untitled

Mar 6th, 2022
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. local ipapi = {
  2.     currentIP = "0.0.0.0",
  3.     spoofMode = false,
  4.     gateway = "",
  5.     isIpRenewRequested = false,
  6.     gatewayMac = "",
  7.     debug=true,
  8.     currentMac = require("component").modem.address
  9. }
  10.  
  11.  
  12. local component = require("component")
  13. local modem = component.modem
  14. local ser = require("serialization")
  15. local event = require("event")
  16.  
  17. function ipapi.newPacket(type, payload, receiver, receiverMac, port)
  18.     local packet = {}
  19.  
  20.     packet.senderIp = ipapi.currentIP
  21.     packet.senderMac = require("computer").address()
  22.     packet.toIP = receiver or "255.255.255.255"
  23.     packet.payload = payload or ""
  24.     packet.toMac = receiverMac
  25.     packet.port = port or 255
  26.     packet.type = type
  27.  
  28.     function packet:send()
  29.         packet.send = nil
  30.         if ipapi.gatewayMac == "" then
  31.             if packet.toMac ~= nil then
  32.                 modem.send(packet.toMac, packet.port, "ipPacket", ser.serialize(packet))
  33.             else
  34.                 modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
  35.             end
  36.         else
  37.             if packet.toIP == "255.255.255.255" then
  38.                 modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
  39.             else
  40.                 modem.send(ipapi.gatewayMac, 255, "ipPacket", ser.serialize(packet))
  41.             end
  42.         end
  43.     end
  44.  
  45.     return packet
  46. end
  47.  
  48. function ipapi.renewIP()
  49.     if ipapi.debug then
  50.         print("Renewing IP")
  51.     end
  52.     ipapi.isIpRenewRequested = true
  53.     local packet = ipapi.newPacket("DHCPDiscover")
  54.     packet:send()
  55. end
  56.  
  57. event.listen("modem_message", function(...)
  58.     local _, _, _, _, _, msg, packet = ...
  59.     if msg == "ipPacket" then
  60.         local packet = ser.unserialize(packet)
  61.  
  62.         if packet.senderIp == ipapi.currentIP or (packet.toIP ~= ipapi.currentIP and ipapi.spoofMode == false and packet.toIP ~= "255.255.255.255") then
  63.             return
  64.         end
  65.  
  66.         event.push("ip_message", packet.senderIp, packet.senderMac, packet.type, packet.payload, packet)
  67.  
  68.         if packet.type == "DHCPOffer" and ipapi.isIpRenewRequested then
  69.             if ipapi.debug then
  70.                 print("Got DHCPOffer")
  71.             end
  72.             local offeredIp = packet.payload.ip
  73.             local offeredGateway = packet.payload.gateway
  74.             local offeredDNS = packet.payload.dns
  75.  
  76.             local packet = ipapi.newPacket("DHCPRequest", {
  77.                 ip = ipapi.currentIP,
  78.             }, "255.255.255.255", packet.senderMac)
  79.  
  80.         end
  81.         if packet.type == "DHCPACK" then
  82.             if ipapi.debug then
  83.                 print("DHCP acknowledged. new ip set.")
  84.             end
  85.             ipapi.currentIP = packet.payload.ip
  86.             ipapi.gateway = packet.payload.gateway
  87.             ipapi.isIpRenewRequested = false
  88.             event.push("new_ip", ipapi.currentIP, ipapi.gatewayMac)
  89.         end
  90.         if packet.type == "pong" then
  91.             if ipapi.debug then
  92.                 print("Got pong")
  93.             end
  94.  
  95.         end
  96.         if packet.type == "ping" then
  97.             if ipapi.debug then
  98.                 print("Got ping")
  99.             end
  100.             local packet = ipapi.newPacket("pong", nil, packet.senderIp)
  101.             packet:send()
  102.         end
  103.         if packet.type == "ARPRequest" then
  104.             if ipapi.debug then
  105.                 print("Got ARPRequest")
  106.             end
  107.             local requestedIp = packet.payload.ip
  108.  
  109.             if requestedIp == ipapi.currentIP then
  110.                 local packet = ipapi.newPacket("ARPReply", {
  111.                     ip = ipapi.currentIP,
  112.                     mac = ipapi.currentMac
  113.                 }, packet.senderIp == "0.0.0.0" and "255.255.255.255" or packet.senderIp)
  114.                 packet:send()
  115.             end
  116.         end
  117.  
  118.  
  119.  
  120.     end
  121. end)
  122.  
  123. if modem.isOpen(255) == false then
  124.     modem.open(255)
  125. end
  126.  
  127. return ipapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement