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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package stats
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "sync"
  6. "time"
  7. "go.uber.org/zap"
  8. "github.com/9seconds/mtg/config"
  9. )
  10. var instance *stats
  11. func Start(conf *config.Config) {
  12. log := zap.S().Named("stats")
  13. instance = &stats{
  14. URLs: conf.GetURLs(),
  15. Uptime: uptime(time.Now()),
  16. mutex: &sync.RWMutex{},
  17. }
  18. go crashManager()
  19. go connectionManager()
  20. go trafficManager()
  21. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  22. w.Header().Set("Content-Type", "application/json")
  23. instance.mutex.Lock()
  24. first, err := json.Marshal(instance)
  25. instance.mutex.Unlock()
  26. if err != nil {
  27. log.Errorw("Cannot encode json", "error", err)
  28. http.Error(w, "Internal server error", 500)
  29. return
  30. }
  31. interm := map[string]interface{}{}
  32. json.Unmarshal(first, &interm)
  33. encoder := json.NewEncoder(w)
  34. encoder.SetEscapeHTML(false)
  35. encoder.SetIndent("", " ")
  36. if err = encoder.Encode(interm); err != nil {
  37. log.Errorw("Cannot encode json", "error", err)
  38. }
  39. })
  40. if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
  41. log.Fatalw("Stats server has been stopped", "error", err)
  42. }
  43. }