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

http.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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() //nolint: errcheck
  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. // NewHTTP returns a file abstraction for HTTP/HTTPS endpoint. You also need to
  35. // provide a valid instance of [http.Client] to access it.
  36. func NewHTTP(client *http.Client, endpoint string) (File, error) {
  37. if client == nil {
  38. return nil, ErrBadHTTPClient
  39. }
  40. parsed, err := url.Parse(endpoint)
  41. if err != nil {
  42. return nil, fmt.Errorf("incorrect url %s: %w", endpoint, err)
  43. }
  44. switch parsed.Scheme {
  45. case "http", "https":
  46. default:
  47. return nil, fmt.Errorf("unsupported url %s", endpoint)
  48. }
  49. return httpFile{
  50. http: client,
  51. url: endpoint,
  52. }, nil
  53. }