handler.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. var (
  111. _ buf.MultiBufferReader = (*Connection)(nil)
  112. _ buf.MultiBufferWriter = (*Connection)(nil)
  113. )
  114. type Connection struct {
  115. stream ray.Ray
  116. closed bool
  117. localAddr net.Addr
  118. remoteAddr net.Addr
  119. bytesReader io.Reader
  120. reader buf.Reader
  121. writer buf.Writer
  122. }
  123. func NewConnection(stream ray.Ray) *Connection {
  124. return &Connection{
  125. stream: stream,
  126. localAddr: &net.TCPAddr{
  127. IP: []byte{0, 0, 0, 0},
  128. Port: 0,
  129. },
  130. remoteAddr: &net.TCPAddr{
  131. IP: []byte{0, 0, 0, 0},
  132. Port: 0,
  133. },
  134. bytesReader: buf.ToBytesReader(stream.InboundOutput()),
  135. reader: stream.InboundOutput(),
  136. writer: stream.InboundInput(),
  137. }
  138. }
  139. // Read implements net.Conn.Read().
  140. func (v *Connection) Read(b []byte) (int, error) {
  141. if v.closed {
  142. return 0, io.EOF
  143. }
  144. return v.bytesReader.Read(b)
  145. }
  146. func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  147. return v.reader.Read()
  148. }
  149. // Write implements net.Conn.Write().
  150. func (v *Connection) Write(b []byte) (int, error) {
  151. if v.closed {
  152. return 0, io.ErrClosedPipe
  153. }
  154. return buf.ToBytesWriter(v.writer).Write(b)
  155. }
  156. func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  157. if v.closed {
  158. return io.ErrClosedPipe
  159. }
  160. return v.writer.Write(mb)
  161. }
  162. // Close implements net.Conn.Close().
  163. func (v *Connection) Close() error {
  164. v.closed = true
  165. v.stream.InboundInput().Close()
  166. v.stream.InboundOutput().CloseError()
  167. return nil
  168. }
  169. // LocalAddr implements net.Conn.LocalAddr().
  170. func (v *Connection) LocalAddr() net.Addr {
  171. return v.localAddr
  172. }
  173. // RemoteAddr implements net.Conn.RemoteAddr().
  174. func (v *Connection) RemoteAddr() net.Addr {
  175. return v.remoteAddr
  176. }
  177. // SetDeadline implements net.Conn.SetDeadline().
  178. func (v *Connection) SetDeadline(t time.Time) error {
  179. return nil
  180. }
  181. // SetReadDeadline implements net.Conn.SetReadDeadline().
  182. func (v *Connection) SetReadDeadline(t time.Time) error {
  183. return nil
  184. }
  185. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  186. func (v *Connection) SetWriteDeadline(t time.Time) error {
  187. return nil
  188. }