freedom.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package freedom
  2. import (
  3. "io"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common/alloc"
  7. "v2ray.com/core/common/dice"
  8. v2io "v2ray.com/core/common/io"
  9. "v2ray.com/core/common/log"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/retry"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/proxy/registry"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/internet/tcp"
  16. "v2ray.com/core/transport/ray"
  17. )
  18. type FreedomConnection struct {
  19. domainStrategy Config_DomainStrategy
  20. timeout uint32
  21. dns dns.Server
  22. meta *proxy.OutboundHandlerMeta
  23. }
  24. func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection {
  25. f := &FreedomConnection{
  26. domainStrategy: config.DomainStrategy,
  27. timeout: config.Timeout,
  28. meta: meta,
  29. }
  30. space.InitializeApplication(func() error {
  31. if config.DomainStrategy == Config_USE_IP {
  32. if !space.HasApp(dns.APP_ID) {
  33. log.Error("Freedom: DNS server is not found in the space.")
  34. return app.ErrMissingApplication
  35. }
  36. f.dns = space.GetApp(dns.APP_ID).(dns.Server)
  37. }
  38. return nil
  39. })
  40. return f
  41. }
  42. // Private: Visible for testing.
  43. func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
  44. if !destination.Address().Family().IsDomain() {
  45. return destination
  46. }
  47. ips := this.dns.Get(destination.Address().Domain())
  48. if len(ips) == 0 {
  49. log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
  50. return destination
  51. }
  52. ip := ips[dice.Roll(len(ips))]
  53. var newDest v2net.Destination
  54. if destination.Network() == v2net.Network_TCP {
  55. newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port())
  56. } else {
  57. newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port())
  58. }
  59. log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
  60. return newDest
  61. }
  62. func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  63. log.Info("Freedom: Opening connection to ", destination)
  64. defer payload.Release()
  65. defer ray.OutboundInput().Release()
  66. defer ray.OutboundOutput().Close()
  67. var conn internet.Connection
  68. if this.domainStrategy == Config_USE_IP && destination.Address().Family().IsDomain() {
  69. destination = this.ResolveIP(destination)
  70. }
  71. err := retry.Timed(5, 100).On(func() error {
  72. rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.StreamSettings)
  73. if err != nil {
  74. return err
  75. }
  76. conn = rawConn
  77. return nil
  78. })
  79. if err != nil {
  80. log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
  81. return err
  82. }
  83. defer conn.Close()
  84. input := ray.OutboundInput()
  85. output := ray.OutboundOutput()
  86. if !payload.IsEmpty() {
  87. conn.Write(payload.Value)
  88. }
  89. go func() {
  90. v2writer := v2io.NewAdaptiveWriter(conn)
  91. defer v2writer.Release()
  92. v2io.Pipe(input, v2writer)
  93. if tcpConn, ok := conn.(*tcp.RawConnection); ok {
  94. tcpConn.CloseWrite()
  95. }
  96. }()
  97. var reader io.Reader = conn
  98. timeout := this.timeout
  99. if destination.Network() == v2net.Network_UDP {
  100. timeout = 16
  101. }
  102. if timeout > 0 {
  103. reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
  104. }
  105. v2reader := v2io.NewAdaptiveReader(reader)
  106. v2io.Pipe(v2reader, output)
  107. v2reader.Release()
  108. ray.OutboundOutput().Close()
  109. return nil
  110. }
  111. type FreedomFactory struct{}
  112. func (this *FreedomFactory) StreamCapability() internet.StreamConnectionType {
  113. return internet.StreamConnectionTypeRawTCP
  114. }
  115. func (this *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  116. return NewFreedomConnection(config.(*Config), space, meta), nil
  117. }
  118. func init() {
  119. registry.MustRegisterOutboundHandlerCreator("freedom", new(FreedomFactory))
  120. }