proxy.go 3.1 KB

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