handler.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. err = errors.Base(err).Message("Proxyman|OutboundHandler: Failed to process outbound traffic.")
  68. if errors.IsActionRequired(err) {
  69. log.Warning(err)
  70. } else {
  71. log.Info(err)
  72. }
  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. err = errors.Base(err).Message("Proxyman|OutboundHandler: Failed to process outbound traffic.")
  79. if errors.IsActionRequired(err) {
  80. log.Warning(err)
  81. } else {
  82. log.Info(err)
  83. }
  84. outboundRay.OutboundOutput().CloseError()
  85. } else {
  86. outboundRay.OutboundOutput().Close()
  87. }
  88. outboundRay.OutboundInput().CloseError()
  89. }
  90. }
  91. // Dial implements proxy.Dialer.Dial().
  92. func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
  93. if h.senderSettings != nil {
  94. if h.senderSettings.ProxySettings.HasTag() {
  95. tag := h.senderSettings.ProxySettings.Tag
  96. handler := h.outboundManager.GetHandler(tag)
  97. if handler != nil {
  98. log.Info("Proxyman|OutboundHandler: Proxying to ", tag)
  99. ctx = proxy.ContextWithTarget(ctx, dest)
  100. stream := ray.NewRay(ctx)
  101. go handler.Dispatch(ctx, stream)
  102. return NewConnection(stream), nil
  103. }
  104. log.Warning("Proxyman|OutboundHandler: Failed to get outbound handler with tag: ", tag)
  105. }
  106. if h.senderSettings.Via != nil {
  107. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  108. }
  109. if h.senderSettings.StreamSettings != nil {
  110. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  111. }
  112. }
  113. return internet.Dial(ctx, dest)
  114. }
  115. type Connection struct {
  116. stream ray.Ray
  117. closed bool
  118. localAddr net.Addr
  119. remoteAddr net.Addr
  120. reader io.Reader
  121. writer io.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. reader: buf.ToBytesReader(stream.InboundOutput()),
  135. writer: buf.ToBytesWriter(stream.InboundInput()),
  136. }
  137. }
  138. // Read implements net.Conn.Read().
  139. func (v *Connection) Read(b []byte) (int, error) {
  140. if v.closed {
  141. return 0, io.EOF
  142. }
  143. return v.reader.Read(b)
  144. }
  145. // Write implements net.Conn.Write().
  146. func (v *Connection) Write(b []byte) (int, error) {
  147. if v.closed {
  148. return 0, io.ErrClosedPipe
  149. }
  150. return v.writer.Write(b)
  151. }
  152. // Close implements net.Conn.Close().
  153. func (v *Connection) Close() error {
  154. v.closed = true
  155. v.stream.InboundInput().Close()
  156. v.stream.InboundOutput().CloseError()
  157. return nil
  158. }
  159. // LocalAddr implements net.Conn.LocalAddr().
  160. func (v *Connection) LocalAddr() net.Addr {
  161. return v.localAddr
  162. }
  163. // RemoteAddr implements net.Conn.RemoteAddr().
  164. func (v *Connection) RemoteAddr() net.Addr {
  165. return v.remoteAddr
  166. }
  167. // SetDeadline implements net.Conn.SetDeadline().
  168. func (v *Connection) SetDeadline(t time.Time) error {
  169. return nil
  170. }
  171. // SetReadDeadline implements net.Conn.SetReadDeadline().
  172. func (v *Connection) SetReadDeadline(t time.Time) error {
  173. return nil
  174. }
  175. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  176. func (v *Connection) SetWriteDeadline(t time.Time) error {
  177. return nil
  178. }
  179. func (v *Connection) Reusable() bool {
  180. return false
  181. }
  182. func (v *Connection) SetReusable(bool) {
  183. }