iteration
This commit is contained in:
		@ -49,6 +49,10 @@ function hash(str)
 | 
			
		||||
    return hash
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function log(str)
 | 
			
		||||
    io.write("[" .. os.time() .. "] " .. str .. "\n")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
load_data()
 | 
			
		||||
math.randomseed(os.time())
 | 
			
		||||
 | 
			
		||||
@ -76,21 +80,23 @@ while true do
 | 
			
		||||
        save_data()
 | 
			
		||||
 | 
			
		||||
        rednet.send(client_id, "ok")
 | 
			
		||||
        log(request.username .. " registered")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if request.action == "login" then
 | 
			
		||||
        if request.username == nil or request.password == nil then
 | 
			
		||||
            rednet.send(client_id, "invalid request")
 | 
			
		||||
        end
 | 
			
		||||
            
 | 
			
		||||
        if data.users[request.username] == nil then
 | 
			
		||||
        else if data.users[request.username] == nil then
 | 
			
		||||
            rednet.send(client_id, "user not found")
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        if convert_password(request.password) == data.users[request.username].password then
 | 
			
		||||
            log(request.username .. " failed log in attempt")
 | 
			
		||||
        else if convert_password(request.password) == data.users[request.username].password then
 | 
			
		||||
            local token = generate_token(request.username)
 | 
			
		||||
 | 
			
		||||
            rednet.send(client_id, token)
 | 
			
		||||
 | 
			
		||||
            log(request.username .. " logged in")
 | 
			
		||||
        else
 | 
			
		||||
            rednet.send(client_id, "invalid password")
 | 
			
		||||
        end
 | 
			
		||||
@ -100,7 +106,7 @@ while true do
 | 
			
		||||
    if request.action == "token" then
 | 
			
		||||
        if request.token == nil then
 | 
			
		||||
            rednet.send(client_id, "invalid request")
 | 
			
		||||
        end
 | 
			
		||||
        else
 | 
			
		||||
 | 
			
		||||
            for user, userdata in pairs(data.users) do
 | 
			
		||||
                if userdata.token == request.token then
 | 
			
		||||
@ -109,6 +115,8 @@ while true do
 | 
			
		||||
            end
 | 
			
		||||
 | 
			
		||||
            rednet.send(client_id, "invalid token")
 | 
			
		||||
            log(request.username .. " failed token check")
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if request.action == "profile" then
 | 
			
		||||
@ -142,6 +150,7 @@ while true do
 | 
			
		||||
            save_data()
 | 
			
		||||
 | 
			
		||||
            rednet.send(client_id, "ok")
 | 
			
		||||
            log(request.username .. " updated their profile")
 | 
			
		||||
        else
 | 
			
		||||
            rednet.send(client_id, "invalid token")
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										68
									
								
								lns.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								lns.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
			
		||||
args = { ... }
 | 
			
		||||
 | 
			
		||||
cache = {}
 | 
			
		||||
 | 
			
		||||
allow_dynamic_lns_hosts = false
 | 
			
		||||
 | 
			
		||||
function lns_lookup(hostname)
 | 
			
		||||
    local data = {
 | 
			
		||||
        ["action"] = "lookup",
 | 
			
		||||
        ["hostname"] = hostname
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    local lns_server_id = 7
 | 
			
		||||
    if allow_dynamic_lns_hosts then
 | 
			
		||||
        lns_server_id = rednet.lookup("lns", lns_server)
 | 
			
		||||
        if lns_server_id == nil then
 | 
			
		||||
            io.write("LNS Server not found!\n")
 | 
			
		||||
            return
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    rednet.send(lns_server_id, data)
 | 
			
		||||
 | 
			
		||||
    while true do
 | 
			
		||||
        id, msg = rednet.receive()
 | 
			
		||||
        if id == lns_server_id then
 | 
			
		||||
            if msg == nil then
 | 
			
		||||
                return nil
 | 
			
		||||
            else
 | 
			
		||||
                return msg
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local action = args[1]
 | 
			
		||||
if action == nil then
 | 
			
		||||
    io.write("Usage: lns <action>\n")
 | 
			
		||||
    return
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if action == "lookup" then
 | 
			
		||||
    local hostname = args[2]
 | 
			
		||||
    if hostname == nil then
 | 
			
		||||
        io.write("Usage: lns lookup <hostname>\n")
 | 
			
		||||
        return
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if cache[hostname] ~= nil then
 | 
			
		||||
        io.write(cache[hostname])
 | 
			
		||||
        return
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    local id = lns_lookup(hostname)
 | 
			
		||||
    cache[hostname] = id
 | 
			
		||||
    if id ~= nil then
 | 
			
		||||
        io.write(id)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
else if action == "clear" then
 | 
			
		||||
    local data = {
 | 
			
		||||
        ["action"] = "reload"
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lns_server_id = rednet.lookup("lns", lns_server)
 | 
			
		||||
    rednet.send(lns_server_id, data)
 | 
			
		||||
    print("Reloaded LNS Server")
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user