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ů.

api.go 796B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. )
  10. const (
  11. apiUserAgent = "github.com/9seconds/mtg"
  12. apiHTTPTimeout = 30 * time.Second
  13. )
  14. var httpClient = http.Client{
  15. Timeout: apiHTTPTimeout,
  16. }
  17. func request(url string) (io.ReadCloser, error) {
  18. ctx, cancel := context.WithTimeout(context.Background(), apiHTTPTimeout)
  19. defer cancel()
  20. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  21. if err != nil {
  22. panic(err)
  23. }
  24. req.Header.Set("Accept", "text/plan")
  25. req.Header.Set("User-Agent", apiUserAgent)
  26. resp, err := httpClient.Do(req)
  27. if err != nil {
  28. if resp != nil {
  29. io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
  30. resp.Body.Close()
  31. }
  32. return nil, fmt.Errorf("cannot perform a request: %w", err)
  33. }
  34. return resp.Body, err
  35. }