forked from GeyserMC/Geyser
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that the time we use is always the same across servers
- Loading branch information
Showing
6 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
common/src/main/java/org/geysermc/floodgate/time/SntpClientUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/Geyser | ||
*/ | ||
|
||
package org.geysermc.floodgate.time; | ||
|
||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.InetAddress; | ||
import java.nio.ByteBuffer; | ||
|
||
/* | ||
* Thanks: | ||
* https://datatracker.ietf.org/doc/html/rfc1769 | ||
* https://github.com/jonsagara/SimpleNtpClient | ||
* https://stackoverflow.com/a/29138806 | ||
*/ | ||
public final class SntpClientUtils { | ||
private static final int NTP_PORT = 123; | ||
|
||
private static final int NTP_PACKET_SIZE = 48; | ||
private static final int NTP_MODE = 3; // client | ||
private static final int NTP_VERSION = 3; | ||
private static final int RECEIVE_TIME_POSITION = 32; | ||
|
||
private static final long NTP_TIME_OFFSET = ((365L * 70L) + 17L) * 24L * 60L * 60L; | ||
|
||
public static long requestTimeOffset(String host, int timeout) { | ||
try (DatagramSocket socket = new DatagramSocket()) { | ||
socket.setSoTimeout(timeout); | ||
|
||
InetAddress address = InetAddress.getByName(host); | ||
|
||
ByteBuffer buff = ByteBuffer.allocate(NTP_PACKET_SIZE); | ||
|
||
DatagramPacket request = new DatagramPacket( | ||
buff.array(), NTP_PACKET_SIZE, address, NTP_PORT | ||
); | ||
|
||
// mode is in the least signification 3 bits | ||
// version is in bits 3-5 | ||
buff.put((byte) (NTP_MODE | (NTP_VERSION << 3))); | ||
|
||
long originateTime = System.currentTimeMillis(); | ||
socket.send(request); | ||
|
||
DatagramPacket response = new DatagramPacket(buff.array(), NTP_PACKET_SIZE); | ||
socket.receive(response); | ||
|
||
long responseTime = System.currentTimeMillis(); | ||
|
||
// everything before isn't important for us | ||
buff.position(RECEIVE_TIME_POSITION); | ||
|
||
long receiveTime = readTimestamp(buff); | ||
long transmitTime = readTimestamp(buff); | ||
|
||
return ((receiveTime - originateTime) + (transmitTime - responseTime)) / 2; | ||
} catch (Exception ignored) { | ||
} | ||
return Long.MIN_VALUE; | ||
} | ||
|
||
private static long readTimestamp(ByteBuffer buffer) { | ||
//todo look into the ntp 2036 problem | ||
long seconds = buffer.getInt() & 0xffffffffL; | ||
long fraction = buffer.getInt() & 0xffffffffL; | ||
return ((seconds - NTP_TIME_OFFSET) * 1000) + ((fraction * 1000) / 0x100000000L); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
common/src/main/java/org/geysermc/floodgate/time/TimeSyncer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/Geyser | ||
*/ | ||
|
||
package org.geysermc.floodgate.time; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class TimeSyncer { | ||
private final ExecutorService executorService; | ||
private long timeOffset = Long.MIN_VALUE; // value when it failed to get the offset | ||
|
||
public TimeSyncer(String timeServer) { | ||
ScheduledExecutorService service = Executors.newScheduledThreadPool(1); | ||
service.scheduleWithFixedDelay(() -> { | ||
// 5 tries to get the time offset | ||
for (int i = 0; i < 5; i++) { | ||
long offset = SntpClientUtils.requestTimeOffset(timeServer, 3000); | ||
if (offset != Long.MIN_VALUE) { | ||
timeOffset = offset; | ||
return; | ||
} | ||
} | ||
}, 0, 30, TimeUnit.MINUTES); | ||
executorService = service; | ||
} | ||
|
||
public void shutdown() { | ||
executorService.shutdown(); | ||
} | ||
|
||
public long getTimeOffset() { | ||
return timeOffset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters