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

103 lines
2.9 KiB
Go
Raw Normal View History

2016-05-18 06:05:52 +00:00
package impl
import (
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
2016-12-16 14:39:47 +00:00
"v2ray.com/core/app/dispatcher"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2016-12-16 14:39:47 +00:00
"v2ray.com/core/common/serial"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-05-18 06:05:52 +00:00
)
type DefaultDispatcher struct {
ohm proxyman.OutboundHandlerManager
2016-10-12 14:11:13 +00:00
router *router.Router
2016-05-18 06:05:52 +00:00
}
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
}
2017-01-01 22:08:39 +00:00
// Initialize initializes the dispatcher.
2016-08-24 09:17:42 +00:00
// Private: Used by app.Space only.
2016-11-27 20:39:09 +00:00
func (v *DefaultDispatcher) Initialize(space app.Space) error {
2016-05-18 06:05:52 +00:00
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
2016-11-21 20:13:01 +00:00
return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
2016-05-18 06:05:52 +00:00
}
2016-11-27 20:39:09 +00:00
v.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
2016-05-18 15:12:04 +00:00
2016-05-18 06:05:52 +00:00
if space.HasApp(router.APP_ID) {
2016-11-27 20:39:09 +00:00
v.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
}
2017-01-01 22:08:39 +00:00
// Release implements common.Releasable.Release().
2016-11-27 20:39:09 +00:00
func (v *DefaultDispatcher) Release() {
2016-05-18 15:12:04 +00:00
2016-05-18 06:05:52 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
2016-05-18 06:05:52 +00:00
direct := ray.NewRay()
2016-11-27 20:39:09 +00:00
dispatcher := v.ohm.GetDefaultHandler()
2016-08-14 15:08:01 +00:00
destination := session.Destination
2016-05-18 06:05:52 +00:00
2016-11-27 20:39:09 +00: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 06:05:52 +00: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 13:33:00 +00:00
if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
2016-12-09 11:08:25 +00:00
go dispatcher.Dispatch(destination, buf.NewLocal(32), direct)
2016-08-12 21:37:21 +00:00
} else {
2016-11-27 20:39:09 +00:00
go v.FilterPacketAndDispatch(destination, direct, dispatcher)
2016-08-12 21:37:21 +00:00
}
2016-05-18 06:05:52 +00:00
return direct
}
2016-12-21 09:46:27 +00:00
// FilterPacketAndDispatch waits for a payload from source and starts dispatching.
2016-08-24 09:17:42 +00:00
// Private: Visible for testing.
2016-11-27 20:39:09 +00:00
func (v *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
2016-05-18 06:05:52 +00:00
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)
}
2016-12-16 14:39:47 +00:00
type DefaultDispatcherFactory struct{}
func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return NewDefaultDispatcher(space), nil
}
func (v DefaultDispatcherFactory) AppId() app.ID {
return dispatcher.APP_ID
}
func init() {
app.RegisterApplicationFactory(serial.GetMessageType(new(dispatcher.Config)), DefaultDispatcherFactory{})
}