serverlistmgr.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. --Minetest
  2. --Copyright (C) 2020 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. serverlistmgr = {
  18. -- continent code we detected for ourselves
  19. my_continent = nil,
  20. -- list of locally favorites servers
  21. favorites = nil,
  22. -- list of servers fetched from public list
  23. servers = nil,
  24. }
  25. do
  26. if check_cache_age("geoip_last_checked", 3600) then
  27. local tmp = cache_settings:get("geoip") or ""
  28. if tmp:match("^[A-Z][A-Z]$") then
  29. serverlistmgr.my_continent = tmp
  30. end
  31. end
  32. end
  33. --------------------------------------------------------------------------------
  34. -- Efficient data structure for normalizing arbitrary scores attached to objects
  35. -- e.g. {{"a", 3.14}, {"b", 3.14}, {"c", 20}, {"d", 0}}
  36. -- -> {["d"] = 0, ["a"] = 0.5, ["b"] = 0.5, ["c"] = 1}
  37. local Normalizer = {}
  38. function Normalizer:new()
  39. local t = {
  40. map = {}
  41. }
  42. setmetatable(t, self)
  43. self.__index = self
  44. return t
  45. end
  46. function Normalizer:push(obj, score)
  47. if not self.map[score] then
  48. self.map[score] = {}
  49. end
  50. local t = self.map[score]
  51. t[#t + 1] = obj
  52. end
  53. function Normalizer:calc()
  54. local list = {}
  55. for k, _ in pairs(self.map) do
  56. list[#list + 1] = k
  57. end
  58. table.sort(list)
  59. local ret = {}
  60. for i, k in ipairs(list) do
  61. local score = #list == 1 and 1 or ( (i - 1) / (#list - 1) )
  62. for _, obj in ipairs(self.map[k]) do
  63. ret[obj] = score
  64. end
  65. end
  66. return ret
  67. end
  68. --------------------------------------------------------------------------------
  69. -- how much the pre-sorted server list contributes to the final ranking
  70. local WEIGHT_SORT = 2
  71. -- how much the estimated latency contributes to the final ranking
  72. local WEIGHT_LATENCY = 1
  73. --- @param list of servers, will be modified.
  74. local function order_server_list_internal(list)
  75. -- calculate the scores
  76. local s1 = Normalizer:new()
  77. local s2 = Normalizer:new()
  78. for i, fav in ipairs(list) do
  79. -- first: the original position
  80. s1:push(fav, #list - i)
  81. -- second: estimated latency
  82. local ping = (fav.ping or 0) * 1000
  83. if ping < 400 then
  84. -- If ping is under 400ms replace it with our own estimate,
  85. -- we assume the server has latency issues anyway otherwise
  86. ping = estimate_continent_latency(serverlistmgr.my_continent, fav) or 0
  87. end
  88. s2:push(fav, -ping)
  89. end
  90. s1 = s1:calc()
  91. s2 = s2:calc()
  92. -- pre-calculate ordering
  93. local order = {}
  94. for _, fav in ipairs(list) do
  95. order[fav] = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
  96. end
  97. -- now sort the list
  98. table.sort(list, function(fav1, fav2)
  99. return order[fav1] > order[fav2]
  100. end)
  101. end
  102. local function order_server_list(list)
  103. -- split the list into two parts and sort them separately, to keep empty
  104. -- servers at the bottom.
  105. local nonempty, empty = {}, {}
  106. for _, fav in ipairs(list) do
  107. if (fav.clients or 0) > 0 then
  108. table.insert(nonempty, fav)
  109. else
  110. table.insert(empty, fav)
  111. end
  112. end
  113. order_server_list_internal(nonempty)
  114. order_server_list_internal(empty)
  115. table.insert_all(nonempty, empty)
  116. return nonempty
  117. end
  118. local public_downloading = false
  119. local geoip_downloading = false
  120. --------------------------------------------------------------------------------
  121. local function fetch_geoip()
  122. local http = core.get_http_api()
  123. local url = core.settings:get("serverlist_url") .. "/geoip"
  124. local response = http.fetch_sync({ url = url })
  125. if not response.succeeded then
  126. return
  127. end
  128. local retval = core.parse_json(response.data)
  129. if type(retval) ~= "table" then
  130. return
  131. end
  132. return type(retval.continent) == "string" and retval.continent
  133. end
  134. function serverlistmgr.sync()
  135. if not serverlistmgr.servers then
  136. serverlistmgr.servers = {{
  137. name = fgettext("Loading..."),
  138. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  139. }}
  140. end
  141. local serverlist_url = core.settings:get("serverlist_url") or ""
  142. if not core.get_http_api or serverlist_url == "" then
  143. serverlistmgr.servers = {{
  144. name = fgettext("Public server list is disabled"),
  145. description = ""
  146. }}
  147. return
  148. end
  149. if not serverlistmgr.my_continent and not geoip_downloading then
  150. geoip_downloading = true
  151. core.handle_async(fetch_geoip, nil, function(result)
  152. geoip_downloading = false
  153. if not result then
  154. return
  155. end
  156. serverlistmgr.my_continent = result
  157. cache_settings:set("geoip", result)
  158. cache_settings:set("geoip_last_checked", tostring(os.time()))
  159. -- re-sort list if applicable
  160. if serverlistmgr.servers then
  161. serverlistmgr.servers = order_server_list(serverlistmgr.servers)
  162. core.event_handler("Refresh")
  163. end
  164. end)
  165. end
  166. if public_downloading then
  167. return
  168. end
  169. public_downloading = true
  170. -- note: this isn't cached because it's way too dynamic
  171. core.handle_async(
  172. function(param)
  173. local http = core.get_http_api()
  174. local url = ("%s/list?proto_version_min=%d&proto_version_max=%d"):format(
  175. core.settings:get("serverlist_url"),
  176. core.get_min_supp_proto(),
  177. core.get_max_supp_proto())
  178. local response = http.fetch_sync({ url = url })
  179. if not response.succeeded then
  180. return {}
  181. end
  182. local retval = core.parse_json(response.data)
  183. return retval and retval.list or {}
  184. end,
  185. nil,
  186. function(result)
  187. public_downloading = false
  188. local favs = order_server_list(result)
  189. if favs[1] then
  190. serverlistmgr.servers = favs
  191. end
  192. core.event_handler("Refresh")
  193. end
  194. )
  195. end
  196. --------------------------------------------------------------------------------
  197. local function get_favorites_path(folder)
  198. local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM
  199. if folder then
  200. return base
  201. end
  202. return base .. core.settings:get("serverlist_file")
  203. end
  204. --------------------------------------------------------------------------------
  205. local function save_favorites(favorites)
  206. local filename = core.settings:get("serverlist_file")
  207. -- If setting specifies legacy format change the filename to the new one
  208. if filename:sub(#filename - 3):lower() == ".txt" then
  209. core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json")
  210. end
  211. assert(core.create_dir(get_favorites_path(true)))
  212. core.safe_file_write(get_favorites_path(), core.write_json(favorites))
  213. end
  214. --------------------------------------------------------------------------------
  215. function serverlistmgr.read_legacy_favorites(path)
  216. local file = io.open(path, "r")
  217. if not file then
  218. return nil
  219. end
  220. local lines = {}
  221. for line in file:lines() do
  222. lines[#lines + 1] = line
  223. end
  224. file:close()
  225. local favorites = {}
  226. local i = 1
  227. while i < #lines do
  228. local function pop()
  229. local line = lines[i]
  230. i = i + 1
  231. return line and line:trim()
  232. end
  233. if pop():lower() == "[server]" then
  234. local name = pop()
  235. local address = pop()
  236. local port = tonumber(pop())
  237. local description = pop()
  238. if name == "" then
  239. name = nil
  240. end
  241. if description == "" then
  242. description = nil
  243. end
  244. if not address or #address < 3 then
  245. core.log("warning", "Malformed favorites file, missing address at line " .. i)
  246. elseif not port or port < 1 or port > 65535 then
  247. core.log("warning", "Malformed favorites file, missing port at line " .. i)
  248. elseif (name and name:upper() == "[SERVER]") or
  249. (address and address:upper() == "[SERVER]") or
  250. (description and description:upper() == "[SERVER]") then
  251. core.log("warning", "Potentially malformed favorites file, overran at line " .. i)
  252. else
  253. favorites[#favorites + 1] = {
  254. name = name,
  255. address = address,
  256. port = port,
  257. description = description
  258. }
  259. end
  260. end
  261. end
  262. return favorites
  263. end
  264. --------------------------------------------------------------------------------
  265. local function read_favorites()
  266. local path = get_favorites_path()
  267. -- If new format configured fall back to reading the legacy file
  268. if path:sub(#path - 4):lower() == ".json" then
  269. local file = io.open(path, "r")
  270. if file then
  271. local json = file:read("*all")
  272. file:close()
  273. return core.parse_json(json)
  274. end
  275. path = path:sub(1, #path - 5) .. ".txt"
  276. end
  277. local favs = serverlistmgr.read_legacy_favorites(path)
  278. if favs then
  279. save_favorites(favs)
  280. os.remove(path)
  281. end
  282. return favs
  283. end
  284. --------------------------------------------------------------------------------
  285. local function delete_favorite(favorites, del_favorite)
  286. for i=1, #favorites do
  287. local fav = favorites[i]
  288. if fav.address == del_favorite.address and fav.port == del_favorite.port then
  289. table.remove(favorites, i)
  290. return
  291. end
  292. end
  293. end
  294. --------------------------------------------------------------------------------
  295. function serverlistmgr.get_favorites()
  296. if serverlistmgr.favorites then
  297. return serverlistmgr.favorites
  298. end
  299. serverlistmgr.favorites = {}
  300. -- Add favorites, removing duplicates
  301. local seen = {}
  302. for _, fav in ipairs(read_favorites() or {}) do
  303. local key = ("%s:%d"):format(fav.address:lower(), fav.port)
  304. if not seen[key] then
  305. seen[key] = true
  306. serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav
  307. end
  308. end
  309. return serverlistmgr.favorites
  310. end
  311. --------------------------------------------------------------------------------
  312. function serverlistmgr.add_favorite(new_favorite)
  313. assert(type(new_favorite.port) == "number")
  314. -- Whitelist favorite keys
  315. new_favorite = {
  316. name = new_favorite.name,
  317. address = new_favorite.address,
  318. port = new_favorite.port,
  319. description = new_favorite.description,
  320. }
  321. local favorites = serverlistmgr.get_favorites()
  322. delete_favorite(favorites, new_favorite)
  323. table.insert(favorites, 1, new_favorite)
  324. save_favorites(favorites)
  325. end
  326. --------------------------------------------------------------------------------
  327. function serverlistmgr.delete_favorite(del_favorite)
  328. local favorites = serverlistmgr.get_favorites()
  329. delete_favorite(favorites, del_favorite)
  330. save_favorites(favorites)
  331. end