httprt.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return &httpTripperClient{config: config}
  17. }
  18. type httpTripperClient struct {
  19. httpRTT http.RoundTripper
  20. config *ClientConfig
  21. assembly request.TransportClientAssembly
  22. }
  23. type unimplementedBackDrop struct {
  24. }
  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. listenAddress net.Addr
  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. err := http.Serve(listener, h)
  103. if err != nil {
  104. newError("unable to serve listener for http tripper server").Base(err).WriteToLog()
  105. }
  106. }()
  107. return nil
  108. }
  109. func (h *httpTripperServer) Close() error {
  110. return h.listener.Close()
  111. }
  112. func init() {
  113. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  114. clientConfig, ok := config.(*ClientConfig)
  115. if !ok {
  116. return nil, newError("not a ClientConfig")
  117. }
  118. return newHTTPRoundTripperClient(ctx, clientConfig), nil
  119. }))
  120. common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  121. serverConfig, ok := config.(*ServerConfig)
  122. if !ok {
  123. return nil, newError("not a ServerConfig")
  124. }
  125. return newHTTPRoundTripperServer(ctx, serverConfig), nil
  126. }))
  127. }