Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ntp.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ntp
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. "github.com/9seconds/mtg/config"
  7. "github.com/beevik/ntp"
  8. "go.uber.org/zap"
  9. )
  10. const autoUpdatePeriod = time.Minute
  11. // Fetch fetches the data on time drift.
  12. func Fetch() (time.Duration, error) {
  13. url := config.C.NTPServers[rand.Intn(len(config.C.NTPServers))] // nolint: gosec
  14. resp, err := ntp.Query(url)
  15. if err != nil {
  16. return 0, fmt.Errorf("cannot fetch NTP server %s: %w", url, err)
  17. }
  18. offsetInt := int64(resp.ClockOffset)
  19. if offsetInt < 0 {
  20. offsetInt = -offsetInt
  21. }
  22. offset := time.Duration(offsetInt)
  23. return offset, nil
  24. }
  25. // AutoUpdate runs periodic check of current time .drift state.
  26. func AutoUpdate() {
  27. logger := zap.S().Named("ntp")
  28. for range time.Tick(autoUpdatePeriod) {
  29. diff, err := Fetch()
  30. if err != nil {
  31. logger.Debugw("Cannot fetch time from NTP", "error", err)
  32. continue
  33. }
  34. switch {
  35. case diff < 400*time.Millisecond:
  36. logger.Debugw("NTP time drift", "value", diff.String())
  37. case diff < 600*time.Millisecond:
  38. logger.Infow("NTP time drift", "value", diff.String())
  39. case diff < 800*time.Millisecond:
  40. logger.Warnw("NTP time drift", "value", diff.String())
  41. default:
  42. logger.Errorw("NTP time drift", "value", diff.String())
  43. }
  44. }
  45. }