handler.go 5.3 KB

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