Skip to content

Commit d902512

Browse files
committed
all: use core.ChooseOne where applicable
1 parent 5d99735 commit d902512

4 files changed

Lines changed: 7 additions & 14 deletions

File tree

intra/dnscrypt/multiserver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/binary"
2121
"errors"
2222
"fmt"
23-
"math/rand"
2423
"net"
2524
"net/netip"
2625
"strings"
@@ -85,10 +84,7 @@ var (
8584
)
8685

8786
func chooseAny[T any](s []T) (zz T) {
88-
if len(s) <= 0 {
89-
return // zz
90-
}
91-
return s[rand.Intn(len(s))]
87+
return core.ChooseOne(s)
9288
}
9389

9490
func udpExchange(pid string, serverInfo *serverinfo, relayAddrs []*net.UDPAddr, sharedKey *[32]byte, encryptedQuery []byte, clientNonce []byte) (res []byte, relay net.Addr, err error) {

intra/ipn/exit64.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package ipn
88

99
import (
1010
"context"
11-
"math/rand"
1211
"net"
1312
"net/netip"
1413
"time"
@@ -283,7 +282,7 @@ func addr4to6(addr string) string {
283282
return ""
284283
}
285284
// embed IPv4 in IPv6
286-
ippre := warp.Net6to4[rand.Intn(len(warp.Net6to4))]
285+
ippre := core.ChooseOne(warp.Net6to4)
287286
ip6 := ip4to6(ippre, ip4)
288287
if !ip6.IsValid() {
289288
log.W("proxy: auto: exit64: addr64: failed to embed(%s) in v6(%s)", ip4, ippre)

intra/ipn/proxies.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func (px *proxifier) ProxyTo(ipp netip.AddrPort, uid string, pids []string) (_ P
483483
}
484484

485485
var lopinned string
486-
var someproxy Proxy
486+
var someproxy []Proxy
487487

488488
pinnedpid, pinok := px.getpin(uid, ipp)
489489
chosen := has(pids, pinnedpid)
@@ -559,8 +559,8 @@ func (px *proxifier) ProxyTo(ipp netip.AddrPort, uid string, pids []string) (_ P
559559
return p, nil
560560
} // else: proxy not ok
561561
notokproxies = append(notokproxies, pid)
562-
if someproxy == nil {
563-
someproxy = p
562+
if p != nil {
563+
someproxy = append(someproxy, p)
564564
}
565565
} else { // else: proxy cannot route; split-tunnel
566566
norouteproxies = append(norouteproxies, pid)
@@ -571,9 +571,7 @@ func (px *proxifier) ProxyTo(ipp netip.AddrPort, uid string, pids []string) (_ P
571571
if len(notokproxies) > 0 {
572572
// stall to allow a non-healthy proxy to recover
573573
stalledSec = px.stall(uid + ippstr)
574-
if someproxy != nil {
575-
return someproxy, nil
576-
}
574+
return core.ChooseOne(someproxy), nil
577575
}
578576

579577
// lopinned is always the first element, if any.

intra/ipn/warp/cfg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var warpDefaultHeaders = map[string]string{
129129
}
130130

131131
func randomWarpCidrs() (v4 netip.Prefix, v6 netip.Prefix) {
132-
return warpCidrs4[rand.Intn(len(warpCidrs4))], warpCidrs6[rand.Intn(len(warpCidrs6))]
132+
return core.ChooseOne(warpCidrs4), core.ChooseOne(warpCidrs6)
133133
}
134134

135135
func randomWarpPort() uint16 {

0 commit comments

Comments
 (0)