freedom.go 3.8 KB

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