dialer.go 2.8 KB

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