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

82 lines
2.2 KiB
Go
Raw Normal View History

2016-05-18 06:05:52 +00:00
package impl
import (
2017-01-13 12:41:40 +00:00
"context"
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"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
"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
)
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-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-06 19:44:20 +00:00
return nil, errors.New("no space in context").Path("App", "Dispatcher", "Default")
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-06 19:44:20 +00:00
return errors.New("OutboundHandlerManager is not found in the space").Path("App", "Dispatcher", "Default")
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-02-01 20:35:40 +00:00
func (DefaultDispatcher) Start() error {
return nil
}
func (DefaultDispatcher) Close() {}
2017-01-13 12:41:40 +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
}
func (v *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
2016-11-27 20:39:09 +00:00
dispatcher := v.ohm.GetDefaultHandler()
if !destination.IsValid() {
panic("Dispatcher: Invalid destination.")
}
2016-05-18 06:05:52 +00:00
2017-02-09 21:49:38 +00:00
ctx = proxy.ContextWithTarget(ctx, destination)
2016-11-27 20:39:09 +00:00
if v.router != nil {
if tag, err := v.router.TakeDetour(ctx); err == nil {
2016-11-27 20:39:09 +00:00
if handler := v.ohm.GetHandler(tag); handler != nil {
2017-04-06 13:13:09 +00:00
log.Trace(errors.New("taking detour [", tag, "] for [", destination, "]").Path("App", "Dispatcher", "Default"))
2016-05-18 06:05:52 +00:00
dispatcher = handler
} else {
2017-04-06 13:13:09 +00:00
log.Trace(errors.New("nonexisting tag: ", tag).AtWarning().Path("App", "Dispatcher", "Default"))
2016-05-18 06:05:52 +00:00
}
} else {
2017-04-06 13:13:09 +00:00
log.Trace(errors.New("default route for ", destination).Path("App", "Dispatcher", "Default"))
2016-05-18 06:05:52 +00:00
}
}
direct := ray.NewRay(ctx)
go dispatcher.Dispatch(ctx, direct)
2017-01-04 11:34:01 +00:00
return direct, nil
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
}