Advertisement
VlaD00m

RemoteDISK NetServer

Dec 12th, 2020
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.83 KB | None | 0 0
  1. --[[ RemoteDISK NetServer v1.03.
  2. Server program for make NetClient Network disks. Client connecting by port, hostname, login and password
  3. Uses settings from "/etc/rc.cfg"
  4. Author: Bs()Dd
  5. More information: https://github.com/Bs0Dd/OpenCompSoft/blob/master/RemoteDISK/README.md
  6. ]]
  7. local component = require("component")
  8. local computer = require("computer")
  9. local fs = require("filesystem")
  10. local event = require("event")
  11. local seriz = require("serialization")
  12.  
  13. function request(_, _, opp, _, _, call, one, two, thr, four)
  14.     event.ignore("modem_message", request)
  15.     logfil, why = fs.open("/etc/rdwork.log", "a")
  16.  
  17.     if call == "RDCL" then
  18.         if args.log then
  19.             logfil:write("[" .. os.date("%T") .. "] " .. "[CONNECT]: " .. opp .. " is connected\n")
  20.         end
  21.         if one == args.hostname then
  22.             card.send(opp, port, "RDAN")
  23.             _, _, opp, _, _, call, one, two = event.pull("modem_message")
  24.             if call == "RDLG" and one == args.login and two == args.password then
  25.                 if online.n == maxonline then
  26.                     card.send(opp, port, "RDAU", "RDFULL")
  27.                     if args.log then
  28.                         logfil:write(
  29.                             "[" .. os.date("%T") .. "] " .. "[LOGIN]: " .. opp .. " tried to login to full server\n"
  30.                         )
  31.                     end
  32.                     logfil:close()
  33.                     event.listen("modem_message", request)
  34.                     return
  35.                 end
  36.                 card.send(opp, port, "RDAU", "OK", rfs.address)
  37.                 if online[opp] == nil then
  38.                     online[opp] = true
  39.                     online.n = online.n + 1
  40.                     ofdesc[opp] = {n = 0}
  41.                 end
  42.                 if args.log then
  43.                     logfil:write("[" .. os.date("%T") .. "] " .. "[LOGIN]: " .. opp .. " is logged in\n")
  44.                 end
  45.             elseif call == "RDLG" then
  46.                 card.send(opp, port, "RDAU", "FAIL")
  47.                 if args.log then
  48.                     logfil:write("[" .. os.date("%T") .. "] " .. "[LOGIN]: " .. opp .. " is failed to log in\n")
  49.                 end
  50.             end
  51.         end
  52.         logfil:close()
  53.         event.listen("modem_message", request)
  54.         return
  55.     end
  56.  
  57.     if online[opp] == nil then
  58.         card.send(opp, port, "RDNAUT")
  59.         if args.log then
  60.             logfil:write(
  61.                 "[" .. os.date("%T") .. "] " .. "[LOGIN]: " .. opp .. " tried to get access without authorization\n"
  62.             )
  63.         end
  64.         logfil:close()
  65.         event.listen("modem_message", request)
  66.         return
  67.     end
  68.  
  69.     if call == "RDISDIR" then
  70.         card.send(opp, port, "RDISDIRA", rfs.isDirectory(one))
  71.         if args.log == 2 then
  72.             logfil:write(
  73.                 "[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called isDirectory("' .. one .. '")\n'
  74.             )
  75.         end
  76.     elseif call == "RDLMOD" then
  77.         card.send(opp, port, "RDLMODA", rfs.lastModified(one))
  78.         if args.log == 2 then
  79.             logfil:write(
  80.                 "[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called lastModified("' .. one .. '")\n'
  81.             )
  82.         end
  83.     elseif call == "RDLIST" then
  84.         lst, err = rfs.list(one)
  85.         if err ~= nil then
  86.             card.send(opp, port, "RDLISTA", nil, err)
  87.         end
  88.         if lst ~= nil then
  89.             card.send(opp, port, "RDLISTA", seriz.serialize(lst))
  90.         else
  91.             card.send(opp, port, "RDLISTA", nil, nil)
  92.         end
  93.         if args.log == 2 then
  94.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called list("' .. one .. '")\n')
  95.         end
  96.     elseif call == "RDTOTL" then
  97.         card.send(opp, port, "RDTOTLA", rfs.spaceTotal())
  98.         if args.log == 2 then
  99.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called spaceTotal()\n")
  100.         end
  101.     elseif call == "RDOPEN" then
  102.         if ofdesc[opp].n == maxdescr then
  103.             card.send(opp, port, "RDOPENA", nil, "too many open handles")
  104.             logfil:close()
  105.             event.listen("modem_message", request)
  106.             return
  107.         else
  108.             fdes, err = rfs.open(one, two)
  109.             if err ~= nil then
  110.                 card.send(opp, port, "RDOPENA", nil, err)
  111.             else
  112.                 ofdesc[opp][tonumber(tostring(fdes))] = fdes
  113.                 ofdesc[opp].n = ofdesc[opp].n + 1
  114.                 card.send(opp, port, "RDOPENA", tonumber(tostring(fdes)))
  115.             end
  116.             if args.log == 2 then
  117.                 logfil:write(
  118.                     "[" ..
  119.                         os.date("%T") ..
  120.                             "] " .. "[VERBOSE]: " .. opp .. ' called open("' .. one .. '", "' .. two .. '")\n'
  121.                 )
  122.             end
  123.         end
  124.     elseif call == "RDRM" then
  125.         card.send(opp, port, "RDRMA", rfs.remove(one))
  126.         if args.log == 2 then
  127.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called remove("' .. one .. '")\n')
  128.         end
  129.     elseif call == "RDRN" then
  130.         card.send(opp, port, "RDRNA", rfs.rename(one, two))
  131.         if args.log == 2 then
  132.             logfil:write(
  133.                 "[" ..
  134.                     os.date("%T") ..
  135.                         "] " .. "[VERBOSE]: " .. opp .. ' called rename("' .. one .. '", "' .. two .. '")\n'
  136.             )
  137.         end
  138.     elseif call == "RDREAD" then
  139.         if ofdesc[opp][one] ~= nil then
  140.             sended = 0
  141.             remain = two
  142.             if two > maxpackspace then
  143.                 bread = maxpackspace
  144.             else
  145.                 bread = two
  146.             end
  147.             while sended < two do
  148.                 rdd = rfs.read(ofdesc[opp][one], bread)
  149.                 card.send(opp, port, "RDREADA", rdd)
  150.                 sended = sended + maxpackspace
  151.                 remain = remain - maxpackspace
  152.                 if remain < maxpackspace then
  153.                     bread = remain
  154.                 end
  155.             end
  156.         else
  157.             card.send(opp, port, "RDREADA", nil, "bad file descriptor")
  158.         end
  159.         if args.log == 2 then
  160.             logfil:write(
  161.                 "[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called read(" .. one .. ', "' .. two .. '")\n'
  162.             )
  163.         end
  164.     elseif call == "RDCLS" then
  165.         if ofdesc[opp][one] == "closed" then
  166.             card.send(opp, port, "RDCLSA")
  167.         elseif ofdesc[opp][one] ~= nil then
  168.             card.send(opp, port, "RDCLSA", rfs.close(ofdesc[opp][one]))
  169.             ofdesc[opp][one] = "closed"
  170.             ofdesc[opp].n = ofdesc[opp].n - 1
  171.         else
  172.             card.send(opp, port, "RDCLSA", nil, "bad file descriptor")
  173.         end
  174.         if args.log == 2 then
  175.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called close(" .. one .. ")\n")
  176.         end
  177.     elseif call == "RDGLAB" then
  178.         card.send(opp, port, "RDGLABA", rfs.getLabel())
  179.         if args.log == 2 then
  180.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called getLabel()\n")
  181.         end
  182.     elseif call == "RDSEEK" then
  183.         card.send(opp, port, "RDSEEKA", rfs.seek(one, two, thr))
  184.         if args.log == 2 then
  185.             logfil:write(
  186.                 "[" ..
  187.                     os.date("%T") ..
  188.                         "] " .. "[VERBOSE]: " .. opp .. " called seek(" .. one .. ', "' .. two .. '", ' .. thr .. ")\n"
  189.             )
  190.         end
  191.     elseif call == "RDFSIZ" then
  192.         card.send(opp, port, "RDFSIZA", rfs.size(one))
  193.         if args.log == 2 then
  194.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called size("' .. one .. '")\n')
  195.         end
  196.     elseif call == "RDISRO" then
  197.         card.send(opp, port, "RDISROA", rfs.isReadOnly())
  198.         if args.log == 2 then
  199.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called isReadOnly()\n")
  200.         end
  201.     elseif call == "RDSLAB" then
  202.         card.send(opp, port, "RDSLABA", rfs.setLabel(one))
  203.         if args.log == 2 then
  204.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called setLabel("' .. one .. '")\n')
  205.         end
  206.     elseif call == "RDMKDR" then
  207.         card.send(opp, port, "RDMKDRA", rfs.makeDirectory(one))
  208.         if args.log == 2 then
  209.             logfil:write(
  210.                 "[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called makeDirectory("' .. one .. '")\n'
  211.             )
  212.         end
  213.     elseif call == "RDISEX" then
  214.         card.send(opp, port, "RDISEXA", rfs.exists(one))
  215.         if args.log == 2 then
  216.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. ' called exists("' .. one .. '")\n')
  217.         end
  218.     elseif call == "RDFREE" then
  219.         card.send(opp, port, "RDFREEA", rfs.spaceUsed())
  220.         if args.log == 2 then
  221.             logfil:write("[" .. os.date("%T") .. "] " .. "[VERBOSE]: " .. opp .. " called spaceUsed()\n")
  222.         end
  223.     elseif call == "RDWRT" then
  224.         oneb = one
  225.         if ofdesc[opp][one] ~= nil then
  226.             fhand = ofdesc[opp][one]
  227.             card.send(opp, port, "RDWRTA")
  228.             readed = 0
  229.             rdata = ""
  230.             while readed < two do
  231.                 while true do
  232.                     _, _, ropp, _, _, call, one = event.pull("modem_message")
  233.                     if ropp == opp then
  234.                         break
  235.                     end
  236.                 end
  237.                 if one ~= nil then
  238.                     rdata = rdata .. one
  239.                 end
  240.                 readed = readed + #one
  241.             end
  242.             stat, err = rfs.write(fhand, rdata)
  243.             card.send(opp, port, "RDWRTPA", stat, err)
  244.         else
  245.             card.send(opp, port, "RDWRTA", nil, "bad file descriptor")
  246.         end
  247.         if args.log == 2 then
  248.             logfil:write(
  249.                 "[" ..
  250.                     os.date("%T") ..
  251.                         "] " .. "[VERBOSE]: " .. opp .. " called write(" .. oneb .. ", data len: " .. #rdata .. ")\n"
  252.             )
  253.         end
  254.     elseif call == "RDBYE" then
  255.         online[opp] = nil
  256.         online.n = online.n - 1
  257.         for _, dcl in pairs(ofdesc[opp]) do
  258.             if dcl ~= "closed" then
  259.                 rfs.close(dcl)
  260.             end
  261.         end
  262.         ofdesc[opp] = nil
  263.         card.send(opp, port, "RDBYEA", "user logged off")
  264.         if args.log == 2 then
  265.             logfil:write("[" .. os.date("%T") .. "] " .. "[CONNECT]: " .. opp .. " disconnected\n")
  266.         end
  267.     end
  268.     logfil:close()
  269.     event.listen("modem_message", request)
  270. end
  271.  
  272. function start()
  273.     if work == nil then
  274.         if component.list("modem")() == nil then
  275.             io.stderr:write("No Network Card is detected.")
  276.             return
  277.         end
  278.         card = component.proxy(component.list("modem")())
  279.         work = true
  280.         print("RemoteDISK NetServer v1.03\n")
  281.  
  282.         if args == nil then
  283.             io.stderr:write("FATAL ERROR! No settings found!")
  284.             return
  285.         end
  286.         port = args.port
  287.         card.open(port)
  288.         maxpackspace = card.maxPacketSize() - 20
  289.         rfs = component.proxy(args.hddaddress)
  290.         if rfs == nil then
  291.             io.stderr:write("FATAL ERROR! Hard drive insn't exists!")
  292.             return
  293.         else
  294.             print("Hard drive " .. rfs.address .. " is selected")
  295.             print('Server "' .. args.hostname .. '", port ' .. port)
  296.         end
  297.         if args.mode == 1 then
  298.             maxonline = 14
  299.             maxdescr = 1
  300.         elseif args.mode == 2 then
  301.             maxonline = 7
  302.             maxdescr = 2
  303.         else
  304.             print("FATAL ERROR! Incorrect mode.")
  305.             return
  306.         end
  307.         if args.log then
  308.             logfil, why = io.open("/etc/rdwork.log", "w")
  309.             if logfil == nil then
  310.                 print("FATAL ERROR! Can't open logging file: " .. why)
  311.                 return
  312.             end
  313.             print("Logging started!")
  314.             logfil:write("[" .. os.date("%T") .. "] " .. "[STATUS]: Server started\n")
  315.             logfil:close()
  316.         end
  317.         online = {n = 0}
  318.         ofdesc = {}
  319.         work = true
  320.         event.listen("modem_message", request)
  321.     else
  322.         io.stderr:write("Server already started!\n")
  323.     end
  324. end
  325.  
  326. function stop()
  327.     if work == true then
  328.         work = nil
  329.         event.ignore("modem_message", request)
  330.         card.close(port)
  331.         if args.log then
  332.             logfil = io.open("/etc/rdwork.log", "a")
  333.             logfil:write("[" .. os.date("%T") .. "] " .. "[STATUS]: Server stopped\n")
  334.             logfil:close()
  335.         end
  336.         print("Server stopped.")
  337.     else
  338.         io.stderr:write("Server isn't working now!\n")
  339.     end
  340. end
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement