httprt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package httprt
  2. //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
  3. import (
  4. "bytes"
  5. "context"
  6. "encoding/base64"
  7. "io"
  8. gonet "net"
  9. "net/http"
  10. "time"
  11. "github.com/v2fly/v2ray-core/v5/transport/internet/transportcommon"
  12. "github.com/v2fly/v2ray-core/v5/common"
  13. "github.com/v2fly/v2ray-core/v5/common/net"
  14. "github.com/v2fly/v2ray-core/v5/transport/internet/request"
  15. )
  16. func newHTTPRoundTripperClient(ctx context.Context, config *ClientConfig) request.RoundTripperClient {
  17. _ = ctx
  18. return &httpTripperClient{config: config}
  19. }
  20. type httpTripperClient struct {
  21. httpRTT http.RoundTripper
  22. config *ClientConfig
  23. assembly request.TransportClientAssembly
  24. }
  25. type unimplementedBackDrop struct{}
  26. func (u unimplementedBackDrop) RoundTrip(r *http.Request) (*http.Response, error) {
  27. return nil, newError("unimplemented")
  28. }
  29. func (h *httpTripperClient) OnTransportClientAssemblyReady(assembly request.TransportClientAssembly) {
  30. h.assembly = assembly
  31. }
  32. func (h *httpTripperClient) RoundTrip(ctx context.Context, req request.Request, opts ...request.RoundTripperOption) (resp request.Response, err error) {
  33. if h.httpRTT == nil {
  34. h.httpRTT = transportcommon.NewALPNAwareHTTPRoundTripper(ctx, func(ctx context.Context, addr string) (gonet.Conn, error) {
  35. return h.assembly.AutoImplDialer().Dial(ctx)
  36. }, unimplementedBackDrop{})
  37. }
  38. connectionTagStr := base64.RawURLEncoding.EncodeToString(req.ConnectionTag)
  39. httpRequest, err := http.NewRequest("POST", h.config.Http.UrlPrefix+h.config.Http.Path, bytes.NewReader(req.Data))
  40. if err != nil {
  41. return
  42. }
  43. httpRequest.Header.Set("X-Session-ID", connectionTagStr)
  44. httpResp, err := h.httpRTT.RoundTrip(httpRequest)
  45. if err != nil {
  46. return
  47. }
  48. defer httpResp.Body.Close()
  49. result, err := io.ReadAll(httpResp.Body)
  50. if err != nil {
  51. return
  52. }
  53. return request.Response{Data: result}, err
  54. }
  55. func newHTTPRoundTripperServer(ctx context.Context, config *ServerConfig) request.RoundTripperServer {
  56. return &httpTripperServer{ctx: ctx, config: config}
  57. }
  58. type httpTripperServer struct {
  59. ctx context.Context
  60. listener net.Listener
  61. assembly request.TransportServerAssembly
  62. config *ServerConfig
  63. }
  64. func (h *httpTripperServer) OnTransportServerAssemblyReady(assembly request.TransportServerAssembly) {
  65. h.assembly = assembly
  66. }
  67. func (h *httpTripperServer) ServeHTTP(writer http.ResponseWriter, r *http.Request) {
  68. h.onRequest(writer, r)
  69. }
  70. func (h *httpTripperServer) onRequest(resp http.ResponseWriter, req *http.Request) {
  71. tail := req.Header.Get("X-Session-ID")
  72. data := []byte(tail)
  73. if !h.config.NoDecodingSessionTag {
  74. decodedData, err := base64.RawURLEncoding.DecodeString(tail)
  75. if err != nil {
  76. newError("unable to decode tag").Base(err).AtInfo().WriteToLog()
  77. return
  78. }
  79. data = decodedData
  80. }
  81. body, err := io.ReadAll(req.Body)
  82. req.Body.Close()
  83. if err != nil {
  84. newError("unable to read body").Base(err).AtInfo().WriteToLog()
  85. }
  86. recvResp, err := h.assembly.TripperReceiver().OnRoundTrip(h.ctx, request.Request{Data: body, ConnectionTag: data})
  87. if err != nil {
  88. newError("unable to process roundtrip").Base(err).AtInfo().WriteToLog()
  89. }
  90. _, err = io.Copy(resp, bytes.NewReader(recvResp.Data))
  91. if err != nil {
  92. newError("unable to send response").Base(err).AtInfo().WriteToLog()
  93. }
  94. }
  95. func (h *httpTripperServer) Start() error {
  96. listener, err := h.assembly.AutoImplListener().Listen(h.ctx)
  97. if err != nil {
  98. return newError("unable to create a listener for http tripper server").Base(err)
  99. }
  100. h.listener = listener
  101. go func() {
  102. httpServer := http.Server{
  103. ReadHeaderTimeout: 15 * time.Second,
  104. ReadTimeout: 15 * time.Second,
  105. WriteTimeout: 10 * time.Second,
  106. IdleTimeout: 30 * time.Second,
  107. }
  108. httpServer.Handler = h
  109. err := httpServer.Serve(h.listener)
  110. if err != nil {
  111. newError("unable to serve listener for http tripper server").Base(err).WriteToLog()
  112. }
  113. }()
  114. return nil
  115. }
  116. func (h *httpTripperServer) Close() error {
  117. return h.listener.Close()
  118. }
  119. func init() {
  120. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  121. clientConfig, ok := config.(*ClientConfig)
  122. if !ok {
  123. return nil, newError("not a ClientConfig")
  124. }
  125. return newHTTPRoundTripperClient(ctx, clientConfig), nil
  126. }))
  127. common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  128. serverConfig, ok := config.(*ServerConfig)
  129. if !ok {
  130. return nil, newError("not a ServerConfig")
  131. }
  132. return newHTTPRoundTripperServer(ctx, serverConfig), nil
  133. }))
  134. }