init.lua 18 KB

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