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

84 lines
2.4 KiB
Go
Raw Normal View History

2016-05-18 06:05:52 +00:00
package impl
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/app/router"
2016-08-12 21:37:21 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-05-18 06:05:52 +00:00
"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 15:12:04 +00: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 06:05:52 +00:00
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
log.Error("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
2016-06-27 06:53:35 +00:00
return app.ErrMissingApplication
2016-05-18 06:05:52 +00:00
}
2016-05-18 15:12:04 +00:00
this.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
2016-05-18 06:05:52 +00:00
if space.HasApp(router.APP_ID) {
2016-05-18 15:12:04 +00:00
this.router = space.GetApp(router.APP_ID).(router.Router)
2016-05-18 06:05:52 +00:00
}
2016-05-18 15:12:04 +00:00
return nil
}
func (this *DefaultDispatcher) Release() {
2016-05-18 06:05:52 +00:00
}
2016-08-14 15:08:01 +00:00
func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay {
2016-05-18 06:05:52 +00:00
direct := ray.NewRay()
dispatcher := this.ohm.GetDefaultHandler()
2016-08-14 15:08:01 +00:00
destination := session.Destination
2016-05-18 06:05:52 +00:00
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)
}
}
2016-08-12 21:37:21 +00:00
if meta.AllowPassiveConnection {
go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct)
} else {
go this.FilterPacketAndDispatch(destination, direct, dispatcher)
}
2016-05-18 06:05:52 +00:00
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 22:57:08 +00:00
log.Info("DefaultDispatcher: No payload towards ", destination, ", stopping now.")
2016-05-18 06:05:52 +00:00
link.OutboundInput().Release()
link.OutboundOutput().Release()
return
}
dispatcher.Dispatch(destination, payload, link)
}