2016-01-20 19:40:52 -05:00
|
|
|
package point
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2016-01-21 16:45:44 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/dice"
|
2016-01-20 19:40:52 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
|
|
|
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler for inbound detour connections.
|
|
|
|
type InboundDetourHandlerAlways struct {
|
|
|
|
space app.Space
|
|
|
|
config *InboundDetourConfig
|
2016-06-04 08:25:13 -04:00
|
|
|
ich []proxy.InboundHandler
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig) (*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-01-20 19:40:52 -05:00
|
|
|
for i := ports.From; i <= ports.To; i++ {
|
|
|
|
ichConfig := config.Settings
|
2016-06-04 08:25:13 -04:00
|
|
|
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{
|
2016-08-12 17:37:21 -04:00
|
|
|
Address: config.ListenOn,
|
|
|
|
Port: i,
|
|
|
|
Tag: config.Tag,
|
|
|
|
StreamSettings: config.StreamSettings,
|
|
|
|
AllowPassiveConnection: config.AllowPassiveConnection,
|
|
|
|
})
|
2016-01-20 19:40:52 -05:00
|
|
|
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)
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:18:24 -05:00
|
|
|
func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) {
|
2016-01-21 16:45:44 -05:00
|
|
|
ich := this.ich[dice.Roll(len(this.ich))]
|
2016-06-04 08:25:13 -04:00
|
|
|
return ich, this.config.Allocation.Refresh
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *InboundDetourHandlerAlways) Close() {
|
|
|
|
for _, ich := range this.ich {
|
2016-06-04 08:25:13 -04:00
|
|
|
ich.Close()
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2016-01-20 19:40:52 -05:00
|
|
|
if err != nil {
|
2016-06-04 08:25:13 -04:00
|
|
|
log.Error("Failed to start inbound detour:", err)
|
2016-01-20 19:40:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|