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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  18. func (n *network) Dial(network, address string) (essentials.Conn, error) {
  19. return n.DialContext(context.Background(), network, address)
  20. }
  21. func (n *network) DialContext(ctx context.Context, network, address string) (essentials.Conn, error) {
  22. switch network {
  23. case "tcp", "tcp4", "tcp6":
  24. default:
  25. return nil, fmt.Errorf("unsupported network %s", network)
  26. }
  27. conn, err := n.Dialer.DialContext(ctx, network, address)
  28. if err != nil {
  29. return nil, err
  30. }
  31. tcpConn := conn.(*net.TCPConn)
  32. return tcpConn, setCommonSocketOptions(tcpConn, n.keepAliveConfig)
  33. }
  34. func (n *network) MakeHTTPClient(
  35. dialFunc func(context.Context, string, string) (essentials.Conn, error),
  36. ) *http.Client {
  37. if dialFunc == nil {
  38. dialFunc = n.DialContext
  39. }
  40. return &http.Client{
  41. Timeout: n.httpTimeout,
  42. Transport: networkHTTPTransport{
  43. userAgent: n.userAgent,
  44. next: &http.Transport{
  45. IdleConnTimeout: n.idleTimeout,
  46. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  47. return dialFunc(ctx, network, address)
  48. },
  49. },
  50. },
  51. }
  52. }
  53. func (n *network) NativeDialer() *net.Dialer {
  54. return &n.Dialer
  55. }
  56. func New(
  57. dnsResolver *net.Resolver,
  58. userAgent string,
  59. tcpTimeout,
  60. httpTimeout,
  61. idleTimeout time.Duration,
  62. keepAliveConfig net.KeepAliveConfig,
  63. ) mtglib.Network {
  64. if dnsResolver == nil {
  65. dnsResolver = net.DefaultResolver
  66. }
  67. if userAgent == "" {
  68. userAgent = UserAgent
  69. }
  70. return &network{
  71. Dialer: net.Dialer{
  72. Timeout: tcpTimeout,
  73. Resolver: dnsResolver,
  74. FallbackDelay: -1,
  75. },
  76. userAgent: userAgent,
  77. idleTimeout: idleTimeout,
  78. httpTimeout: httpTimeout,
  79. keepAliveConfig: keepAliveConfig,
  80. }
  81. }