dialer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. client, err := getHTTPClient(ctx, dest)
  60. if err != nil {
  61. return nil, err
  62. }
  63. preader, pwriter := io.Pipe()
  64. request := &http.Request{
  65. Method: "PUT",
  66. Host: "www.v2ray.com",
  67. Body: preader,
  68. URL: &url.URL{
  69. Scheme: "https",
  70. Host: dest.NetAddr(),
  71. Path: "/",
  72. },
  73. Proto: "HTTP/2",
  74. ProtoMajor: 2,
  75. ProtoMinor: 0,
  76. }
  77. response, err := client.Do(request)
  78. if err != nil {
  79. return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
  80. }
  81. if response.StatusCode != 200 {
  82. return nil, newError("unexpected status", response.StatusCode).AtWarning()
  83. }
  84. return &Connection{
  85. Reader: response.Body,
  86. Writer: pwriter,
  87. Closer: common.NewChainedClosable(preader, pwriter, response.Body),
  88. Local: &net.TCPAddr{
  89. IP: []byte{0, 0, 0, 0},
  90. Port: 0,
  91. },
  92. Remote: &net.TCPAddr{
  93. IP: []byte{0, 0, 0, 0},
  94. Port: 0,
  95. },
  96. }, nil
  97. }
  98. func init() {
  99. common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_HTTP, Dial))
  100. }