proxy.go 3.4 KB

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