1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/shell/point/inbound_detour.go

60 lines
1.6 KiB
Go
Raw Normal View History

2015-10-31 19:11:41 -04:00
package point
import (
"github.com/v2ray/v2ray-core/common/log"
2015-12-02 06:47:54 -05:00
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-31 19:11:41 -04:00
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
2015-11-29 08:45:32 -05:00
"github.com/v2ray/v2ray-core/shell/point/config"
2015-10-31 19:11:41 -04:00
)
type InboundConnectionHandlerWithPort struct {
2015-12-02 06:47:54 -05:00
port v2net.Port
2015-10-31 19:11:41 -04:00
handler connhandler.InboundConnectionHandler
}
2015-12-03 09:12:47 -05:00
// Handler for inbound detour connections.
2015-10-31 19:11:41 -04:00
type InboundDetourHandler struct {
point *Point
config config.InboundDetourConfig
ich []*InboundConnectionHandlerWithPort
}
func (this *InboundDetourHandler) Initialize() error {
ichFactory := connhandler.GetInboundConnectionHandlerFactory(this.config.Protocol())
if ichFactory == nil {
log.Error("Unknown inbound connection handler factory %s", this.config.Protocol())
return config.BadConfiguration
}
ports := this.config.PortRange()
2015-12-03 14:57:33 -05:00
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1)
2015-10-31 19:11:41 -04:00
for i := ports.From(); i <= ports.To(); i++ {
ichConfig := this.config.Settings()
ich, err := ichFactory.Create(this.point, ichConfig)
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
}
2015-12-03 09:12:47 -05:00
// Starts the inbound connection handler.
2015-10-31 19:11:41 -04:00
func (this *InboundDetourHandler) Start() error {
for _, ich := range this.ich {
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
2015-12-02 15:44:01 -05:00
err := ich.handler.Listen(ich.port)
2015-10-31 19:11:41 -04:00
if err != nil {
return err
}
return nil
})
}
return nil
}