1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00
v2fly/proxy/freedom/freedom.go

140 lines
3.5 KiB
Go
Raw Normal View History

2015-09-11 12:12:09 +00:00
package freedom
2015-09-09 15:39:06 +00:00
import (
2016-11-22 07:21:10 +00:00
"io"
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"
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"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
2016-12-15 10:51:09 +00:00
"v2ray.com/core/common/serial"
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
2016-06-04 12:25:13 +00:00
meta *proxy.OutboundHandlerMeta
2016-05-22 20:30:21 +00:00
}
2016-12-22 12:33:28 +00:00
func New(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *Handler {
f := &Handler{
2016-05-22 20:30:21 +00:00
domainStrategy: config.DomainStrategy,
2016-06-02 21:18:44 +00:00
timeout: config.Timeout,
2016-06-04 12:25:13 +00:00
meta: meta,
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 {
2016-11-21 20:13:01 +00:00
return errors.New("Freedom: DNS server is not found in the space.")
2016-05-22 20:30:21 +00:00
}
}
return nil
})
return f
}
2016-08-24 09:17:42 +00:00
// Private: Visible for testing.
2016-12-22 12:33:28 +00:00
func (v *Handler) ResolveIP(destination v2net.Destination) v2net.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 {
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 09:53:05 +00:00
if destination.Network == v2net.Network_TCP {
newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port)
2016-05-22 20:30:21 +00:00
} else {
2016-09-20 09:53:05 +00:00
newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port)
2016-05-22 20:30:21 +00:00
}
log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
return newDest
2015-09-09 15:39:06 +00:00
}
2017-01-04 11:34:01 +00:00
func (v *Handler) Dispatch(destination v2net.Destination, ray ray.OutboundRay) {
2016-04-25 22:13:26 +00:00
log.Info("Freedom: Opening connection to ", destination)
2015-12-17 00:19:04 +00:00
2016-12-29 23:51:39 +00:00
input := ray.OutboundInput()
output := ray.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 {
2016-11-27 20:39:09 +00:00
rawConn, err := internet.Dial(v.meta.Address, destination, v.meta.GetDialerOptions())
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 {
2016-06-03 18:21:46 +00:00
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
return
}
2015-12-15 15:38:25 +00:00
defer conn.Close()
2017-01-03 13:53:59 +00:00
conn.SetReusable(false)
2016-12-29 23:32:20 +00:00
requestDone := signal.ExecuteAsync(func() error {
2016-12-09 12:17:34 +00:00
v2writer := buf.NewWriter(conn)
if err := buf.PipeUntilEOF(input, v2writer); err != nil {
2016-12-29 23:32:20 +00:00
return 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
var reader io.Reader = conn
2016-11-27 20:39:09 +00:00
timeout := v.timeout
2016-09-20 09:53:05 +00:00
if destination.Network == v2net.Network_UDP {
timeout = 16
2015-12-15 15:38:25 +00:00
}
if timeout > 0 {
2016-08-25 19:55:49 +00:00
reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
}
2016-12-29 23:32:20 +00: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 13:22:42 +00:00
input.CloseError()
output.CloseError()
2016-11-21 23:17:49 +00:00
}
2015-09-09 15:39:06 +00:00
}
2016-05-22 20:30:21 +00:00
2016-12-22 12:33:28 +00:00
type Factory struct{}
2016-06-14 20:54:08 +00:00
2016-12-22 12:33:28 +00:00
func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return New(config.(*Config), space, meta), nil
2016-06-14 20:54:08 +00:00
}
2016-05-22 20:30:21 +00:00
func init() {
2016-12-27 23:53:29 +00:00
common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
2016-05-22 20:30:21 +00:00
}