Advertisement
einsteinK

CCraft - SciTE Link

Dec 8th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.45 KB | None | 0 0
  1.  
  2. --[[encryption]]--
  3.  
  4. local chars = {}
  5. for i=0,9 do
  6.     table.insert(chars,48+i)
  7. end
  8. for i=0,25 do
  9.     table.insert(chars,65+i)
  10.     table.insert(chars,97+i)
  11. end
  12.  
  13. local key = {}
  14. if fs.exists("rn.key") and not fs.isDir("rn.key") then
  15.     local f = fs.open("rn.key","r")
  16.     f = f.readAll(),f.close()
  17.     for c in f:gmatch(".") do
  18.         table.insert(key,c:byte())
  19.     end
  20. else
  21.     for i=1,128 do
  22.         table.insert(key,chars[math.random(#chars)])
  23.     end
  24.     local f = fs.open("rn.key","w")
  25.     for k,v in pairs(key) do
  26.         f.write(string.char(v))
  27.     end f.close()
  28. end
  29.  
  30. local function r()
  31.     local res = {}
  32.     for i=1,math.random(5,10) do
  33.         table.insert(res,string.char(chars[math.random(#chars)]))
  34.     end return table.concat(res)
  35. end
  36.  
  37. local hex = {[0]=0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"}
  38.  
  39. local floor = math.floor
  40. function encrypt(str)
  41.     local i = 1
  42.     str = r().."\1\2\3"..str
  43.     return str:gsub(".",function(c)
  44.         c = (c:byte() + key[i])%256
  45.         i = i % #key + 1
  46.         return hex[floor(c/16)]..hex[c%16]
  47.     end)
  48. end
  49.  
  50. local dec = {
  51.     A=10,B=11,C=12,D=13,E=14,F=15
  52. }
  53. for i=0,10 do
  54.     dec[tostring(i)] = i
  55. end
  56.  
  57. function decrypt(str)
  58.     local i = 1
  59.     local pre,msg = str:gsub("(.)(.)",function(a,b)
  60.         if not dec[a] then print("ERR: ",a," ",a:byte()) return end
  61.         if not dec[b] then print("ERR: ",b," ",b:byte()) return end
  62.         return string.char(dec[a]*16+dec[b])
  63.     end):gsub(".",function(c)
  64.         c = c:byte() - key[i]
  65.         i = i % #key + 1
  66.         return string.char(c%256)
  67.     end):match("^(%w+)\1\2\3(.*)$")
  68.     if not pre then return nil end
  69.     if #pre < 5 or #pre > 10 then
  70.         return nil
  71.     end return msg
  72. end
  73.  
  74. --[[reddernet]]--
  75.  
  76. local rednet = rednet
  77. if rednet.reddernet then
  78.     error("RedderNet already ran")
  79. end
  80.  
  81. pcall(os.loadAPI,"encryption")
  82. local encryption = encryption
  83.  
  84. reddernet = getfenv()
  85.  
  86. CHANNEL_BROADCAST = rednet.CHANNEL_BROADCAST
  87. CHANNEL_REPEAT = rednet.CHANNEL_REPEAT
  88.  
  89. local tReceivedMessages = {}
  90. local tReceivedMessageTimeouts = {}
  91. local tHostnames = {}
  92.  
  93. function open( sModem )
  94.     if type( sModem ) ~= "string" then
  95.         error( "expected string", 2 )
  96.     end
  97.     if peripheral.getType( sModem ) ~= "modem" then
  98.         error( "No such modem: "..sModem, 2 )
  99.     end
  100.     peripheral.call( sModem, "open", os.getComputerID() )
  101.     peripheral.call( sModem, "open", CHANNEL_BROADCAST )
  102. end
  103.  
  104. function close( sModem )
  105.     if sModem then
  106.         -- Close a specific modem
  107.         if type( sModem ) ~= "string" then
  108.             error( "expected string", 2 )
  109.         end
  110.         if peripheral.getType( sModem ) ~= "modem" then
  111.             error( "No such modem: "..sModem, 2 )
  112.         end
  113.         peripheral.call( sModem, "close", os.getComputerID() )
  114.         peripheral.call( sModem, "close", CHANNEL_BROADCAST )
  115.     else
  116.         -- Close all modems
  117.         for n,sModem in ipairs( peripheral.getNames() ) do
  118.             if isOpen( sModem ) then
  119.                 close( sModem )
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. function isOpen( sModem )
  126.     if sModem then
  127.         -- Check if a specific modem is open
  128.         if type( sModem ) ~= "string" then
  129.             error( "expected string", 2 )
  130.         end
  131.         if peripheral.getType( sModem ) == "modem" then
  132.             return peripheral.call( sModem, "isOpen", os.getComputerID() ) and peripheral.call( sModem, "isOpen", CHANNEL_BROADCAST )
  133.         end
  134.     else
  135.         -- Check if any modem is open
  136.         for n,sModem in ipairs( peripheral.getNames() ) do
  137.             if isOpen( sModem ) then
  138.                 return true
  139.             end
  140.         end
  141.     end
  142.     return false
  143. end
  144.  
  145. local encrypted = true
  146. function setEncrypted(bool)
  147.     encrypted = bool
  148. end
  149.  
  150. function send( nRecipient, message, sProtocol )
  151.     -- Generate a (probably) unique message ID
  152.     -- We could do other things to guarantee uniqueness, but we really don't need to
  153.     -- Store it to ensure we don't get our own messages back
  154.     local nMessageID = math.random( 1, 2147483647 )
  155.     tReceivedMessages[ nMessageID ] = true
  156.     tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID
  157.  
  158.     -- Create the message
  159.     local nReplyChannel = os.getComputerID()
  160.     local tMessage = {
  161.         nMessageID = nMessageID,
  162.         nRecipient = nRecipient,
  163.         message = encrypted and encryption.encrypt(message) or message,
  164.         sProtocol = sProtocol,
  165.         reddernet = true,
  166.         encrypted = encrypted
  167.     }
  168.  
  169.     if nRecipient == os.getComputerID() then
  170.         -- Loopback to ourselves
  171.         os.queueEvent( "rednet_message", nReplyChannel, message, sProtocol )
  172.  
  173.     else
  174.         -- Send on all open modems, to the target and to repeaters
  175.         local sent = false
  176.         for n,sModem in ipairs( peripheral.getNames() ) do
  177.             if isOpen( sModem ) then
  178.                 peripheral.call( sModem, "transmit", nRecipient, nReplyChannel, tMessage );
  179.                 peripheral.call( sModem, "transmit", CHANNEL_REPEAT, nReplyChannel, tMessage );
  180.                 sent = true
  181.             end
  182.         end
  183.     end
  184. end
  185.  
  186. function broadcast( message, sProtocol )
  187.     send( CHANNEL_BROADCAST, message, sProtocol )
  188. end
  189.  
  190. function receive( sProtocolFilter, nTimeout )
  191.     -- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
  192.     if type(sProtocolFilter) == "number" and nTimeout == nil then
  193.         sProtocolFilter, nTimeout = nil, sProtocolFilter
  194.     end
  195.  
  196.     -- Start the timer
  197.     local timer = nil
  198.     local sFilter = nil
  199.     if nTimeout then
  200.         timer = os.startTimer( nTimeout )
  201.         sFilter = nil
  202.     else
  203.         sFilter = "rednet_message"
  204.     end
  205.  
  206.     -- Wait for events
  207.     while true do
  208.         local sEvent, p1, p2, p3 = os.pullEvent( sFilter )
  209.         if sEvent == "rednet_message" then
  210.             -- Return the first matching rednet_message
  211.             local nSenderID, message, sProtocol = p1, p2, p3
  212.             if sProtocolFilter == nil or sProtocol == sProtocolFilter then
  213.                 return nSenderID, message, sProtocol
  214.             end
  215.         elseif sEvent == "timer" then
  216.             -- Return nil if we timeout
  217.             if p1 == timer then
  218.                 return nil
  219.             end
  220.         end
  221.     end
  222. end
  223.  
  224. function host( sProtocol, sHostname )
  225.     if type( sProtocol ) ~= "string" or type( sHostname ) ~= "string" then
  226.         error( "expected string, string", 2 )
  227.     end
  228.     if sHostname == "localhost" then
  229.         error( "Reserved hostname", 2 )
  230.     end
  231.     if tHostnames[ sProtocol ] ~= sHostname then
  232.         if lookup( sProtocol, sHostname ) ~= nil then
  233.             error( "Hostname in use", 2 )
  234.         end
  235.         tHostnames[ sProtocol ] = sHostname
  236.     end
  237. end
  238.  
  239. function unhost( sProtocol )
  240.     if type( sProtocol ) ~= "string" then
  241.         error( "expected string", 2 )
  242.     end
  243.     tHostnames[ sProtocol ] = nil
  244. end
  245.  
  246. function lookup( sProtocol, sHostname )
  247.     if type( sProtocol ) ~= "string" then
  248.         error( "expected string", 2 )
  249.     end
  250.  
  251.     -- Build list of host IDs
  252.     local tResults = nil
  253.     if sHostname == nil then
  254.         tResults = {}
  255.     end
  256.  
  257.     -- Check localhost first
  258.     if tHostnames[ sProtocol ] then
  259.         if sHostname == nil then
  260.             table.insert( tResults, os.getComputerID() )
  261.         elseif sHostname == "localhost" or sHostname == tHostnames[ sProtocol ] then
  262.             return os.getComputerID()
  263.         end
  264.     end
  265.  
  266.     if not isOpen() then
  267.         if tResults then
  268.             return table.unpack( tResults )
  269.         end
  270.         return nil
  271.     end
  272.  
  273.     -- Broadcast a lookup packet
  274.     broadcast( {
  275.         sType = "lookup",
  276.         sProtocol = sProtocol,
  277.         sHostname = sHostname,
  278.     }, "dns" )
  279.  
  280.     -- Start a timer
  281.     local timer = os.startTimer( 2 )
  282.  
  283.     -- Wait for events
  284.     while true do
  285.         local event, p1, p2, p3 = os.pullEvent()
  286.         if event == "rednet_message" then
  287.             -- Got a rednet message, check if it's the response to our request
  288.             local nSenderID, tMessage, sMessageProtocol = p1, p2, p3
  289.             if sMessageProtocol == "dns" and tMessage.sType == "lookup response" then
  290.                 if tMessage.sProtocol == sProtocol then
  291.                     if sHostname == nil then
  292.                         table.insert( tResults, nSenderID )
  293.                     elseif tMessage.sHostname == sHostname then
  294.                         return nSenderID
  295.                     end
  296.                 end
  297.             end
  298.         else
  299.             -- Got a timer event, check it's the end of our timeout
  300.             if p1 == timer then
  301.                 break
  302.             end
  303.         end
  304.     end
  305.     if tResults then
  306.         return table.unpack( tResults )
  307.     end
  308.     return nil
  309. end
  310.  
  311. local function clean(str)
  312.     if type(str) ~= "string" then return str end
  313.     return str:gsub("[^a-zA-Z0-9]","?")
  314. end
  315.  
  316. local done = setmetatable({},{__mode="k"})
  317. local pullRaw = coroutine.yield
  318. local function pull(filter)
  319.     local hmm = {pullRaw(filter)}
  320.     local ev = hmm[1]
  321.     if ev == "modem_message" then
  322.         local msg = hmm[5]
  323.         if type(msg) == "table" and msg.reddernet and msg.encrypted then
  324.             if done[msg] then return hmm end
  325.             local m = msg.message
  326.             local txt = encryption.decrypt(m)
  327.             if not txt then return end
  328.             msg.encryptedMessage = m
  329.             msg.encrypted = false
  330.             msg.message = txt
  331.         end
  332.     end return hmm
  333. end
  334. function os.pullEventRaw(filter)
  335.     while true do
  336.         local res = pull(filter)
  337.         if res then
  338.             return unpack(res)
  339.         end
  340.     end
  341. end
  342.  
  343. local bRunning = false
  344. local function run()
  345.     if bRunning then
  346.         error( "reddernet is already running", 2 )
  347.     end bRunning = true
  348.  
  349.     while bRunning do
  350.         local sEvent, p1, p2, p3, p4 = os.pullEventRaw()
  351.         if sEvent == "modem_message" then
  352.             -- Got a modem message, process it and add it to the rednet event queue
  353.             local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4
  354.             if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then
  355.                 if type( tMessage ) == "table" and tMessage.nMessageID then
  356.                     if not tReceivedMessages[ tMessage.nMessageID ] then
  357.                         tReceivedMessages[ tMessage.nMessageID ] = true
  358.                         tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID
  359.                         os.queueEvent( "rednet_message", nReplyChannel, tMessage.message, tMessage.sProtocol )
  360.                     end
  361.                 end
  362.             end
  363.  
  364.         elseif sEvent == "rednet_message" then
  365.             -- Got a rednet message (queued from above), respond to dns lookup
  366.             local nSenderID, tMessage, sProtocol = p1, p2, p3
  367.             if sProtocol == "dns" and tMessage.sType == "lookup" then
  368.                 local sHostname = tHostnames[ tMessage.sProtocol ]
  369.                 if sHostname and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then
  370.                     rednet.send( nSenderID, {
  371.                         sType = "lookup response",
  372.                         sHostname = sHostname,
  373.                         sProtocol = tMessage.sProtocol,
  374.                     }, "dns" )
  375.                 end
  376.             end
  377.  
  378.         elseif sEvent == "timer" then
  379.             -- Got a timer event, use it to clear the event queue
  380.             local nTimer = p1
  381.             local nMessage = tReceivedMessageTimeouts[ nTimer ]
  382.             if nMessage then
  383.                 tReceivedMessageTimeouts[ nTimer ] = nil
  384.                 tReceivedMessages[ nMessage ] = nil
  385.             end
  386.         end
  387.     end
  388. end
  389.  
  390. do -- Take over rednet!
  391.     local raw = os.pullEventRaw
  392.     function os.pullEventRaw(ev)
  393.         local s,e = xpcall(function()
  394.             error("banana",4)
  395.         end,function(e) return e end)
  396.         if e:match("^rednet:%d+: banana$") then
  397.             os.pullEventRaw = raw
  398.             _G.DONE = true run()
  399.         end return raw(ev)
  400.     end os.queueEvent("")
  401. end
  402.  
  403. for k,v in pairs(reddernet) do
  404.     rednet[k] = v
  405. end
  406.  
  407.  
  408.  
  409. --[[networking]]--
  410.  
  411.  
  412. --[[ Notes
  413.     Address format: 24 bit
  414.      -> 6 hexadecimal characters
  415.         16 bits  8 bits
  416.         ABCD    : EF
  417.         Network    Host
  418.     #Networks: 65536
  419.     #Hosts: 16777216
  420.     #Hosts/Network: 256
  421.     (Should definitly be plenty)
  422.  
  423.     Notations:
  424.         Normal: 00AB:0C
  425.         Shortened: AB:C
  426.         Long: 00AB0C
  427.         Simplified Long: AB0C
  428.         (Always hexadecimal)
  429. --]]
  430.  
  431. local function writeBits(tab,s,num)
  432.     local _,bits = math.frexp(num)
  433.     s = s - 1
  434.     for i=bits,1,-1 do
  435.         local v = num%2
  436.         tab[i+s] = v
  437.         num = (num-v)/2
  438.     end return tab
  439. end
  440.  
  441. local function fillBits(tab,n)
  442.     for i=1,n-#tab do
  443.         table.insert(tab,1,0)
  444.     end return tab
  445. end
  446.  
  447. local function opAnd(a,b)
  448.     local res = {}
  449.     for i=1,#a do
  450.         res[i] = a[i] + b[i] == 2 and 1 or 0
  451.     end return res
  452. end
  453. local function opOr(a,b)
  454.     local res = {}
  455.     for i=1,#a do
  456.         res[i] = a[i] + b[i] > 0 and 1 or 0
  457.     end return res
  458. end
  459.  
  460. address = {
  461.     toBit = function(str)
  462.         local net,host = str:match("(%x+):(%x+)")
  463.         if not net then return nil end
  464.         net = tonumber(net,16)
  465.         host = tonumber(host,16)
  466.         local res = writeBits({},1,net)
  467.         for i=1,16-#res do
  468.             table.insert(res,1,0)
  469.         end
  470.         print(table.concat(res))
  471.         writeBits(res,17,host)
  472.         for i=1,24-#res do
  473.             table.insert(res,17,0)
  474.         end return res
  475.     end;
  476.     applyMask = function(a,b,mask)
  477.         -- Resulting bitlist contains the
  478.         -- bits of a where mask[i]==1 and
  479.         -- the bits of b where mask[i]==0
  480.         local res = {}
  481.         for i=1,#mask do
  482.             res[i] = a[i]
  483.         end
  484.         for i=#mask+1,#a do
  485.             res[i] = b[i]
  486.         end
  487.     end;
  488. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement