1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-26 13:56:12 -04:00
v2fly/proxy/freedom/freedom.go

154 lines
4.0 KiB
Go
Raw Normal View History

2015-09-11 08:12:09 -04:00
package freedom
2015-09-09 11:39:06 -04:00
import (
2016-02-01 10:36:33 -05:00
"io"
2015-09-23 08:14:53 -04:00
"sync"
2015-09-09 18:50:21 -04:00
2016-05-22 16:30:21 -04:00
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dns"
2016-04-25 18:13:26 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-05-22 16:30:21 -04:00
"github.com/v2ray/v2ray-core/common/dice"
2016-01-29 08:39:55 -05:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-12-16 19:19:04 -05:00
"github.com/v2ray/v2ray-core/common/retry"
2016-05-22 16:30:21 -04:00
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/internal"
2016-06-14 16:54:08 -04:00
"github.com/v2ray/v2ray-core/transport/internet"
"github.com/v2ray/v2ray-core/transport/internet/tcp"
2015-10-14 08:51:19 -04:00
"github.com/v2ray/v2ray-core/transport/ray"
2015-09-09 11:39:06 -04:00
)
2015-09-12 16:11:54 -04:00
type FreedomConnection struct {
2016-05-22 16:30:21 -04:00
domainStrategy DomainStrategy
2016-06-02 17:18:44 -04:00
timeout uint32
2016-05-22 16:30:21 -04:00
dns dns.Server
2016-06-04 08:25:13 -04:00
meta *proxy.OutboundHandlerMeta
2016-05-22 16:30:21 -04:00
}
2016-06-04 08:25:13 -04:00
func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection {
2016-05-22 16:30:21 -04:00
f := &FreedomConnection{
domainStrategy: config.DomainStrategy,
2016-06-02 17:18:44 -04:00
timeout: config.Timeout,
2016-06-04 08:25:13 -04:00
meta: meta,
2016-05-22 16:30:21 -04:00
}
space.InitializeApplication(func() error {
if config.DomainStrategy == DomainStrategyUseIP {
if !space.HasApp(dns.APP_ID) {
log.Error("Freedom: DNS server is not found in the space.")
2016-06-27 02:53:35 -04:00
return app.ErrMissingApplication
2016-05-22 16:30:21 -04:00
}
f.dns = space.GetApp(dns.APP_ID).(dns.Server)
}
return nil
})
return f
}
// @Private
func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
2016-08-14 12:14:12 -04:00
if !destination.Address().Family().IsDomain() {
2016-05-22 16:30:21 -04:00
return destination
}
ips := this.dns.Get(destination.Address().Domain())
if len(ips) == 0 {
log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
return destination
}
ip := ips[dice.Roll(len(ips))]
var newDest v2net.Destination
if destination.IsTCP() {
newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port())
} else {
newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port())
}
log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
return newDest
2015-09-09 11:39:06 -04:00
}
2016-04-25 18:13:26 -04:00
func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to ", destination)
2015-12-16 19:19:04 -05:00
2016-04-25 18:13:26 -04:00
defer payload.Release()
2016-04-18 12:44:10 -04:00
defer ray.OutboundInput().Release()
2016-05-06 18:19:06 -04:00
defer ray.OutboundOutput().Close()
2016-04-18 12:44:10 -04:00
2016-06-14 16:54:08 -04:00
var conn internet.Connection
2016-08-14 12:14:12 -04:00
if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() {
2016-05-22 16:30:21 -04:00
destination = this.ResolveIP(destination)
}
2015-12-16 19:19:04 -05:00
err := retry.Timed(5, 100).On(func() error {
2016-06-14 16:54:08 -04:00
rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.StreamSettings)
2015-12-16 19:19:04 -05:00
if err != nil {
return err
}
conn = rawConn
return nil
})
2015-09-09 11:39:25 -04:00
if err != nil {
2016-06-03 14:21:46 -04:00
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
return err
}
2015-12-15 10:38:25 -05:00
defer conn.Close()
2015-10-02 09:32:26 -04:00
input := ray.OutboundInput()
output := ray.OutboundOutput()
var readMutex, writeMutex sync.Mutex
readMutex.Lock()
writeMutex.Lock()
2016-04-25 18:13:26 -04:00
conn.Write(payload.Value)
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
2015-09-09 11:39:25 -04:00
2016-04-25 18:13:26 -04:00
v2io.Pipe(input, v2writer)
2015-10-02 09:32:26 -04:00
writeMutex.Unlock()
2016-04-25 18:13:26 -04:00
}()
2015-11-27 15:50:28 -05:00
go func() {
defer readMutex.Unlock()
2016-02-03 15:36:52 -05:00
var reader io.Reader = conn
2016-06-02 17:18:44 -04:00
timeout := this.timeout
2016-04-25 18:13:26 -04:00
if destination.IsUDP() {
2016-06-02 17:18:44 -04:00
timeout = 16
}
if timeout > 0 {
reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
2015-11-27 15:50:28 -05:00
}
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, output)
2016-05-07 03:53:15 -04:00
ray.OutboundOutput().Close()
2015-11-27 15:50:28 -05:00
}()
2015-09-22 12:31:06 -04:00
2015-10-09 19:25:12 -04:00
writeMutex.Lock()
2016-06-14 16:54:08 -04:00
if tcpConn, ok := conn.(*tcp.RawConnection); ok {
2015-12-15 10:38:25 -05:00
tcpConn.CloseWrite()
}
2015-10-09 19:25:12 -04:00
readMutex.Lock()
2015-09-22 12:43:30 -04:00
2015-09-09 18:50:21 -04:00
return nil
2015-09-09 11:39:06 -04:00
}
2016-05-22 16:30:21 -04:00
2016-06-14 16:54:08 -04:00
type FreedomFactory struct{}
func (this *FreedomFactory) StreamCapability() internet.StreamConnectionType {
return internet.StreamConnectionTypeRawTCP
}
func (this *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return NewFreedomConnection(config.(*Config), space, meta), nil
}
2016-05-22 16:30:21 -04:00
func init() {
2016-06-14 16:54:08 -04:00
internal.MustRegisterOutboundHandlerCreator("freedom", new(FreedomFactory))
2016-05-22 16:30:21 -04:00
}