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 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package files
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. )
  9. type httpFile struct {
  10. http *http.Client
  11. url string
  12. }
  13. func (h httpFile) Open(ctx context.Context) (io.ReadCloser, error) {
  14. request, err := http.NewRequestWithContext(ctx, http.MethodGet, h.url, nil)
  15. if err != nil {
  16. panic(err)
  17. }
  18. response, err := h.http.Do(request)
  19. if err != nil {
  20. if response != nil {
  21. io.Copy(io.Discard, response.Body) // nolint: errcheck
  22. response.Body.Close()
  23. }
  24. return nil, fmt.Errorf("cannot get url %s: %w", h.url, err)
  25. }
  26. if response.StatusCode >= http.StatusBadRequest {
  27. return nil, fmt.Errorf("unexpected status code %d", response.StatusCode)
  28. }
  29. return response.Body, nil
  30. }
  31. func (h httpFile) String() string {
  32. return h.url
  33. }
  34. func NewHTTP(client *http.Client, endpoint string) (File, error) {
  35. if client == nil {
  36. return nil, ErrBadHTTPClient
  37. }
  38. parsed, err := url.Parse(endpoint)
  39. if err != nil {
  40. return nil, fmt.Errorf("incorrect url %s: %w", endpoint, err)
  41. }
  42. switch parsed.Scheme {
  43. case "http", "https":
  44. default:
  45. return nil, fmt.Errorf("unsupported url %s", endpoint)
  46. }
  47. return httpFile{
  48. http: client,
  49. url: endpoint,
  50. }, nil
  51. }