2015-09-11 08:12:09 -04:00
|
|
|
package freedom
|
2015-09-09 11:39:06 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2016-11-22 02:21:10 -05:00
|
|
|
"io"
|
2016-12-09 07:17:34 -05:00
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dns"
|
2016-12-27 18:53:29 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/dice"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/retry"
|
2016-12-29 18:32:20 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/ray"
|
2015-09-09 11:39:06 -04:00
|
|
|
)
|
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
type Handler 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
|
2017-01-26 17:05:24 -05:00
|
|
|
destOverride *DestinationOverride
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:56:21 -05: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.")
|
|
|
|
}
|
2016-12-22 07:33:28 -05:00
|
|
|
f := &Handler{
|
2016-05-22 16:30:21 -04:00
|
|
|
domainStrategy: config.DomainStrategy,
|
2016-06-02 17:18:44 -04:00
|
|
|
timeout: config.Timeout,
|
2017-01-26 17:05:24 -05:00
|
|
|
destOverride: config.DestinationOverride,
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
2017-01-06 09:32:36 -05:00
|
|
|
space.OnInitialize(func() error {
|
2016-08-25 15:55:49 -04:00
|
|
|
if config.DomainStrategy == Config_USE_IP {
|
2017-01-06 09:32:36 -05:00
|
|
|
f.dns = dns.FromSpace(space)
|
|
|
|
if f.dns == nil {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-01-12 18:56:21 -05:00
|
|
|
return f, nil
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
|
2016-08-24 05:17:42 -04:00
|
|
|
// Private: Visible for testing.
|
2017-01-13 17:42:39 -05:00
|
|
|
func (v *Handler) ResolveIP(destination net.Destination) net.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-11-27 15:39:09 -05:00
|
|
|
ips := v.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))]
|
2017-01-13 17:42:39 -05:00
|
|
|
var newDest net.Destination
|
|
|
|
if destination.Network == net.Network_TCP {
|
|
|
|
newDest = net.TCPDestination(net.IPAddress(ip), destination.Port)
|
2016-05-22 16:30:21 -04:00
|
|
|
} else {
|
2017-01-13 17:42:39 -05:00
|
|
|
newDest = net.UDPDestination(net.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
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay) error {
|
|
|
|
destination := proxy.DestinationFromContext(ctx)
|
2017-01-26 17:05:24 -05:00
|
|
|
if v.destOverride != nil {
|
|
|
|
server := v.destOverride.Server
|
|
|
|
destination = net.Destination{
|
|
|
|
Network: destination.Network,
|
|
|
|
Address: server.Address.AsAddress(),
|
|
|
|
Port: net.Port(server.Port),
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 18:13:26 -04:00
|
|
|
log.Info("Freedom: Opening connection to ", destination)
|
2015-12-16 19:19:04 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
input := outboundRay.OutboundInput()
|
|
|
|
output := outboundRay.OutboundOutput()
|
2016-04-18 12:44:10 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
var conn internet.Connection
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
|
|
|
|
destination = v.ResolveIP(destination)
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
dialer := proxy.DialerFromContext(ctx)
|
2016-11-20 15:47:51 -05:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
rawConn, err := dialer.Dial(ctx, destination)
|
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)
|
2017-01-26 14:46:44 -05: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
|
|
|
|
2017-01-03 08:53:59 -05:00
|
|
|
conn.SetReusable(false)
|
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2016-12-09 07:17:34 -05:00
|
|
|
v2writer := buf.NewWriter(conn)
|
|
|
|
if err := buf.PipeUntilEOF(input, v2writer); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
|
|
|
})
|
2015-09-22 12:31:06 -04:00
|
|
|
|
2016-08-15 08:31:46 -04:00
|
|
|
var reader io.Reader = conn
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
timeout := v.timeout
|
2017-01-13 17:42:39 -05:00
|
|
|
if destination.Network == net.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 {
|
2017-01-13 17:42:39 -05:00
|
|
|
reader = net.NewTimeOutReader(timeout /* seconds */, conn)
|
2016-08-15 08:31:46 -04:00
|
|
|
}
|
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
v2reader := buf.NewReader(reader)
|
2017-01-28 16:31:21 -05:00
|
|
|
if err := buf.Pipe(v2reader, output); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
log.Info("Freedom: Connection ending with ", err)
|
2017-01-10 08:22:42 -05:00
|
|
|
input.CloseError()
|
|
|
|
output.CloseError()
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
2016-05-22 16:30:21 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 18:56:21 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*Config))
|
|
|
|
}))
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|