freedom.go 3.5 KB

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