proxy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package proxy
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "time"
  7. "v2ray.com/core/app"
  8. "v2ray.com/core/app/proxyman"
  9. "v2ray.com/core/common/alloc"
  10. v2io "v2ray.com/core/common/io"
  11. "v2ray.com/core/common/log"
  12. v2net "v2ray.com/core/common/net"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. const (
  17. APP_ID = 7
  18. )
  19. type OutboundProxy struct {
  20. outboundManager proxyman.OutboundHandlerManager
  21. }
  22. func NewOutboundProxy(space app.Space) *OutboundProxy {
  23. proxy := new(OutboundProxy)
  24. space.InitializeApplication(func() error {
  25. if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
  26. return errors.New("Proxy: Outbound handler manager not found.")
  27. }
  28. proxy.outboundManager = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
  29. return nil
  30. })
  31. return proxy
  32. }
  33. func (this *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
  34. handler := this.outboundManager.GetHandler(options.ProxyTag)
  35. if handler == nil {
  36. log.Warning("Proxy: Failed to get outbound handler with tag: ", options.ProxyTag)
  37. return internet.Dial(src, dest, internet.DialerOptions{
  38. Stream: options.Stream,
  39. })
  40. }
  41. stream := ray.NewRay()
  42. if err := handler.Dispatch(dest, alloc.NewLocalBuffer(32).Clear(), stream); err != nil {
  43. return nil, err
  44. }
  45. return NewProxyConnection(src, dest, stream), nil
  46. }
  47. func (this *OutboundProxy) Release() {
  48. }
  49. type ProxyConnection struct {
  50. stream ray.Ray
  51. closed bool
  52. localAddr net.Addr
  53. remoteAddr net.Addr
  54. reader *v2io.ChanReader
  55. writer *v2io.ChainWriter
  56. }
  57. func NewProxyConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *ProxyConnection {
  58. return &ProxyConnection{
  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: v2io.NewChanReader(stream.InboundOutput()),
  69. writer: v2io.NewChainWriter(stream.InboundInput()),
  70. }
  71. }
  72. func (this *ProxyConnection) Read(b []byte) (int, error) {
  73. if this.closed {
  74. return 0, io.EOF
  75. }
  76. return this.reader.Read(b)
  77. }
  78. func (this *ProxyConnection) Write(b []byte) (int, error) {
  79. if this.closed {
  80. return 0, io.EOF
  81. }
  82. return this.writer.Write(b)
  83. }
  84. func (this *ProxyConnection) Close() error {
  85. this.closed = true
  86. this.stream.InboundInput().Close()
  87. this.stream.InboundOutput().Release()
  88. return nil
  89. }
  90. func (this *ProxyConnection) LocalAddr() net.Addr {
  91. return this.localAddr
  92. }
  93. func (this *ProxyConnection) RemoteAddr() net.Addr {
  94. return this.remoteAddr
  95. }
  96. func (this *ProxyConnection) SetDeadline(t time.Time) error {
  97. return nil
  98. }
  99. func (this *ProxyConnection) SetReadDeadline(t time.Time) error {
  100. return nil
  101. }
  102. func (this *ProxyConnection) SetWriteDeadline(t time.Time) error {
  103. return nil
  104. }
  105. func (this *ProxyConnection) Reusable() bool {
  106. return false
  107. }
  108. func (this *ProxyConnection) SetReusable(bool) {
  109. }