handler.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.On(app.SpaceInitializing, func(interface{}) 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. outboundRay.OutboundOutput().CloseError()
  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 net.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.Reader = (*Connection)(nil)
  112. _ buf.Writer = (*Connection)(nil)
  113. )
  114. type Connection struct {
  115. stream ray.Ray
  116. closed bool
  117. localAddr net.Addr
  118. remoteAddr net.Addr
  119. reader *buf.BufferedReader
  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. reader: buf.NewBufferedReader(stream.InboundOutput()),
  134. writer: stream.InboundInput(),
  135. }
  136. }
  137. // Read implements net.Conn.Read().
  138. func (v *Connection) Read(b []byte) (int, error) {
  139. if v.closed {
  140. return 0, io.EOF
  141. }
  142. return v.reader.Read(b)
  143. }
  144. func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  145. return v.reader.ReadMultiBuffer()
  146. }
  147. // Write implements net.Conn.Write().
  148. func (v *Connection) Write(b []byte) (int, error) {
  149. if v.closed {
  150. return 0, io.ErrClosedPipe
  151. }
  152. l := len(b)
  153. mb := buf.NewMultiBufferCap(l/buf.Size + 1)
  154. mb.Write(b)
  155. return l, v.writer.WriteMultiBuffer(mb)
  156. }
  157. func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  158. if v.closed {
  159. return io.ErrClosedPipe
  160. }
  161. return v.writer.WriteMultiBuffer(mb)
  162. }
  163. // Close implements net.Conn.Close().
  164. func (v *Connection) Close() error {
  165. v.closed = true
  166. v.stream.InboundInput().Close()
  167. v.stream.InboundOutput().CloseError()
  168. return nil
  169. }
  170. // LocalAddr implements net.Conn.LocalAddr().
  171. func (v *Connection) LocalAddr() net.Addr {
  172. return v.localAddr
  173. }
  174. // RemoteAddr implements net.Conn.RemoteAddr().
  175. func (v *Connection) RemoteAddr() net.Addr {
  176. return v.remoteAddr
  177. }
  178. // SetDeadline implements net.Conn.SetDeadline().
  179. func (v *Connection) SetDeadline(t time.Time) error {
  180. return nil
  181. }
  182. // SetReadDeadline implements net.Conn.SetReadDeadline().
  183. func (v *Connection) SetReadDeadline(t time.Time) error {
  184. return nil
  185. }
  186. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  187. func (v *Connection) SetWriteDeadline(t time.Time) error {
  188. return nil
  189. }