84 lines
2.3 KiB
Go
Raw Normal View History

2016-05-17 23:05:52 -07:00
package impl
import (
2016-08-20 20:55:45 +02:00
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-05-17 23:05:52 -07:00
)
type DefaultDispatcher struct {
ohm proxyman.OutboundHandlerManager
2016-10-12 16:11:13 +02:00
router *router.Router
2016-05-17 23:05:52 -07:00
}
func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
2016-05-18 08:12:04 -07:00
d := &DefaultDispatcher{}
space.InitializeApplication(func() error {
return d.Initialize(space)
})
return d
}
2016-08-24 11:17:42 +02:00
// Private: Used by app.Space only.
2016-05-18 08:12:04 -07:00
func (this *DefaultDispatcher) Initialize(space app.Space) error {
2016-05-17 23:05:52 -07:00
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
log.Error("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
2016-06-27 08:53:35 +02:00
return app.ErrMissingApplication
2016-05-17 23:05:52 -07:00
}
2016-05-18 08:12:04 -07:00
this.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
2016-05-17 23:05:52 -07:00
if space.HasApp(router.APP_ID) {
2016-10-12 16:11:13 +02:00
this.router = space.GetApp(router.APP_ID).(*router.Router)
2016-05-17 23:05:52 -07:00
}
2016-05-18 08:12:04 -07:00
return nil
}
func (this *DefaultDispatcher) Release() {
2016-05-17 23:05:52 -07:00
}
2016-08-14 17:08:01 +02:00
func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay {
2016-05-17 23:05:52 -07:00
direct := ray.NewRay()
dispatcher := this.ohm.GetDefaultHandler()
2016-08-14 17:08:01 +02:00
destination := session.Destination
2016-05-17 23:05:52 -07:00
if this.router != nil {
2016-10-18 23:01:39 +02:00
if tag, err := this.router.TakeDetour(session); err == nil {
2016-05-17 23:05:52 -07:00
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 23:37:21 +02:00
if meta.AllowPassiveConnection {
go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct)
} else {
go this.FilterPacketAndDispatch(destination, direct, dispatcher)
}
2016-05-17 23:05:52 -07:00
return direct
}
2016-08-24 11:17:42 +02:00
// Private: Visible for testing.
2016-05-17 23:05:52 -07:00
func (this *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
payload, err := link.OutboundInput().Read()
if err != nil {
2016-06-02 00:57:08 +02:00
log.Info("DefaultDispatcher: No payload towards ", destination, ", stopping now.")
2016-05-17 23:05:52 -07:00
link.OutboundInput().Release()
link.OutboundOutput().Release()
return
}
dispatcher.Dispatch(destination, payload, link)
}