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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

network.go 1.7KB

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