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.
v2ray-core/shell/point/inbound_detour.go

65 lines
1.6 KiB

9 years ago
package point
import (
"github.com/v2ray/v2ray-core/app"
9 years ago
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
9 years ago
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy"
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
9 years ago
)
type InboundConnectionHandlerWithPort struct {
port v2net.Port
handler proxy.InboundConnectionHandler
9 years ago
}
9 years ago
// Handler for inbound detour connections.
9 years ago
type InboundDetourHandler struct {
space app.Space
config InboundDetourConfig
9 years ago
ich []*InboundConnectionHandlerWithPort
}
func (this *InboundDetourHandler) Initialize() error {
ports := this.config.PortRange()
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1)
9 years ago
for i := ports.From(); i <= ports.To(); i++ {
ichConfig := this.config.Settings()
ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig)
9 years ago
if err != nil {
log.Error("Failed to create inbound connection handler: %v", err)
return err
}
this.ich = append(this.ich, &InboundConnectionHandlerWithPort{
port: i,
handler: ich,
})
}
return nil
}
func (this *InboundDetourHandler) Close() {
for _, ich := range this.ich {
ich.handler.Close()
}
}
9 years ago
// Starts the inbound connection handler.
9 years ago
func (this *InboundDetourHandler) Start() error {
for _, ich := range this.ich {
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
err := ich.handler.Listen(ich.port)
9 years ago
if err != nil {
log.Error("Failed to start inbound detour on port %d: %v", ich.port, err)
9 years ago
return err
}
return nil
})
if err != nil {
9 years ago
return err
}
9 years ago
}
return nil
}