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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

network.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return &network{
  66. Dialer: net.Dialer{
  67. Timeout: tcpTimeout,
  68. Resolver: dnsResolver,
  69. FallbackDelay: -1,
  70. },
  71. userAgent: userAgent,
  72. idleTimeout: idleTimeout,
  73. httpTimeout: httpTimeout,
  74. }
  75. }