proxy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, stream)
  47. return NewConnection(src, dest, stream), nil
  48. }
  49. type Connection struct {
  50. stream ray.Ray
  51. closed bool
  52. localAddr net.Addr
  53. remoteAddr net.Addr
  54. reader *buf.BufferToBytesReader
  55. writer *buf.BytesToBufferWriter
  56. }
  57. func NewConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *Connection {
  58. return &Connection{
  59. stream: stream,
  60. localAddr: &net.TCPAddr{
  61. IP: []byte{0, 0, 0, 0},
  62. Port: 0,
  63. },
  64. remoteAddr: &net.TCPAddr{
  65. IP: []byte{0, 0, 0, 0},
  66. Port: 0,
  67. },
  68. reader: buf.NewBytesReader(stream.InboundOutput()),
  69. writer: buf.NewBytesWriter(stream.InboundInput()),
  70. }
  71. }
  72. // Read implements net.Conn.Read().
  73. func (v *Connection) Read(b []byte) (int, error) {
  74. if v.closed {
  75. return 0, io.EOF
  76. }
  77. return v.reader.Read(b)
  78. }
  79. // Write implements net.Conn.Write().
  80. func (v *Connection) Write(b []byte) (int, error) {
  81. if v.closed {
  82. return 0, io.ErrClosedPipe
  83. }
  84. return v.writer.Write(b)
  85. }
  86. // Close implements net.Conn.Close().
  87. func (v *Connection) Close() error {
  88. v.closed = true
  89. v.stream.InboundInput().Close()
  90. v.stream.InboundOutput().ForceClose()
  91. return nil
  92. }
  93. // LocalAddr implements net.Conn.LocalAddr().
  94. func (v *Connection) LocalAddr() net.Addr {
  95. return v.localAddr
  96. }
  97. // RemoteAddr implements net.Conn.RemoteAddr().
  98. func (v *Connection) RemoteAddr() net.Addr {
  99. return v.remoteAddr
  100. }
  101. func (v *Connection) SetDeadline(t time.Time) error {
  102. return nil
  103. }
  104. func (v *Connection) SetReadDeadline(t time.Time) error {
  105. return nil
  106. }
  107. func (v *Connection) SetWriteDeadline(t time.Time) error {
  108. return nil
  109. }
  110. func (v *Connection) Reusable() bool {
  111. return false
  112. }
  113. func (v *Connection) SetReusable(bool) {
  114. }