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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ntp.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package ntp
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. "github.com/beevik/ntp"
  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, fmt.Errorf("Cannot fetch NTP server %s: %w", url, err)
  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. }