2016-05-18 02:05:52 -04:00
|
|
|
package impl
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg impl -path App,Dispatcher,Default
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
import (
|
2017-01-13 07:41:40 -05:00
|
|
|
"context"
|
2017-01-04 06:34:01 -05:00
|
|
|
|
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"
|
2017-02-01 15:35:40 -05:00
|
|
|
"v2ray.com/core/app/log"
|
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"
|
2017-02-03 16:35:09 -05:00
|
|
|
"v2ray.com/core/common/net"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("no space in context")
|
2017-01-13 07:41:40 -05:00
|
|
|
}
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("OutboundHandlerManager is not found in the space")
|
2017-01-06 09:32:36 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-01 15:35:40 -05:00
|
|
|
func (DefaultDispatcher) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (DefaultDispatcher) Close() {}
|
|
|
|
|
2017-01-13 07:41:40 -05:00
|
|
|
func (DefaultDispatcher) Interface() interface{} {
|
2017-01-13 07:53:44 -05:00
|
|
|
return (*dispatcher.Interface)(nil)
|
2016-05-18 11:12:04 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 16:35:09 -05:00
|
|
|
func (v *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
|
2016-11-27 15:39:09 -05:00
|
|
|
dispatcher := v.ohm.GetDefaultHandler()
|
2017-01-26 14:46:44 -05:00
|
|
|
if !destination.IsValid() {
|
|
|
|
panic("Dispatcher: Invalid destination.")
|
|
|
|
}
|
2016-05-18 02:05:52 -04:00
|
|
|
|
2017-02-09 16:49:38 -05:00
|
|
|
ctx = proxy.ContextWithTarget(ctx, destination)
|
2017-02-03 16:35:09 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.router != nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("taking detour [", tag, "] for [", destination, "]"))
|
2016-05-18 02:05:52 -04:00
|
|
|
dispatcher = handler
|
|
|
|
} else {
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("nonexisting tag: ", tag).AtWarning())
|
2016-05-18 02:05:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-08 19:43:25 -04:00
|
|
|
log.Trace(newError("default route for ", destination))
|
2016-05-18 02:05:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
direct := ray.NewRay(ctx)
|
2017-02-07 11:37:26 -05:00
|
|
|
go dispatcher.Dispatch(ctx, direct)
|
2017-01-04 06:34:01 -05:00
|
|
|
|
2017-02-03 16:35:09 -05:00
|
|
|
return direct, nil
|
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
|
|
|
}
|