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.2KB

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