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 kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

init.go 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 defines a halfopen timeout for circuit
  53. // breaker.
  54. ProxyDialerHalfOpenTimeout = time.Minute
  55. // ProxyDialerResetFailuresTimeout defines a timeout for resetting a
  56. // failure.
  57. ProxyDialerResetFailuresTimeout = 10 * time.Second
  58. // DefaultDOHHostname defines a default IP address for DOH host.
  59. // Since mtg is simple, please pass IP address here. We do not
  60. // have bootstrap servers here embedded.
  61. DefaultDOHHostname = "9.9.9.9"
  62. // DNSTimeout defines a timeout for DNS queries.
  63. DNSTimeout = 5 * time.Second
  64. // tcpLingerTimeout defines a number of seconds to wait for sending
  65. // unacknowledged data.
  66. tcpLingerTimeout = 1
  67. )
  68. var (
  69. // ErrCircuitBreakerOpened is returned when proxy is being accessed
  70. // but circuit breaker is opened.
  71. ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
  72. // ErrCannotDialWithAllProxies is returned when load balancing
  73. // client is trying to access proxies but all of them are failed.
  74. ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
  75. )
  76. // Dialer defines an interface which is required to bootstrap a network
  77. // instance from.
  78. type Dialer interface {
  79. Dial(network, address string) (net.Conn, error)
  80. DialContext(ctx context.Context, network, address string) (net.Conn, error)
  81. }