handler.go 4.6 KB

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