freedom.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package freedom
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/app/dns"
  7. "github.com/v2ray/v2ray-core/common/alloc"
  8. "github.com/v2ray/v2ray-core/common/dice"
  9. v2io "github.com/v2ray/v2ray-core/common/io"
  10. "github.com/v2ray/v2ray-core/common/log"
  11. v2net "github.com/v2ray/v2ray-core/common/net"
  12. "github.com/v2ray/v2ray-core/common/retry"
  13. "github.com/v2ray/v2ray-core/proxy"
  14. "github.com/v2ray/v2ray-core/proxy/internal"
  15. "github.com/v2ray/v2ray-core/transport/internet"
  16. "github.com/v2ray/v2ray-core/transport/internet/tcp"
  17. "github.com/v2ray/v2ray-core/transport/ray"
  18. )
  19. type FreedomConnection struct {
  20. domainStrategy 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 == DomainStrategyUseIP {
  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
  44. func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
  45. if !destination.Address().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.IsTCP() {
  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 == DomainStrategyUseIP && destination.Address().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.StreamSettings)
  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. var readMutex, writeMutex sync.Mutex
  88. readMutex.Lock()
  89. writeMutex.Lock()
  90. conn.Write(payload.Value)
  91. go func() {
  92. v2writer := v2io.NewAdaptiveWriter(conn)
  93. defer v2writer.Release()
  94. v2io.Pipe(input, v2writer)
  95. writeMutex.Unlock()
  96. }()
  97. go func() {
  98. defer readMutex.Unlock()
  99. var reader io.Reader = conn
  100. timeout := this.timeout
  101. if destination.IsUDP() {
  102. timeout = 16
  103. }
  104. if timeout > 0 {
  105. reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
  106. }
  107. v2reader := v2io.NewAdaptiveReader(reader)
  108. defer v2reader.Release()
  109. v2io.Pipe(v2reader, output)
  110. ray.OutboundOutput().Close()
  111. }()
  112. writeMutex.Lock()
  113. if tcpConn, ok := conn.(*tcp.RawConnection); ok {
  114. tcpConn.CloseWrite()
  115. }
  116. readMutex.Lock()
  117. return nil
  118. }
  119. type FreedomFactory struct{}
  120. func (this *FreedomFactory) StreamCapability() internet.StreamConnectionType {
  121. return internet.StreamConnectionTypeRawTCP
  122. }
  123. func (this *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  124. return NewFreedomConnection(config.(*Config), space, meta), nil
  125. }
  126. func init() {
  127. internal.MustRegisterOutboundHandlerCreator("freedom", new(FreedomFactory))
  128. }