v2ray-core/proxy/freedom/freedom.go

170 lines
4.1 KiB
Go
Raw Normal View History

2015-09-11 12:12:09 +00:00
package freedom
2015-09-09 15:39:06 +00:00
2017-04-08 23:43:25 +00:00
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg freedom -path Proxy,Freedom
2015-09-09 15:39:06 +00:00
import (
"context"
2017-04-16 07:57:28 +00:00
"io"
2017-01-31 11:42:05 +00:00
"runtime"
2017-04-08 23:43:25 +00:00
"time"
2016-12-09 12:17:34 +00:00
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/log"
2016-12-27 23:53:29 +00:00
"v2ray.com/core/common"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/dice"
2017-01-13 22:42:39 +00:00
"v2ray.com/core/common/net"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/retry"
2016-12-29 23:32:20 +00:00
"v2ray.com/core/common/signal"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-09 15:39:06 +00:00
)
2016-12-22 12:33:28 +00:00
type Handler struct {
2016-08-25 19:55:49 +00:00
domainStrategy Config_DomainStrategy
2016-06-02 21:18:44 +00:00
timeout uint32
2016-05-22 20:30:21 +00:00
dns dns.Server
destOverride *DestinationOverride
2016-05-22 20:30:21 +00:00
}
func New(ctx context.Context, config *Config) (*Handler, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-08 23:43:25 +00:00
return nil, newError("no space in context")
}
2016-12-22 12:33:28 +00:00
f := &Handler{
2016-05-22 20:30:21 +00:00
domainStrategy: config.DomainStrategy,
2016-06-02 21:18:44 +00:00
timeout: config.Timeout,
destOverride: config.DestinationOverride,
2016-05-22 20:30:21 +00:00
}
2017-01-06 14:32:36 +00:00
space.OnInitialize(func() error {
2016-08-25 19:55:49 +00:00
if config.DomainStrategy == Config_USE_IP {
2017-01-06 14:32:36 +00:00
f.dns = dns.FromSpace(space)
if f.dns == nil {
2017-04-08 23:43:25 +00:00
return newError("DNS server is not found in the space")
2016-05-22 20:30:21 +00:00
}
}
return nil
})
return f, nil
2016-05-22 20:30:21 +00:00
}
2017-01-13 22:42:39 +00:00
func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
2016-09-20 09:53:05 +00:00
if !destination.Address.Family().IsDomain() {
2016-05-22 20:30:21 +00:00
return destination
}
2016-11-27 20:39:09 +00:00
ips := v.dns.Get(destination.Address.Domain())
2016-05-22 20:30:21 +00:00
if len(ips) == 0 {
2017-04-08 23:43:25 +00:00
log.Trace(newError("DNS returns nil answer. Keep domain as is."))
2016-05-22 20:30:21 +00:00
return destination
}
ip := ips[dice.Roll(len(ips))]
2017-01-13 22:42:39 +00:00
var newDest net.Destination
if destination.Network == net.Network_TCP {
newDest = net.TCPDestination(net.IPAddress(ip), destination.Port)
2016-05-22 20:30:21 +00:00
} else {
2017-01-13 22:42:39 +00:00
newDest = net.UDPDestination(net.IPAddress(ip), destination.Port)
2016-05-22 20:30:21 +00:00
}
2017-04-08 23:43:25 +00:00
log.Trace(newError("changing destination from ", destination, " to ", newDest))
2016-05-22 20:30:21 +00:00
return newDest
2015-09-09 15:39:06 +00:00
}
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
2017-02-09 21:49:38 +00:00
destination, _ := proxy.TargetFromContext(ctx)
if v.destOverride != nil {
server := v.destOverride.Server
destination = net.Destination{
Network: destination.Network,
Address: server.Address.AsAddress(),
Port: net.Port(server.Port),
}
}
2017-04-08 23:43:25 +00:00
log.Trace(newError("opening connection to ", destination))
2015-12-17 00:19:04 +00:00
input := outboundRay.OutboundInput()
output := outboundRay.OutboundOutput()
2016-04-18 16:44:10 +00:00
2016-06-14 20:54:08 +00:00
var conn internet.Connection
2016-11-27 20:39:09 +00:00
if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
destination = v.ResolveIP(destination)
2016-05-22 20:30:21 +00:00
}
2016-11-20 20:47:51 +00:00
err := retry.ExponentialBackoff(5, 100).On(func() error {
rawConn, err := dialer.Dial(ctx, destination)
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 {
2017-04-08 23:43:25 +00:00
return newError("failed to open connection to ", destination).Base(err)
}
2015-12-15 15:38:25 +00:00
defer conn.Close()
2017-01-31 11:42:05 +00:00
timeout := time.Second * time.Duration(v.timeout)
if timeout == 0 {
2017-03-31 15:45:55 +00:00
timeout = time.Minute * 5
2017-01-31 11:42:05 +00:00
}
2017-03-31 19:45:43 +00:00
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
2017-01-31 11:42:05 +00:00
2016-12-29 23:32:20 +00:00
requestDone := signal.ExecuteAsync(func() error {
2017-04-16 07:57:28 +00:00
var writer buf.Writer
if destination.Network == net.Network_TCP {
writer = buf.NewWriter(conn)
} else {
writer = &seqWriter{writer: conn}
}
if err := buf.PipeUntilEOF(timer, input, writer); err != nil {
2017-04-16 20:48:51 +00:00
return newError("failed to process request").Base(err)
2016-11-21 23:17:49 +00:00
}
2016-12-29 23:32:20 +00:00
return nil
})
2015-09-22 16:31:06 +00:00
2016-12-29 23:32:20 +00:00
responseDone := signal.ExecuteAsync(func() error {
defer output.Close()
2017-01-31 11:42:05 +00:00
v2reader := buf.NewReader(conn)
if err := buf.PipeUntilEOF(timer, v2reader, output); err != nil {
2017-04-16 20:48:51 +00:00
return newError("failed to process response").Base(err)
2016-12-29 23:32:20 +00:00
}
return nil
})
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
2017-01-10 13:22:42 +00:00
input.CloseError()
output.CloseError()
2017-04-08 23:43:25 +00:00
return newError("connection ends").Base(err)
2016-11-21 23:17:49 +00:00
}
2017-01-31 11:42:05 +00:00
runtime.KeepAlive(timer)
return nil
2015-09-09 15:39:06 +00:00
}
2016-05-22 20:30:21 +00: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 20:30:21 +00:00
}
2017-04-16 07:57:28 +00:00
type seqWriter struct {
writer io.Writer
}
func (w *seqWriter) Write(mb buf.MultiBuffer) error {
defer mb.Release()
for _, b := range mb {
if _, err := w.writer.Write(b.Bytes()); err != nil {
return err
}
}
return nil
}