httprt.go 4.2 KB

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