1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/inbound_detour_always.go

75 lines
2.0 KiB
Go
Raw Normal View History

2016-10-12 10:46:02 -04:00
package core
import (
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
"v2ray.com/core/common/retry"
"v2ray.com/core/proxy"
proxyregistry "v2ray.com/core/proxy/registry"
)
// Handler for inbound detour connections.
type InboundDetourHandlerAlways struct {
space app.Space
2016-10-14 16:21:45 -04:00
config *InboundConnectionConfig
2016-06-04 08:25:13 -04:00
ich []proxy.InboundHandler
}
2016-10-14 16:21:45 -04:00
func NewInboundDetourHandlerAlways(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerAlways, error) {
handler := &InboundDetourHandlerAlways{
space: space,
config: config,
}
ports := config.PortRange
2016-06-04 08:25:13 -04:00
handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1)
2016-08-26 18:04:35 -04:00
for i := ports.FromPort(); i <= ports.ToPort(); i++ {
2016-10-14 16:21:45 -04:00
ichConfig, err := config.GetTypedSettings()
if err != nil {
return nil, err
}
2016-10-16 08:22:21 -04:00
ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
2016-10-17 08:35:13 -04:00
Address: config.GetListenOnValue(),
2016-08-12 17:37:21 -04:00
Port: i,
Tag: config.Tag,
StreamSettings: config.StreamSettings,
AllowPassiveConnection: config.AllowPassiveConnection,
})
if err != nil {
log.Error("Failed to create inbound connection handler: ", err)
return nil, err
}
2016-06-04 08:25:13 -04:00
handler.ich = append(handler.ich, ich)
}
return handler, nil
}
func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) {
2016-01-21 16:45:44 -05:00
ich := this.ich[dice.Roll(len(this.ich))]
2016-10-14 16:21:45 -04:00
return ich, int(this.config.GetAllocationStrategyValue().Refresh.GetValue())
}
func (this *InboundDetourHandlerAlways) Close() {
for _, ich := range this.ich {
2016-06-04 08:25:13 -04:00
ich.Close()
}
}
// Starts the inbound connection handler.
func (this *InboundDetourHandlerAlways) Start() error {
for _, ich := range this.ich {
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
2016-06-04 08:25:13 -04:00
err := ich.Start()
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("Failed to start inbound detour:", err)
return err
}
return nil
})
if err != nil {
return err
}
}
return nil
}