v2fly/app/dispatcher/default.go

255 lines
6.3 KiB
Go
Raw Normal View History

package dispatcher
2016-05-18 06:05:52 +00:00
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2017-04-08 23:43:25 +00:00
2016-05-18 06:05:52 +00:00
import (
2017-01-13 12:41:40 +00:00
"context"
2018-07-16 11:47:00 +00:00
"strings"
2018-07-21 10:11:49 +00:00
"sync"
2017-04-24 22:49:27 +00:00
"time"
2017-01-04 11:34:01 +00:00
"v2ray.com/core"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app/proxyman"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
2017-04-25 08:48:06 +00:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2018-03-30 21:48:45 +00:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/session"
2018-04-16 22:31:10 +00:00
"v2ray.com/core/common/stats"
"v2ray.com/core/common/vio"
"v2ray.com/core/features/outbound"
2018-10-11 20:34:31 +00:00
"v2ray.com/core/features/policy"
"v2ray.com/core/features/routing"
feature_stats "v2ray.com/core/features/stats"
2018-04-16 22:31:10 +00:00
"v2ray.com/core/transport/pipe"
2016-05-18 06:05:52 +00:00
)
2017-04-24 22:49:27 +00:00
var (
errSniffingTimeout = newError("timeout on sniffing")
)
2018-04-16 22:31:10 +00:00
type cachedReader struct {
2018-07-21 10:11:49 +00:00
sync.Mutex
2018-04-16 22:31:10 +00:00
reader *pipe.Reader
cache buf.MultiBuffer
}
func (r *cachedReader) Cache(b *buf.Buffer) {
mb, _ := r.reader.ReadMultiBufferTimeout(time.Millisecond * 100)
2018-07-21 10:11:49 +00:00
r.Lock()
2018-04-16 22:31:10 +00:00
if !mb.IsEmpty() {
2018-04-19 19:33:18 +00:00
common.Must(r.cache.WriteMultiBuffer(mb))
2018-04-16 22:31:10 +00:00
}
common.Must(b.Reset(func(x []byte) (int, error) {
return r.cache.Copy(x), nil
}))
2018-07-21 10:11:49 +00:00
r.Unlock()
2018-04-16 22:31:10 +00:00
}
func (r *cachedReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
2018-07-21 10:11:49 +00:00
r.Lock()
defer r.Unlock()
2018-07-23 21:39:55 +00:00
if r.cache != nil && !r.cache.IsEmpty() {
2018-04-16 22:31:10 +00:00
mb := r.cache
r.cache = nil
return mb, nil
}
return r.reader.ReadMultiBuffer()
}
func (r *cachedReader) ReadMultiBufferTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
2018-07-21 10:11:49 +00:00
r.Lock()
defer r.Unlock()
2018-07-23 21:39:55 +00:00
if r.cache != nil && !r.cache.IsEmpty() {
mb := r.cache
r.cache = nil
return mb, nil
}
return r.reader.ReadMultiBufferTimeout(timeout)
}
2018-04-16 22:31:10 +00:00
func (r *cachedReader) CloseError() {
2018-07-21 10:11:49 +00:00
r.Lock()
2018-07-23 21:39:55 +00:00
if r.cache != nil {
r.cache.Release()
r.cache = nil
}
2018-07-21 10:11:49 +00:00
r.Unlock()
2018-04-16 22:31:10 +00:00
r.reader.CloseError()
}
2017-04-25 08:48:06 +00:00
// DefaultDispatcher is a default implementation of Dispatcher.
2016-05-18 06:05:52 +00:00
type DefaultDispatcher struct {
ohm outbound.HandlerManager
router routing.Router
2018-10-11 20:34:31 +00:00
policy policy.Manager
stats feature_stats.Manager
2016-05-18 06:05:52 +00:00
}
2017-04-25 08:48:06 +00:00
// NewDefaultDispatcher create a new DefaultDispatcher.
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
2018-02-21 16:05:29 +00:00
v := core.MustFromContext(ctx)
d := &DefaultDispatcher{
ohm: v.OutboundHandlerManager(),
router: v.Router(),
2018-03-30 21:48:45 +00:00
policy: v.PolicyManager(),
stats: v.Stats(),
}
if err := v.RegisterFeature((*routing.Dispatcher)(nil), d); err != nil {
2018-04-11 18:17:08 +00:00
return nil, newError("unable to register Dispatcher").Base(err)
2017-01-13 12:41:40 +00:00
}
return d, nil
}
2018-04-03 09:11:54 +00:00
// Start implements common.Runnable.
2017-04-25 08:48:06 +00:00
func (*DefaultDispatcher) Start() error {
2017-02-01 20:35:40 +00:00
return nil
}
2018-04-03 09:11:54 +00:00
// Close implements common.Closable.
2018-02-08 14:39:46 +00:00
func (*DefaultDispatcher) Close() error { return nil }
2017-02-01 20:35:40 +00:00
func (d *DefaultDispatcher) getLink(ctx context.Context) (*vio.Link, *vio.Link) {
2018-05-25 10:08:28 +00:00
opt := pipe.OptionsFromContext(ctx)
uplinkReader, uplinkWriter := pipe.New(opt...)
downlinkReader, downlinkWriter := pipe.New(opt...)
2018-03-30 21:48:45 +00:00
inboundLink := &vio.Link{
2018-04-16 22:31:10 +00:00
Reader: downlinkReader,
Writer: uplinkWriter,
}
outboundLink := &vio.Link{
2018-04-16 22:31:10 +00:00
Reader: uplinkReader,
Writer: downlinkWriter,
}
user := protocol.UserFromContext(ctx)
2018-03-30 21:48:45 +00:00
if user != nil && len(user.Email) > 0 {
2018-03-31 18:30:49 +00:00
p := d.policy.ForLevel(user.Level)
if p.Stats.UserUplink {
name := "user>>>" + user.Email + ">>>traffic>>>uplink"
if c, _ := feature_stats.GetOrRegisterCounter(d.stats, name); c != nil {
2018-04-16 22:31:10 +00:00
inboundLink.Writer = &stats.SizeStatWriter{
Counter: c,
Writer: inboundLink.Writer,
}
2018-03-31 18:30:49 +00:00
}
2018-03-30 21:48:45 +00:00
}
2018-03-31 18:30:49 +00:00
if p.Stats.UserDownlink {
name := "user>>>" + user.Email + ">>>traffic>>>downlink"
if c, _ := feature_stats.GetOrRegisterCounter(d.stats, name); c != nil {
2018-04-16 22:31:10 +00:00
outboundLink.Writer = &stats.SizeStatWriter{
Counter: c,
Writer: outboundLink.Writer,
}
2018-03-31 18:30:49 +00:00
}
2018-03-30 21:48:45 +00:00
}
2018-03-31 18:30:49 +00:00
}
2018-03-30 21:48:45 +00:00
2018-04-16 22:31:10 +00:00
return inboundLink, outboundLink
2018-03-31 18:30:49 +00:00
}
2018-07-16 11:47:00 +00:00
func shouldOverride(result SniffResult, domainOverride []string) bool {
for _, p := range domainOverride {
if strings.HasPrefix(result.Protocol(), p) {
return true
}
}
return false
}
// Dispatch implements routing.Dispatcher.
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (*vio.Link, error) {
2018-03-31 18:30:49 +00:00
if !destination.IsValid() {
panic("Dispatcher: Invalid destination.")
2018-03-30 21:48:45 +00:00
}
ob := &session.Outbound{
Target: destination,
}
ctx = session.ContextWithOutbound(ctx, ob)
2018-03-31 18:30:49 +00:00
2018-04-16 22:31:10 +00:00
inbound, outbound := d.getLink(ctx)
2018-07-16 11:47:00 +00:00
sniffingConfig := proxyman.SniffingConfigFromContext(ctx)
if destination.Network != net.Network_TCP || sniffingConfig == nil || !sniffingConfig.Enabled {
2017-04-24 22:49:27 +00:00
go d.routedDispatch(ctx, outbound, destination)
} else {
go func() {
2018-04-16 22:31:10 +00:00
cReader := &cachedReader{
reader: outbound.Reader.(*pipe.Reader),
}
outbound.Reader = cReader
2018-07-16 11:47:00 +00:00
result, err := sniffer(ctx, cReader)
2017-04-25 22:43:59 +00:00
if err == nil {
2018-07-16 11:47:00 +00:00
ctx = ContextWithSniffingResult(ctx, result)
}
2018-07-20 11:09:42 +00:00
if err == nil && shouldOverride(result, sniffingConfig.DestinationOverride) {
2018-07-16 11:47:00 +00:00
domain := result.Domain()
newError("sniffed domain: ", domain).WriteToLog(session.ExportIDToError(ctx))
2017-04-25 22:43:59 +00:00
destination.Address = net.ParseAddress(domain)
ob.Target = destination
2017-04-24 22:49:27 +00:00
}
d.routedDispatch(ctx, outbound, destination)
}()
}
2018-04-16 22:31:10 +00:00
return inbound, nil
2017-04-24 22:49:27 +00:00
}
2018-07-16 11:47:00 +00:00
func sniffer(ctx context.Context, cReader *cachedReader) (SniffResult, error) {
2017-04-25 08:48:06 +00:00
payload := buf.New()
defer payload.Release()
2018-07-16 11:47:00 +00:00
sniffer := NewSniffer()
2017-04-24 22:49:27 +00:00
totalAttempt := 0
for {
select {
case <-ctx.Done():
2018-07-16 11:47:00 +00:00
return nil, ctx.Err()
2017-04-26 19:24:58 +00:00
default:
2017-04-24 22:49:27 +00:00
totalAttempt++
2018-08-28 17:52:31 +00:00
if totalAttempt > 2 {
2018-07-16 11:47:00 +00:00
return nil, errSniffingTimeout
2017-04-24 22:49:27 +00:00
}
2018-04-16 22:31:10 +00:00
cReader.Cache(payload)
2017-04-26 19:24:58 +00:00
if !payload.IsEmpty() {
2018-07-16 11:47:00 +00:00
result, err := sniffer.Sniff(payload.Bytes())
if err != common.ErrNoClue {
2018-07-16 11:47:00 +00:00
return result, err
2017-04-24 22:49:27 +00:00
}
}
2017-04-25 08:48:06 +00:00
if payload.IsFull() {
2018-07-16 11:47:00 +00:00
return nil, errUnknownContent
2017-04-24 23:40:34 +00:00
}
2017-04-24 22:49:27 +00:00
}
}
}
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *vio.Link, destination net.Destination) {
2017-04-24 22:49:27 +00:00
dispatcher := d.ohm.GetDefaultHandler()
2017-04-23 17:16:56 +00:00
if d.router != nil {
if tag, err := d.router.PickRoute(ctx); err == nil {
2017-04-23 17:16:56 +00:00
if handler := d.ohm.GetHandler(tag); handler != nil {
newError("taking detour [", tag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx))
2016-05-18 06:05:52 +00:00
dispatcher = handler
} else {
newError("non existing tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
2016-05-18 06:05:52 +00:00
}
} else {
newError("default route for ", destination).WriteToLog(session.ExportIDToError(ctx))
2016-05-18 06:05:52 +00:00
}
}
2018-04-16 22:31:10 +00:00
dispatcher.Dispatch(ctx, link)
2016-05-18 06:05:52 +00:00
}
2016-12-16 14:39:47 +00:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewDefaultDispatcher(ctx, config.(*Config))
2017-01-13 12:41:40 +00:00
}))
2016-12-16 14:39:47 +00:00
}