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

init.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Network contains a default implementation of the network.
  2. //
  3. // Please see mtglib.Network interface to get some basic idea behind
  4. // this abstraction.
  5. //
  6. // Some notable feature of this implementation:
  7. //
  8. // 1. It detaches dialer from a network. Dialer is something which
  9. // implements a real dialer and network completes it with more higher
  10. // level details.
  11. //
  12. // 2. It uses only TCP connections. Even for DNS it uses DNS-Over-HTTPS
  13. //
  14. // 3. It has some simple implementation of DNS cache which is good
  15. // enough for our purpose.
  16. //
  17. // 4. It sets uses SO_REUSEPORT port if applicable.
  18. package network
  19. import (
  20. "context"
  21. "errors"
  22. "net"
  23. "time"
  24. )
  25. const (
  26. // DefaultTimeout is a default timeout for establishing TCP
  27. // connection.
  28. DefaultTimeout = 10 * time.Second
  29. // DefaultHTTPTimeout defines a default timeout for making HTTP
  30. // request.
  31. DefaultHTTPTimeout = 10 * time.Second
  32. // DefaultBufferSize defines a TCP buffer size. Both read and write, so
  33. // for real size, please multiply this number by 2.
  34. DefaultBufferSize = 16 * 1024 // 16 kib
  35. // ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
  36. // only.
  37. //
  38. // This dialer uses circuit breaker with of 3 stages: OPEN,
  39. // HALF_OPEN and CLOSED. If state is CLOSED, all requests go in
  40. // a normal mode. If you get more that ProxyDialerOpenThreshold
  41. // errors, circuit breaker goes into OPEN mode.
  42. //
  43. // When circuit breaker is in OPEN mode, it forbids all request to
  44. // a given proxy. But after ProxyDialerHalfOpenTimeout it gives a
  45. // second chance and opens an access for a SINGLE request. If this
  46. // request success, then circuit breaker closes, otherwise opens
  47. // again.
  48. //
  49. // When circuit breaker is closed, it clears an error states each
  50. // ProxyDialerResetFailuresTimeout.
  51. ProxyDialerOpenThreshold = 5
  52. ProxyDialerHalfOpenTimeout = time.Minute
  53. ProxyDialerResetFailuresTimeout = 10 * time.Second
  54. // DefaultDOHHostname defines a default IP address for DOH host.
  55. // Since mtg is simple, please pass IP address here. We do not
  56. // have bootstrap servers here embedded.
  57. DefaultDOHHostname = "9.9.9.9"
  58. // DNSTimeout defines a timeout for DNS queries.
  59. DNSTimeout = 5 * time.Second
  60. )
  61. var (
  62. // ErrCircuitBreakerOpened is returned when proxy is being accessed
  63. // but circuit breaker is opened.
  64. ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
  65. // ErrCannotDialWithAllProxies is returned when load balancing
  66. // client is trying to access proxies but all of them are failed.
  67. ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
  68. )
  69. // Dialer defines an interface which is required to bootstrap a network
  70. // instance from.
  71. type Dialer interface {
  72. Dial(network, address string) (net.Conn, error)
  73. DialContext(ctx context.Context, network, address string) (net.Conn, error)
  74. }