Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server-side code to handle user authentication requests
- local SERVER_PORT = 5000
- local USERS_FOLDER = "/disk/users/"
- local DOMAIN_FILE = "/disk/domain.txt"
- local function handleClient(client)
- local computerID = client.read()
- local username = client.read()
- local password = client.read()
- -- Check if the computer ID is valid
- local domainFile = fs.open(DOMAIN_FILE, "r")
- local allowedID = domainFile.readLine()
- domainFile.close()
- if computerID ~= allowedID then
- client.write("FAIL")
- client.close()
- return
- end
- -- Check user credentials
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- if fs.exists(passwordFile) then
- local file = fs.open(passwordFile, "r")
- local storedPassword = file.readLine()
- file.close()
- if password == storedPassword then
- client.write("OK")
- else
- client.write("FAIL")
- end
- else
- client.write("FAIL")
- end
- client.close()
- end
- local function startServer()
- local socket = require("socket")
- local server = socket.tcp()
- server:bind("*", SERVER_PORT)
- server:listen()
- while true do
- local client = server:accept()
- parallel.waitForAny(
- function()
- handleClient(client)
- end
- )
- end
- end
- startServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement