Add backwards compatibility fallback to firefox

This commit is contained in:
Andy Wang 2025-06-08 18:14:39 +01:00
parent 51ed286f35
commit 8af137637e
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
3 changed files with 45 additions and 14 deletions

View File

@ -21,8 +21,10 @@ func MakeSession(connConfig RemoteConnConfig, authInfo AuthInfo, dialer common.D
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < connConfig.NumConn; i++ { for i := 0; i < connConfig.NumConn; i++ {
wg.Add(1) wg.Add(1)
transportConfig := connConfig.Transport
go func() { go func() {
makeconn: makeconn:
transportConn := transportConfig.CreateTransport()
remoteConn, err := dialer.Dial("tcp", connConfig.RemoteAddr) remoteConn, err := dialer.Dial("tcp", connConfig.RemoteAddr)
if err != nil { if err != nil {
log.Errorf("Failed to establish new connections to remote: %v", err) log.Errorf("Failed to establish new connections to remote: %v", err)
@ -31,12 +33,20 @@ func MakeSession(connConfig RemoteConnConfig, authInfo AuthInfo, dialer common.D
goto makeconn goto makeconn
} }
transportConn := connConfig.TransportMaker()
sk, err := transportConn.Handshake(remoteConn, authInfo) sk, err := transportConn.Handshake(remoteConn, authInfo)
if err != nil { if err != nil {
log.Errorf("Failed to prepare connection to remote: %v", err) log.Errorf("Failed to prepare connection to remote: %v", err)
transportConn.Close() transportConn.Close()
// In Cloak v2.11.0, we've updated uTLS version and subsequently increased the first packet size for chrome above 1500
// https://github.com/cbeuw/Cloak/pull/306#issuecomment-2862728738. As a backwards compatibility feature, if we fail
// to connect using chrome signature, retry with firefox which has a smaller packet size.
if transportConfig.mode == "direct" && transportConfig.browser == chrome {
transportConfig.browser = firefox
log.Warnf("failed to connect with chrome signature, falling back to retry with firefox")
}
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
goto makeconn goto makeconn
} }
// sessionKey given by each connection should be identical // sessionKey given by each connection should be identical

View File

@ -47,7 +47,7 @@ type RemoteConnConfig struct {
NumConn int NumConn int
KeepAlive time.Duration KeepAlive time.Duration
RemoteAddr string RemoteAddr string
TransportMaker func() Transport Transport TransportConfig
} }
type LocalConnConfig struct { type LocalConnConfig struct {
@ -230,11 +230,10 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
raw.CDNWsUrlPath = "/" raw.CDNWsUrlPath = "/"
} }
remote.TransportMaker = func() Transport { remote.Transport = TransportConfig{
return &WSOverTLS{ mode: "cdn",
wsUrl: "ws://" + cdnDomainPort + raw.CDNWsUrlPath, wsUrl: "ws://" + cdnDomainPort + raw.CDNWsUrlPath,
} }
}
case "direct": case "direct":
fallthrough fallthrough
default: default:
@ -249,12 +248,11 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
default: default:
browser = chrome browser = chrome
} }
remote.TransportMaker = func() Transport { remote.Transport = TransportConfig{
return &DirectTLS{ mode: "direct",
browser: browser, browser: browser,
} }
} }
}
// KeepAlive // KeepAlive
if raw.KeepAlive <= 0 { if raw.KeepAlive <= 0 {

View File

@ -8,3 +8,26 @@ type Transport interface {
Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey [32]byte, err error) Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey [32]byte, err error)
net.Conn net.Conn
} }
type TransportConfig struct {
mode string
wsUrl string
browser browser
}
func (t TransportConfig) CreateTransport() Transport {
switch t.mode {
case "cdn":
return &WSOverTLS{
wsUrl: t.wsUrl,
}
case "direct":
return &DirectTLS{
browser: t.browser,
}
default:
return nil
}
}