proxy.go 2.9 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/errors"
  9. v2io "v2ray.com/core/common/io"
  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. 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, nil, stream)
  46. return NewProxyConnection(src, dest, stream), nil
  47. }
  48. func (v *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 (v *ProxyConnection) Read(b []byte) (int, error) {
  74. if v.closed {
  75. return 0, io.EOF
  76. }
  77. return v.reader.Read(b)
  78. }
  79. func (v *ProxyConnection) Write(b []byte) (int, error) {
  80. if v.closed {
  81. return 0, io.ErrClosedPipe
  82. }
  83. return v.writer.Write(b)
  84. }
  85. func (v *ProxyConnection) Close() error {
  86. v.closed = true
  87. v.stream.InboundInput().Close()
  88. v.stream.InboundOutput().Release()
  89. v.reader.Release()
  90. v.writer.Release()
  91. return nil
  92. }
  93. func (v *ProxyConnection) LocalAddr() net.Addr {
  94. return v.localAddr
  95. }
  96. func (v *ProxyConnection) RemoteAddr() net.Addr {
  97. return v.remoteAddr
  98. }
  99. func (v *ProxyConnection) SetDeadline(t time.Time) error {
  100. return nil
  101. }
  102. func (v *ProxyConnection) SetReadDeadline(t time.Time) error {
  103. return nil
  104. }
  105. func (v *ProxyConnection) SetWriteDeadline(t time.Time) error {
  106. return nil
  107. }
  108. func (v *ProxyConnection) Reusable() bool {
  109. return false
  110. }
  111. func (v *ProxyConnection) SetReusable(bool) {
  112. }