httprt.go 4.3 KB

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