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 символов.

network.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "time"
  8. "github.com/9seconds/mtg/v2/essentials"
  9. "github.com/9seconds/mtg/v2/mtglib"
  10. )
  11. type network struct {
  12. net.Dialer
  13. keepAliveConfig net.KeepAliveConfig
  14. httpTimeout time.Duration
  15. idleTimeout time.Duration
  16. userAgent string
  17. tcpNotSentLowat int
  18. }
  19. func (n *network) Dial(network, address string) (essentials.Conn, error) {
  20. return n.DialContext(context.Background(), network, address)
  21. }
  22. func (n *network) DialContext(ctx context.Context, network, address string) (essentials.Conn, error) {
  23. switch network {
  24. case "tcp", "tcp4", "tcp6":
  25. default:
  26. return nil, fmt.Errorf("unsupported network %s", network)
  27. }
  28. conn, err := n.Dialer.DialContext(ctx, network, address)
  29. if err != nil {
  30. return nil, err
  31. }
  32. tcpConn := conn.(*net.TCPConn)
  33. return tcpConn, setCommonSocketOptions(tcpConn, n.keepAliveConfig, n.tcpNotSentLowat)
  34. }
  35. func (n *network) MakeHTTPClient(
  36. dialFunc func(context.Context, string, string) (essentials.Conn, error),
  37. ) *http.Client {
  38. if dialFunc == nil {
  39. dialFunc = n.DialContext
  40. }
  41. return &http.Client{
  42. Timeout: n.httpTimeout,
  43. Transport: networkHTTPTransport{
  44. userAgent: n.userAgent,
  45. next: &http.Transport{
  46. IdleConnTimeout: n.idleTimeout,
  47. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  48. return dialFunc(ctx, network, address)
  49. },
  50. },
  51. },
  52. }
  53. }
  54. func (n *network) NativeDialer() *net.Dialer {
  55. return &n.Dialer
  56. }
  57. func New(
  58. dnsResolver *net.Resolver,
  59. userAgent string,
  60. tcpTimeout,
  61. httpTimeout,
  62. idleTimeout time.Duration,
  63. keepAliveConfig net.KeepAliveConfig,
  64. tcpNotSentLowat int,
  65. ) mtglib.Network {
  66. if dnsResolver == nil {
  67. dnsResolver = net.DefaultResolver
  68. }
  69. if userAgent == "" {
  70. userAgent = UserAgent
  71. }
  72. if tcpNotSentLowat == 0 {
  73. tcpNotSentLowat = DefaultTCPNotSentLowat
  74. }
  75. return &network{
  76. Dialer: net.Dialer{
  77. Timeout: tcpTimeout,
  78. Resolver: dnsResolver,
  79. FallbackDelay: -1,
  80. },
  81. userAgent: userAgent,
  82. idleTimeout: idleTimeout,
  83. httpTimeout: httpTimeout,
  84. keepAliveConfig: keepAliveConfig,
  85. tcpNotSentLowat: tcpNotSentLowat,
  86. }
  87. }