2016-10-12 10:46:02 -04:00
|
|
|
package core
|
2016-01-20 19:40:52 -05:00
|
|
|
|
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
"v2ray.com/core/common/retry"
|
|
|
|
"v2ray.com/core/proxy"
|
2016-01-20 19:40:52 -05:00
|
|
|
)
|
|
|
|
|
2016-12-21 09:37:16 -05:00
|
|
|
// InboundDetourHandlerAlways is a handler for inbound detour connections.
|
2016-01-20 19:40:52 -05:00
|
|
|
type InboundDetourHandlerAlways struct {
|
|
|
|
space app.Space
|
2016-10-14 16:21:45 -04:00
|
|
|
config *InboundConnectionConfig
|
2016-06-04 08:25:13 -04:00
|
|
|
ich []proxy.InboundHandler
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:56:21 -05:00
|
|
|
func NewInboundDetourHandlerAlways(ctx context.Context, config *InboundConnectionConfig) (*InboundDetourHandlerAlways, error) {
|
|
|
|
space := app.SpaceFromContext(ctx)
|
2016-01-20 19:40:52 -05:00
|
|
|
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-08-26 18:04:35 -04:00
|
|
|
for i := ports.FromPort(); i <= ports.ToPort(); i++ {
|
2016-10-14 16:21:45 -04:00
|
|
|
ichConfig, err := config.GetTypedSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-12 18:56:21 -05:00
|
|
|
ich, err := proxy.CreateInboundHandler(proxy.ContextWithInboundMeta(ctx, &proxy.InboundHandlerMeta{
|
2016-10-17 08:35:13 -04:00
|
|
|
Address: config.GetListenOnValue(),
|
2016-08-12 17:37:21 -04:00
|
|
|
Port: i,
|
|
|
|
Tag: config.Tag,
|
|
|
|
StreamSettings: config.StreamSettings,
|
|
|
|
AllowPassiveConnection: config.AllowPassiveConnection,
|
2017-01-12 18:56:21 -05:00
|
|
|
}), ichConfig)
|
|
|
|
|
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-11-27 15:39:09 -05:00
|
|
|
func (v *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) {
|
|
|
|
ich := v.ich[dice.Roll(len(v.ich))]
|
2016-12-23 06:42:25 -05:00
|
|
|
return ich, int(v.config.GetAllocationStrategyValue().GetRefreshValue())
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundDetourHandlerAlways) Close() {
|
|
|
|
for _, ich := range v.ich {
|
2016-06-04 08:25:13 -04:00
|
|
|
ich.Close()
|
2016-01-20 19:40:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 09:37:16 -05:00
|
|
|
// Start starts the inbound connection handler.
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundDetourHandlerAlways) Start() error {
|
|
|
|
for _, ich := range v.ich {
|
2016-11-20 15:47:51 -05:00
|
|
|
err := retry.ExponentialBackoff(10 /* times */, 200 /* 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
|
|
|
|
}
|