2016-05-18 06:05:52 +00:00
|
|
|
package impl
|
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg impl -path App,Dispatcher,Default
|
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
import (
|
2017-01-13 12:41:40 +00:00
|
|
|
"context"
|
2017-04-24 22:49:27 +00:00
|
|
|
"time"
|
2017-01-04 11:34:01 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
2016-12-16 14:39:47 +00:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-01 20:35:40 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/app/router"
|
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"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/ray"
|
2016-05-18 06:05:52 +00:00
|
|
|
)
|
|
|
|
|
2017-04-24 22:49:27 +00:00
|
|
|
var (
|
|
|
|
errSniffingTimeout = newError("timeout on sniffing")
|
|
|
|
)
|
|
|
|
|
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 proxyman.OutboundHandlerManager
|
2016-10-12 14:11:13 +00:00
|
|
|
router *router.Router
|
2016-05-18 06:05:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
// NewDefaultDispatcher create a new DefaultDispatcher.
|
2017-01-13 12:41:40 +00:00
|
|
|
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
|
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("no space in context")
|
2017-01-13 12:41:40 +00:00
|
|
|
}
|
2016-05-18 15:12:04 +00:00
|
|
|
d := &DefaultDispatcher{}
|
2017-01-06 14:32:36 +00:00
|
|
|
space.OnInitialize(func() error {
|
|
|
|
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
|
|
|
|
if d.ohm == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("OutboundHandlerManager is not found in the space")
|
2017-01-06 14:32:36 +00:00
|
|
|
}
|
|
|
|
d.router = router.FromSpace(space)
|
|
|
|
return nil
|
2016-05-18 15:12:04 +00:00
|
|
|
})
|
2017-01-13 12:41:40 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
func (*DefaultDispatcher) Start() error {
|
2017-02-01 20:35:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
func (*DefaultDispatcher) Close() {}
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
func (*DefaultDispatcher) Interface() interface{} {
|
2017-01-13 12:53:44 +00:00
|
|
|
return (*dispatcher.Interface)(nil)
|
2016-05-18 15:12:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
// Dispatch implements Dispatcher.Interface.
|
2017-04-23 17:16:56 +00:00
|
|
|
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
|
2017-01-26 19:46:44 +00:00
|
|
|
if !destination.IsValid() {
|
|
|
|
panic("Dispatcher: Invalid destination.")
|
|
|
|
}
|
2017-02-09 21:49:38 +00:00
|
|
|
ctx = proxy.ContextWithTarget(ctx, destination)
|
2017-02-03 21:35:09 +00:00
|
|
|
|
2017-04-24 22:49:27 +00:00
|
|
|
outbound := ray.NewRay(ctx)
|
|
|
|
sniferList := proxyman.ProtocoSniffersFromContext(ctx)
|
2017-04-24 23:40:34 +00:00
|
|
|
if destination.Address.Family().IsDomain() || len(sniferList) == 0 {
|
2017-04-24 22:49:27 +00:00
|
|
|
go d.routedDispatch(ctx, outbound, destination)
|
|
|
|
} else {
|
|
|
|
go func() {
|
2017-04-25 08:48:06 +00:00
|
|
|
domain, err := snifer(ctx, sniferList, outbound)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace(newError("failed to snif").Base(err))
|
2017-04-24 23:40:34 +00:00
|
|
|
return
|
2017-04-24 22:49:27 +00:00
|
|
|
}
|
2017-04-25 08:48:06 +00:00
|
|
|
log.Trace(newError("sniffed domain: ", domain))
|
|
|
|
destination.Address = net.ParseAddress(domain)
|
2017-04-24 22:49:27 +00:00
|
|
|
ctx = proxy.ContextWithTarget(ctx, destination)
|
|
|
|
d.routedDispatch(ctx, outbound, destination)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return outbound, nil
|
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
|
|
|
|
payload := buf.New()
|
|
|
|
defer payload.Release()
|
|
|
|
|
2017-04-24 22:49:27 +00:00
|
|
|
totalAttempt := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-04-25 08:48:06 +00:00
|
|
|
return "", ctx.Err()
|
2017-04-24 22:49:27 +00:00
|
|
|
case <-time.After(time.Millisecond * 100):
|
|
|
|
totalAttempt++
|
|
|
|
if totalAttempt > 5 {
|
2017-04-25 08:48:06 +00:00
|
|
|
return "", errSniffingTimeout
|
2017-04-24 22:49:27 +00:00
|
|
|
}
|
2017-04-25 08:48:06 +00:00
|
|
|
outbound.OutboundInput().Peek(payload)
|
|
|
|
if payload.IsEmpty() {
|
2017-04-24 23:40:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-04-24 22:49:27 +00:00
|
|
|
for _, protocol := range sniferList {
|
|
|
|
var f func([]byte) (string, error)
|
|
|
|
switch protocol {
|
|
|
|
case proxyman.KnownProtocols_HTTP:
|
|
|
|
f = SniffHTTP
|
|
|
|
case proxyman.KnownProtocols_TLS:
|
|
|
|
f = SniffTLS
|
|
|
|
default:
|
|
|
|
panic("Unsupported protocol")
|
|
|
|
}
|
|
|
|
|
2017-04-25 08:48:06 +00:00
|
|
|
domain, err := f(payload.Bytes())
|
2017-04-24 22:49:27 +00:00
|
|
|
if err != ErrMoreData {
|
2017-04-25 08:48:06 +00:00
|
|
|
return domain, err
|
2017-04-24 22:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 08:48:06 +00:00
|
|
|
if payload.IsFull() {
|
|
|
|
return "", ErrInvalidData
|
2017-04-24 23:40:34 +00:00
|
|
|
}
|
2017-04-24 22:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
|
|
|
|
dispatcher := d.ohm.GetDefaultHandler()
|
2017-04-23 17:16:56 +00:00
|
|
|
if d.router != nil {
|
|
|
|
if tag, err := d.router.TakeDetour(ctx); err == nil {
|
|
|
|
if handler := d.ohm.GetHandler(tag); handler != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("taking detour [", tag, "] for [", destination, "]"))
|
2016-05-18 06:05:52 +00:00
|
|
|
dispatcher = handler
|
|
|
|
} else {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("nonexisting tag: ", tag).AtWarning())
|
2016-05-18 06:05:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("default route for ", destination))
|
2016-05-18 06:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 22:49:27 +00:00
|
|
|
dispatcher.Dispatch(ctx, outbound)
|
2016-05-18 06:05:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:39:47 +00:00
|
|
|
func init() {
|
2017-01-13 12:41:40 +00:00
|
|
|
common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
|
|
|
|
}))
|
2016-12-16 14:39:47 +00:00
|
|
|
}
|