Skip to content

Commit

Permalink
fix: check system time against remote time server
Browse files Browse the repository at this point in the history
fixes issue where incorrect time data is sent and causes desyncs
  • Loading branch information
My-Name-Is-Jeff committed Feb 1, 2024
1 parent ebd814f commit 17da0ec
Show file tree
Hide file tree
Showing 3 changed files with 455 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/main/kotlin/gg/skytils/skytilsmod/Skytils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package gg.skytils.skytilsmod
import gg.essential.api.EssentialAPI
import gg.essential.universal.UChat
import gg.essential.universal.UKeyboard
import gg.skytils.event.Events
import gg.skytils.skytilsmod.commands.impl.*
import gg.skytils.skytilsmod.commands.stats.impl.CataCommand
import gg.skytils.skytilsmod.commands.stats.impl.SlayerCommand
Expand Down Expand Up @@ -103,10 +102,14 @@ import net.minecraftforge.fml.common.network.FMLNetworkEvent
import skytils.hylin.HylinAPI
import sun.misc.Unsafe
import java.io.File
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ThreadPoolExecutor
import kotlin.coroutines.CoroutineContext
import kotlin.math.abs

@Mod(
modid = Skytils.MOD_ID,
Expand Down Expand Up @@ -232,6 +235,8 @@ class Skytils {
const val prefix = "§9§lSkytils §8»"
const val successPrefix = "§a§lSkytils §8»"
const val failPrefix = "§c§lSkytils §8»"

var trustClientTime = false
}

@Mod.EventHandler
Expand Down Expand Up @@ -413,6 +418,8 @@ class Skytils {
UChat.chat("$prefix §fYou are on a development version of Skytils. Change your update channel to pre-release to get notified of new updates.")
}
}

checkSystemTime()
}

@SubscribeEvent(priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -585,4 +592,34 @@ class Skytils {
}
}
}

private fun checkSystemTime() {
IO.launch {
DatagramSocket().use { socket ->
val address = InetAddress.getByName("time.nist.gov")
val buffer = NtpMessage().toByteArray()
var packet = DatagramPacket(buffer, buffer.size, address, 123)
socket.send(packet)
packet = DatagramPacket(buffer, buffer.size)

val destinationTimestamp = NtpMessage.now()
val msg = NtpMessage(packet.data)

val localClockOffset =
((msg.receiveTimestamp - msg.originateTimestamp) +
(msg.transmitTimestamp - destinationTimestamp)) / 2

println("Got local clock offset: $localClockOffset")
if (abs(localClockOffset) > 3) {
if (ModChecker.canShowNotifications) {
EssentialAPI.getNotifications().push("Skytils", "Your system time is inaccurate.", 3f)
} else {
UChat.chat("$prefix §fYour system time appears to be inaccurate. Please sync your system time to avoid issues with Skytils.")
}
} else {
trustClientTime = true
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ object MayorInfo {

fun sendJerryData(mayor: Mayor?, nextSwitch: Long) = Skytils.IO.launch {
if (mayor == null || nextSwitch <= System.currentTimeMillis()) return@launch
if (!Skytils.trustClientTime) {
println("Client's time isn't trusted, skip sending jerry data.")
return@launch
}
try {
val serverId = UUID.randomUUID().toString().replace("-".toRegex(), "")
val commentForDecompilers =
Expand Down
Loading

0 comments on commit 17da0ec

Please sign in to comment.