dialer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package http
  2. import (
  3. "context"
  4. gotls "crypto/tls"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "golang.org/x/net/http2"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/net"
  12. "v2ray.com/core/transport/internet"
  13. "v2ray.com/core/transport/internet/tls"
  14. )
  15. var (
  16. globalDialerMap map[net.Destination]*http.Client
  17. globalDailerAccess sync.Mutex
  18. )
  19. func getHTTPClient(ctx context.Context, dest net.Destination) (*http.Client, error) {
  20. globalDailerAccess.Lock()
  21. defer globalDailerAccess.Unlock()
  22. if globalDialerMap == nil {
  23. globalDialerMap = make(map[net.Destination]*http.Client)
  24. }
  25. if client, found := globalDialerMap[dest]; found {
  26. return client, nil
  27. }
  28. config := tls.ConfigFromContext(ctx)
  29. if config == nil {
  30. return nil, newError("TLS must be enabled for http transport.").AtWarning()
  31. }
  32. transport := &http2.Transport{
  33. DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
  34. rawHost, rawPort, err := net.SplitHostPort(addr)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if len(rawPort) == 0 {
  39. rawPort = "443"
  40. }
  41. port, err := net.PortFromString(rawPort)
  42. if err != nil {
  43. return nil, err
  44. }
  45. address := net.ParseAddress(rawHost)
  46. pconn, err := internet.DialSystem(context.Background(), nil, net.TCPDestination(address, port))
  47. if err != nil {
  48. return nil, err
  49. }
  50. return gotls.Client(pconn, tlsConfig), nil
  51. },
  52. TLSClientConfig: config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")),
  53. }
  54. client := &http.Client{
  55. Transport: transport,
  56. }
  57. globalDialerMap[dest] = client
  58. return client, nil
  59. }
  60. // Dial dials a new TCP connection to the given destination.
  61. func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  62. rawSettings := internet.TransportSettingsFromContext(ctx)
  63. httpSettings, ok := rawSettings.(*Config)
  64. if !ok {
  65. return nil, newError("HTTP config is not set.").AtError()
  66. }
  67. client, err := getHTTPClient(ctx, dest)
  68. if err != nil {
  69. return nil, err
  70. }
  71. preader, pwriter := io.Pipe()
  72. request := &http.Request{
  73. Method: "PUT",
  74. Host: httpSettings.getRandomHost(),
  75. Body: preader,
  76. URL: &url.URL{
  77. Scheme: "https",
  78. Host: dest.NetAddr(),
  79. Path: httpSettings.getNormalizedPath(),
  80. },
  81. Proto: "HTTP/2",
  82. ProtoMajor: 2,
  83. ProtoMinor: 0,
  84. }
  85. response, err := client.Do(request)
  86. if err != nil {
  87. return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
  88. }
  89. if response.StatusCode != 200 {
  90. return nil, newError("unexpected status", response.StatusCode).AtWarning()
  91. }
  92. return &Connection{
  93. Reader: response.Body,
  94. Writer: pwriter,
  95. Closer: common.NewChainedClosable(preader, pwriter, response.Body),
  96. Local: &net.TCPAddr{
  97. IP: []byte{0, 0, 0, 0},
  98. Port: 0,
  99. },
  100. Remote: &net.TCPAddr{
  101. IP: []byte{0, 0, 0, 0},
  102. Port: 0,
  103. },
  104. }, nil
  105. }
  106. func init() {
  107. common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_HTTP, Dial))
  108. }