1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/app/dispatcher/impl/default.go

133 lines
3.2 KiB
Go
Raw Normal View History

2016-05-18 02:05:52 -04:00
package impl
import (
2017-01-13 07:41:40 -05:00
"context"
2017-01-04 06:34:01 -05:00
"time"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
2016-12-16 09:39:47 -05:00
"v2ray.com/core/app/dispatcher"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
2017-01-06 09:32:36 -05:00
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-05-18 02:05:52 -04:00
)
type DefaultDispatcher struct {
ohm proxyman.OutboundHandlerManager
2016-10-12 10:11:13 -04:00
router *router.Router
2016-05-18 02:05:52 -04:00
}
2017-01-13 07:41:40 -05:00
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("DefaultDispatcher: No space in context.")
}
2016-05-18 11:12:04 -04:00
d := &DefaultDispatcher{}
2017-01-06 09:32:36 -05:00
space.OnInitialize(func() error {
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
if d.ohm == nil {
return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
}
d.router = router.FromSpace(space)
return nil
2016-05-18 11:12:04 -04:00
})
2017-01-13 07:41:40 -05:00
return d, nil
}
func (DefaultDispatcher) Interface() interface{} {
2017-01-13 07:53:44 -05:00
return (*dispatcher.Interface)(nil)
2016-05-18 11:12:04 -04:00
}
func (v *DefaultDispatcher) DispatchToOutbound(ctx context.Context) ray.InboundRay {
2016-11-27 15:39:09 -05:00
dispatcher := v.ohm.GetDefaultHandler()
destination := proxy.DestinationFromContext(ctx)
if !destination.IsValid() {
panic("Dispatcher: Invalid destination.")
}
2016-05-18 02:05:52 -04:00
2016-11-27 15:39:09 -05:00
if v.router != nil {
if tag, err := v.router.TakeDetour(ctx); err == nil {
2016-11-27 15:39:09 -05:00
if handler := v.ohm.GetHandler(tag); handler != nil {
2016-05-18 02:05:52 -04:00
log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
dispatcher = handler
} else {
log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
}
} else {
log.Info("DefaultDispatcher: Default route for ", destination)
}
}
direct := ray.NewRay(ctx)
2017-01-04 06:34:01 -05:00
var waitFunc func() error
if allowPassiveConnection, ok := proxy.AllowPassiveConnectionFromContext(ctx); ok && allowPassiveConnection {
2017-01-04 06:34:01 -05:00
waitFunc = noOpWait()
2016-08-12 17:37:21 -04:00
} else {
2017-01-04 06:34:01 -05:00
wdi := &waitDataInspector{
hasData: make(chan bool, 1),
}
direct.AddInspector(wdi)
waitFunc = waitForData(wdi)
2016-08-12 17:37:21 -04:00
}
2016-05-18 02:05:52 -04:00
go v.waitAndDispatch(ctx, waitFunc, direct, dispatcher)
2017-01-04 06:34:01 -05:00
2016-05-18 02:05:52 -04:00
return direct
}
func (v *DefaultDispatcher) waitAndDispatch(ctx context.Context, wait func() error, link ray.OutboundRay, dispatcher proxyman.OutboundHandler) {
2017-01-04 06:34:01 -05:00
if err := wait(); err != nil {
log.Info("DefaultDispatcher: Failed precondition: ", err)
2017-01-10 08:22:42 -05:00
link.OutboundInput().CloseError()
link.OutboundOutput().CloseError()
2016-05-18 02:05:52 -04:00
return
}
2017-01-04 06:34:01 -05:00
dispatcher.Dispatch(ctx, link)
2016-05-18 02:05:52 -04:00
}
2016-12-16 09:39:47 -05:00
func init() {
2017-01-13 07:41:40 -05: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 09:39:47 -05:00
}
2017-01-04 06:34:01 -05:00
type waitDataInspector struct {
hasData chan bool
}
func (wdi *waitDataInspector) Input(*buf.Buffer) {
select {
case wdi.hasData <- true:
default:
}
}
func (wdi *waitDataInspector) WaitForData() bool {
select {
case <-wdi.hasData:
return true
case <-time.After(time.Minute):
return false
}
}
func waitForData(wdi *waitDataInspector) func() error {
return func() error {
if wdi.WaitForData() {
return nil
}
return errors.New("DefaultDispatcher: No data.")
}
}
func noOpWait() func() error {
return func() error {
return nil
}
}