init.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. -- For the future:
  2. -- Reduce checks if the server is laggy (globalstep is slowed down).
  3. -- * Requires: a reliable way to determine current server lag.
  4. -- Increase checks on a player if others report them for cheating.
  5. -- * Requires: a reliable way for players to report cheaters that is not
  6. -- * trivially open to abuse.
  7. ac = ac or {}
  8. ac.modpath = minetest.get_modpath("ac")
  9. ac.wpath = minetest.get_worldpath()
  10. ac.players = ac.players or {} -- Per-player data for this session ONLY.
  11. -- Random delay to first check on player's first join (this session).
  12. ac.initial_delay_min = 1
  13. ac.initial_delay_max = 20
  14. -- Default (base) random delay between player checks.
  15. ac.default_delay_min = 5
  16. ac.default_delay_max = 30
  17. -- Random time to decrease between checks for players in city areas.
  18. ac.city_reduce_min = 1
  19. ac.city_reduce_max = 20
  20. -- Random time to add between checks for players in outlands.
  21. ac.outland_increase_min = 5
  22. ac.outland_increase_max = 30
  23. -- The average amounts of accumulated suspicion per session considered low/high.
  24. -- Average suspicion is calculated by total suspicion ever recorded, divided by
  25. -- number of clean sessions in which no suspicion was recorded (for that player).
  26. ac.low_average_suspicion = 5
  27. ac.high_average_suspicion = 20
  28. -- Random time to decrease between checks for players with high avg suspicion.
  29. ac.high_suspicion_reduce_min = 1
  30. ac.high_suspicion_reduce_max = 10
  31. -- Random time to add between checks for players with low suspicion.
  32. ac.low_suspicion_increase_min = 1
  33. ac.low_suspicion_increase_max = 10
  34. -- Once accumulated suspicion for a single session (not including total suspicion
  35. -- over all sessions) exceeds this amount, player is registered as a confirmed
  36. -- cheater. Note that the player will not automatically be registered as a cheater
  37. -- if they merely have high avg suspicion over multiple sessions.
  38. ac.cheat_registration_threshold = 50
  39. ac.admin_name = "MustTest"
  40. -- Open logfile if not already opened.
  41. if not ac.logfile then
  42. -- Open file in append mode.
  43. ac.logfile = io.open(ac.wpath .. "/ac.txt", "a")
  44. end
  45. -- Open mod storage if not already opened.
  46. if not ac.storage then
  47. ac.storage = minetest.get_mod_storage()
  48. end
  49. function ac.get_suspicion_count(pname)
  50. if ac.players[pname] then
  51. if ac.players[pname].suspicion_count then
  52. -- Must be a non-negative integer.
  53. return ac.players[pname].suspicion_count
  54. end
  55. end
  56. return 0
  57. end
  58. -- Get timestamp of the last time a cheat was detected by the standard checking
  59. -- function (and its confirmation spawns), not including cheats detected along a
  60. -- path.
  61. function ac.get_last_cheat_time(pname)
  62. if ac.players[pname] then
  63. if ac.players[pname].last_cheat_time then
  64. return ac.players[pname].last_cheat_time
  65. end
  66. end
  67. return 0
  68. end
  69. function ac.get_total_suspicion(pname)
  70. if ac.players[pname] then
  71. local cs = ac.get_suspicion_count(pname)
  72. if ac.players[pname].total_suspicion then
  73. -- Must be a non-negative integer.
  74. return ac.players[pname].total_suspicion + cs
  75. end
  76. local k = pname .. ":total_suspicion"
  77. local ts = ac.storage:get_int(k)
  78. ac.players[pname].total_suspicion = ts
  79. return ts + cs
  80. else
  81. local cs = ac.get_suspicion_count(pname)
  82. local k = pname .. ":total_suspicion"
  83. local ts = ac.storage:get_int(k)
  84. ac.players[pname] = {total_suspicion=ts}
  85. return ts + cs
  86. end
  87. return 0
  88. end
  89. function ac.get_clean_sessions(pname)
  90. if ac.players[pname] then
  91. if ac.players[pname].clean_sessions then
  92. -- Must be a non-negative integer.
  93. return ac.players[pname].clean_sessions
  94. end
  95. local k = pname .. ":clean_sessions"
  96. local cc = ac.storage:get_int(k)
  97. ac.players[pname].clean_sessions = cc
  98. return cc
  99. else
  100. local k = pname .. ":clean_sessions"
  101. local cc = ac.storage:get_int(k)
  102. ac.players[pname] = {clean_sessions=cc}
  103. return cc
  104. end
  105. return 0
  106. end
  107. function ac.get_position_at_last_check_or_nil(pname)
  108. if ac.players[pname] then
  109. if ac.players[pname].last_pos then
  110. return ac.players[pname].last_pos
  111. end
  112. end
  113. end
  114. -- Log to file.
  115. function ac.log_suspicious_act(pname, pos, time, act)
  116. local s = pname .. "|" .. act .. "|" .. time .. "|" ..
  117. math.floor(pos.x) .. "," .. math.floor(pos.y) .. "," .. math.floor(pos.z) ..
  118. "|" .. ac.get_suspicion_count(pname) .. "\n"
  119. ac.logfile:write(s)
  120. ac.logfile:flush()
  121. end
  122. -- Record in current session memory.
  123. -- Note: this may be called out of sequence! Therefore we shouldn't use current
  124. -- time or player's current position.
  125. function ac.record_suspicious_act(pname, time, act)
  126. local pdata = ac.players[pname]
  127. if not pdata then
  128. ac.players[pname] = {}
  129. pdata = ac.players[pname]
  130. end
  131. -- Increment suspicion count.
  132. pdata.suspicion_count = (pdata.suspicion_count or 0) + 1
  133. if act == "fly" then
  134. pdata.fly_count = (pdata.fly_count or 0) + 1
  135. elseif act == "clip" then
  136. pdata.clip_count = (pdata.clip_count or 0) + 1
  137. end
  138. -- This is used for recording the most recent last time when a cheat was
  139. -- detected by the cheat-confirmation functions. This should be nil when we're
  140. -- checking a path, as that is considered "a separate feature". Cheats detected
  141. -- along a path should all be considered part of the same "instance" of cheating,
  142. -- so we don't record timestamps in that case.
  143. if time ~= nil then
  144. pdata.last_cheat_time = time
  145. end
  146. end
  147. -- Report to admin (if logged in).
  148. function ac.report_suspicious_act(pname, pos, act)
  149. local pref = minetest.get_player_by_name(ac.admin_name)
  150. if pref then
  151. minetest.chat_send_player(ac.admin_name,
  152. "# Server: <" .. rename.gpn(pname) ..
  153. "> caught in suspicious activity: '" .. act .. "' at " ..
  154. rc.pos_to_namestr(pos) .. ". Suspicion: " ..
  155. ac.get_suspicion_count(pname) .. ".")
  156. end
  157. end
  158. function ac.record_player_position(pname, pos)
  159. local pdata = ac.players[pname]
  160. if not pdata then
  161. ac.players[pname] = {}
  162. pdata = ac.players[pname]
  163. end
  164. -- This is for recording the position of the player when they were last
  165. -- checked by the standard check function. Thus, if the standard check func
  166. -- detects a possible cheat, following sub-checks that get spawned via
  167. -- minetest.after() can refer back to the player's position at the first
  168. -- check.
  169. pdata.last_pos = pos
  170. end
  171. -- This function is the main "brain" of the AC logic (for fly cheaters).
  172. -- It must be as accurate as possible for a given position!
  173. function ac.is_flying(pos)
  174. -- We assume that the input position is rounded to nearest integer.
  175. local under = vector.add(pos, {x=0, y=-1, z=0})
  176. local node = minetest.get_node(under)
  177. -- If non-air below this position, then player is probably not flying.
  178. if node.name ~= "air" then return false end
  179. -- Check up to 2 meters below player, and 1 meter all around.
  180. -- Fly cheaters tend to be pretty blatent in their cheating,
  181. -- and I want to avoid logging players who do a lot of jumping.
  182. local minp = {x=pos.x-1, y=pos.y-2, z=pos.z-1}
  183. local maxp = {x=pos.x+1, y=pos.y+0, z=pos.z+1}
  184. local tb = minetest.find_nodes_in_area(minp, maxp, "air")
  185. if #tb >= 27 then
  186. -- If all nodes under player are air, then player is not supported.
  187. return true
  188. end
  189. -- Not flying.
  190. return false
  191. end
  192. local is_solid_dt = function(dt)
  193. if dt == "normal" then
  194. return true
  195. elseif dt == "glasslike" then
  196. return true
  197. elseif dt == "glasslike_framed" then
  198. return true
  199. elseif dt == "glasslike_framed_optional" then
  200. return true
  201. elseif dt == "allfaces" then
  202. return true
  203. elseif dt == "allfaces_optional" then
  204. return true
  205. end
  206. end
  207. -- This function is the main "brain" of the AC logic (for noclip cheaters).
  208. -- It must be as accurate as possible for a given position!
  209. function ac.is_clipping(pos)
  210. -- We assume that the input position is rounded to nearest integer.
  211. local under = vector.add(pos, {x=0, y=-1, z=0})
  212. local above = vector.add(pos, {x=0, y=1, z=0})
  213. local n1 = minetest.get_node(under)
  214. local n2 = minetest.get_node(pos)
  215. local n3 = minetest.get_node(above)
  216. if n1.name ~= "air" and n2.name ~= "air" and n3.name ~= "air" then
  217. local d1 = minetest.reg_ns_nodes[n1.name]
  218. local d2 = minetest.reg_ns_nodes[n2.name]
  219. local d3 = minetest.reg_ns_nodes[n3.name]
  220. -- One of the nodes is a stairsplus node, or similar.
  221. if not d1 or not d2 or not d3 then
  222. return false
  223. end
  224. -- Check if all three nodes are solid, walkable nodes.
  225. if d1.walkable and d2.walkable and d3.walkable then
  226. local d1d = d1.drawtype
  227. local d2d = d2.drawtype
  228. local d3d = d3.drawtype
  229. if is_solid_dt(d1d) and is_solid_dt(d2d) and is_solid_dt(d3d) then
  230. return true
  231. end
  232. end
  233. end
  234. -- Not clipping.
  235. return false
  236. end
  237. function ac.check_prior_position(pname, pos, time, act)
  238. local cheat = false
  239. if act == "fly" then
  240. if ac.is_flying(pos) then cheat = true end
  241. elseif act == "clip" then
  242. if ac.is_clipping(pos) then cheat = true end
  243. end
  244. if cheat then
  245. ac.record_suspicious_act(pname, nil, act) -- Record in current session memory.
  246. ac.report_suspicious_act(pname, pos, act) -- Report to admin (if logged in).
  247. ac.log_suspicious_act(pname, pos, time, act) -- Log to file.
  248. end
  249. end
  250. function ac.check_prior_path(pname, act)
  251. -- Get prior known locations for this player.
  252. -- Locations should be provided in order, with timestamps.
  253. local path = ap.get_position_list(pname)
  254. -- Spread checking of the path out over a few seconds.
  255. for i=1, #path, 1 do
  256. local t = path[i]
  257. local delay = (math.random(1, 300) / 300) -- Get fractional random number.
  258. minetest.after(delay, ac.check_prior_position, pname, vector.round(t.pos), t.time, act)
  259. end
  260. end
  261. function ac.confirm_flying(pname, last_pos)
  262. -- Check if player still logged on.
  263. -- This function is designed to be called from minetest.after right after an
  264. -- initial trigger of suspicion, to try and confirm it.
  265. local pref = minetest.get_player_by_name(pname)
  266. if pref then
  267. local pos = vector.round(pref:get_pos())
  268. -- If player is falling at least somewhat quickly, then they aren't flying.
  269. if pos.y < (last_pos.y - 1) then return end
  270. -- If player stopped flying, then it might have been a false-positive.
  271. if not ac.is_flying(pos) then return end
  272. local time = os.time()
  273. local prevtime = ac.get_last_cheat_time(pname)
  274. -- If we reach here then the player is still flying!
  275. ac.record_suspicious_act(pname, time, "fly") -- Record in current session memory.
  276. ac.report_suspicious_act(pname, pos, "fly") -- Report to admin (if logged in).
  277. ac.log_suspicious_act(pname, pos, time, "fly") -- Log to file.
  278. -- Register as confirmed cheater if suspicion for this session exceeds threshold.
  279. local ts = ac.get_suspicion_count(pname)
  280. if ts > ac.cheat_registration_threshold then
  281. if not sheriff.is_cheater(pname) then
  282. sheriff.register_cheater(pname)
  283. end
  284. end
  285. -- Check the player's prior path if we haven't done so recently.
  286. if (time - prevtime) > ap.get_record_time() then
  287. ac.check_prior_path(pname, "fly")
  288. end
  289. end
  290. end
  291. function ac.confirm_clipping(pname, last_pos)
  292. -- Check if player still logged on.
  293. -- This function is designed to be called from minetest.after right after an
  294. -- initial trigger of suspicion, to try and confirm it.
  295. local pref = minetest.get_player_by_name(pname)
  296. if pref then
  297. local pos = vector.round(pref:get_pos())
  298. -- If player stopped clipping, then it might have been a false-positive.
  299. if not ac.is_clipping(pos) then return end
  300. local time = os.time()
  301. local prevtime = ac.get_last_cheat_time(pname)
  302. -- If we reach here then the player is still clipping!
  303. ac.record_suspicious_act(pname, time, "clip") -- Record in current session memory.
  304. ac.report_suspicious_act(pname, pos, "clip") -- Report to admin (if logged in).
  305. ac.log_suspicious_act(pname, pos, time, "clip") -- Log to file.
  306. -- Register as confirmed cheater if suspicion for this session exceeds threshold.
  307. local ts = ac.get_suspicion_count(pname)
  308. if ts > ac.cheat_registration_threshold then
  309. if not sheriff.is_cheater(pname) then
  310. sheriff.register_cheater(pname)
  311. end
  312. end
  313. -- Check the player's prior path if we haven't done so recently.
  314. if (time - prevtime) > ap.get_record_time() then
  315. ac.check_prior_path(pname, "clip")
  316. end
  317. end
  318. end
  319. function ac.do_standard_check(pname, pref)
  320. --minetest.chat_send_player(pname, "# Server: Check player!")
  321. local pos = vector.round(pref:get_pos())
  322. if ac.is_flying(pos) then
  323. -- Check again in a moment.
  324. local delay = math.random(1, 3)
  325. minetest.after(delay, ac.confirm_flying, pname, pos)
  326. end
  327. if ac.is_clipping(pos) then
  328. -- Check again in a moment.
  329. local delay = math.random(1, 3)
  330. minetest.after(delay, ac.confirm_clipping, pname, pos)
  331. end
  332. end
  333. function ac.nearby_player_count(pname, pref)
  334. local p1 = pref:get_pos()
  335. local players = minetest.get_connected_players()
  336. local count = 0
  337. for k, v in ipairs(players) do
  338. if v:get_player_name() ~= pname then
  339. local p2 = v:get_pos()
  340. if vector.distance(p1, p2) < 75 then
  341. count = count + 1
  342. end
  343. end
  344. end
  345. return count
  346. end
  347. function ac.check_player(pname)
  348. -- If this player is already a registered cheater, don't bother checking them
  349. -- for further cheats. Unless such checks ought to trigger immediate punishments
  350. -- when failed, it's probably a waste of resources.
  351. if sheriff.is_cheater(pname) then
  352. return
  353. end
  354. -- Check if player still logged in.
  355. local pref = minetest.get_player_by_name(pname)
  356. if pref then
  357. local pp = pref:get_pos()
  358. -- Don't bother performing checks for dead players.
  359. if pref:get_hp() > 0 then
  360. -- Don't check players attached to entities.
  361. if not default.player_attached[pname] then
  362. local op = ac.get_position_at_last_check_or_nil(pname)
  363. -- Don't bother checking player if they haven't moved.
  364. if not op or vector.distance(pp, op) > 1 then
  365. -- Don't check players in the Outback.
  366. if rc.current_realm_at_pos(pp) ~= "abyss" then
  367. ac.record_player_position(pname, pp)
  368. ac.do_standard_check(pname, pref)
  369. end
  370. end
  371. end
  372. end
  373. -- Check this player again after some delay.
  374. -- Reduce time to next check if player has some suspicion on them.
  375. local delay = math.random(ac.default_delay_min, ac.default_delay_max)
  376. delay = delay - ac.get_suspicion_count(pname)
  377. if city_block:in_city(pp) then
  378. -- Decrease time to next check if the position is within the city.
  379. delay = delay - math.random(ac.city_reduce_min, ac.city_reduce_max)
  380. elseif not city_block:in_no_leecher_zone(pp) then
  381. -- Increase time to next check if the position is in the outlands.
  382. delay = delay + math.random(ac.outland_increase_min, ac.outland_increase_max)
  383. end
  384. -- Increase time between standard checks if many players are logged in.
  385. local players = minetest.get_connected_players()
  386. delay = delay + ((#players) - 1) * 4
  387. -- Increase time to next standard check if player has little recorded
  388. -- suspicion generated from prior sessions. Decrease time to next check if
  389. -- player's average suspicion levels seem to be high.
  390. local total_suspicion = ac.get_total_suspicion(pname)
  391. local clean_sessions = ac.get_clean_sessions(pname)
  392. if clean_sessions < 1 then clean_sessions = 1 end
  393. local avg_suspicion = total_suspicion / clean_sessions
  394. if avg_suspicion < ac.low_average_suspicion then
  395. delay = delay + math.random(ac.low_suspicion_increase_min, ac.low_suspicion_increase_max)
  396. elseif avg_suspicion > ac.high_average_suspicion then
  397. delay = delay - math.random(ac.high_suspicion_reduce_min, ac.high_suspicion_reduce_max)
  398. end
  399. -- Reduce time to next check if player is near others.
  400. local others = ac.nearby_player_count(pname, pref)
  401. if others > 0 then
  402. delay = delay - math.random(0, others * 10)
  403. end
  404. -- Schedule check not less than 1 second future.
  405. if delay < 1 then delay = 1 end
  406. minetest.after(delay, ac.check_player, pname)
  407. end
  408. end
  409. function ac.on_joinplayer(pref)
  410. local pname = pref:get_player_name()
  411. -- Do not perform AC checks for admin player.
  412. if gdac.player_is_admin(pname) then return end
  413. local delay = math.random(ac.initial_delay_min, ac.initial_delay_max)
  414. -- Reduce time to next check if they have some suspicion on them.
  415. delay = delay - ac.get_suspicion_count(pname)
  416. if delay < 1 then delay = 1 end
  417. -- Schedule check.
  418. minetest.after(delay, ac.check_player, pname)
  419. end
  420. function ac.on_shutdown()
  421. -- On session shutdown (usually nightly) record overall clean/dirty status for
  422. -- registered players.
  423. for pname, pdata in pairs(ac.players) do
  424. if passport.player_registered(pname) then
  425. local suspicion = ac.get_suspicion_count(pname)
  426. if suspicion == 0 then
  427. local k1 = pname .. ":clean_sessions"
  428. local k2 = pname .. ":last_session_dirty"
  429. local cc = ac.storage:get_int(k1)
  430. cc = cc + 1
  431. ac.storage:set_int(k1, cc)
  432. ac.storage:set_int(k2, 0) -- The last session (this one) was clean.
  433. else
  434. local k1 = pname .. ":dirty_sessions"
  435. local k2 = pname .. ":last_session_dirty"
  436. local k3 = pname .. ":total_suspicion"
  437. local dd = ac.storage:get_int(k1)
  438. dd = dd + 1
  439. ac.storage:set_int(k1, dd)
  440. ac.storage:set_int(k2, 1) -- The last session (this one) was dirty.
  441. -- Add suspicion count from this session to the permanent total for this
  442. -- player.
  443. local ts = ac.storage:get_int(k3)
  444. ts = ts + ac.get_suspicion_count(pname)
  445. ac.storage:set_int(k3, ts)
  446. end
  447. end
  448. end
  449. end
  450. if not ac.registered then
  451. minetest.register_on_joinplayer(function(...)
  452. ac.on_joinplayer(...)
  453. end)
  454. minetest.register_on_shutdown(function(...)
  455. ac.on_shutdown(...)
  456. end)
  457. local c = "ac:core"
  458. local f = ac.modpath .. "/init.lua"
  459. reload.register_file(c, f, false)
  460. ac.registered = true
  461. end