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-09 18:50:21 -04:00
|
|
|
|
2016-11-21 15:13:01 -05:00
|
|
|
"errors"
|
2016-08-20 14:55:45 -04: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"
|
2016-10-16 08:22:21 -04:00
|
|
|
"v2ray.com/core/common/loader"
|
2016-08-20 14:55:45 -04:00
|
|
|
"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 11:39:06 -04:00
|
|
|
)
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
type FreedomConnection struct {
|
2016-08-25 15:55:49 -04:00
|
|
|
domainStrategy Config_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 {
|
2016-08-25 15:55:49 -04:00
|
|
|
if config.DomainStrategy == Config_USE_IP {
|
2016-05-22 16:30:21 -04:00
|
|
|
if !space.HasApp(dns.APP_ID) {
|
2016-11-21 15:13:01 -05:00
|
|
|
return errors.New("Freedom: DNS server is not found in the space.")
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
f.dns = space.GetApp(dns.APP_ID).(dns.Server)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:17:42 -04:00
|
|
|
// Private: Visible for testing.
|
2016-05-22 16:30:21 -04:00
|
|
|
func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
|
2016-09-20 05:53:05 -04:00
|
|
|
if !destination.Address.Family().IsDomain() {
|
2016-05-22 16:30:21 -04:00
|
|
|
return destination
|
|
|
|
}
|
|
|
|
|
2016-09-20 05:53:05 -04:00
|
|
|
ips := this.dns.Get(destination.Address.Domain())
|
2016-05-22 16:30:21 -04:00
|
|
|
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
|
2016-09-20 05:53:05 -04:00
|
|
|
if destination.Network == v2net.Network_TCP {
|
|
|
|
newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port)
|
2016-05-22 16:30:21 -04:00
|
|
|
} else {
|
2016-09-20 05:53:05 -04:00
|
|
|
newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port)
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
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-09-20 05:53:05 -04:00
|
|
|
if this.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
|
2016-05-22 16:30:21 -04:00
|
|
|
destination = this.ResolveIP(destination)
|
|
|
|
}
|
2016-11-20 15:47:51 -05:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2016-11-10 17:41:28 -05:00
|
|
|
rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.GetDialerOptions())
|
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)
|
2015-10-13 12:27:29 -04:00
|
|
|
return err
|
2015-09-22 08:45:03 -04:00
|
|
|
}
|
2015-12-15 10:38:25 -05:00
|
|
|
defer conn.Close()
|
2015-09-22 08:45:03 -04:00
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
|
2016-08-15 06:23:35 -04:00
|
|
|
if !payload.IsEmpty() {
|
|
|
|
conn.Write(payload.Value)
|
|
|
|
}
|
2016-04-25 18:13:26 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
v2writer := v2io.NewAdaptiveWriter(conn)
|
|
|
|
defer v2writer.Release()
|
2015-09-09 11:39:25 -04:00
|
|
|
|
2016-11-21 18:17:49 -05:00
|
|
|
if err := v2io.PipeUntilEOF(input, v2writer); err != nil {
|
|
|
|
log.Info("Freedom: Failed to transport all TCP request: ", err)
|
|
|
|
}
|
2016-08-15 08:31:46 -04:00
|
|
|
if tcpConn, ok := conn.(*tcp.RawConnection); ok {
|
|
|
|
tcpConn.CloseWrite()
|
2015-11-27 15:50:28 -05:00
|
|
|
}
|
|
|
|
}()
|
2015-09-22 12:31:06 -04:00
|
|
|
|
2016-08-15 08:31:46 -04:00
|
|
|
var reader io.Reader = conn
|
|
|
|
|
|
|
|
timeout := this.timeout
|
2016-09-20 05:53:05 -04:00
|
|
|
if destination.Network == v2net.Network_UDP {
|
2016-08-15 08:31:46 -04:00
|
|
|
timeout = 16
|
2015-12-15 10:38:25 -05:00
|
|
|
}
|
2016-08-15 08:31:46 -04:00
|
|
|
if timeout > 0 {
|
2016-08-25 15:55:49 -04:00
|
|
|
reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
|
2016-08-15 08:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
v2reader := v2io.NewAdaptiveReader(reader)
|
2016-11-21 18:17:49 -05:00
|
|
|
if err := v2io.PipeUntilEOF(v2reader, output); err != nil {
|
|
|
|
log.Info("Freedom: Failed to transport all TCP response: ", err)
|
|
|
|
}
|
2016-08-15 08:31:46 -04:00
|
|
|
v2reader.Release()
|
|
|
|
ray.OutboundOutput().Close()
|
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{}
|
|
|
|
|
2016-10-02 17:43:58 -04:00
|
|
|
func (this *FreedomFactory) StreamCapability() v2net.NetworkList {
|
|
|
|
return v2net.NetworkList{
|
|
|
|
Network: []v2net.Network{v2net.Network_RawTCP},
|
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-16 08:22:21 -04:00
|
|
|
registry.MustRegisterOutboundHandlerCreator(loader.GetType(new(Config)), new(FreedomFactory))
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|