handler.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package outbound
  2. import (
  3. "context"
  4. "errors"
  5. "io"
  6. "net"
  7. "time"
  8. "v2ray.com/core/app"
  9. "v2ray.com/core/app/proxyman"
  10. "v2ray.com/core/common/buf"
  11. "v2ray.com/core/common/log"
  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.OutboundHandler
  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(proxy.ContextWithDialer(ctx, h))
  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. ctx = proxy.ContextWithDialer(ctx, h)
  60. h.proxy.Process(ctx, outboundRay)
  61. }
  62. func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
  63. if h.senderSettings != nil {
  64. if h.senderSettings.ProxySettings.HasTag() {
  65. tag := h.senderSettings.ProxySettings.Tag
  66. handler := h.outboundManager.GetHandler(tag)
  67. if handler != nil {
  68. log.Info("Proxyman|OutboundHandler: Proxying to ", dest)
  69. ctx = proxy.ContextWithDestination(ctx, dest)
  70. stream := ray.NewRay(ctx)
  71. go handler.Dispatch(ctx, stream)
  72. return NewConnection(stream), nil
  73. }
  74. log.Warning("Proxyman|OutboundHandler: Failed to get outbound handler with tag: ", tag)
  75. }
  76. if h.senderSettings.Via != nil {
  77. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  78. }
  79. if h.senderSettings != nil {
  80. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  81. }
  82. }
  83. return internet.Dial(ctx, dest)
  84. }
  85. type Connection struct {
  86. stream ray.Ray
  87. closed bool
  88. localAddr net.Addr
  89. remoteAddr net.Addr
  90. reader *buf.BufferToBytesReader
  91. writer *buf.BytesToBufferWriter
  92. }
  93. func NewConnection(stream ray.Ray) *Connection {
  94. return &Connection{
  95. stream: stream,
  96. localAddr: &net.TCPAddr{
  97. IP: []byte{0, 0, 0, 0},
  98. Port: 0,
  99. },
  100. remoteAddr: &net.TCPAddr{
  101. IP: []byte{0, 0, 0, 0},
  102. Port: 0,
  103. },
  104. reader: buf.NewBytesReader(stream.InboundOutput()),
  105. writer: buf.NewBytesWriter(stream.InboundInput()),
  106. }
  107. }
  108. // Read implements net.Conn.Read().
  109. func (v *Connection) Read(b []byte) (int, error) {
  110. if v.closed {
  111. return 0, io.EOF
  112. }
  113. return v.reader.Read(b)
  114. }
  115. // Write implements net.Conn.Write().
  116. func (v *Connection) Write(b []byte) (int, error) {
  117. if v.closed {
  118. return 0, io.ErrClosedPipe
  119. }
  120. return v.writer.Write(b)
  121. }
  122. // Close implements net.Conn.Close().
  123. func (v *Connection) Close() error {
  124. v.closed = true
  125. v.stream.InboundInput().Close()
  126. v.stream.InboundOutput().CloseError()
  127. return nil
  128. }
  129. // LocalAddr implements net.Conn.LocalAddr().
  130. func (v *Connection) LocalAddr() net.Addr {
  131. return v.localAddr
  132. }
  133. // RemoteAddr implements net.Conn.RemoteAddr().
  134. func (v *Connection) RemoteAddr() net.Addr {
  135. return v.remoteAddr
  136. }
  137. func (v *Connection) SetDeadline(t time.Time) error {
  138. return nil
  139. }
  140. func (v *Connection) SetReadDeadline(t time.Time) error {
  141. return nil
  142. }
  143. func (v *Connection) SetWriteDeadline(t time.Time) error {
  144. return nil
  145. }
  146. func (v *Connection) Reusable() bool {
  147. return false
  148. }
  149. func (v *Connection) SetReusable(bool) {
  150. }