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

126 lines
2.9 KiB
Go
Raw Normal View History

2016-05-18 06:05:52 +00:00
package impl
import (
2017-01-04 11:34:01 +00:00
"time"
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"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
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"
"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{}
2017-01-06 14:32:36 +00:00
space.OnInitialize(func() error {
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
if d.ohm == nil {
return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
}
d.router = router.FromSpace(space)
return nil
2016-05-18 15:12:04 +00:00
})
return d
}
2016-11-27 20:39:09 +00:00
func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
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)
}
}
2017-01-04 11:34:01 +00:00
direct := ray.NewRay()
var waitFunc func() error
2016-11-13 13:33:00 +00:00
if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
2017-01-04 11:34:01 +00:00
waitFunc = noOpWait()
2016-08-12 21:37:21 +00:00
} else {
2017-01-04 11:34:01 +00:00
wdi := &waitDataInspector{
hasData: make(chan bool, 1),
}
direct.AddInspector(wdi)
waitFunc = waitForData(wdi)
2016-08-12 21:37:21 +00:00
}
2016-05-18 06:05:52 +00:00
2017-01-04 11:34:01 +00:00
go v.waitAndDispatch(waitFunc, destination, direct, dispatcher)
2016-05-18 06:05:52 +00:00
return direct
}
2017-01-04 11:34:01 +00:00
func (v *DefaultDispatcher) waitAndDispatch(wait func() error, destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
if err := wait(); err != nil {
log.Info("DefaultDispatcher: Failed precondition: ", err)
2017-01-10 13:22:42 +00:00
link.OutboundInput().CloseError()
link.OutboundOutput().CloseError()
2016-05-18 06:05:52 +00:00
return
}
2017-01-04 11:34:01 +00:00
dispatcher.Dispatch(destination, link)
2016-05-18 06:05:52 +00:00
}
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 init() {
2017-01-06 14:32:36 +00:00
common.Must(app.RegisterApplicationFactory((*dispatcher.Config)(nil), DefaultDispatcherFactory{}))
2016-12-16 14:39:47 +00:00
}
2017-01-04 11:34:01 +00:00
type waitDataInspector struct {
hasData chan bool
}
func (wdi *waitDataInspector) Input(*buf.Buffer) {
select {
case wdi.hasData <- true:
default:
}
}
func (wdi *waitDataInspector) WaitForData() bool {
select {
case <-wdi.hasData:
return true
case <-time.After(time.Minute):
return false
}
}
func waitForData(wdi *waitDataInspector) func() error {
return func() error {
if wdi.WaitForData() {
return nil
}
return errors.New("DefaultDispatcher: No data.")
}
}
func noOpWait() func() error {
return func() error {
return nil
}
}