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个字符

init.go 3.7KB

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