handler.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package outbound
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "time"
  7. "v2ray.com/core/app"
  8. "v2ray.com/core/app/log"
  9. "v2ray.com/core/app/proxyman"
  10. "v2ray.com/core/app/proxyman/mux"
  11. "v2ray.com/core/common/buf"
  12. "v2ray.com/core/common/errors"
  13. v2net "v2ray.com/core/common/net"
  14. "v2ray.com/core/proxy"
  15. "v2ray.com/core/transport/internet"
  16. "v2ray.com/core/transport/ray"
  17. )
  18. type Handler struct {
  19. config *proxyman.OutboundHandlerConfig
  20. senderSettings *proxyman.SenderConfig
  21. proxy proxy.Outbound
  22. outboundManager proxyman.OutboundHandlerManager
  23. mux *mux.ClientManager
  24. }
  25. func NewHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) (*Handler, error) {
  26. h := &Handler{
  27. config: config,
  28. }
  29. space := app.SpaceFromContext(ctx)
  30. if space == nil {
  31. return nil, newError("no space in context")
  32. }
  33. space.OnInitialize(func() error {
  34. ohm := proxyman.OutboundHandlerManagerFromSpace(space)
  35. if ohm == nil {
  36. return newError("no OutboundManager in space")
  37. }
  38. h.outboundManager = ohm
  39. return nil
  40. })
  41. if config.SenderSettings != nil {
  42. senderSettings, err := config.SenderSettings.GetInstance()
  43. if err != nil {
  44. return nil, err
  45. }
  46. switch s := senderSettings.(type) {
  47. case *proxyman.SenderConfig:
  48. h.senderSettings = s
  49. default:
  50. return nil, newError("settings is not SenderConfig")
  51. }
  52. }
  53. proxyHandler, err := config.GetProxyHandler(ctx)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  58. config := h.senderSettings.MultiplexSettings
  59. if config.Concurrency < 1 || config.Concurrency > 1024 {
  60. return nil, newError("invalid mux concurrency: ", config.Concurrency)
  61. }
  62. h.mux = mux.NewClientManager(proxyHandler, h, config)
  63. }
  64. h.proxy = proxyHandler
  65. return h, nil
  66. }
  67. // Dispatch implements proxy.Outbound.Dispatch.
  68. func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  69. if h.mux != nil {
  70. err := h.mux.Dispatch(ctx, outboundRay)
  71. if err != nil {
  72. log.Trace(newError("failed to process outbound traffic").Base(err))
  73. }
  74. } else {
  75. err := h.proxy.Process(ctx, outboundRay, h)
  76. // Ensure outbound ray is properly closed.
  77. if err != nil && errors.Cause(err) != io.EOF {
  78. log.Trace(newError("failed to process outbound traffic").Base(err))
  79. outboundRay.OutboundOutput().CloseError()
  80. } else {
  81. outboundRay.OutboundOutput().Close()
  82. }
  83. outboundRay.OutboundInput().CloseError()
  84. }
  85. }
  86. // Dial implements proxy.Dialer.Dial().
  87. func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
  88. if h.senderSettings != nil {
  89. if h.senderSettings.ProxySettings.HasTag() {
  90. tag := h.senderSettings.ProxySettings.Tag
  91. handler := h.outboundManager.GetHandler(tag)
  92. if handler != nil {
  93. log.Trace(newError("proxying to ", tag).AtDebug())
  94. ctx = proxy.ContextWithTarget(ctx, dest)
  95. stream := ray.NewRay(ctx)
  96. go handler.Dispatch(ctx, stream)
  97. return NewConnection(stream), nil
  98. }
  99. log.Trace(newError("failed to get outbound handler with tag: ", tag).AtWarning())
  100. }
  101. if h.senderSettings.Via != nil {
  102. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  103. }
  104. if h.senderSettings.StreamSettings != nil {
  105. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  106. }
  107. }
  108. return internet.Dial(ctx, dest)
  109. }
  110. type Connection struct {
  111. stream ray.Ray
  112. closed bool
  113. localAddr net.Addr
  114. remoteAddr net.Addr
  115. reader io.Reader
  116. writer io.Writer
  117. }
  118. func NewConnection(stream ray.Ray) *Connection {
  119. return &Connection{
  120. stream: stream,
  121. localAddr: &net.TCPAddr{
  122. IP: []byte{0, 0, 0, 0},
  123. Port: 0,
  124. },
  125. remoteAddr: &net.TCPAddr{
  126. IP: []byte{0, 0, 0, 0},
  127. Port: 0,
  128. },
  129. reader: buf.ToBytesReader(stream.InboundOutput()),
  130. writer: buf.ToBytesWriter(stream.InboundInput()),
  131. }
  132. }
  133. // Read implements net.Conn.Read().
  134. func (v *Connection) Read(b []byte) (int, error) {
  135. if v.closed {
  136. return 0, io.EOF
  137. }
  138. return v.reader.Read(b)
  139. }
  140. // Write implements net.Conn.Write().
  141. func (v *Connection) Write(b []byte) (int, error) {
  142. if v.closed {
  143. return 0, io.ErrClosedPipe
  144. }
  145. return v.writer.Write(b)
  146. }
  147. // Close implements net.Conn.Close().
  148. func (v *Connection) Close() error {
  149. v.closed = true
  150. v.stream.InboundInput().Close()
  151. v.stream.InboundOutput().CloseError()
  152. return nil
  153. }
  154. // LocalAddr implements net.Conn.LocalAddr().
  155. func (v *Connection) LocalAddr() net.Addr {
  156. return v.localAddr
  157. }
  158. // RemoteAddr implements net.Conn.RemoteAddr().
  159. func (v *Connection) RemoteAddr() net.Addr {
  160. return v.remoteAddr
  161. }
  162. // SetDeadline implements net.Conn.SetDeadline().
  163. func (v *Connection) SetDeadline(t time.Time) error {
  164. return nil
  165. }
  166. // SetReadDeadline implements net.Conn.SetReadDeadline().
  167. func (v *Connection) SetReadDeadline(t time.Time) error {
  168. return nil
  169. }
  170. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  171. func (v *Connection) SetWriteDeadline(t time.Time) error {
  172. return nil
  173. }