1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00
v2fly/proxy/freedom/freedom.go

143 lines
3.7 KiB
Go
Raw Normal View History

2015-09-11 12:12:09 +00:00
package freedom
2015-09-09 15:39:06 +00:00
import (
2016-02-01 15:36:33 +00:00
"io"
2015-09-09 22:50:21 +00:00
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/dice"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tcp"
"v2ray.com/core/transport/ray"
2015-09-09 15:39:06 +00:00
)
2015-09-12 20:11:54 +00:00
type FreedomConnection struct {
2016-05-22 20:30:21 +00:00
domainStrategy DomainStrategy
2016-06-02 21:18:44 +00:00
timeout uint32
2016-05-22 20:30:21 +00:00
dns dns.Server
2016-06-04 12:25:13 +00:00
meta *proxy.OutboundHandlerMeta
2016-05-22 20:30:21 +00:00
}
2016-06-04 12:25:13 +00:00
func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection {
2016-05-22 20:30:21 +00:00
f := &FreedomConnection{
domainStrategy: config.DomainStrategy,
2016-06-02 21:18:44 +00:00
timeout: config.Timeout,
2016-06-04 12:25:13 +00:00
meta: meta,
2016-05-22 20:30:21 +00: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 06:53:35 +00:00
return app.ErrMissingApplication
2016-05-22 20:30:21 +00: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 16:14:12 +00:00
if !destination.Address().Family().IsDomain() {
2016-05-22 20:30:21 +00: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 15:39:06 +00:00
}
2016-04-25 22:13:26 +00:00
func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
log.Info("Freedom: Opening connection to ", destination)
2015-12-17 00:19:04 +00:00
2016-04-25 22:13:26 +00:00
defer payload.Release()
2016-04-18 16:44:10 +00:00
defer ray.OutboundInput().Release()
2016-05-06 22:19:06 +00:00
defer ray.OutboundOutput().Close()
2016-04-18 16:44:10 +00:00
2016-06-14 20:54:08 +00:00
var conn internet.Connection
2016-08-14 16:14:12 +00:00
if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() {
2016-05-22 20:30:21 +00:00
destination = this.ResolveIP(destination)
}
2015-12-17 00:19:04 +00:00
err := retry.Timed(5, 100).On(func() error {
2016-06-14 20:54:08 +00:00
rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.StreamSettings)
2015-12-17 00:19:04 +00:00
if err != nil {
return err
}
conn = rawConn
return nil
})
2015-09-09 15:39:25 +00:00
if err != nil {
2016-06-03 18:21:46 +00:00
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
return err
}
2015-12-15 15:38:25 +00:00
defer conn.Close()
2015-10-02 13:32:26 +00:00
input := ray.OutboundInput()
output := ray.OutboundOutput()
if !payload.IsEmpty() {
conn.Write(payload.Value)
}
2016-04-25 22:13:26 +00:00
go func() {
v2writer := v2io.NewAdaptiveWriter(conn)
defer v2writer.Release()
2015-09-09 15:39:25 +00:00
2016-04-25 22:13:26 +00:00
v2io.Pipe(input, v2writer)
if tcpConn, ok := conn.(*tcp.RawConnection); ok {
tcpConn.CloseWrite()
2015-11-27 20:50:28 +00:00
}
}()
2015-09-22 16:31:06 +00:00
var reader io.Reader = conn
timeout := this.timeout
if destination.IsUDP() {
timeout = 16
2015-12-15 15:38:25 +00:00
}
if timeout > 0 {
reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
}
v2reader := v2io.NewAdaptiveReader(reader)
v2io.Pipe(v2reader, output)
v2reader.Release()
ray.OutboundOutput().Close()
2015-09-22 16:43:30 +00:00
2015-09-09 22:50:21 +00:00
return nil
2015-09-09 15:39:06 +00:00
}
2016-05-22 20:30:21 +00:00
2016-06-14 20:54:08 +00: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 20:30:21 +00:00
func init() {
registry.MustRegisterOutboundHandlerCreator("freedom", new(FreedomFactory))
2016-05-22 20:30:21 +00:00
}