diff --git a/go.mod b/go.mod index a0eb2a5a..0e9c7522 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( require ( github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/VictoriaMetrics/fastcache v1.6.0 // indirect + github.com/beevik/ntp v0.3.0 github.com/benbjohnson/clock v1.3.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd v0.20.1-beta // indirect diff --git a/go.sum b/go.sum index 307bfcd0..a6f0521c 100644 --- a/go.sum +++ b/go.sum @@ -197,6 +197,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.7.2/go.mod h1:8EzeIqfWt2wWT4rJVu3f21 github.com/aws/smithy-go v1.1.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw= github.com/aws/smithy-go v1.7.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw= +github.com/beevik/ntp v0.3.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= diff --git a/utils/ntp.go b/utils/ntp.go new file mode 100644 index 00000000..4033094a --- /dev/null +++ b/utils/ntp.go @@ -0,0 +1,38 @@ +package utils + +import ( + "time" + + "github.com/beevik/ntp" +) + +var NTPServer = "pool.ntp.org" + +func GetNTPTime() (time.Time, error) { + t, err := ntp.Time(NTPServer) + if err != nil { + return t, err + } + + return t, nil +} + +func GetNTPMetadata() (*ntp.Response, error) { + options := ntp.QueryOptions{Timeout: 60 * time.Second, TTL: 10} + response, err := ntp.QueryWithOptions(NTPServer, options) + if err != nil { + return nil, err + } + + return response, nil +} + +func GetTimeOffset() (time.Duration, error) { + options := ntp.QueryOptions{Timeout: 60 * time.Second, TTL: 10} + response, err := ntp.QueryWithOptions(NTPServer, options) + if err != nil { + return 0, err + } + + return response.ClockOffset, nil +}