1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 18:36:11 -05:00
v2fly/app/dispatcher/default.go

169 lines
4.5 KiB
Go
Raw Normal View History

package dispatcher
2016-05-18 02:05:52 -04:00
2017-12-02 19:04:57 -05:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg impl -path App,Dispatcher,Default
2017-04-08 19:43:25 -04:00
2016-05-18 02:05:52 -04:00
import (
2017-01-13 07:41:40 -05:00
"context"
2017-04-24 18:49:27 -04:00
"time"
2017-01-04 06:34:01 -05:00
"v2ray.com/core"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app/proxyman"
2017-01-06 09:32:36 -05:00
"v2ray.com/core/common"
2017-04-25 04:48:06 -04:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2018-03-30 17:48:45 -04:00
"v2ray.com/core/common/protocol"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-05-18 02:05:52 -04:00
)
2017-04-24 18:49:27 -04:00
var (
errSniffingTimeout = newError("timeout on sniffing")
)
2017-04-25 04:48:06 -04:00
// DefaultDispatcher is a default implementation of Dispatcher.
2016-05-18 02:05:52 -04:00
type DefaultDispatcher struct {
ohm core.OutboundHandlerManager
router core.Router
2018-03-30 17:48:45 -04:00
policy core.PolicyManager
stats core.StatManager
2016-05-18 02:05:52 -04:00
}
2017-04-25 04:48:06 -04:00
// NewDefaultDispatcher create a new DefaultDispatcher.
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
2018-02-21 11:05:29 -05:00
v := core.MustFromContext(ctx)
d := &DefaultDispatcher{
ohm: v.OutboundHandlerManager(),
router: v.Router(),
2018-03-30 17:48:45 -04:00
policy: v.PolicyManager(),
stats: v.Stats(),
}
if err := v.RegisterFeature((*core.Dispatcher)(nil), d); err != nil {
return nil, newError("unable to register Dispatcher")
2017-01-13 07:41:40 -05:00
}
return d, nil
}
2018-04-03 05:11:54 -04:00
// Start implements common.Runnable.
2017-04-25 04:48:06 -04:00
func (*DefaultDispatcher) Start() error {
2017-02-01 15:35:40 -05:00
return nil
}
2018-04-03 05:11:54 -04:00
// Close implements common.Closable.
2018-02-08 09:39:46 -05:00
func (*DefaultDispatcher) Close() error { return nil }
2017-02-01 15:35:40 -05:00
2018-03-31 14:30:49 -04:00
func (d *DefaultDispatcher) getStatCounter(name string) core.StatCounter {
c := d.stats.GetCounter(name)
if c != nil {
return c
}
2018-03-31 14:30:49 -04:00
c, err := d.stats.RegisterCounter(name)
if err != nil {
return nil
}
return c
}
2018-03-31 14:30:49 -04:00
func (d *DefaultDispatcher) getRayOption(user *protocol.User) []ray.Option {
2018-03-30 17:48:45 -04:00
var rayOptions []ray.Option
if user != nil && len(user.Email) > 0 {
2018-03-31 14:30:49 -04:00
p := d.policy.ForLevel(user.Level)
if p.Stats.UserUplink {
name := "user>>>" + user.Email + ">>>traffic>>>uplink"
if c := d.getStatCounter(name); c != nil {
rayOptions = append(rayOptions, ray.WithUplinkStatCounter(c))
}
2018-03-30 17:48:45 -04:00
}
2018-03-31 14:30:49 -04:00
if p.Stats.UserDownlink {
name := "user>>>" + user.Email + ">>>traffic>>>downlink"
if c := d.getStatCounter(name); c != nil {
rayOptions = append(rayOptions, ray.WithDownlinkStatCounter(c))
}
2018-03-30 17:48:45 -04:00
}
2018-03-31 14:30:49 -04:00
}
2018-03-30 17:48:45 -04:00
2018-03-31 14:30:49 -04:00
return rayOptions
}
// Dispatch implements core.Dispatcher.
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
if !destination.IsValid() {
panic("Dispatcher: Invalid destination.")
2018-03-30 17:48:45 -04:00
}
2018-03-31 14:30:49 -04:00
ctx = proxy.ContextWithTarget(ctx, destination)
user := protocol.UserFromContext(ctx)
rayOptions := d.getRayOption(user)
2018-03-30 17:48:45 -04:00
outbound := ray.New(ctx, rayOptions...)
2018-03-14 22:32:10 -04:00
snifferList := proxyman.ProtocolSniffersFromContext(ctx)
2018-02-25 20:06:27 -05:00
if destination.Address.Family().IsDomain() || len(snifferList) == 0 {
2017-04-24 18:49:27 -04:00
go d.routedDispatch(ctx, outbound, destination)
} else {
go func() {
2018-02-25 20:06:27 -05:00
domain, err := sniffer(ctx, snifferList, outbound)
2017-04-25 18:43:59 -04:00
if err == nil {
2018-02-22 09:26:00 -05:00
newError("sniffed domain: ", domain).WithContext(ctx).WriteToLog()
2017-04-25 18:43:59 -04:00
destination.Address = net.ParseAddress(domain)
ctx = proxy.ContextWithTarget(ctx, destination)
2017-04-24 18:49:27 -04:00
}
d.routedDispatch(ctx, outbound, destination)
}()
}
return outbound, nil
}
2018-02-25 20:06:27 -05:00
func sniffer(ctx context.Context, snifferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
2017-04-25 04:48:06 -04:00
payload := buf.New()
defer payload.Release()
2018-02-25 20:06:27 -05:00
sniffer := NewSniffer(snifferList)
2017-04-24 18:49:27 -04:00
totalAttempt := 0
for {
select {
case <-ctx.Done():
2017-04-25 04:48:06 -04:00
return "", ctx.Err()
2017-04-26 15:24:58 -04:00
default:
2017-04-24 18:49:27 -04:00
totalAttempt++
if totalAttempt > 5 {
2017-04-25 04:48:06 -04:00
return "", errSniffingTimeout
2017-04-24 18:49:27 -04:00
}
2017-04-25 04:48:06 -04:00
outbound.OutboundInput().Peek(payload)
2017-04-26 15:24:58 -04:00
if !payload.IsEmpty() {
2017-06-05 17:27:50 -04:00
domain, err := sniffer.Sniff(payload.Bytes())
2017-04-24 18:49:27 -04:00
if err != ErrMoreData {
2017-04-25 04:48:06 -04:00
return domain, err
2017-04-24 18:49:27 -04:00
}
}
2017-04-25 04:48:06 -04:00
if payload.IsFull() {
return "", ErrInvalidData
2017-04-24 19:40:34 -04:00
}
2017-04-26 15:24:58 -04:00
time.Sleep(time.Millisecond * 100)
2017-04-24 18:49:27 -04:00
}
}
}
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
dispatcher := d.ohm.GetDefaultHandler()
2017-04-23 13:16:56 -04:00
if d.router != nil {
if tag, err := d.router.PickRoute(ctx); err == nil {
2017-04-23 13:16:56 -04:00
if handler := d.ohm.GetHandler(tag); handler != nil {
2018-02-22 09:26:00 -05:00
newError("taking detour [", tag, "] for [", destination, "]").WithContext(ctx).WriteToLog()
2016-05-18 02:05:52 -04:00
dispatcher = handler
} else {
2018-03-14 22:32:10 -04:00
newError("non existing tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
2016-05-18 02:05:52 -04:00
}
} else {
2018-02-22 09:26:00 -05:00
newError("default route for ", destination).WithContext(ctx).WriteToLog()
2016-05-18 02:05:52 -04:00
}
}
2017-04-24 18:49:27 -04:00
dispatcher.Dispatch(ctx, outbound)
2016-05-18 02:05:52 -04:00
}
2016-12-16 09:39:47 -05: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 07:41:40 -05:00
}))
2016-12-16 09:39:47 -05:00
}