handler.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package outbound
  2. import (
  3. "context"
  4. "io"
  5. "time"
  6. "v2ray.com/core"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/app/proxyman/mux"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/errors"
  11. "v2ray.com/core/common/net"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. type Handler struct {
  17. config *core.OutboundHandlerConfig
  18. senderSettings *proxyman.SenderConfig
  19. proxy proxy.Outbound
  20. outboundManager core.OutboundHandlerManager
  21. mux *mux.ClientManager
  22. }
  23. func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (*Handler, error) {
  24. v := core.FromContext(ctx)
  25. if v == nil {
  26. return nil, newError("V is not in context")
  27. }
  28. h := &Handler{
  29. config: config,
  30. outboundManager: v.OutboundHandlerManager(),
  31. }
  32. if config.SenderSettings != nil {
  33. senderSettings, err := config.SenderSettings.GetInstance()
  34. if err != nil {
  35. return nil, err
  36. }
  37. switch s := senderSettings.(type) {
  38. case *proxyman.SenderConfig:
  39. h.senderSettings = s
  40. default:
  41. return nil, newError("settings is not SenderConfig")
  42. }
  43. }
  44. proxyConfig, err := config.ProxySettings.GetInstance()
  45. if err != nil {
  46. return nil, err
  47. }
  48. proxyHandler, err := proxy.CreateOutboundHandler(ctx, proxyConfig)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  53. config := h.senderSettings.MultiplexSettings
  54. if config.Concurrency < 1 || config.Concurrency > 1024 {
  55. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  56. }
  57. h.mux = mux.NewClientManager(proxyHandler, h, config)
  58. }
  59. h.proxy = proxyHandler
  60. return h, nil
  61. }
  62. func (h *Handler) Tag() string {
  63. return h.config.Tag
  64. }
  65. // Dispatch implements proxy.Outbound.Dispatch.
  66. func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  67. if h.mux != nil {
  68. err := h.mux.Dispatch(ctx, outboundRay)
  69. if err != nil {
  70. newError("failed to process outbound traffic").Base(err).WriteToLog()
  71. outboundRay.OutboundOutput().CloseError()
  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. newError("failed to process outbound traffic").Base(err).WriteToLog()
  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. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog()
  93. ctx = proxy.ContextWithTarget(ctx, dest)
  94. stream := ray.NewRay(ctx)
  95. go handler.Dispatch(ctx, stream)
  96. return NewConnection(stream), nil
  97. }
  98. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog()
  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.Reader = (*Connection)(nil)
  111. _ buf.Writer = (*Connection)(nil)
  112. )
  113. type Connection struct {
  114. stream ray.Ray
  115. closed bool
  116. localAddr net.Addr
  117. remoteAddr net.Addr
  118. reader *buf.BufferedReader
  119. writer buf.Writer
  120. }
  121. func NewConnection(stream ray.Ray) *Connection {
  122. return &Connection{
  123. stream: stream,
  124. localAddr: &net.TCPAddr{
  125. IP: []byte{0, 0, 0, 0},
  126. Port: 0,
  127. },
  128. remoteAddr: &net.TCPAddr{
  129. IP: []byte{0, 0, 0, 0},
  130. Port: 0,
  131. },
  132. reader: buf.NewBufferedReader(stream.InboundOutput()),
  133. writer: stream.InboundInput(),
  134. }
  135. }
  136. // Read implements net.Conn.Read().
  137. func (v *Connection) Read(b []byte) (int, error) {
  138. if v.closed {
  139. return 0, io.EOF
  140. }
  141. return v.reader.Read(b)
  142. }
  143. func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  144. return v.reader.ReadMultiBuffer()
  145. }
  146. // Write implements net.Conn.Write().
  147. func (v *Connection) Write(b []byte) (int, error) {
  148. if v.closed {
  149. return 0, io.ErrClosedPipe
  150. }
  151. l := len(b)
  152. mb := buf.NewMultiBufferCap(l/buf.Size + 1)
  153. mb.Write(b)
  154. return l, v.writer.WriteMultiBuffer(mb)
  155. }
  156. func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  157. if v.closed {
  158. return io.ErrClosedPipe
  159. }
  160. return v.writer.WriteMultiBuffer(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. }