mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 02:16:47 +00:00
fix: allow JLS fallback before the server flight
Decide whether fallback is safe based on bytes actually written instead of JLS authentication status. This allows authenticated preflight failures, including HRR and TLS configuration mismatches, to relay the untouched ClientHello to the camouflage target. Keep rejecting fallback after a partial or complete server flight, update jls-tls for pre-write alert suppression, and add coverage for both sides of the write boundary.
This commit is contained in:
1 parent
f86bc224dc
commit
a5167fa4ab
4 files changed
+70
-17
No files matched your search
@@ -24,7 +24,7 @@ require (
|
||||
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759
|
||||
github.com/metacubex/http v0.1.6
|
||||
github.com/metacubex/jls-quic-go v0.0.0-20260716141257-bee72c5b0a6c
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716140250-3d001a2e3788
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716145614-4bf88db633e2
|
||||
github.com/metacubex/kcp-go v0.0.0-20260105040817-550693377604
|
||||
github.com/metacubex/mhurl v0.1.0
|
||||
github.com/metacubex/mlkem v0.1.0
|
||||
|
||||
@@ -133,8 +133,8 @@ github.com/metacubex/http v0.1.6 h1:xvXuvXMCMxCWMF5nEJF4yiKvXL+p2atWMzs37e80m1I=
|
||||
github.com/metacubex/http v0.1.6/go.mod h1:Nxx0zZAo2AhRfanyL+fmmK6ACMtVsfpwIl1aFAik2Eg=
|
||||
github.com/metacubex/jls-quic-go v0.0.0-20260716141257-bee72c5b0a6c h1:TK7l/879ivHrWnRQ+IZAuLCpJH4eTvo1ylNxtWF3nVk=
|
||||
github.com/metacubex/jls-quic-go v0.0.0-20260716141257-bee72c5b0a6c/go.mod h1:q4vsumnl4LVzMuVawpwvnWRIHSyOzp3b0n1ZY9A9wvo=
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716140250-3d001a2e3788 h1:vqtCy6N0WiYwk4/4vL3qibvWbBjoQOxBgNRLeNRFXuQ=
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716140250-3d001a2e3788/go.mod h1:mmqs889W/TqPlfNRDa2UyJvRiLyiTJIEnWHkcj3SKB8=
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716145614-4bf88db633e2 h1:n+qiR8opOJEerewq/S92zGoHBDXZb+dKmyYq0aKJtis=
|
||||
github.com/metacubex/jls-tls v0.0.0-20260716145614-4bf88db633e2/go.mod h1:mmqs889W/TqPlfNRDa2UyJvRiLyiTJIEnWHkcj3SKB8=
|
||||
github.com/metacubex/jsonv2 v0.0.0-20260518173308-f4597c22f1df h1:S0vBzqjXok24VopstOgPd1JdgglW9tXehrqvwpQWbQ8=
|
||||
github.com/metacubex/jsonv2 v0.0.0-20260518173308-f4597c22f1df/go.mod h1:F4sVXat6QjPXkNsKRDyyG3BhSkxPFFnRPEIwmmyCgbg=
|
||||
github.com/metacubex/kcp-go v0.0.0-20260105040817-550693377604 h1:hJwCVlE3ojViC35MGHB+FBr8TuIf3BUFn2EQ1VIamsI=
|
||||
|
||||
@@ -180,14 +180,16 @@ func Server(ctx context.Context, conn net.Conn, config *ServerConfig) (net.Conn,
|
||||
recorder := &handshakeRecorderConn{Conn: conn, recording: true}
|
||||
tlsConn := tls.Server(recorder, config.TLSConfig.Clone())
|
||||
if err := tlsConn.HandshakeContext(ctx); err != nil {
|
||||
// Forwarding after authentication or a local write would mix two server handshakes.
|
||||
if tlsConn.ConnectionState().JLS.Status == tls.JLSAuthenticated || recorder.wroteToClient() {
|
||||
// A partial or complete server flight may have been written before a later
|
||||
// client message or network error; forwarding would then mix two TLS handshakes.
|
||||
if recorder.wroteToClient() {
|
||||
recorder.discard()
|
||||
return nil, err
|
||||
}
|
||||
return nil, relayFallback(ctx, conn, recorder.stop(), config)
|
||||
}
|
||||
recorder.discard()
|
||||
// Defensively reject a successful TLS handshake if custom configuration bypassed JLS authentication.
|
||||
if tlsConn.ConnectionState().JLS.Status != tls.JLSAuthenticated {
|
||||
return nil, ErrJLSAuthFailed
|
||||
}
|
||||
|
||||
+63
-12
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -186,8 +187,40 @@ func TestJLSUTLSClientFallback(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJLSServerFallsBackInsteadOfHelloRetryRequest(t *testing.T) {
|
||||
func TestJLSServerFallsBackBeforeServerFlight(t *testing.T) {
|
||||
user := User{Username: "user", Password: "password"}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
configure func(*ServerConfig, *ClientConfig)
|
||||
}{
|
||||
{
|
||||
name: "HelloRetryRequest",
|
||||
configure: func(serverConfig *ServerConfig, _ *ClientConfig) {
|
||||
serverConfig.TLSConfig.CurvePreferences = []tls.CurveID{tls.CurveP256}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "missing certificate",
|
||||
configure: func(serverConfig *ServerConfig, _ *ClientConfig) {
|
||||
serverConfig.TLSConfig.Certificates = nil
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ALPN mismatch",
|
||||
configure: func(serverConfig *ServerConfig, clientConfig *ClientConfig) {
|
||||
serverConfig.TLSConfig.NextProtos = []string{"h3"}
|
||||
clientConfig.ALPN = []string{"http/1.1"}
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
testJLSServerFallsBackBeforeServerFlight(t, user, test.configure)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testJLSServerFallsBackBeforeServerFlight(t *testing.T, user User, configure func(*ServerConfig, *ClientConfig)) {
|
||||
t.Helper()
|
||||
fallbackRequest := make(chan struct{}, 1)
|
||||
fallback := httptest.NewUnstartedServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
fallbackRequest <- struct{}{}
|
||||
@@ -208,7 +241,12 @@ func TestJLSServerFallsBackInsteadOfHelloRetryRequest(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
serverConfig.TLSConfig.CurvePreferences = []tls.CurveID{tls.CurveP256}
|
||||
clientConfig, err := NewClientConfig("example.com", user.Username, user.Password, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clientConfig.ClientFingerprint = "chrome"
|
||||
configure(serverConfig, clientConfig)
|
||||
|
||||
serverSide, clientSide := newLocalTCPPair(t)
|
||||
serverDone := make(chan error, 1)
|
||||
@@ -218,11 +256,6 @@ func TestJLSServerFallsBackInsteadOfHelloRetryRequest(t *testing.T) {
|
||||
serverDone <- serverErr
|
||||
}()
|
||||
|
||||
clientConfig, err := NewClientConfig("example.com", user.Username, user.Password, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clientConfig.ClientFingerprint = "chrome"
|
||||
if conn, clientErr := NewClient(context.Background(), clientSide, clientConfig); !errors.Is(clientErr, ErrJLSAuthFailed) {
|
||||
if conn != nil {
|
||||
_ = conn.Close()
|
||||
@@ -323,7 +356,7 @@ func testJLSServerFallback(t *testing.T, version uint16) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestJLSServerDoesNotFallbackAfterAuthentication(t *testing.T) {
|
||||
func TestJLSServerDoesNotFallbackAfterServerFlight(t *testing.T) {
|
||||
user := User{Username: "user", Password: "password"}
|
||||
fallbackDialed := false
|
||||
serverConfig, err := NewServerConfig("camouflage.example", "camouflage.example:443", []User{user}, nil, 0, func(context.Context, string, string) (net.Conn, error) {
|
||||
@@ -333,8 +366,6 @@ func TestJLSServerDoesNotFallbackAfterAuthentication(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
serverConfig.TLSConfig.Certificates = nil
|
||||
|
||||
clientConfig, err := NewClientConfig("camouflage.example", user.Username, user.Password, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -346,7 +377,7 @@ func TestJLSServerDoesNotFallbackAfterAuthentication(t *testing.T) {
|
||||
serverDone <- err
|
||||
}()
|
||||
|
||||
client, clientErr := NewClient(context.Background(), clientSide, clientConfig)
|
||||
client, clientErr := NewClient(context.Background(), &failWritesAfterReadConn{Conn: clientSide}, clientConfig)
|
||||
if client != nil {
|
||||
_ = client.Close()
|
||||
}
|
||||
@@ -358,10 +389,30 @@ func TestJLSServerDoesNotFallbackAfterAuthentication(t *testing.T) {
|
||||
t.Fatal("server handshake unexpectedly succeeded")
|
||||
}
|
||||
if fallbackDialed {
|
||||
t.Fatal("authenticated handshake failure dialed fallback")
|
||||
t.Fatal("post-flight handshake failure dialed fallback")
|
||||
}
|
||||
}
|
||||
|
||||
type failWritesAfterReadConn struct {
|
||||
net.Conn
|
||||
read atomic.Bool
|
||||
}
|
||||
|
||||
func (c *failWritesAfterReadConn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
if n > 0 {
|
||||
c.read.Store(true)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c *failWritesAfterReadConn) Write(p []byte) (int, error) {
|
||||
if c.read.Load() {
|
||||
return 0, errors.New("test client write failed after reading server flight")
|
||||
}
|
||||
return c.Conn.Write(p)
|
||||
}
|
||||
|
||||
func TestJLSServerFallbackReplaysRejectedTLS(t *testing.T) {
|
||||
request := []byte{23, 3, 3, 0, 1, 0} // Application data before ClientHello.
|
||||
response := []byte("camouflage response")
|
||||
|
||||
Reference in new issue
Block a user