handler.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, errors.New("Proxyman|OutboundHandler: No space in context.")
  32. }
  33. space.OnInitialize(func() error {
  34. ohm := proxyman.OutboundHandlerManagerFromSpace(space)
  35. if ohm == nil {
  36. return errors.New("Proxyman|OutboundHandler: 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, errors.New("Proxyman|DefaultOutboundHandler: 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. h.mux = mux.NewClientManager(proxyHandler, h)
  59. }
  60. h.proxy = proxyHandler
  61. return h, nil
  62. }
  63. func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  64. if h.mux != nil {
  65. err := h.mux.Dispatch(ctx, outboundRay)
  66. if err != nil {
  67. log.Trace(errors.New("failed to process outbound traffic").Base(err).Path("Proxyman", "OutboundHandler"))
  68. }
  69. } else {
  70. err := h.proxy.Process(ctx, outboundRay, h)
  71. // Ensure outbound ray is properly closed.
  72. if err != nil && errors.Cause(err) != io.EOF {
  73. log.Trace(errors.New("failed to process outbound traffic").Base(err).Path("Proxyman", "OutboundHandler"))
  74. outboundRay.OutboundOutput().CloseError()
  75. } else {
  76. outboundRay.OutboundOutput().Close()
  77. }
  78. outboundRay.OutboundInput().CloseError()
  79. }
  80. }
  81. // Dial implements proxy.Dialer.Dial().
  82. func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
  83. if h.senderSettings != nil {
  84. if h.senderSettings.ProxySettings.HasTag() {
  85. tag := h.senderSettings.ProxySettings.Tag
  86. handler := h.outboundManager.GetHandler(tag)
  87. if handler != nil {
  88. log.Trace(errors.New("proxying to ", tag).AtDebug().Path("App", "Proxyman", "OutboundHandler"))
  89. ctx = proxy.ContextWithTarget(ctx, dest)
  90. stream := ray.NewRay(ctx)
  91. go handler.Dispatch(ctx, stream)
  92. return NewConnection(stream), nil
  93. }
  94. log.Trace(errors.New("Proxyman|OutboundHandler: Failed to get outbound handler with tag: ", tag).AtWarning())
  95. }
  96. if h.senderSettings.Via != nil {
  97. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  98. }
  99. if h.senderSettings.StreamSettings != nil {
  100. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  101. }
  102. }
  103. return internet.Dial(ctx, dest)
  104. }
  105. type Connection struct {
  106. stream ray.Ray
  107. closed bool
  108. localAddr net.Addr
  109. remoteAddr net.Addr
  110. reader io.Reader
  111. writer io.Writer
  112. }
  113. func NewConnection(stream ray.Ray) *Connection {
  114. return &Connection{
  115. stream: stream,
  116. localAddr: &net.TCPAddr{
  117. IP: []byte{0, 0, 0, 0},
  118. Port: 0,
  119. },
  120. remoteAddr: &net.TCPAddr{
  121. IP: []byte{0, 0, 0, 0},
  122. Port: 0,
  123. },
  124. reader: buf.ToBytesReader(stream.InboundOutput()),
  125. writer: buf.ToBytesWriter(stream.InboundInput()),
  126. }
  127. }
  128. // Read implements net.Conn.Read().
  129. func (v *Connection) Read(b []byte) (int, error) {
  130. if v.closed {
  131. return 0, io.EOF
  132. }
  133. return v.reader.Read(b)
  134. }
  135. // Write implements net.Conn.Write().
  136. func (v *Connection) Write(b []byte) (int, error) {
  137. if v.closed {
  138. return 0, io.ErrClosedPipe
  139. }
  140. return v.writer.Write(b)
  141. }
  142. // Close implements net.Conn.Close().
  143. func (v *Connection) Close() error {
  144. v.closed = true
  145. v.stream.InboundInput().Close()
  146. v.stream.InboundOutput().CloseError()
  147. return nil
  148. }
  149. // LocalAddr implements net.Conn.LocalAddr().
  150. func (v *Connection) LocalAddr() net.Addr {
  151. return v.localAddr
  152. }
  153. // RemoteAddr implements net.Conn.RemoteAddr().
  154. func (v *Connection) RemoteAddr() net.Addr {
  155. return v.remoteAddr
  156. }
  157. // SetDeadline implements net.Conn.SetDeadline().
  158. func (v *Connection) SetDeadline(t time.Time) error {
  159. return nil
  160. }
  161. // SetReadDeadline implements net.Conn.SetReadDeadline().
  162. func (v *Connection) SetReadDeadline(t time.Time) error {
  163. return nil
  164. }
  165. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  166. func (v *Connection) SetWriteDeadline(t time.Time) error {
  167. return nil
  168. }
  169. func (v *Connection) Reusable() bool {
  170. return false
  171. }
  172. func (v *Connection) SetReusable(bool) {
  173. }