mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-21 00:36:25 -05:00
dice.Roll()
This commit is contained in:
parent
d623b1809d
commit
37a9d8ef50
12
common/dice/dice.go
Normal file
12
common/dice/dice.go
Normal file
@ -0,0 +1,12 @@
|
||||
package dice
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func Roll(n int) int {
|
||||
if n == 1 {
|
||||
return 0
|
||||
}
|
||||
return rand.Intn(n)
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/dice"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||
)
|
||||
@ -44,12 +44,7 @@ func (this *Receiver) AddUser(user *vmess.User) {
|
||||
}
|
||||
|
||||
func (this *Receiver) PickUser() *vmess.User {
|
||||
userLen := len(this.Accounts)
|
||||
userIdx := 0
|
||||
if userLen > 1 {
|
||||
userIdx = rand.Intn(userLen)
|
||||
}
|
||||
return this.Accounts[userIdx]
|
||||
return this.Accounts[dice.Roll(len(this.Accounts))]
|
||||
}
|
||||
|
||||
type ExpiringReceiver struct {
|
||||
@ -108,11 +103,7 @@ func (this *ReceiverManager) pickDetour() *Receiver {
|
||||
return nil
|
||||
}
|
||||
this.detourAccess.RLock()
|
||||
idx := 0
|
||||
detourLen := len(this.detours)
|
||||
if detourLen > 1 {
|
||||
idx = rand.Intn(detourLen)
|
||||
}
|
||||
idx := dice.Roll(len(this.detours))
|
||||
rec := this.detours[idx]
|
||||
this.detourAccess.RUnlock()
|
||||
|
||||
@ -129,14 +120,7 @@ func (this *ReceiverManager) pickDetour() *Receiver {
|
||||
}
|
||||
|
||||
func (this *ReceiverManager) pickStdReceiver() *Receiver {
|
||||
receiverLen := len(this.receivers)
|
||||
|
||||
receiverIdx := 0
|
||||
if receiverLen > 1 {
|
||||
receiverIdx = rand.Intn(receiverLen)
|
||||
}
|
||||
|
||||
return this.receivers[receiverIdx]
|
||||
return this.receivers[dice.Roll(len(this.receivers))]
|
||||
}
|
||||
|
||||
func (this *ReceiverManager) PickReceiver() (v2net.Destination, *vmess.User) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package vmess
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"github.com/v2ray/v2ray-core/common/dice"
|
||||
)
|
||||
|
||||
type UserLevel byte
|
||||
@ -39,11 +39,7 @@ func (this *User) AnyValidID() *ID {
|
||||
if len(this.AlterIDs) == 0 {
|
||||
return this.ID
|
||||
}
|
||||
if len(this.AlterIDs) == 1 {
|
||||
return this.AlterIDs[0]
|
||||
}
|
||||
idx := rand.Intn(len(this.AlterIDs))
|
||||
return this.AlterIDs[idx]
|
||||
return this.AlterIDs[dice.Roll(len(this.AlterIDs))]
|
||||
}
|
||||
|
||||
type UserSettings struct {
|
||||
|
@ -1,9 +1,8 @@
|
||||
package point
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/common/dice"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/retry"
|
||||
@ -46,8 +45,7 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig)
|
||||
}
|
||||
|
||||
func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundConnectionHandler, int) {
|
||||
idx := rand.Intn(len(this.ich))
|
||||
ich := this.ich[idx]
|
||||
ich := this.ich[dice.Roll(len(this.ich))]
|
||||
return ich.handler, this.config.Allocation.Refresh
|
||||
}
|
||||
|
||||
|
@ -2,9 +2,9 @@ package dialer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"net"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/dice"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
@ -24,11 +24,7 @@ func Dial(dest v2net.Destination) (net.Conn, error) {
|
||||
if len(ips) == 0 {
|
||||
return nil, ErrInvalidHost
|
||||
}
|
||||
if len(ips) == 1 {
|
||||
ip = ips[0]
|
||||
} else {
|
||||
ip = ips[rand.Intn(len(ips))]
|
||||
}
|
||||
ip = ips[dice.Roll(len(ips))]
|
||||
}
|
||||
if dest.IsTCP() {
|
||||
return net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
|
@ -19,6 +19,7 @@ func TestDialDomain(t *testing.T) {
|
||||
}
|
||||
dest, err := server.Start()
|
||||
assert.Error(err).IsNil()
|
||||
defer server.Close()
|
||||
|
||||
conn, err := Dial(v2net.TCPDestination(v2net.DomainAddress("local.v2ray.com"), dest.Port()))
|
||||
assert.Error(err).IsNil()
|
||||
|
Loading…
Reference in New Issue
Block a user