Advertisement
Guest User

DSLAM

a guest
Sep 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. input = peripheral.wrap("bottom")
  2. out = peripheral.wrap("top")
  3.  
  4. input.setListening(1, true)
  5. input.setDefaultChannel(1)
  6. out.open(1)
  7.  
  8. local split = function(inputstr, sep)
  9.   if sep == nil then
  10.     sep = "%s"
  11.   end
  12.   sep = tostring(sep)
  13.   local t={}
  14.   local i=1
  15.   for str in string.gmatch(inputstr, "([^"..sep.."]*)"..sep.."?") do
  16.     t[i] = str
  17.     i = i+1
  18.   end
  19.   return t
  20. end
  21.  
  22. if fs.exists("conf.cfg") then
  23.   conf = fs.open("conf.cfg", "r")
  24.   MyIP = conf.readAll()
  25.   conf.close()
  26. else
  27.   input.send("REG_IP")
  28.   print("Waiting for registration...")
  29.   local _, _, _, _, msg = os.pullEvent("lan_message")
  30.   MyIP = msg
  31.   conf = fs.open("conf.cfg", "w")
  32.   conf.writeLine(msg)
  33.   conf.close()
  34. end
  35.  
  36. term.clear()
  37. print("DLSAM by O2 (0.1) / IP: " .. MyIP)
  38. print("--------------------------------------")
  39.  
  40. while true do
  41.  
  42.   local income = {os.pullEvent()}
  43.  
  44.   if income[1] == "lan_message" then
  45.     from = income[3]
  46.     msg = income[5]
  47.    
  48.     cut = split(msg, ";")
  49.    
  50.     if cut[1] == MyIP then      
  51.       out.transmit(1, 1, cut[2])  
  52.       print("OUT > IN")
  53.     end
  54.        
  55.   elseif income[1] == "modem_message" then
  56.     from = income[3]
  57.     msg = income[5]
  58.    
  59.     if msg == "REG_IP" then
  60.       repeat
  61.         lc = math.random(2,255)
  62.         tip = "10.0.0." .. lc
  63.         if fs.exists("LOCAL_ADRESS/" .. tip) then
  64.           r = 0
  65.         else
  66.           r = 1
  67.         end
  68.       until r == 1
  69.       nc = fs.open("LOCAL_ADRESS/" .. tip, "w")
  70.       nc.writeLine(from)
  71.       nc.close()
  72.      
  73.       out.transmit(from .. "/" .. tip)
  74.       print("Registered PC " .. from .. " to " .. tip)
  75.      
  76.     else
  77.       cut = split(msg, "*")
  78.       if cut[1] == "10.0.0.1" then      
  79.         input.send(msg)    
  80.         print("IN > OUT")
  81.       end
  82.     end              
  83.   end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement