proxy.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package proxy
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. "context"
  7. "v2ray.com/core/app"
  8. "v2ray.com/core/app/proxyman"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/buf"
  11. "v2ray.com/core/common/errors"
  12. "v2ray.com/core/common/log"
  13. v2net "v2ray.com/core/common/net"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/ray"
  16. )
  17. type OutboundProxy struct {
  18. outboundManager proxyman.OutboundHandlerManager
  19. }
  20. func NewOutboundProxy(ctx context.Context, config *Config) (*OutboundProxy, error) {
  21. space := app.SpaceFromContext(ctx)
  22. if space == nil {
  23. return nil, errors.New("OutboundProxy: No space in context.")
  24. }
  25. proxy := new(OutboundProxy)
  26. space.OnInitialize(func() error {
  27. proxy.outboundManager = proxyman.OutboundHandlerManagerFromSpace(space)
  28. if proxy.outboundManager == nil {
  29. return errors.New("Proxy: Outbound handler manager not found in space.")
  30. }
  31. return nil
  32. })
  33. return proxy, nil
  34. }
  35. func (OutboundProxy) Interface() interface{} {
  36. return (*OutboundProxy)(nil)
  37. }
  38. func (v *OutboundProxy) RegisterDialer() {
  39. internet.ProxyDialer = v.Dial
  40. }
  41. // Dial implements internet.Dialer.
  42. func (v *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
  43. handler := v.outboundManager.GetHandler(options.Proxy.Tag)
  44. if handler == nil {
  45. log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag)
  46. return internet.Dial(src, dest, internet.DialerOptions{
  47. Stream: options.Stream,
  48. })
  49. }
  50. log.Info("Proxy: Dialing to ", dest)
  51. stream := ray.NewRay()
  52. go handler.Dispatch(dest, stream)
  53. return NewConnection(src, dest, stream), nil
  54. }
  55. type Connection struct {
  56. stream ray.Ray
  57. closed bool
  58. localAddr net.Addr
  59. remoteAddr net.Addr
  60. reader *buf.BufferToBytesReader
  61. writer *buf.BytesToBufferWriter
  62. }
  63. func NewConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *Connection {
  64. return &Connection{
  65. stream: stream,
  66. localAddr: &net.TCPAddr{
  67. IP: []byte{0, 0, 0, 0},
  68. Port: 0,
  69. },
  70. remoteAddr: &net.TCPAddr{
  71. IP: []byte{0, 0, 0, 0},
  72. Port: 0,
  73. },
  74. reader: buf.NewBytesReader(stream.InboundOutput()),
  75. writer: buf.NewBytesWriter(stream.InboundInput()),
  76. }
  77. }
  78. // Read implements net.Conn.Read().
  79. func (v *Connection) Read(b []byte) (int, error) {
  80. if v.closed {
  81. return 0, io.EOF
  82. }
  83. return v.reader.Read(b)
  84. }
  85. // Write implements net.Conn.Write().
  86. func (v *Connection) Write(b []byte) (int, error) {
  87. if v.closed {
  88. return 0, io.ErrClosedPipe
  89. }
  90. return v.writer.Write(b)
  91. }
  92. // Close implements net.Conn.Close().
  93. func (v *Connection) Close() error {
  94. v.closed = true
  95. v.stream.InboundInput().Close()
  96. v.stream.InboundOutput().CloseError()
  97. return nil
  98. }
  99. // LocalAddr implements net.Conn.LocalAddr().
  100. func (v *Connection) LocalAddr() net.Addr {
  101. return v.localAddr
  102. }
  103. // RemoteAddr implements net.Conn.RemoteAddr().
  104. func (v *Connection) RemoteAddr() net.Addr {
  105. return v.remoteAddr
  106. }
  107. func (v *Connection) SetDeadline(t time.Time) error {
  108. return nil
  109. }
  110. func (v *Connection) SetReadDeadline(t time.Time) error {
  111. return nil
  112. }
  113. func (v *Connection) SetWriteDeadline(t time.Time) error {
  114. return nil
  115. }
  116. func (v *Connection) Reusable() bool {
  117. return false
  118. }
  119. func (v *Connection) SetReusable(bool) {
  120. }
  121. func OutboundProxyFromSpace(space app.Space) *OutboundProxy {
  122. app := space.GetApplication((*OutboundProxy)(nil))
  123. if app == nil {
  124. return nil
  125. }
  126. return app.(*OutboundProxy)
  127. }
  128. func init() {
  129. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  130. return NewOutboundProxy(ctx, config.(*Config))
  131. }))
  132. }