1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 02:38:10 -05:00
v2fly/app/dispatcher/impl/default.go

78 lines
2.2 KiB
Go
Raw Normal View History

2016-05-18 02:05:52 -04:00
package impl
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
)
type DefaultDispatcher struct {
ohm proxyman.OutboundHandlerManager
router router.Router
}
func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
2016-05-18 11:12:04 -04:00
d := &DefaultDispatcher{}
space.InitializeApplication(func() error {
return d.Initialize(space)
})
return d
}
// @Private
func (this *DefaultDispatcher) Initialize(space app.Space) error {
2016-05-18 02:05:52 -04:00
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
log.Error("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
2016-05-18 11:12:04 -04:00
return app.ErrorMissingApplication
2016-05-18 02:05:52 -04:00
}
2016-05-18 11:12:04 -04:00
this.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
2016-05-18 02:05:52 -04:00
if space.HasApp(router.APP_ID) {
2016-05-18 11:12:04 -04:00
this.router = space.GetApp(router.APP_ID).(router.Router)
2016-05-18 02:05:52 -04:00
}
2016-05-18 11:12:04 -04:00
return nil
}
func (this *DefaultDispatcher) Release() {
2016-05-18 02:05:52 -04:00
}
func (this *DefaultDispatcher) DispatchToOutbound(destination v2net.Destination) ray.InboundRay {
direct := ray.NewRay()
dispatcher := this.ohm.GetDefaultHandler()
if this.router != nil {
if tag, err := this.router.TakeDetour(destination); err == nil {
if handler := this.ohm.GetHandler(tag); handler != nil {
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)
}
}
go this.FilterPacketAndDispatch(destination, direct, dispatcher)
return direct
}
// @Private
func (this *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
payload, err := link.OutboundInput().Read()
if err != nil {
2016-06-01 18:57:08 -04:00
log.Info("DefaultDispatcher: No payload towards ", destination, ", stopping now.")
2016-05-18 02:05:52 -04:00
link.OutboundInput().Release()
link.OutboundOutput().Release()
return
}
dispatcher.Dispatch(destination, payload, link)
}