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

84 lines
2.3 KiB
Go
Raw Normal View History

2016-05-18 02:05:52 -04:00
package impl
import (
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common/alloc"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"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
}
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
}
2016-08-24 05:17:42 -04:00
// Private: Used by app.Space only.
2016-11-27 15:39:09 -05:00
func (v *DefaultDispatcher) Initialize(space app.Space) error {
2016-05-18 02:05:52 -04:00
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
2016-11-21 15:13:01 -05:00
return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
2016-05-18 02:05:52 -04:00
}
2016-11-27 15:39:09 -05:00
v.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
2016-05-18 11:12:04 -04:00
2016-05-18 02:05:52 -04:00
if space.HasApp(router.APP_ID) {
2016-11-27 15:39:09 -05:00
v.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
}
2016-11-27 15:39:09 -05:00
func (v *DefaultDispatcher) Release() {
2016-05-18 11:12:04 -04:00
2016-05-18 02:05:52 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
2016-05-18 02:05:52 -04:00
direct := ray.NewRay()
2016-11-27 15:39:09 -05:00
dispatcher := v.ohm.GetDefaultHandler()
2016-08-14 11:08:01 -04:00
destination := session.Destination
2016-05-18 02:05:52 -04:00
2016-11-27 15:39:09 -05:00
if v.router != nil {
if tag, err := v.router.TakeDetour(session); err == nil {
if handler := v.ohm.GetHandler(tag); handler != nil {
2016-05-18 02:05:52 -04:00
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-11-13 08:33:00 -05:00
if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
2016-08-12 17:37:21 -04:00
go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct)
} else {
2016-11-27 15:39:09 -05:00
go v.FilterPacketAndDispatch(destination, direct, dispatcher)
2016-08-12 17:37:21 -04:00
}
2016-05-18 02:05:52 -04:00
return direct
}
2016-08-24 05:17:42 -04:00
// Private: Visible for testing.
2016-11-27 15:39:09 -05:00
func (v *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
2016-05-18 02:05:52 -04:00
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)
}