Advertisement
Guest User

router.lua

a guest
Apr 9th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.25 KB | None | 0 0
  1. local lstSides = {"left","right","top","bottom","front","back"}
  2. local DHCP = {}
  3. local PacketLog = {}
  4. local RouterPassword = "pass1"
  5. local exitting = false
  6. function OpenAll()
  7.     for i, side in pairs(lstSides) do
  8.         if (peripheral.isPresent(side)) then
  9.            if (peripheral.getType(side) == string.lower("modem")) then
  10.                 if not rednet.isOpen(side) then
  11.                     rednet.open(side)
  12.                 else
  13.                 end
  14.            end
  15.         end
  16.     end
  17. end
  18. function MainListener()
  19.     while exitting == false do
  20.         local id, msg, protocol = rednet.receive()
  21.         if protocol == "FireFlyISP" then
  22.             if string.find(msg, "|") then
  23.                 if CheckTable(PacketLog, msg)==false then
  24.                     if sep == nil then
  25.                         sep = "|"
  26.                     end--end if sep is null
  27.                     sep = tostring(sep)
  28.                     local Args={} ; i=1
  29.                     for str in string.gmatch(msg, "([^"..sep.."]+)") do
  30.                         Args[i] = str
  31.                         i = i + 1
  32.                     end--end for
  33.                     MyPacketID = Args[1]
  34.                     Payload = decode(Args[2])
  35.                     Args={} ; i=1
  36.                     for str in string.gmatch(Payload, "([^"..sep.."]+)") do
  37.                         Args[i] = str
  38.                         i = i + 1
  39.                     end--end for
  40.                     MyToID = Args[1]
  41.                     MyFromID = Args[2]
  42.                     MyProtocol = Args[3]
  43.                     MyData = Args[4]
  44.                     if CheckTable(DHCP, MyToID) then
  45.                         rednet.send(tonumber(MyToID), Payload)
  46.                     else
  47.                     end
  48.                     table.insert(PacketLog, msg)
  49.                 end
  50.             end
  51.            
  52.         else
  53.             if string.lower(msg) == "exit" then
  54.                 print("Exiting router")
  55.                 exitting = true
  56.             else
  57.                 if string.find(msg, "|") then
  58.                     --|Send|RHost|LHost|Data|
  59.                     if sep == nil then
  60.                         sep = "|"
  61.                     end--end if sep is null
  62.                     sep = tostring(sep)
  63.                     local Args={} ; i=1
  64.                     for str in string.gmatch(msg, "([^"..sep.."]+)") do
  65.                         Args[i] = str
  66.                         i = i + 1
  67.                     end--end for
  68.                     if string.lower(Args[1])=="ping" then
  69.                         rednet.send(id, "|PONG|"..os.getComputerID().."|", protocol)
  70.                     elseif string.lower(Args[1])=="join" then
  71.                         if (#Args == 2) then
  72.                             code1 = Args[2]
  73.                             code1 = code1:gsub('{PIPE}','|')
  74.                             if code1 == RouterPassword then
  75.                                 if CheckTable(DHCP, id) == false then
  76.                                     table.insert(DHCP, id)
  77.                                     print(id.." Joined the network")
  78.                                 else
  79.                                     print("Failed Validation, Already joined: "..id)
  80.                                 end
  81.                                 rednet.send(id, "200",protocol)
  82.                             else
  83.                                 rednet.send(id, "403",protocol)
  84.                             end
  85.  
  86.                         end
  87.                     elseif string.lower(Args[1])=="leave" then
  88.                         if CheckTable(DHCP, id)  then
  89.                             --table.remove(DHCP, id)
  90.                             RemoveFromTable(DHCP, id)
  91.                             print(id.." Left the network")
  92.                         else
  93.                             print("Failed Validation already removed: "..id)                        
  94.                         end
  95.                         rednet.send(id, "200",protocol)
  96.                     elseif string.lower(Args[1])=="send" then
  97.                         --|ToID|FromID|protocol|Packet|
  98.                         --|Send|ToID|Prot|Pack|
  99.                         if CheckTable(DHCP, id) then
  100.                             if(#Args==4) then
  101.                                 Payload = "|"..Args[2].."|"..id.."|"..Args[3].."|"..Args[4].."|"
  102.                                 if CheckTable(DHCP, Args[2]) then
  103.                                     rednet.send(tonumber(Args[2]), Payload)
  104.                                     rednet.send(id, "200",protocol)
  105.                                 else
  106.                                     Payload = "|"..getStr(15).."|"..encode(Payload).."|"--|PacketID|Packet|
  107.                                     rednet.broadcast(Payload,"FireFlyISP")
  108.                                     table.insert(PacketLog, Payload)
  109.                                     rednet.send(id, "200",protocol)
  110.                                 end
  111.                                
  112.                             end
  113.                         else
  114.                             rednet.send(id, "403",protocol)
  115.                         end
  116.                     end--end if arg0 equals
  117.                 end--end of if contains pipe
  118.             end--end if not exit
  119.         end--end if protocol fireflyisp
  120.     end--end while
  121. end--end function
  122.  
  123. function getStr(lenght)
  124.     str = ""
  125.     for i=1, lenght do str = str..string.char(math.random(1, 255)) end
  126.     if string.find(str, "|") then
  127.         str = str:gsub('|','f')
  128.     end
  129.     return str
  130.   end
  131. local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  132.  
  133.  
  134. function CheckTable(Table1, CompareData)
  135.     var1 = false
  136.     if #Table1 > 0 then
  137.         for i=1, #Table1 do
  138.             if Table1[i] ~= nil then
  139.                 if Table1[i].."" == ""..CompareData then
  140.                     return true
  141.                 end
  142.             end
  143.             i=i + 1
  144.         end
  145.     end
  146.     return var1
  147. end--end function
  148.  
  149. function RemoveFromTable(Table1, RemoveData)
  150.     NewTable = {}
  151.     if #Table1 > 0 then
  152.         for i=1, #Table1 do
  153.             if Table1[i] ~= nil then
  154.                 if Table1[i].."" == ""..RemoveData then
  155.                     table.remove(Table1, i)
  156.                 end
  157.             end
  158.             i=i + 1
  159.         end
  160.     end
  161. end--end function
  162. -- Encoding
  163. function encode(data)
  164.     return ((data:gsub('.', function(x)
  165.         local r,b='',x:byte()
  166.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  167.         return r;
  168.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  169.         if (#x < 6) then return '' end
  170.         local c=0
  171.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  172.         return b:sub(c+1,c+1)
  173.     end)..({ '', '==', '=' })[#data%3+1])
  174. end
  175.  
  176. -- Decoding
  177. function decode(data)
  178.     data = string.gsub(data, '[^'..b..'=]', '')
  179.     return (data:gsub('.', function(x)
  180.         if (x == '=') then return '' end
  181.         local r,f='',(b:find(x)-1)
  182.         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  183.         return r;
  184.     end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  185.         if (#x ~= 8) then return '' end
  186.         local c=0
  187.         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  188.         return string.char(c)
  189.     end))
  190. end
  191.  
  192. OpenAll()
  193. MainListener()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement