Skip to content

Commit ab0b1e1

Browse files
committed
revert injection of STUNBatch function and http.Client, and implement working STUN batch function
1 parent abb3a9b commit ab0b1e1

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

option/unbounded.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package option
22

33
import (
4-
"net/http"
5-
64
"github.com/sagernet/sing-box/option"
75
)
86

@@ -28,12 +26,10 @@ type UnboundedOutboundOptions struct {
2826
NATFailTimeout int `json:"nat_fail_timeout,omitempty"`
2927
STUNBatchSize int `json:"stun_batch_size,omitempty"`
3028
STUNServers []string `json:"stun_servers,omitempty"`
31-
STUNBatch func(size uint32) (batch []string, err error) `json:"-"`
32-
HTTPClient *http.Client `json:"-"`
33-
Tag string `json:"tag,omitempty"`
34-
Patience int `json:"patience,omitempty"`
35-
ErrorBackoff int `json:"error_backoff,omitempty"`
36-
ConsumerSessionID string `json:"consumer_session_id,omitempty"`
29+
Tag string `json:"tag,omitempty"`
30+
Patience int `json:"patience,omitempty"`
31+
ErrorBackoff int `json:"error_backoff,omitempty"`
32+
ConsumerSessionID string `json:"consumer_session_id,omitempty"`
3733
// EgressOptions
3834
EgressAddr string `json:"egress_addr,omitempty"`
3935
EgressEndpoint string `json:"egress_endpoint,omitempty"`

protocol/unbounded/outbound.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"log"
1313
"math/big"
14+
mathrand "math/rand"
1415
"net"
1516
"net/http"
1617
"os"
@@ -119,17 +120,19 @@ func NewOutbound(
119120
rtcOpt.STUNBatchSize = uint32(options.STUNBatchSize)
120121
}
121122

122-
if options.STUNBatch != nil {
123-
rtcOpt.STUNBatch = options.STUNBatch
124-
} else if len(options.STUNServers) > 0 {
125-
servers := make([]string, len(options.STUNServers))
126-
copy(servers, options.STUNServers)
127-
rtcOpt.STUNBatch = func(size uint32) ([]string, error) {
128-
if int(size) >= len(servers) {
129-
return servers, nil
130-
}
131-
return servers[:size], nil
123+
servers := make([]string, len(options.STUNServers))
124+
copy(servers, options.STUNServers)
125+
126+
rtcOpt.STUNBatch = func(size uint32) (batch []string, err error) {
127+
// At batch time, select N random servers from the list provided in the options
128+
for i := 0; i < int(size) && len(servers) > 0; i++ {
129+
idx := mathrand.Intn(len(servers))
130+
batch = append(batch, servers[idx])
131+
servers[idx] = servers[len(servers)-1]
132+
servers = servers[:len(servers)-1]
132133
}
134+
135+
return batch, nil
133136
}
134137

135138
if options.Tag != "" {
@@ -184,20 +187,17 @@ func NewOutbound(
184187
return nil, err
185188
}
186189
rtcOpt.Net = rtcNet
187-
if options.HTTPClient != nil {
188-
rtcOpt.HTTPClient = options.HTTPClient
189-
} else {
190-
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
191-
return outboundDialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
192-
}
193-
rtcOpt.HTTPClient = &http.Client{
194-
Transport: &http.Transport{
195-
Dial: func(network, addr string) (net.Conn, error) {
196-
return dialContext(ctx, network, addr)
197-
},
198-
DialContext: dialContext,
190+
191+
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
192+
return outboundDialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
193+
}
194+
rtcOpt.HTTPClient = &http.Client{
195+
Transport: &http.Transport{
196+
Dial: func(network, addr string) (net.Conn, error) {
197+
return dialContext(ctx, network, addr)
199198
},
200-
}
199+
DialContext: dialContext,
200+
},
201201
}
202202

203203
tlsConfig, err := generateSelfSignedTLSConfig(options.InsecureDoNotVerifyClientCert, options.EgressCA)

0 commit comments

Comments
 (0)