2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
2019-12-27 19:35:43 +01:00
|
|
|
"time"
|
2018-09-11 11:09:28 +02:00
|
|
|
)
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// UpdateTime updates cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func UpdateTime(newTime int64) {
|
2021-10-18 11:33:02 +02:00
|
|
|
pgp.lock.Lock()
|
|
|
|
|
defer pgp.lock.Unlock()
|
|
|
|
|
|
2020-07-21 13:42:41 +02:00
|
|
|
if newTime > pgp.latestServerTime {
|
|
|
|
|
pgp.latestServerTime = newTime
|
|
|
|
|
}
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-01 19:44:49 +01:00
|
|
|
// SetKeyGenerationOffset updates the offset when generating keys.
|
|
|
|
|
func SetKeyGenerationOffset(offset int64) {
|
2021-10-18 11:33:02 +02:00
|
|
|
pgp.lock.Lock()
|
|
|
|
|
defer pgp.lock.Unlock()
|
|
|
|
|
|
2020-12-01 19:44:49 +01:00
|
|
|
pgp.generationOffset = offset
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// GetUnixTime gets latest cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func GetUnixTime() int64 {
|
|
|
|
|
return getNow().Unix()
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// GetTime gets latest cached time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func GetTime() time.Time {
|
|
|
|
|
return getNow()
|
2018-09-20 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
// ----- INTERNAL FUNCTIONS -----
|
|
|
|
|
|
2021-06-16 14:06:07 +02:00
|
|
|
// getNow returns the latest server time.
|
2019-10-22 18:44:45 +02:00
|
|
|
func getNow() time.Time {
|
2021-10-19 08:09:59 +02:00
|
|
|
pgp.lock.RLock()
|
|
|
|
|
defer pgp.lock.RUnlock()
|
2021-10-18 11:33:02 +02:00
|
|
|
|
2021-06-16 14:06:07 +02:00
|
|
|
if pgp.latestServerTime == 0 {
|
2019-10-22 18:44:45 +02:00
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 14:06:07 +02:00
|
|
|
return time.Unix(pgp.latestServerTime, 0)
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:55:36 +02:00
|
|
|
// getTimeGenerator Returns a time generator function.
|
2019-10-22 18:44:45 +02:00
|
|
|
func getTimeGenerator() func() time.Time {
|
2019-12-27 19:35:43 +01:00
|
|
|
return getNow
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
2020-12-01 19:44:49 +01:00
|
|
|
|
|
|
|
|
// getNowKeyGenerationOffset returns the current time with the key generation offset.
|
|
|
|
|
func getNowKeyGenerationOffset() time.Time {
|
2021-10-19 08:09:59 +02:00
|
|
|
pgp.lock.RLock()
|
|
|
|
|
defer pgp.lock.RUnlock()
|
2021-10-18 11:33:02 +02:00
|
|
|
|
2021-06-16 14:06:07 +02:00
|
|
|
if pgp.latestServerTime == 0 {
|
2020-12-01 19:44:49 +01:00
|
|
|
return time.Unix(time.Now().Unix()+pgp.generationOffset, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 14:06:07 +02:00
|
|
|
return time.Unix(pgp.latestServerTime+pgp.generationOffset, 0)
|
2020-12-01 19:44:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getKeyGenerationTimeGenerator Returns a time generator function with the key generation offset.
|
|
|
|
|
func getKeyGenerationTimeGenerator() func() time.Time {
|
|
|
|
|
return getNowKeyGenerationOffset
|
|
|
|
|
}
|