proxy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) RegisterDialer() {
  34. internet.ProxyDialer = this.Dial
  35. }
  36. func (this *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
  37. handler := this.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. stream := ray.NewRay()
  45. go handler.Dispatch(dest, alloc.NewLocalBuffer(32).Clear(), stream)
  46. return NewProxyConnection(src, dest, stream), nil
  47. }
  48. func (this *OutboundProxy) Release() {
  49. }
  50. type ProxyConnection struct {
  51. stream ray.Ray
  52. closed bool
  53. localAddr net.Addr
  54. remoteAddr net.Addr
  55. reader *v2io.ChanReader
  56. writer *v2io.ChainWriter
  57. }
  58. func NewProxyConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *ProxyConnection {
  59. return &ProxyConnection{
  60. stream: stream,
  61. localAddr: &net.TCPAddr{
  62. IP: []byte{0, 0, 0, 0},
  63. Port: 0,
  64. },
  65. remoteAddr: &net.TCPAddr{
  66. IP: []byte{0, 0, 0, 0},
  67. Port: 0,
  68. },
  69. reader: v2io.NewChanReader(stream.InboundOutput()),
  70. writer: v2io.NewChainWriter(stream.InboundInput()),
  71. }
  72. }
  73. func (this *ProxyConnection) Read(b []byte) (int, error) {
  74. if this.closed {
  75. return 0, io.EOF
  76. }
  77. return this.reader.Read(b)
  78. }
  79. func (this *ProxyConnection) Write(b []byte) (int, error) {
  80. if this.closed {
  81. return 0, io.ErrClosedPipe
  82. }
  83. return this.writer.Write(b)
  84. }
  85. func (this *ProxyConnection) Close() error {
  86. this.closed = true
  87. this.stream.InboundInput().Close()
  88. this.stream.InboundOutput().Release()
  89. return nil
  90. }
  91. func (this *ProxyConnection) LocalAddr() net.Addr {
  92. return this.localAddr
  93. }
  94. func (this *ProxyConnection) RemoteAddr() net.Addr {
  95. return this.remoteAddr
  96. }
  97. func (this *ProxyConnection) SetDeadline(t time.Time) error {
  98. return nil
  99. }
  100. func (this *ProxyConnection) SetReadDeadline(t time.Time) error {
  101. return nil
  102. }
  103. func (this *ProxyConnection) SetWriteDeadline(t time.Time) error {
  104. return nil
  105. }
  106. func (this *ProxyConnection) Reusable() bool {
  107. return false
  108. }
  109. func (this *ProxyConnection) SetReusable(bool) {
  110. }