2015-09-11 08:12:09 -04:00
|
|
|
package freedom
|
2015-09-09 11:39:06 -04:00
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg freedom -path Proxy,Freedom
|
|
|
|
|
2015-09-09 11:39:06 -04:00
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2017-04-08 19:43:25 -04:00
|
|
|
"time"
|
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"
|
2017-02-03 16:35:09 -05:00
|
|
|
"v2ray.com/core/app/log"
|
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"
|
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
|
|
|
)
|
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
// Handler handles Freedom connections.
|
2016-12-22 07:33:28 -05:00
|
|
|
type Handler struct {
|
2017-09-27 09:10:25 -04:00
|
|
|
domainStrategy Config_DomainStrategy
|
|
|
|
timeout uint32
|
|
|
|
dns dns.Server
|
|
|
|
destOverride *DestinationOverride
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
// New creates a new Freedom handler.
|
2017-01-12 18:56:21 -05:00
|
|
|
func New(ctx context.Context, config *Config) (*Handler, error) {
|
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("no space in context")
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
2016-12-22 07:33:28 -05:00
|
|
|
f := &Handler{
|
2017-09-27 09:10:25 -04:00
|
|
|
domainStrategy: config.DomainStrategy,
|
|
|
|
timeout: config.Timeout,
|
|
|
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("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
|
|
|
}
|
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
func (h *Handler) resolveIP(ctx context.Context, domain string) net.Address {
|
2017-11-15 06:55:54 -05:00
|
|
|
if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
|
|
|
|
ips := resolver.Resolve()
|
|
|
|
if len(ips) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ips[dice.Roll(len(ips))]
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
ips := h.dns.Get(domain)
|
2016-05-22 16:30:21 -04:00
|
|
|
if len(ips) == 0 {
|
2017-11-15 06:55:54 -05:00
|
|
|
return nil
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
2017-11-15 06:55:54 -05:00
|
|
|
return net.IPAddress(ips[dice.Roll(len(ips))])
|
2015-09-09 11:39:06 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
// Process implements proxy.Outbound.
|
|
|
|
func (h *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
2017-02-09 16:49:38 -05:00
|
|
|
destination, _ := proxy.TargetFromContext(ctx)
|
2017-11-15 12:28:39 -05:00
|
|
|
if h.destOverride != nil {
|
|
|
|
server := h.destOverride.Server
|
2017-01-26 17:05:24 -05:00
|
|
|
destination = net.Destination{
|
|
|
|
Network: destination.Network,
|
|
|
|
Address: server.Address.AsAddress(),
|
|
|
|
Port: net.Port(server.Port),
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("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
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
if h.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
|
|
|
|
ip := h.resolveIP(ctx, destination.Address.Domain())
|
2017-11-15 06:55:54 -05:00
|
|
|
if ip != nil {
|
|
|
|
destination = net.Destination{
|
|
|
|
Network: destination.Network,
|
|
|
|
Address: ip,
|
|
|
|
Port: destination.Port,
|
|
|
|
}
|
|
|
|
log.Trace(newError("changing destination to ", destination))
|
|
|
|
}
|
2016-05-22 16:30:21 -04:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-11-15 12:28:39 -05:00
|
|
|
var conn internet.Connection
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to open connection to ", destination).Base(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-11-15 12:28:39 -05:00
|
|
|
timeout := time.Second * time.Duration(h.timeout)
|
2017-01-31 06:42:05 -05:00
|
|
|
if timeout == 0 {
|
2017-03-31 11:45:55 -04:00
|
|
|
timeout = time.Minute * 5
|
2017-01-31 06:42:05 -05:00
|
|
|
}
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
|
2017-01-31 06:42:05 -05:00
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-04-16 03:57:28 -04:00
|
|
|
var writer buf.Writer
|
|
|
|
if destination.Network == net.Network_TCP {
|
|
|
|
writer = buf.NewWriter(conn)
|
|
|
|
} else {
|
2017-04-21 08:51:09 -04:00
|
|
|
writer = buf.NewSequentialWriter(conn)
|
2017-04-16 03:57:28 -04:00
|
|
|
}
|
2017-04-27 16:30:48 -04:00
|
|
|
if err := buf.Copy(input, writer, buf.UpdateActivity(timer)); err != nil {
|
2017-04-16 16:48:51 -04:00
|
|
|
return newError("failed to process request").Base(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-12-29 18:32:20 -05:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-09-27 09:10:25 -04:00
|
|
|
defer output.Close()
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
v2reader := buf.NewReader(conn)
|
2017-04-27 16:30:48 -04:00
|
|
|
if err := buf.Copy(v2reader, output, buf.UpdateActivity(timer)); err != nil {
|
2017-04-16 16:48:51 -04:00
|
|
|
return newError("failed to process response").Base(err)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-10 08:22:42 -05:00
|
|
|
input.CloseError()
|
|
|
|
output.CloseError()
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("connection ends").Base(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
|
|
|
}
|