2015-10-31 19:11:41 -04:00
|
|
|
package point
|
|
|
|
|
|
|
|
import (
|
2015-12-05 16:55:45 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-10-31 19:11:41 -04:00
|
|
|
"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"
|
2016-01-01 17:44:11 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-02 17:32:18 -05:00
|
|
|
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
|
2015-10-31 19:11:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type InboundConnectionHandlerWithPort struct {
|
2015-12-02 06:47:54 -05:00
|
|
|
port v2net.Port
|
2016-01-02 17:32:18 -05:00
|
|
|
handler proxy.InboundConnectionHandler
|
2015-10-31 19:11:41 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 09:12:47 -05:00
|
|
|
// Handler for inbound detour connections.
|
2015-10-31 19:11:41 -04:00
|
|
|
type InboundDetourHandler struct {
|
2015-12-11 06:01:20 -05:00
|
|
|
space app.Space
|
2015-12-06 10:41:41 -05:00
|
|
|
config InboundDetourConfig
|
2015-10-31 19:11:41 -04:00
|
|
|
ich []*InboundConnectionHandlerWithPort
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InboundDetourHandler) Initialize() error {
|
|
|
|
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()
|
2016-01-02 17:32:18 -05:00
|
|
|
ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig)
|
2015-10-31 19:11:41 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-03 18:33:25 -05:00
|
|
|
func (this *InboundDetourHandler) Close() {
|
|
|
|
for _, ich := range this.ich {
|
|
|
|
ich.handler.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-12-04 10:49:10 -05:00
|
|
|
err := 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 {
|
2015-12-04 09:30:41 -05:00
|
|
|
log.Error("Failed to start inbound detour on port %d: %v", ich.port, err)
|
2015-10-31 19:11:41 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2015-12-04 10:49:10 -05:00
|
|
|
if err != nil {
|
2015-12-04 10:49:45 -05:00
|
|
|
return err
|
2015-12-04 10:49:10 -05:00
|
|
|
}
|
2015-10-31 19:11:41 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|