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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

network.go 1.8KB

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