v2ray-core/proxy/freedom/freedom.go

144 lines
3.5 KiB
Go
Raw Normal View History

2015-09-11 14:12:09 +02:00
package freedom
2015-09-09 17:39:06 +02:00
import (
"context"
2016-11-22 08:21:10 +01:00
"io"
2016-12-09 13:17:34 +01:00
2016-08-20 20:55:45 +02:00
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
2016-12-28 00:53:29 +01:00
"v2ray.com/core/common"
2016-12-09 11:35:27 +01:00
"v2ray.com/core/common/buf"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/dice"
2016-12-04 09:10:47 +01:00
"v2ray.com/core/common/errors"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/log"
2017-01-13 23:42:39 +01:00
"v2ray.com/core/common/net"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/retry"
2016-12-30 00:32:20 +01:00
"v2ray.com/core/common/signal"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-09 17:39:06 +02:00
)
2016-12-22 13:33:28 +01:00
type Handler struct {
2016-08-25 21:55:49 +02:00
domainStrategy Config_DomainStrategy
2016-06-02 23:18:44 +02:00
timeout uint32
2016-05-22 22:30:21 +02:00
dns dns.Server
2016-06-04 14:25:13 +02:00
meta *proxy.OutboundHandlerMeta
2016-05-22 22:30:21 +02:00
}
func New(ctx context.Context, config *Config) (*Handler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Freedom: No space in context.")
}
meta := proxy.OutboundMetaFromContext(ctx)
if meta == nil {
return nil, errors.New("Freedom: No outbound meta in context.")
}
2016-12-22 13:33:28 +01:00
f := &Handler{
2016-05-22 22:30:21 +02:00
domainStrategy: config.DomainStrategy,
2016-06-02 23:18:44 +02:00
timeout: config.Timeout,
2016-06-04 14:25:13 +02:00
meta: meta,
2016-05-22 22:30:21 +02:00
}
2017-01-06 15:32:36 +01:00
space.OnInitialize(func() error {
2016-08-25 21:55:49 +02:00
if config.DomainStrategy == Config_USE_IP {
2017-01-06 15:32:36 +01:00
f.dns = dns.FromSpace(space)
if f.dns == nil {
2016-11-21 21:13:01 +01:00
return errors.New("Freedom: DNS server is not found in the space.")
2016-05-22 22:30:21 +02:00
}
}
return nil
})
return f, nil
2016-05-22 22:30:21 +02:00
}
2016-08-24 11:17:42 +02:00
// Private: Visible for testing.
2017-01-13 23:42:39 +01:00
func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
2016-09-20 11:53:05 +02:00
if !destination.Address.Family().IsDomain() {
2016-05-22 22:30:21 +02:00
return destination
}
2016-11-27 21:39:09 +01:00
ips := v.dns.Get(destination.Address.Domain())
2016-05-22 22:30:21 +02:00
if len(ips) == 0 {
log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
return destination
}
ip := ips[dice.Roll(len(ips))]
2017-01-13 23:42:39 +01:00
var newDest net.Destination
if destination.Network == net.Network_TCP {
newDest = net.TCPDestination(net.IPAddress(ip), destination.Port)
2016-05-22 22:30:21 +02:00
} else {
2017-01-13 23:42:39 +01:00
newDest = net.UDPDestination(net.IPAddress(ip), destination.Port)
2016-05-22 22:30:21 +02:00
}
log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
return newDest
2015-09-09 17:39:06 +02:00
}
2017-01-13 23:42:39 +01:00
func (v *Handler) Dispatch(destination net.Destination, ray ray.OutboundRay) {
2016-04-26 00:13:26 +02:00
log.Info("Freedom: Opening connection to ", destination)
2015-12-17 01:19:04 +01:00
2016-12-30 00:51:39 +01:00
input := ray.OutboundInput()
output := ray.OutboundOutput()
2016-04-18 18:44:10 +02:00
2016-06-14 22:54:08 +02:00
var conn internet.Connection
2016-11-27 21:39:09 +01:00
if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
destination = v.ResolveIP(destination)
2016-05-22 22:30:21 +02:00
}
2016-11-20 21:47:51 +01:00
err := retry.ExponentialBackoff(5, 100).On(func() error {
2016-11-27 21:39:09 +01:00
rawConn, err := internet.Dial(v.meta.Address, destination, v.meta.GetDialerOptions())
2015-12-17 01:19:04 +01:00
if err != nil {
return err
}
conn = rawConn
return nil
})
2015-09-09 17:39:25 +02:00
if err != nil {
2016-06-03 20:21:46 +02:00
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
return
}
2015-12-15 16:38:25 +01:00
defer conn.Close()
2017-01-03 14:53:59 +01:00
conn.SetReusable(false)
2016-12-30 00:32:20 +01:00
requestDone := signal.ExecuteAsync(func() error {
2016-12-09 13:17:34 +01:00
v2writer := buf.NewWriter(conn)
if err := buf.PipeUntilEOF(input, v2writer); err != nil {
2016-12-30 00:32:20 +01:00
return err
2016-11-22 00:17:49 +01:00
}
2016-12-30 00:32:20 +01:00
return nil
})
2015-09-22 18:31:06 +02:00
var reader io.Reader = conn
2016-11-27 21:39:09 +01:00
timeout := v.timeout
2017-01-13 23:42:39 +01:00
if destination.Network == net.Network_UDP {
timeout = 16
2015-12-15 16:38:25 +01:00
}
if timeout > 0 {
2017-01-13 23:42:39 +01:00
reader = net.NewTimeOutReader(timeout /* seconds */, conn)
}
2016-12-30 00:32:20 +01:00
responseDone := signal.ExecuteAsync(func() error {
defer output.Close()
v2reader := buf.NewReader(reader)
if err := buf.PipeUntilEOF(v2reader, output); err != nil {
return err
}
return nil
})
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("Freedom: Connection ending with ", err)
2017-01-10 14:22:42 +01:00
input.CloseError()
output.CloseError()
2016-11-22 00:17:49 +01:00
}
2015-09-09 17:39:06 +02:00
}
2016-05-22 22:30:21 +02:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*Config))
}))
2016-05-22 22:30:21 +02:00
}