Skip to content

Commit 8af1376

Browse files
committed
Add backwards compatibility fallback to firefox
1 parent 51ed286 commit 8af1376

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

internal/client/connector.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ func MakeSession(connConfig RemoteConnConfig, authInfo AuthInfo, dialer common.D
2121
var wg sync.WaitGroup
2222
for i := 0; i < connConfig.NumConn; i++ {
2323
wg.Add(1)
24+
transportConfig := connConfig.Transport
2425
go func() {
2526
makeconn:
27+
transportConn := transportConfig.CreateTransport()
2628
remoteConn, err := dialer.Dial("tcp", connConfig.RemoteAddr)
2729
if err != nil {
2830
log.Errorf("Failed to establish new connections to remote: %v", err)
@@ -31,12 +33,20 @@ func MakeSession(connConfig RemoteConnConfig, authInfo AuthInfo, dialer common.D
3133
goto makeconn
3234
}
3335

34-
transportConn := connConfig.TransportMaker()
3536
sk, err := transportConn.Handshake(remoteConn, authInfo)
3637
if err != nil {
3738
log.Errorf("Failed to prepare connection to remote: %v", err)
3839
transportConn.Close()
40+
41+
// In Cloak v2.11.0, we've updated uTLS version and subsequently increased the first packet size for chrome above 1500
42+
// https://github.com/cbeuw/Cloak/pull/306#issuecomment-2862728738. As a backwards compatibility feature, if we fail
43+
// to connect using chrome signature, retry with firefox which has a smaller packet size.
44+
if transportConfig.mode == "direct" && transportConfig.browser == chrome {
45+
transportConfig.browser = firefox
46+
log.Warnf("failed to connect with chrome signature, falling back to retry with firefox")
47+
}
3948
time.Sleep(time.Second * 3)
49+
4050
goto makeconn
4151
}
4252
// sessionKey given by each connection should be identical

internal/client/state.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ type RawConfig struct {
4343
}
4444

4545
type RemoteConnConfig struct {
46-
Singleplex bool
47-
NumConn int
48-
KeepAlive time.Duration
49-
RemoteAddr string
50-
TransportMaker func() Transport
46+
Singleplex bool
47+
NumConn int
48+
KeepAlive time.Duration
49+
RemoteAddr string
50+
Transport TransportConfig
5151
}
5252

5353
type LocalConnConfig struct {
@@ -230,10 +230,9 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
230230
raw.CDNWsUrlPath = "/"
231231
}
232232

233-
remote.TransportMaker = func() Transport {
234-
return &WSOverTLS{
235-
wsUrl: "ws://" + cdnDomainPort + raw.CDNWsUrlPath,
236-
}
233+
remote.Transport = TransportConfig{
234+
mode: "cdn",
235+
wsUrl: "ws://" + cdnDomainPort + raw.CDNWsUrlPath,
237236
}
238237
case "direct":
239238
fallthrough
@@ -249,10 +248,9 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
249248
default:
250249
browser = chrome
251250
}
252-
remote.TransportMaker = func() Transport {
253-
return &DirectTLS{
254-
browser: browser,
255-
}
251+
remote.Transport = TransportConfig{
252+
mode: "direct",
253+
browser: browser,
256254
}
257255
}
258256

internal/client/transport.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,26 @@ type Transport interface {
88
Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey [32]byte, err error)
99
net.Conn
1010
}
11+
12+
type TransportConfig struct {
13+
mode string
14+
15+
wsUrl string
16+
17+
browser browser
18+
}
19+
20+
func (t TransportConfig) CreateTransport() Transport {
21+
switch t.mode {
22+
case "cdn":
23+
return &WSOverTLS{
24+
wsUrl: t.wsUrl,
25+
}
26+
case "direct":
27+
return &DirectTLS{
28+
browser: t.browser,
29+
}
30+
default:
31+
return nil
32+
}
33+
}

0 commit comments

Comments
 (0)