|
1 | 1 | -- Warning, given a bug in this program, the client could potentially access files outside the folder |
2 | 2 | -- Best to chroot/limit permissions for this server |
3 | 3 | local server, curclient, totalspace, curspace, port, label, change, debug, recurseCount |
4 | | -local clients, hndls, servert = {}, {}, {} |
| 4 | +local sockets, hndls = {}, {} |
5 | 5 |
|
6 | 6 | -- Configuration |
7 | 7 | totalspace = math.huge |
@@ -86,7 +86,7 @@ if not stat then |
86 | 86 | end |
87 | 87 | local sID, sPort = server:getsockname() |
88 | 88 | server:settimeout(0) |
89 | | -servert[1] = server |
| 89 | +sockets[1] = server |
90 | 90 |
|
91 | 91 | print("Listening on " .. sID .. ":" .. sPort) |
92 | 92 |
|
@@ -172,30 +172,29 @@ local function checkArg(pos,obj,what) |
172 | 172 | end |
173 | 173 |
|
174 | 174 | local function update() |
175 | | - -- Check for new clients |
176 | | - local test = socket.select(servert,nil,0) |
177 | | - if test and test[server] then |
178 | | - local client = server:accept() |
179 | | - if client ~= nil then |
180 | | - local ci,cp = client:getpeername() |
181 | | - print("User connected from: " .. ci .. ":" .. cp) |
182 | | - clients[#clients + 1] = client |
183 | | - client:settimeout(0) |
| 175 | + -- Check for new data or new clients |
| 176 | + local ready, err, err2 = socket.select(sockets,nil) or {} |
| 177 | + for _, client in ipairs(ready) do |
| 178 | + if client == server then |
| 179 | + client = server:accept() |
| 180 | + if client ~= nil then |
| 181 | + local ci,cp = client:getpeername() |
| 182 | + print("User connected from: " .. ci .. ":" .. cp) |
| 183 | + sockets[#sockets + 1] = client |
| 184 | + client:settimeout(0) |
| 185 | + end |
| 186 | + break |
184 | 187 | end |
185 | | - end |
186 | | - -- Check for new data |
187 | | - local ready = socket.select(clients,nil,0) or {} |
188 | | - for _,client in ipairs(ready) do |
189 | 188 | curclient = client |
190 | 189 | local line, err = client:receive() |
191 | 190 | if not line then |
192 | 191 | print("socket receive gave: " .. err) |
193 | 192 | if err ~= "closed" then |
194 | 193 | pcall(client.close,client) |
195 | 194 | end |
196 | | - for i = 1,#clients do |
197 | | - if clients[i] == client then |
198 | | - table.remove(clients, i) |
| 195 | + for i = 1,#sockets do |
| 196 | + if sockets[i] == client then |
| 197 | + table.remove(sockets, i) |
199 | 198 | break |
200 | 199 | end |
201 | 200 | end |
|
0 commit comments