Advertisement
Patriik

Untitled

Mar 6th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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. print(packet.port, "ipPacket", ser.serialize(packet))
  35. modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
  36. end
  37. else
  38. if packet.toIP == "255.255.255.255" then
  39. modem.broadcast(packet.port, "ipPacket", ser.serialize(packet))
  40. else
  41. modem.send(ipapi.gatewayMac, 255, "ipPacket", ser.serialize(packet))
  42. end
  43. end
  44. end
  45.  
  46. return packet
  47. end
  48.  
  49. function ipapi.renewIP()
  50. if ipapi.debug then
  51. print("Renewing IP")
  52. end
  53. ipapi.isIpRenewRequested = true
  54. local packet = ipapi.newPacket("DHCPDiscover")
  55. packet:send()
  56. end
  57.  
  58. event.listen("modem_message", function(...)
  59. local _, _, _, _, _, msg, packet = ...
  60. if msg == "ipPacket" then
  61. local packet = ser.unserialize(packet)
  62.  
  63. if packet.senderIp == ipapi.currentIP or (packet.toIP ~= ipapi.currentIP and ipapi.spoofMode == false) or packet.toIP == "255.255.255.255" then
  64. return
  65. end
  66. print("Received packet from " .. packet.senderIp .. " to " .. packet.toIP..", payload: "..packet.payload)
  67.  
  68. event.push("ip_message", packet.senderIp, packet.senderMac, packet.type, packet.payload, packet)
  69.  
  70. if packet.type == "DHCPOffer" and ipapi.isIpRenewRequested then
  71. if ipapi.debug then
  72. print("Got DHCPOffer")
  73. end
  74. local offeredIp = packet.payload.ip
  75. local offeredGateway = packet.payload.gateway
  76. local offeredDNS = packet.payload.dns
  77.  
  78. local packet = ipapi.newPacket("DHCPRequest", {
  79. ip = ipapi.currentIP,
  80. }, "255.255.255.255", packet.senderMac)
  81.  
  82. end
  83. if packet.type == "DHCPACK" then
  84. if ipapi.debug then
  85. print("DHCP acknowledged. new ip set.")
  86. end
  87. ipapi.currentIP = packet.payload.ip
  88. ipapi.gateway = packet.payload.gateway
  89. ipapi.isIpRenewRequested = false
  90. event.push("new_ip", ipapi.currentIP, ipapi.gatewayMac)
  91. end
  92. if packet.type == "pong" then
  93. if ipapi.debug then
  94. print("Got pong")
  95. end
  96.  
  97. end
  98. if packet.type == "ping" then
  99. if ipapi.debug then
  100. print("Got ping")
  101. end
  102. local packet = ipapi.newPacket("pong", nil, packet.senderIp)
  103. packet:send()
  104. end
  105. if packet.type == "ARPRequest" then
  106. if ipapi.debug then
  107. print("Got ARPRequest")
  108. end
  109. local requestedIp = packet.payload.ip
  110.  
  111. if requestedIp == ipapi.currentIP then
  112. local packet = ipapi.newPacket("ARPReply", {
  113. ip = ipapi.currentIP,
  114. mac = ipapi.currentMac
  115. }, packet.senderIp == "0.0.0.0" and "255.255.255.255" or packet.senderIp)
  116. packet:send()
  117. end
  118. end
  119.  
  120.  
  121.  
  122. end
  123. end)
  124.  
  125. if modem.isOpen(255) == false then
  126. modem.open(255)
  127. end
  128.  
  129. return ipapi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement