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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

init.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "time"
  23. "github.com/9seconds/mtg/v2/essentials"
  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. // Deprecated:
  33. //
  34. // DefaultBufferSize defines a TCP buffer size. Both read and write, so
  35. // for real size, please multiply this number by 2.
  36. DefaultBufferSize = 16 * 1024 // 16 kib
  37. // DefaultTCPKeepAlivePeriod defines a time period between 2
  38. // consequitive probes.
  39. DefaultTCPKeepAlivePeriod = 10 * time.Second
  40. // ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
  41. // only.
  42. //
  43. // This dialer uses circuit breaker with of 3 stages: OPEN,
  44. // HALF_OPEN and CLOSED. If state is CLOSED, all requests go in
  45. // a normal mode. If you get more that ProxyDialerOpenThreshold
  46. // errors, circuit breaker goes into OPEN mode.
  47. //
  48. // When circuit breaker is in OPEN mode, it forbids all request to
  49. // a given proxy. But after ProxyDialerHalfOpenTimeout it gives a
  50. // second chance and opens an access for a SINGLE request. If this
  51. // request success, then circuit breaker closes, otherwise opens
  52. // again.
  53. //
  54. // When circuit breaker is closed, it clears an error states each
  55. // ProxyDialerResetFailuresTimeout.
  56. ProxyDialerOpenThreshold = 5
  57. // ProxyDialerHalfOpenTimeout defines a halfopen timeout for circuit
  58. // breaker.
  59. ProxyDialerHalfOpenTimeout = time.Minute
  60. // ProxyDialerResetFailuresTimeout defines a timeout for resetting a
  61. // failure.
  62. ProxyDialerResetFailuresTimeout = 10 * time.Second
  63. // DefaultDOHHostname defines a default IP address for DOH host.
  64. // Since mtg is simple, please pass IP address here. We do not
  65. // have bootstrap servers here embedded.
  66. DefaultDOHHostname = "9.9.9.9"
  67. // DNSTimeout defines a timeout for DNS queries.
  68. DNSTimeout = 5 * time.Second
  69. // tcpLingerTimeout defines a number of seconds to wait for sending
  70. // unacknowledged data.
  71. tcpLingerTimeout = 1
  72. )
  73. var (
  74. // ErrCircuitBreakerOpened is returned when proxy is being accessed
  75. // but circuit breaker is opened.
  76. ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
  77. // ErrCannotDialWithAllProxies is returned when load balancing
  78. // client is trying to access proxies but all of them are failed.
  79. ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
  80. )
  81. // Dialer defines an interface which is required to bootstrap a network
  82. // instance from.
  83. type Dialer interface {
  84. Dial(network, address string) (essentials.Conn, error)
  85. DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
  86. }