mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
72 lines
1.9 KiB
8 years ago
|
package core
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"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"
|
||
9 years ago
|
)
|
||
|
|
||
|
// Handler for inbound detour connections.
|
||
|
type InboundDetourHandlerAlways struct {
|
||
|
space app.Space
|
||
|
config *InboundDetourConfig
|
||
9 years ago
|
ich []proxy.InboundHandler
|
||
9 years ago
|
}
|
||
|
|
||
|
func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig) (*InboundDetourHandlerAlways, error) {
|
||
|
handler := &InboundDetourHandlerAlways{
|
||
|
space: space,
|
||
|
config: config,
|
||
|
}
|
||
|
ports := config.PortRange
|
||
9 years ago
|
handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1)
|
||
8 years ago
|
for i := ports.FromPort(); i <= ports.ToPort(); i++ {
|
||
9 years ago
|
ichConfig := config.Settings
|
||
8 years ago
|
ich, err := proxyregistry.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{
|
||
8 years ago
|
Address: config.ListenOn,
|
||
|
Port: i,
|
||
|
Tag: config.Tag,
|
||
|
StreamSettings: config.StreamSettings,
|
||
|
AllowPassiveConnection: config.AllowPassiveConnection,
|
||
|
})
|
||
9 years ago
|
if err != nil {
|
||
|
log.Error("Failed to create inbound connection handler: ", err)
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
handler.ich = append(handler.ich, ich)
|
||
9 years ago
|
}
|
||
|
return handler, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) {
|
||
9 years ago
|
ich := this.ich[dice.Roll(len(this.ich))]
|
||
9 years ago
|
return ich, this.config.Allocation.Refresh
|
||
9 years ago
|
}
|
||
|
|
||
|
func (this *InboundDetourHandlerAlways) Close() {
|
||
|
for _, ich := range this.ich {
|
||
9 years ago
|
ich.Close()
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// 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 {
|
||
9 years ago
|
err := ich.Start()
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
log.Error("Failed to start inbound detour:", err)
|
||
9 years ago
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|