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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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