This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
boudragon's profile picture

Server Side (in-game Computer) File Access?

Started by boudragon, 15 October 2013 - 04:26 AM
boudragon #1
Posted 15 October 2013 - 06:26 AM
Ok so after doing some research I decided I wanted my login and system manager to be set server-side while being able to be accessed via wireless modem by any computer that chooses to access it but I am having trouble figuring out the best way to do this since both the login and manager were originally built without networking in mind not to mention all options are menu based rather than simple text input from the user.

I managed to test on how to have one computer tell another to open a program which in turn displays some text or even a simple question but aside from that I am completely lost! Any help would be greatly appreciated! I will post my login program as an example to base your answers by. Please keep in mind that though the login DOES work it may be a little sloppy.

Spoiler

		local function readN(len, replaceChar)
		  len = len or 12
		  local input=""
		  local key = 0
		  term.setCursorBlink(true)
		  repeat
			local e,p1 = os.pullEvent()
			if e=="char" then
			  if #input < len then
				input = input .. p1
				term.write(replaceChar or p1)
			  end
			elseif e=="key" and p1==keys.backspace and #input > 0 then
			  input = input:sub(1,#input-1)
			  local x,y = term.getCursorPos()
			  term.setCursorPos(x-1,y)
			  term.write(" ")
			  term.setCursorPos(x-1,y)
			end
		  until p1==keys.enter
		  term.setCursorBlink(false)
		  return input
		end
		
		sid = 0
		lockdown = 0
		actc = ""
		actpw = ""
		actpc = ""
		userlog = ""
		userpas = ""
		puselog = ""
		prog = 0
		
		function clear()
		term.clear()
		term.setCursorPos(1,1)
		end
		
		function menu(id, text)
		  if sid == id then
			write">> "
		  else
			write"   "
		  end
		  print(text)
		end
		
		if not fs.exists("pending") then
		  shell.run("mkdir pending")
		  if not fs.exists("users") then
			shell.run("mkdir users")
		  else
		  end
		elseif not fs.exists("users") then
		  shell.run("mkdir users")
		end
		
		clear()
		while prog == 0 do
		--os.pullEvent = os.pullEventRaw
		rep1 = 0
		rep2 = 0
		rep3 = 0
		rep4 = 0
		rep5 = 0
		rep6 = 0
		ioerror = 0
		term.setCursorPos(1, 12)
		term.clearLine()
		term.setCursorPos(1, 13)
		term.clearLine()
		term.setCursorPos(1, 14)
		term.clearLine()
		term.setCursorPos(1, 15)
		term.clearLine()
		term.setCursorPos(1, 16)
		term.clearLine()
		term.setCursorPos(1, 17)
		term.clearLine()
		term.setCursorPos(1,1)
		print("|################################################|")
		print("|			  BOUTECH SYSTEM LOGIN			  |")
		print("|				OS Version : 1.3				|")
		print("|################################################|")
		term.setCursorPos(13,7)
		print("|------------------------|")
		term.setCursorPos(13,8)
		print("|						|")
		term.setCursorPos(13,9)
		print("|						|")
		term.setCursorPos(13,10)
		print("|						|")
		term.setCursorPos(13,11)
		print("|------------------------|")
		term.setCursorPos(16,8)
		menu(0, "Login")
		term.setCursorPos(16,9)
		menu(1, "Create Account")
		term.setCursorPos(16,10)
		menu(2, "Shutdown")
		event, key = os.pullEvent("key")
		
		if key == 200 then
		  if sid == 0 then
			sid = sid+2
		  elseif sid == 1 then
			sid = sid-1
		  elseif sid == 2 then
			sid = sid-1
		  end
		elseif key == 208 then
		  if sid == 2 then
			sid = sid-2
		  elseif sid == 1 then
			sid = sid+1
		  elseif sid == 0 then
			sid = sid+1
		  end
		elseif key == 28 then
		  if sid == 0 then
			term.setCursorPos(13, 12)
			print("|   Enter Credentials.   |")
			term.setCursorPos(13, 13)
			print("|						|")
			term.setCursorPos(13, 14)
			print("|						|")
			term.setCursorPos(13, 15)
			print("|						|")
			term.setCursorPos(13, 16)
			print("|						|")
			term.setCursorPos(13, 17)
			print("|------------------------|")
		  
			while rep5 == 0 do
			  term.setCursorPos(1, 14)
			  term.clearLine()
			  term.setCursorPos(1, 16)
			  term.clearLine()
			  term.setCursorPos(13, 16)
			  print("|						|")
			  term.setCursorPos(13, 14)
			  print("|						|")
			  term.setCursorPos(15, 14)
			  write("Username: ")
			  userlog = readN()
			  if userlog == "adminovrride" then
				rnd = math.random(100, 500)
				term.setCursorPos(1, 18)
				term.clearLine()
				term.setCursorPos (17, 18)
				print("Override Code: ".. rnd)
				apass = tostring((rnd + 11) * 83)
				term.setCursorPos(1, 16)
				term.clearLine()
				term.setCursorPos(13, 16)
				print("|						|")
				term.setCursorPos(15, 16)
				write("Password: ")
				ovrpass = readN(12, "*")
				if ovrpass == apass then
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(15, 18)
				  print("Override Successful...")
				  sleep(3)
				  term.clear()
				  term.setCursorPos(1, 1)
				  rep5 = rep5+1
				  rep6 = rep6+1
				  prog = prog+1
				else
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(15, 18)
				  print("  Override Failed...")
				  sleep(1)
				  rep5 = rep5+1
				  rep6 = rep6+1
				  sid = 0
				end
			  else	
				if userlog == "" then
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(18, 18)
				  print("Invalid Username")
				  sleep(1)
				  ioerror = ioerror+1
				  if ioerror == 3 then
					rep5 = rep5+1
					rep6 = rep6+1
					sid = 0
				  else
				  end
				else
				  ioerror = 0
				  ulog = fs.exists("users/".. userlog)
				  if ulog == true then
					term.setCursorPos(1, 16)
					term.clearLine()
					term.setCursorPos(13, 16)
					print("|						|")
					term.setCursorPos(15, 16)
					write("Password: ")
					userpas = readN(12, "*")
					if userpas == "" then
					  term.setCursorPos(1, 18)
					  term.clearLine()
					  term.setCursorPos(15, 18)
					  print("Invalid Password Entry")
					  sleep(1)
					  ioerror = ioerror+1
					  term.setCursorPos(1, 18)
					  term.clearLine()
					  if ioerror == 3 then
						rep5 = rep5+1
						sid = 0
					  else
					  end
					else
					  ulog = io.open("users/".. userlog, "r") or nil
					  pass = ulog and ulog:read()
					  ulog:close()
					  if (pass and pass==userpas) then
						term.setCursorPos(1, 18)
						term.clearLine()
						term.setCursorPos(16, 18)
						print("  Login Success...")
						currentw = fs.open("current", "w")
						accessr = fs.open("users/".. userlog, "r")
						accessl = accessr.readLine() and accessr.readLine()
						currentw.writeLine(userlog)
						currentw.writeLine(accessl)
						currentw.close()
						accessr.close()
						rep5 = rep5+1
						rep6 = rep6+1
						prog = prog+1
						sid = 0
						sleep(2)
						shell.run("system")
					  else
						if lockdown < 2 then
						  term.setCursorPos(1, 18)
						  term.clearLine()
						  term.setCursorPos(16, 18)
						  print("   Wrong Password")
						  sleep(1)
						  if userlog == puselog then
							lockdown = lockdown+1				  
						  else
							lockdown = 0
							puselog = userlog
							lockdown = lockdown+1
						  end
						else
						  while rep6 == 0 do
							term.setCursorPos(15, 18)
							rnd = tostring(math.random(101, 999))
							if not fs.exists("pending/".. rnd) then
							  fs.move("users/".. userlog, "pending/".. rnd)
							  term.setCursorPos(1, 18)
							  term.clearLine()
							  term.setCursorPos(15, 18)
							  print(" Lockdown Ref #: "..rnd)
							  sleep(1)
							  ulog = fs.open("pending/".. rnd, "a")
							  ulog.writeLine("Username: ".. userlog)
							  ulog.close()
							  rep6 = rep6+1
							  rep5 = rep5+1
							  sid = 0
							else
							end
						  end
						end
					  end
					end
				  else
					term.setCursorPos(14, 18)
					print("  Bad/Unknown Username")
					sleep(1)
				  end
				end
			  end
			end
		  elseif sid == 1 then
			term.setCursorPos(13, 12)
			print("|  Submit Account Info.  |")
			term.setCursorPos(13, 13)
			print("|						|")
			term.setCursorPos(13, 14)
			print("|						|")
			term.setCursorPos(13, 15)
			print("|						|")
			term.setCursorPos(13, 16)
			print("|						|")
			term.setCursorPos(13, 17)
			print("|------------------------|")
			  
			while rep1 == 0 do
			  term.setCursorPos(1, 14)
			  term.clearLine()
			  term.setCursorPos(13, 14)
			  print("|						|")
			  term.setCursorPos(15, 14)
			  write("Username: ")
			  input = readN()
			  actc = input
			  actex = fs.exists("users/".. actc)
			  if input == "adminovrride" then
				term.setCursorPos(1, 18)
				term.clearLine()
				term.setCursorPos(18, 18)
				print("Invalid Username")
				sleep(1)
				ioerror = ioerror+1
				if ioerror == 3 then
				  rep1 = 1
				  rep2 = 1
				  rep3 = 1
				  sid = 0
				else
				end
			  else
				if input == "" then
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(18, 18)
				  print("Invalid Username")
				  sleep(1)
				  ioerror = ioerror+1
				  if ioerror == 3 then
					rep1 = 1
					rep2 = 1
					rep3 = 1
					sid = 0
				  else
				  end
				elseif actex == true then
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(15, 18)
				  print (" Username Unavailable")
				  sleep(1)
				else
				  ioerror = 0
				  rep1 = 1
				end
			  end
			end
		  
			while rep2 == 0 do
			  while rep3 == 0 do
				term.setCursorPos(15, 15)
				write("Password: ")
				input = readN(12, "*")
				if input == "" then
				  term.setCursorPos(1, 18)
				  term.clearLine()
				  term.setCursorPos(15, 18)
				  print("Invalid Password Entry")
				  sleep(1)
				  ioerror = ioerror+1
				  if ioerror == 3 then
					rep1 = 1
					rep2 = 1
					rep3 = 1
				  else
				  end
				else
				  ioerror = 0
				  actpw = input
				  rep3 = 1
				  rep2 = 1
				end
			  end
			
			  term.setCursorPos(13, 16)
			  print("|						|")
			  term.setCursorPos(15, 16)
			  write("Confirm : ")
			  input = readN(12, "*")
			  actpc = input
			  if (actpc) == (actpw) then
				while rep4 == 0 do
				  rnd = tostring(math.random(1011, 9999))
				  if not fs.exists("pending/".. rnd) then
					file = fs.open("pending/".. rnd, "w")
					file.writeLine(actc)
					file.writeLine(actpw)
					file.close()
					rep4 = rep4+1
				  else
				  end
				end
				term.setCursorPos(1, 18)
				term.clearLine()
				term.setCursorPos(13, 18)
				print("  Pending... Ref #: ", rnd)
				sleep(1)
				ioerror = 0
				rep2 = 1
				sid = 0
			  else
				term.setCursorPos(1, 18)
				term.clearLine()
				term.setCursorPos(15, 18)
				print("Password Doesn't Match")
				sleep(1)
				rep2 = 0
				ioerror = ioerror+1
				if ioerror == 3 then
				  rep1 = 1
				  rep2 = 1
				  rep3 = 1
				else
				end
			  end
			end
		  
		  elseif sid == 2 then
			term.setCursorPos(13,14)
			print("|########################|")
			term.setCursorPos(13,15)
			print("# SYSTEM SHUTTING DOWN!! #")
			term.setCursorPos(13,16)
			print("|########################|")
			sleep(3)
			os.shutdown()
			end
		  end
		end

Thank you in advance!

::EDIT::

Just to clarify the files need to be on the server, run from the clients and displayed on the client side systems rather than the server.
Bomb Bloke #2
Posted 15 October 2013 - 09:49 AM
So if I'm understanding correctly, you want to be able to login into another computer remotely, then have access to the file system on that server?

I would do this by extending the shell somewhat (or writing my own version, more likely, but that's probably making things harder then it has to be). It'd capture attempts to get directory listings on the client drive and "add in" folders from the server. Attempting to "cd" into these folders would prompt for login credentials, and attempting to access files in those folders would see them transferred to the client before execution/editing/whatever. All other commands would just be passed directly to the regular shell to handle.

Exactly how you'd handle the actual file/directory access depends on whether you're main goal is security or convenience - are you simply wanting to be able to access all your files from any of your terminals, or are you really wanting to lock things down so that only you can access all your files from any of your terminals? What sort of files - scripts, data files, or both? You'd also need to take into account whether or not you mind having the server's files stored (even if temporarily) on the client's drives, as the complexity would greatly increase if that were taboo.
boudragon #3
Posted 15 October 2013 - 07:12 PM
Basically remote access. I want to be able to login via a terminal say 500 blocks away from my Nuclear Reactor where the server (system manager) for said reactor resides. In which case I could turn them on or off etc. This would also be use to manage messages sent and received from other users who have appropriate access. Having the files temp. to the client side isn't that big of a deal since the system will be fairly secured.
Bomb Bloke #4
Posted 15 October 2013 - 07:46 PM
Depending on the distances involved, you may need to configure a number of relay servers - wireless modems have limited range, and creating a 500 block long cable sounds more trouble then it's worth.

For starters, I recommend plonking down three or four computers. Write a program that allows each to detect the presense of the others - have it send a broadcast of its label on boot, then sit and wait for transmissions.

If a computer receives such a broadcast, have it add the label and ID of the computer that sent it to a table, then send a message containing that table to every computer in the table.

In this way, every computer that runs this script will detect other computers in their range, and notify those computers of what other computers they can reach. Have each computer print status updates etc as they discover new systems, and once you're happy you've got that working, you'd then want to start incorporating methods of distinguishing servers from clients and passing messages between clients through the server chain.
Lyqyd #5
Posted 15 October 2013 - 07:47 PM
Well, for the actual remote access part, you could try using something like nsh. To make your program compatible, you'd want a backend that does all of the actual control and a frontend program that could be started for each computer that connects to the server. The frontend programs would change global variables or the contents of files, which the backend program would use the values/contents of to control the system however you command it to.
boudragon #6
Posted 15 October 2013 - 07:58 PM
As far as distance thats not an issue. Most likely can use a world anchor or something similar to keep the chunk loaded. The furthest I have gotten was getting one computer to tell another to run a program and then to relay a line of text back to the original computer which was coded into the program that was run on the 2nd system. Reason why I came here after getting that to work is it seems like that would require a TON of editing my original code. If that's the cause at this point it almost seems like a total rewrite would be easier.

Well, for the actual remote access part, you could try using something like nsh. To make your program compatible, you'd want a backend that does all of the actual control and a frontend program that could be started for each computer that connects to the server. The frontend programs would change global variables or the contents of files, which the backend program would use the values/contents of to control the system however you command it to.

I will DEF. look into that when I have a bit more time. And how did I know this post might grab your attention? :-p
boudragon #7
Posted 16 October 2013 - 06:15 AM
Ok so I looked at your NSH and it looks promising! However I have no idea how I'd tie that into my software… still quite the noob! :-p Anyways… would it require much editing of mine (or your) code? Or does it work on the same principle of extending your computer display to another monitor?

::EDIT::

Ok so after fooling around with it for about 5 minutes I am VERY impressed with this! I think this is exactly what I need! I will let you know with further testing! If it works how I need it to I will be sure to give credit where it's due my friend thank you!