• Hello
Search Results for

    Show / Hide Table of Contents

    Universal - Device Time - Monitors (Pro)

    Many games use the device clock for trials, daily rewards, or timers. The player can change that clock. Two monitors watch it and report when it was moved.

    They only report. The Device Time Cheating Detector decides if that is cheating.

    These monitors need the Pro version.

    Walkthrough: Protecting against device clock cheating.

    The two monitors

    Both are in the namespace GUPS.AntiCheat.Monitor.Time.

    • DeviceTimeContinuousMonitor checks while the game runs. It compares the device clock with a second clock that the player cannot move in the date and time settings.
    • DeviceTimeEventMonitor checks at certain moments: when the game starts and when the player comes back to it. It compares the device clock with a time from the internet, and with the trusted time saved from the last session.

    Do you need to add them?

    Most of the time, no. The Device Time Cheating Detector prefab already includes both monitors. That prefab is part of the predefined Anti-Cheat-Monitor prefab.

    Add the components by hand only if you build your own setup, or if you only want the raw readings. Put both on the same GameObject as the DeviceTimeCheatingDetector. The detector looks for them there.

    Add the device time monitor components.

    What they report

    Each check sends its own DeviceTimeStatus. Check tells you which check sent it.

    public struct DeviceTimeStatus : IWatchedSubject
    {
        // Which check sent this status: Clock, Internet, or Local.
        public EDeviceTimeCheck Check { get; }
    
        // How the device clock differs from the time it was compared with.
        public EDeviceTimeDeviation Deviation { get; }
    }
    

    Check uses this enum:

    public enum EDeviceTimeCheck : byte
    {
        Clock = 0,      // The ongoing check while the game runs.
        Internet = 1,   // The check against the time from the server.
        Local = 2       // The check against the trusted time saved from the last session.
    }
    

    Deviation uses this enum:

    public enum EDeviceTimeDeviation : byte
    {
        None = 0,       // The clock is fine.
        Stopped = 1,    // The device clock stands still.
        Backward = 2,   // The device clock is behind.
        Forward = 3,    // The device clock is ahead.
        Untrusted = 4   // The server certificate did not match the hash you set.
    }
    

    You do not get a report every frame. A status is sent when a check finds a difference, and again with None when the clocks agree again.

    Settings

    Both monitors have:

    • Is Active (isActive, bool, default true): Turns the monitor on or off.

    The tolerance is on the DeviceTimeCheatingDetector. The detector passes it to both monitors.

    • Tolerance (tolerance, float, default 25, minimum 1): How many seconds the device clock may be off before it is reported. A good range is 10 to 60. Very small values can report normal clock updates by the operating system.

    The internet settings are on the DeviceTimeEventMonitor:

    • Use Internet Time (useInternetTime, bool, default true): When on, the trusted time is read from a web server.
    • Server Address (serverAddress, string, default https://cloudflare.com/cdn-cgi/trace): The server that gives the time. The default works on every platform. Your own server can send a Unix timestamp in the response body, as a ts= line or as the bare number. A Unix timestamp is the number of seconds since 1970. Otherwise the Date header of the response is used. Keep the response small.
    • Server Certificate Hash (serverCertificateHash, string, optional): The SHA-1 thumbprint of the server certificate, written as hex. A thumbprint is a short code that identifies one certificate. Colons and spaces are ignored. If it does not match, the check reports Untrusted. WebGL does not use this field.
    Warning

    A server gets a new certificate from time to time, and the hash changes with it. If you set Server Certificate Hash, use a server you control and update the hash before the certificate is renewed. Otherwise every player is reported as Untrusted.

    Good to know

    • Sleep is not a cheat: Locking the device or putting it to sleep is not reported.
    • No internet is not a cheat: A failed or slow read from the server is not reported. The last trusted time is kept, and the monitor tries again later.
    • A fixed clock clears the report: When the player sets the clock right again and the server confirms it, the earlier report is cleared.
    • After a restart of the device: Without internet time, only a clock that was moved back can be noticed. Leave Use Internet Time on for the best protection.
    • The first server read takes a moment: Until it works, the trusted time comes from the device clock. See Wait for the server time below.

    Platforms and requirements

    The monitors work on every platform. There are no extra requirements. Internet time needs a network connection to the server you set.

    On WebGL, the browser only lets the game read a server that allows it (CORS). The default server does. If you use your own server on WebGL, allow CORS. Send the timestamp in the body, or expose the Date header with Access-Control-Expose-Headers: Date.

    Read it in code

    You can listen to the monitors yourself. Subscribe returns an IDisposable. Dispose it when you want to stop.

    using System;
    using GUPS.AntiCheat.Core.Watch;
    using GUPS.AntiCheat.Monitor.Time;
    using UnityEngine;
    
    public class DeviceTimeStatusLogger : MonoBehaviour, IObserver<IWatchedSubject>
    {
        private void Start()
        {
            var continuous = GetComponentInChildren<DeviceTimeContinuousMonitor>();
            var events = GetComponentInChildren<DeviceTimeEventMonitor>();
            continuous.Subscribe(this);
            events.Subscribe(this);
        }
    
        public void OnNext(IWatchedSubject subject)
        {
            if (subject is DeviceTimeStatus status)
            {
                Debug.Log($"Device time {status.Check} check: {status.Deviation}");
            }
        }
    
        public void OnError(Exception error) { }
        public void OnCompleted() { }
    }
    

    Wait for the server time

    Right after the start, the first server read is still running. If a reward should only use the server time, check IsServerTimeResolved first. It is on the detector, not on the monitors:

    var detector = AntiCheatMonitor.Instance.GetDetector<DeviceTimeCheatingDetector>();
    
    if (detector != null && detector.IsServerTimeResolved)
    {
        // detector.CurrentUtcTime comes from the server.
    }
    

    IsServerTimeResolved stays false while Use Internet Time is off or the device is offline.

    Detect a changed clock

    To decide if a report is cheating, use the Device Time Cheating Detector.


    Back to the Monitor overview.

    In This Article
    Back to top GuardingPearSoftware documentation