Skip to content

Commit

Permalink
Warn if we were unable to check if the system clock is accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed May 26, 2021
1 parent 776fc4e commit 934fc12
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
13 changes: 12 additions & 1 deletion common/src/main/java/org/geysermc/floodgate/time/TimeSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class TimeSyncer {
public TimeSyncer(String timeServer) {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleWithFixedDelay(() -> {
// 5 tries to get the time offset
// 5 tries to get the time offset, since UDP doesn't guaranty a response
for (int i = 0; i < 5; i++) {
long offset = SntpClientUtils.requestTimeOffset(timeServer, 3000);
if (offset != Long.MIN_VALUE) {
Expand All @@ -56,4 +56,15 @@ public void shutdown() {
public long getTimeOffset() {
return timeOffset;
}

public long getRealMillis() {
if (hasUsefulOffset()) {
return System.currentTimeMillis() + getTimeOffset();
}
return System.currentTimeMillis();
}

public boolean hasUsefulOffset() {
return timeOffset != Long.MIN_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,9 @@ public static BedrockData of(
String languageCode, int uiProfile, int inputMode, String ip,
LinkedPlayer linkedPlayer, boolean fromProxy, int subscribeId,
String verifyCode, TimeSyncer timeSyncer) {

long realMillis = System.currentTimeMillis();
if (timeSyncer.getTimeOffset() != Long.MIN_VALUE) {
realMillis += timeSyncer.getTimeOffset();
}

return new BedrockData(version, username, xuid, deviceOs, languageCode, inputMode,
uiProfile, ip, linkedPlayer, fromProxy, subscribeId, verifyCode,
realMillis, EXPECTED_LENGTH);
timeSyncer.getRealMillis(), EXPECTED_LENGTH);
}

public static BedrockData of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ public void packetSending(PacketSendingEvent event) {
skinUploader.getVerifyCode(),
connector.getTimeSyncer()
).toString());

if (!connector.getTimeSyncer().hasUsefulOffset()) {
connector.getLogger().warning(
"We couldn't make sure that your system clock is accurate. " +
"This can cause issues with logging in."
);
}

} catch (Exception e) {
connector.getLogger().error(LanguageUtils.getLocaleStringLog("geyser.auth.floodgate.encrypt_fail"), e);
disconnect(LanguageUtils.getPlayerLocaleString("geyser.auth.floodgate.encryption_fail", getClientData().getLanguageCode()));
Expand Down

0 comments on commit 934fc12

Please sign in to comment.