iteration
This commit is contained in:
		
							
								
								
									
										17
									
								
								auth.lua
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								auth.lua
									
									
									
									
									
								
							@ -6,7 +6,20 @@ if args[1] == nil then
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local action = args[1]
 | 
			
		||||
local server = "auth.layla.mc"
 | 
			
		||||
 | 
			
		||||
settings.define("auth.token", {
 | 
			
		||||
    description = "Authentication token",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = ""
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
settings.define("auth.server", {
 | 
			
		||||
    description = "Authentication server",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = "auth.box"
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
local server = settings.get("auth.server")
 | 
			
		||||
 | 
			
		||||
function lns_lookup(hostname)
 | 
			
		||||
    local data = {
 | 
			
		||||
@ -65,8 +78,8 @@ if action == "login" then
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    settings.set("auth.token", token)
 | 
			
		||||
    io.write("Logged in as " .. username .. "\n")
 | 
			
		||||
    io.write("Token: " .. token .. "\n")
 | 
			
		||||
    return
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,31 @@
 | 
			
		||||
 | 
			
		||||
modem_location = "right"
 | 
			
		||||
settings.define("lns_server.modem_location", {
 | 
			
		||||
    description = "Modem location",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = "right"
 | 
			
		||||
})
 | 
			
		||||
modem_location = settings.get("lns_server.modem_location")
 | 
			
		||||
 | 
			
		||||
settings.define("lns_server.rednet_hostname", {
 | 
			
		||||
    description = "Rednet hostname",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = "test"
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
rednet.host("lns", "default")
 | 
			
		||||
settings.define("lns_server.require_auth", {
 | 
			
		||||
    description = "Require authentication",
 | 
			
		||||
    type = "boolean",
 | 
			
		||||
    default = false
 | 
			
		||||
})
 | 
			
		||||
require_auth = settings.get("lns_server.require_auth")
 | 
			
		||||
 | 
			
		||||
settings.define("lns_server.auth_server", {
 | 
			
		||||
    description = "Authentication server",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = "auth.box"
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
rednet.host("lns", settings.get("lns_server.rednet_hostname"))
 | 
			
		||||
rednet.open(modem_location)
 | 
			
		||||
 | 
			
		||||
local data = {}
 | 
			
		||||
@ -68,9 +91,37 @@ while true do
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if request.action == "register" then
 | 
			
		||||
 | 
			
		||||
        local auth_passed = false
 | 
			
		||||
        if require_auth then
 | 
			
		||||
            local authID = data[auth_server]
 | 
			
		||||
            if authID ~= nil then
 | 
			
		||||
                rednet.send(authID, {
 | 
			
		||||
                    action = "token",
 | 
			
		||||
                    token = request.token
 | 
			
		||||
                })
 | 
			
		||||
 | 
			
		||||
                local auth_response = rednet.receive()
 | 
			
		||||
                local errorPos, errorEnd = string.find(auth_response, "invalid")
 | 
			
		||||
                if errorPos then
 | 
			
		||||
                    log("Error: " .. auth_response)
 | 
			
		||||
                else
 | 
			
		||||
                    auth_passed = true
 | 
			
		||||
                end
 | 
			
		||||
            else
 | 
			
		||||
                log("Error: Auth server not found to " .. client_id)
 | 
			
		||||
            end
 | 
			
		||||
        else
 | 
			
		||||
            auth_passed = true
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        if auth_passed then
 | 
			
		||||
            data[request.hostname] = client_id
 | 
			
		||||
            log("Registered " .. request.hostname .. " as " .. client_id)
 | 
			
		||||
            save_data()
 | 
			
		||||
            rednet.send(client_id, "ok")
 | 
			
		||||
        else
 | 
			
		||||
            rednet.send(client_id, "auth required")
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
@ -21,7 +21,8 @@ current_time = os.time()
 | 
			
		||||
 | 
			
		||||
data = {
 | 
			
		||||
    ["action"] = "register",
 | 
			
		||||
    ["hostname"] = hostname
 | 
			
		||||
    ["hostname"] = hostname,
 | 
			
		||||
    ["token"] = settings.get("auth.token", nil)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
rednet.send(receiverID, data)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user