Advertisement
Guest User

r(-yut

a guest
Aug 24th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.47 KB | None | 0 0
  1. --[[
  2. CraftOS+ By Sirharry0077
  3.  
  4. ]]--
  5.  
  6. -- Varibles
  7. devVersion = "8.0.0" -- Development Purposes Only
  8. version = "8.0"
  9. OSName = "CraftOS+"
  10. defaultTextColor = 16
  11. defaultTitleColor = 16384
  12. defaultBackgroundColor = 32768
  13. bin = "bin"
  14. configFile = bin.."/config.txt"
  15.  
  16. -- Functions
  17. function API()
  18.  
  19. function setSetting(file, name, setting)
  20. reading = true
  21. exist = true
  22. if fs.exists(file) == false then
  23. reading = false
  24. exist = false
  25. end
  26. if exist == true then
  27. config = fs.open(file, "r")
  28. end
  29. n = 0
  30. settings = {}
  31. while reading do -- Find if setting already exists and skip it
  32. n = n + 1
  33. line = config.readLine()
  34. if line == nil then
  35. reading = false
  36. elseif string.sub(line, 1, string.len(name) + 1) == name..":" then
  37. line = "Skipped"
  38. else
  39. settings[#settings + 1] = line -- Save all settings except for pre-existing new one
  40. end
  41. end
  42. if exist == true then config.close() end
  43. fs.delete(file) -- Delete File
  44. config = fs.open(file, "w")
  45. if exist == true then writing = true else writing = false end
  46. n = 0
  47. while writing do -- Rewrite File
  48. if #settings ~= 0 then
  49. n = n + 1
  50. config.writeLine(settings[n])
  51. end
  52. if n == #settings then
  53. writing = false
  54. end
  55. end
  56. config.writeLine(name..": "..setting) -- Write New Setting
  57. config.close()
  58. end
  59.  
  60. function getSetting(file, name)
  61. if fs.exists(file) == false then error("No Such Config File", true) end
  62. config = fs.open(file, "r")
  63. reading = true
  64. while reading do
  65. line = config.readLine()
  66. if string.sub(line, 1, string.len(name) + 1) == name..":" then
  67. setting = string.sub(line, string.len(name) + 3, string.len(line))
  68. reading = false
  69. elseif line == nil then
  70. setting = nil
  71. reading = false
  72. end
  73. end
  74. config.close()
  75. return setting
  76. end
  77.  
  78. function displayMessage(title, what, time)
  79. clear()
  80. titleColor()
  81. print(title)
  82. print(makeLine(title))
  83. textColor()
  84. print(what)
  85. sleep(time)
  86. end
  87.  
  88. function askInput(title, what)
  89. clear()
  90. titleColor()
  91. print(title)
  92. print(makeLine(title))
  93. textColor()
  94. write(what..": ")
  95. return read()
  96. end
  97.  
  98. function textColor()
  99. term.setTextColor(defaultTextColor)
  100. end
  101.  
  102. function titleColor()
  103. term.setTextColor(defaultTitleColor)
  104. end
  105.  
  106. function backgroundColor()
  107. term.setBackgroundColor(defaultBackgroundColor)
  108. end
  109.  
  110. function ENC()
  111.  
  112. local function zfill(N)
  113. N=string.format("%X",N)
  114. Zs=""
  115. if #N==1 then
  116. Zs="0"
  117. end
  118. return Zs..N
  119. end
  120.  
  121. local function serializeImpl(t)
  122. local sType = type(t)
  123. if sType == "table" then
  124. local lstcnt=0
  125. for k,v in pairs(t) do
  126. lstcnt = lstcnt + 1
  127. end
  128. local result = "{"
  129. local aset=1
  130. for k,v in pairs(t) do
  131. if k==aset then
  132. result = result..serializeImpl(v)..","
  133. aset=aset+1
  134. else
  135. result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  136. end
  137. end
  138. result = result.."}"
  139. return result
  140. elseif sType == "string" then
  141. return string.format("%q",t)
  142. elseif sType == "number" or sType == "boolean" or sType == "nil" then
  143. return tostring(t)
  144. elseif sType == "function" then
  145. local status,data=pcall(string.dump,t)
  146. if status then
  147. return 'func('..string.format("%q",data)..')'
  148. else
  149. error()
  150. end
  151. else
  152. error()
  153. end
  154. end
  155.  
  156. local function split(T,func)
  157. if func then
  158. T=func(T)
  159. end
  160. local Out={}
  161. if type(T)=="table" then
  162. for k,v in pairs(T) do
  163. Out[split(k)]=split(v)
  164. end
  165. else
  166. Out=T
  167. end
  168. return Out
  169. end
  170.  
  171. local function serialize( t )
  172. t=split(t)
  173. return serializeImpl( t, tTracking )
  174. end
  175.  
  176. local function unserialize( s )
  177. local func, e = loadstring( "return "..s, "serialize" )
  178. local funcs={}
  179. if not func then
  180. return e
  181. end
  182. setfenv( func, {
  183. func=function(S)
  184. local new={}
  185. funcs[new]=S
  186. return new
  187. end,
  188. })
  189. return split(func(),function(val)
  190. if funcs[val] then
  191. return loadstring(funcs[val])
  192. else
  193. return val
  194. end
  195. end)
  196. end
  197.  
  198. local function sure(N,n)
  199. if (l2-n)<1 then N="0" end
  200. return N
  201. end
  202.  
  203. local function splitnum(S)
  204. Out=""
  205. for l1=1,#S,2 do
  206. l2=(#S-l1)+1
  207. CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
  208. Out=string.char(CNum)..Out
  209. end
  210. return Out
  211. end
  212.  
  213. local function wrap(N)
  214. return N-(math.floor(N/256)*256)
  215. end
  216.  
  217. function checksum(S,num)
  218. local sum=0
  219. for char in string.gmatch(S,".") do
  220. for l1=1,(num or 1) do
  221. math.randomseed(string.byte(char)+sum)
  222. sum=sum+math.random(0,9999)
  223. end
  224. end
  225. math.randomseed(sum)
  226. return sum
  227. end
  228.  
  229. local function genkey(len,psw)
  230. checksum(psw)
  231. local key={}
  232. local tKeys={}
  233. for l1=1,len do
  234. local num=math.random(1,len)
  235. while tKeys[num] do
  236. num=math.random(1,len)
  237. end
  238. tKeys[num]=true
  239. key[l1]={num,math.random(0,255)}
  240. end
  241. return key
  242. end
  243.  
  244. function encrypt(data,psw)
  245. data=serialize(data)
  246. local chs=checksum(data)
  247. local key=genkey(#data,psw)
  248. local out={}
  249. local cnt=1
  250. for char in string.gmatch(data,".") do
  251. table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
  252. cnt=cnt+1
  253. end
  254. return string.sub(serialize({chs,table.concat(out)}),2,-3)
  255. end
  256.  
  257. function decrypt(data,psw)
  258. local oData=data
  259. data=unserialize("{"..data.."}")
  260. if type(data)~="table" then
  261. return oData
  262. end
  263. local chs=data[1]
  264. data=data[2]
  265. local key=genkey((#data)/2,psw)
  266. local sKey={}
  267. for k,v in pairs(key) do
  268. sKey[v[1]]={k,v[2]}
  269. end
  270. local str=splitnum(data)
  271. local cnt=1
  272. local out={}
  273. for char in string.gmatch(str,".") do
  274. table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
  275. cnt=cnt+1
  276. end
  277. out=table.concat(out)
  278. if checksum(out or "")==chs then
  279. return unserialize(out)
  280. end
  281. return oData,out,chs
  282. end
  283.  
  284. end
  285.  
  286. function download(url, path)
  287. http.request(url)
  288. local requesting = true
  289. while requesting do
  290. local event, url, sourceText = os.pullEvent()
  291. if event == "http_success" then
  292. --local respondedText = sourceText.readAll()
  293. requesting = false
  294. local getFile = http.get(url)
  295. local text = getFile.readAll()
  296. --getFile.close()
  297. if fs.exists("path") == true then
  298. fs.delete("path")
  299. end
  300. local file = fs.open(path, "w")
  301. file.write(text)
  302. file.close()
  303. return true
  304. elseif event == "http_failure" then
  305. requesting = false
  306. return false
  307. end
  308. end
  309. end
  310.  
  311. function makeLine(text)
  312. length = string.len(text)
  313. length = length - 1
  314. line = "-"
  315. repeat
  316. line = line.."-"
  317. length = length - 1
  318. until length == 0
  319. return line
  320. end
  321.  
  322. function yn(title, question)
  323. clear()
  324. print(title)
  325. print(makeLine(title))
  326. print(question)
  327. stop = false
  328. repeat
  329. write("Y/N: ")
  330. local response = read()
  331. response = string.lower(response)
  332. if response == "y" or "yes" then
  333. stop = true
  334. return true
  335. elseif response == "n" or "no" then
  336. stop = true
  337. return false
  338. else
  339. print("Invalid Response")
  340. end
  341. until stop == true
  342. end
  343.  
  344. function centerPrint(text, xoffset, yoffset)
  345. if xoffset == nil then xoffset = 0 end
  346. if yoffset == nil then yoffset = 0 end
  347. if text ~= nil then
  348. len = string.len(text)
  349. len = math.ceil(len / 2)
  350. else
  351. len = 2
  352. end
  353. local x,y = term.getSize()
  354. x = math.ceil((x / 2) - len + xoffset)
  355. y = math.ceil((y / 2) + yoffset)
  356. term.setCursorPos(x, y)
  357. if text ~= nil then
  358. print(text)
  359. end
  360. end
  361.  
  362. function clear()
  363. term.clear()
  364. term.setCursorPos(1,1)
  365. end
  366.  
  367. function startMenu( tMenu, startXPos, startYPos, sTitle )
  368. local function printMenu( tMenu, sTitle, nSelected )
  369. for index, text in pairs( tMenu ) do
  370. term.setCursorPos( startXPos, startYPos + index - 1 )
  371. write( (nSelected == index and '[' or ' ') .. text .. (nSelected == index and ']' or ' ') )
  372. end
  373. end
  374. local selection = 1
  375. while true do
  376. printMenu( tMenu, sTitle, selection )
  377. event, button = os.pullEvent("key")
  378. if button == keys.up then
  379. -- Up
  380. selection = selection - 1
  381. elseif button == keys.down then
  382. -- Down
  383. selection = selection + 1
  384. elseif button == keys.enter then
  385. -- Enter
  386. return tMenu[selection], selection -- returns the text AND the number
  387. end
  388. selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
  389. end
  390. end
  391.  
  392. function error(error, terminate)
  393. clear()
  394. titleColor()
  395. local title = "Error"
  396. print(title)
  397. print(makeLine(title))
  398. textColor()
  399. print("ERROR: "..error)
  400. sleep(5)
  401. if terminate == true then
  402. os.reboot()
  403. end
  404. end
  405.  
  406. ENC() -- Loads Encryption Functions
  407.  
  408. end
  409.  
  410. function Client()
  411. function mainMenu()
  412. clear()
  413. term.setCursorPos(1,1)
  414. titleColor()
  415. print(OSName.." "..version)
  416. print("------------")
  417. textColor()
  418. local tSelections = {
  419. "Command Line",
  420. "Rednet",
  421. "Logout",
  422. "Help"
  423. }
  424.  
  425.  
  426. sOption, nNumb = startMenu(tSelections, 4, 4)
  427.  
  428. if nNumb == 1 then
  429. commandLine()
  430. elseif sOption == "Help" then
  431. helpFunction()
  432. mainMenu()
  433. elseif sOption == "Logout" then
  434. writeLoginScreen()
  435. elseif sOption == "Rednet" then
  436. rednet()
  437. end
  438. end
  439.  
  440. function writeLoginScreen()
  441. clear()
  442. term.setCursorPos(1, 1)
  443. textColor()
  444. print("Computer #"..os.getComputerID())
  445.  
  446. titleColor()
  447. title = OSName.." "..version
  448. centerPrint(title, 0, -4)
  449. centerPrint(makeLine(title), 0, -3)
  450.  
  451. textColor()
  452. title = "Username: "
  453. centerPrint(title, -4, -1)
  454. ux, uy = term.getCursorPos()
  455. title = "Password: "
  456. centerPrint(title, -4, 0)
  457. px, py = term.getCursorPos()
  458.  
  459.  
  460. centerPrint(nil, 3, -1)
  461. user = read()
  462. centerPrint(nil, 3, 0)
  463. tPassword = read("*")
  464.  
  465. if fs.exists("bin/UserData") == false then
  466. error("No User Data", true)
  467. else
  468. file = fs.open("bin/UserData", "r")
  469. repeat
  470. line = file.readLine()
  471. until line == nil or line == user
  472. if line == nil then
  473. error("Incorrect Username/Password", false)
  474. writeLoginScreen()
  475. elseif line == user then
  476. ePassword = file.readLine()
  477. end
  478.  
  479. if tPassword == decrypt(ePassword, tPassword) then
  480. mainMenu()
  481. else
  482. error("Incorrect Username/Password", false)
  483. writeLoginScreen()
  484. end
  485. end
  486. end
  487.  
  488. function createAccount()
  489. local username = askInput("Pick Username", "Username")
  490. repeat
  491. password = askInput("Create Password", "Password")
  492. password2 = askInput("Re-Enter Your Password", "Password")
  493. if password ~= password2 then
  494. displayMessage("ERROR", "Passwords Did Not Match. Please Try Again.", 3)
  495. end
  496. until password == password2
  497. file = fs.open("bin/UserData", "a")
  498. file.writeLine(username)
  499. file.writeLine(encrypt(password, password))
  500. file.close()
  501. end
  502.  
  503. function checkFirstTime()
  504. if fs.exists("bin/UserData") == false then
  505. fs.makeDir("bin")
  506. createAccount()
  507. end
  508. end
  509.  
  510. function ClientStartup()
  511. checkFirstTime()
  512. writeLoginScreen()
  513. end
  514.  
  515.  
  516. ClientStartup() -- Start Client
  517. end
  518.  
  519. function Server()
  520.  
  521. end
  522.  
  523. function KeyPad()
  524. number = {}
  525. number[1] = {1, 2, 1}
  526. number[2] = {2, 2, 2}
  527. number[3] = {3, 2, 3}
  528. number[4] = {1, 3, 4}
  529. number[5] = {2, 3, 5}
  530. number[6] = {3, 3, 6}
  531. number[7] = {1, 4, 7}
  532. number[8] = {2, 4, 8}
  533. number[9] = {3, 4, 9}
  534. number[10] = {2, 5, 0}
  535. function draw()
  536. mon.setCursorPos(1,1)
  537. mon.clearLine()
  538. if #input ~= 0 then
  539. n = #input
  540. repeat
  541. mon.write("*")
  542. n = n - 1
  543. until n == 0
  544. end
  545. mon.setCursorPos(7,1)
  546. mon.write("X")
  547. pn = 0
  548. printing = true
  549. while printing do
  550. pn = pn + 1
  551. mon.setCursorPos(number[pn][1], number[pn][2])
  552. mon.write(tostring(number[pn][3]))
  553. if pn == 10 then printing = false end
  554. end
  555. end
  556. function waitInput()
  557. event, side, x, y = os.pullEvent("monitor_touch")
  558. return x, y
  559. end
  560. function figure(x, y)
  561. figureing = true
  562. success = false
  563. rn = 0
  564. while figureing do
  565. rn = rn + 1
  566. if number[rn][1] == x and number[rn][2] == y then
  567. success = true
  568. figureing = false
  569. elseif rn == 10 then
  570. figureing = false
  571. end
  572. end
  573. if success == true then
  574. return rn
  575. elseif success == false then
  576. return nil
  577. end
  578. end
  579. function after(n, x, y)
  580. if n ~= nil then
  581. input[#input + 1] = tostring(number[n][3])
  582. draw()
  583. end
  584. if x == 7 and y == 1 then
  585. if #input ~= 0 and #input ~= 1 then
  586. a = #input - 1
  587. temp = {}
  588. repeat
  589. temp[a] = input[a]
  590. a = a - 1
  591. until a == 0
  592. input = {}
  593. a = 0
  594. repeat
  595. a = a + 1
  596. input[a] = temp[a]
  597. until a == #temp
  598. draw()
  599. elseif #input == 1 then
  600. input = {}
  601. draw()
  602. end
  603. end
  604. if #input == 4 then
  605. if tostring(input[1]..input[2]..input[3]..input[4]) == password then
  606. mon.clear()
  607. mon.setCursorPos(1,1)
  608. mon.write(" ")
  609. mon.setBackgroundColor(8192)
  610. input = {}
  611. draw()
  612.  
  613. -- Password Correct Code Here
  614.  
  615. mon.setBackgroundColor(32768)
  616. else
  617.  
  618. mon.clear()
  619. mon.setCursorPos(1,1)
  620. mon.write(" ")
  621. mon.setBackgroundColor(16384)
  622.  
  623. input = {}
  624. sleep(2)
  625. draw()
  626. end
  627. end
  628. end
  629. password = "1234"
  630. mon = peripheral.wrap("back")
  631. input = {}
  632. working = true
  633. draw()
  634. rn = 0
  635. while working do
  636. rx, ry = waitInput()
  637. z = figure(rx, ry)
  638. after(z, rx, ry)
  639. end
  640. end
  641.  
  642. function Load()
  643. if fs.exists(bin) == false then
  644. fs.makeDir(bin)
  645. end
  646. oldpullEvent = os.pullEvent
  647. os.pullEvent = os.pullEventRaw
  648. API()
  649. if fs.exists("bin") == false or fs.exists("bin") == true then
  650. clear()
  651. titleColor()
  652. local title = "Welcome To "..OSName
  653. print(title)
  654. print(makeLine(title))
  655. print("Chose Installation Type")
  656. textColor()
  657. options = {
  658. "Client",
  659. "Server",
  660. "Key Pad",
  661. "Card Reader"
  662. }
  663. option, number = startMenu( options, 4, 5)
  664.  
  665. if option == "Client" then
  666. Client()
  667. elseif option == "Server" then
  668. Server()
  669. elseif option == "Key Pad" then
  670. KeyPad()
  671. elseif option == "Card Reader" then
  672. cardReader()
  673. else
  674. error("Invalid Menu Option", true)
  675. end
  676. end
  677. end
  678.  
  679. function cardReader()
  680. rConfig = "bin/ReaderConfig"
  681. if fs.exists("rConfig") == false then
  682. masterPass = "1"
  683. repeat
  684. masterPass = masterPass..tostring(math.random(1, 9))
  685. until string.len(masterPass) == 32
  686. setSetting(rConfig, "MasterPass", masterPass)
  687. end
  688. function menu()
  689. tMenu = {
  690. "Start",
  691. "Stop",
  692. "Generate New"
  693. }
  694. allowed = true
  695. startXPos = 4
  696. startYPos = 4
  697. sTitle = "Ello"
  698. local function printMenu( tMenu, sTitle, nSelected )
  699. clear()
  700. titleColor()
  701. title = "Card Reader"
  702. print(title)
  703. print(makeLine(title))
  704. if allowed == true then
  705. print("Status: Enabled")
  706. elseif allowed == false then
  707. print("Status: Disabled")
  708. end
  709. for index, text in pairs( tMenu ) do
  710. term.setCursorPos( startXPos, startYPos + index - 1 )
  711. write( (nSelected == index and '[' or ' ') .. text .. (nSelected == index and ']' or ' ') )
  712. end
  713. end
  714. local selection = 1
  715. event = {}
  716. while true do
  717. printMenu( tMenu, sTitle, selection )
  718. event, event2 = os.pullEvent()
  719. if event == "key" then
  720. if event2 == keys.up then
  721. -- Up
  722. selection = selection - 1
  723. elseif event2 == keys.down then
  724. -- Down
  725. selection = selection + 1
  726. elseif event2 == keys.enter then
  727. -- Enter
  728. tMenu[selection] = pick
  729. if pick == "Start" then
  730. allowed = true
  731. elseif pick == "Stop" then
  732. allowed = false
  733. elseif pick == "Generate New" then
  734. clear()
  735. titleColor()
  736. title = "Generate New Card"
  737. print(title)
  738. print(makeLine(title))
  739. textColor()
  740. print("Insert Disk")
  741. id = tostring(disk.getID())
  742. event, side = os.pullEvent("disk")
  743. write("Name: ")
  744. name = read()
  745. number = "1"
  746. repeat
  747. number = number..tostring(math.random(1, 9))
  748. until string.len(number) == 256
  749. setSetting(rconfig, number, id)
  750. setSetting(rconfig, id, name)
  751. number = encrypt(number, getSetting(rConfig, "MasterPass"))
  752. path = disk.getMountPath(side)
  753. file = fs.open(path.."/number", "w")
  754. file.write(number)
  755. file.close()
  756. disk.setLabel(side, name.."'s ID Card")
  757. print("Done")
  758. print("Ejecting Disk")
  759. disk.eject(side)
  760. sleep(2)
  761. end
  762. end
  763. selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
  764. elseif event == "disk" then
  765. side = event2
  766. id = tostring(disk.getID(side))
  767. path = disk.getMountPath(side)
  768. if fs.exists(path.."/number") == true then
  769. file = fs.open(path.."/number", "r")
  770. number = file.readLine()
  771. file.close()
  772. if number ~= nil then
  773. number = decrypt(number, getSetting(rConfig, "MasterPass"))
  774. rID = getSetting(rConfig, number)
  775. if rID ~= nil then
  776. if rID == id then
  777. --Allowed Card Inserted Code Here
  778. disk.eject(side)
  779. redstone.setOutput("back", true)
  780. sleep(3)
  781. redstone.setOutput("back", false)
  782. end
  783. end
  784. end
  785. else
  786. disk.eject(side)
  787. end
  788. end
  789. end
  790. end
  791. menu()
  792. end
  793.  
  794. -- Startup
  795. Load() -- Loads API and Intializes Program
  796.  
  797. -- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement