197 lines
6.2 KiB
Diff
197 lines
6.2 KiB
Diff
diff --git a/adapter/outbound/reality.go b/adapter/outbound/reality.go
|
|
index 70c7bc7a..2796c9bc 100644
|
|
--- a/adapter/outbound/reality.go
|
|
+++ b/adapter/outbound/reality.go
|
|
@@ -6,6 +6,8 @@ import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
+ "strconv"
|
|
+ "strings"
|
|
|
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
)
|
|
@@ -15,12 +17,28 @@ type RealityOptions struct {
|
|
ShortID string `proxy:"short-id,omitempty"`
|
|
|
|
SupportX25519MLKEM768 bool `proxy:"support-x25519mlkem768,omitempty"`
|
|
+
|
|
+ // Xray version reported to the server, "x.y.z" (default tlsC.RealityDefaultClientVersion)
|
|
+ ClientVersion string `proxy:"client-version,omitempty"`
|
|
}
|
|
|
|
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
|
|
if o.PublicKey != "" {
|
|
config := new(tlsC.RealityConfig)
|
|
config.SupportX25519MLKEM768 = o.SupportX25519MLKEM768
|
|
+ if o.ClientVersion != "" {
|
|
+ parts := strings.Split(o.ClientVersion, ".")
|
|
+ if len(parts) != 3 {
|
|
+ return nil, errors.New("invalid REALITY client-version, want x.y.z")
|
|
+ }
|
|
+ for i, part := range parts {
|
|
+ n, err := strconv.ParseUint(part, 10, 8)
|
|
+ if err != nil {
|
|
+ return nil, errors.New("invalid REALITY client-version, want x.y.z (each 0-255)")
|
|
+ }
|
|
+ config.ClientVersion[i] = byte(n)
|
|
+ }
|
|
+ }
|
|
|
|
const x25519ScalarSize = 32
|
|
publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
|
|
diff --git a/adapter/outbound/reality_version_test.go b/adapter/outbound/reality_version_test.go
|
|
new file mode 100644
|
|
index 00000000..444e50c7
|
|
--- /dev/null
|
|
+++ b/adapter/outbound/reality_version_test.go
|
|
@@ -0,0 +1,22 @@
|
|
+package outbound
|
|
+
|
|
+import "testing"
|
|
+
|
|
+func TestRealityClientVersion(t *testing.T) {
|
|
+ base := RealityOptions{PublicKey: "Nc9ZSDuQMwHN4M6TeDHjfJhgZ6SBTBbfMwx5iHQZ1ns", ShortID: "ab"}
|
|
+ c, err := base.Parse()
|
|
+ if err != nil || c.ClientVersion != [3]byte{} {
|
|
+ t.Fatalf("no client-version: %v %v", c, err)
|
|
+ }
|
|
+ o := base
|
|
+ o.ClientVersion = "25.3.6"
|
|
+ if c, err = o.Parse(); err != nil || c.ClientVersion != [3]byte{25, 3, 6} {
|
|
+ t.Fatalf("25.3.6: %v %v", c, err)
|
|
+ }
|
|
+ for _, bad := range []string{"1.8", "1.8.2.4", "a.b.c", "256.0.0", "-1.0.0"} {
|
|
+ o.ClientVersion = bad
|
|
+ if _, err = o.Parse(); err == nil {
|
|
+ t.Fatalf("%q accepted", bad)
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/component/tls/reality.go b/component/tls/reality.go
|
|
index ff6a0a3d..d2bc3b3b 100644
|
|
--- a/component/tls/reality.go
|
|
+++ b/component/tls/reality.go
|
|
@@ -28,11 +28,19 @@ import (
|
|
|
|
const RealityMaxShortIDLen = 8
|
|
|
|
+// RealityDefaultClientVersion is the Xray version a REALITY client reports in the
|
|
+// session ID. Upstream sends 1.8.2; servers with minClientVer above it (set by some
|
|
+// panels) reject that while Xray clients pass, so report a current Xray release.
|
|
+var RealityDefaultClientVersion = [3]byte{26, 10, 21}
|
|
+
|
|
type RealityConfig struct {
|
|
PublicKey *ecdh.PublicKey
|
|
ShortID [RealityMaxShortIDLen]byte
|
|
|
|
SupportX25519MLKEM768 bool
|
|
+
|
|
+ // ClientVersion: x.y.z reported to the server; zero = RealityDefaultClientVersion
|
|
+ ClientVersion [3]byte
|
|
}
|
|
|
|
func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHelloID, serverName string, realityConfig *RealityConfig) (net.Conn, error) {
|
|
@@ -71,9 +79,11 @@ func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHello
|
|
binary.BigEndian.PutUint64(hello.SessionId, uint64(ntp.Now().Unix()))
|
|
|
|
copy(hello.SessionId[8:], realityConfig.ShortID[:])
|
|
- hello.SessionId[0] = 1
|
|
- hello.SessionId[1] = 8
|
|
- hello.SessionId[2] = 2
|
|
+ version := realityConfig.ClientVersion
|
|
+ if version == [3]byte{} {
|
|
+ version = RealityDefaultClientVersion
|
|
+ }
|
|
+ copy(hello.SessionId[:3], version[:])
|
|
|
|
//log.Debugln("REALITY hello.sessionId[:16]: %v", hello.SessionId[:16])
|
|
|
|
diff --git a/component/tls/reality_version_test.go b/component/tls/reality_version_test.go
|
|
new file mode 100644
|
|
index 00000000..31389118
|
|
--- /dev/null
|
|
+++ b/component/tls/reality_version_test.go
|
|
@@ -0,0 +1,81 @@
|
|
+package tls
|
|
+
|
|
+import (
|
|
+ "context"
|
|
+ "crypto/ecdh"
|
|
+ "crypto/rand"
|
|
+ "net"
|
|
+ "net/http"
|
|
+ "net/http/httptest"
|
|
+ "testing"
|
|
+ "time"
|
|
+
|
|
+ utls "github.com/metacubex/utls"
|
|
+)
|
|
+
|
|
+func TestRealityDefaultClientVersion(t *testing.T) {
|
|
+ if RealityDefaultClientVersion == [3]byte{} || RealityDefaultClientVersion == [3]byte{1, 8, 2} {
|
|
+ t.Fatal("the default must be a current Xray version, not upstream's 1.8.2")
|
|
+ }
|
|
+}
|
|
+
|
|
+// a REALITY server with minClientVer (as some panels set it): the reported client
|
|
+// version decides whether the handshake authenticates
|
|
+func TestRealityMinClientVer(t *testing.T) {
|
|
+ key, err := ecdh.X25519().GenerateKey(rand.Reader)
|
|
+ if err != nil {
|
|
+ t.Fatal(err)
|
|
+ }
|
|
+ // the "dest" site REALITY mirrors (and relays failed probes to)
|
|
+ dest := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
|
+ defer dest.Close()
|
|
+ var shortID [8]byte
|
|
+ copy(shortID[:], []byte{0x52, 0x97})
|
|
+ server := &utls.RealityConfig{
|
|
+ Type: "tcp",
|
|
+ Dest: dest.Listener.Addr().String(),
|
|
+ ServerNames: map[string]bool{"www.example.com": true},
|
|
+ PrivateKey: key.Bytes(),
|
|
+ ShortIds: map[[8]byte]bool{shortID: true},
|
|
+ DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
+ return (&net.Dialer{}).DialContext(ctx, network, address)
|
|
+ },
|
|
+ Log: t.Logf,
|
|
+ MinClientVer: []byte{25, 0, 0},
|
|
+ }
|
|
+ server.SessionTicketsDisabled = true
|
|
+ fp, _ := GetFingerprint("chrome")
|
|
+
|
|
+ try := func(version [3]byte) error {
|
|
+ c, s := net.Pipe()
|
|
+ defer c.Close()
|
|
+ defer s.Close()
|
|
+ go func() {
|
|
+ if conn, err := utls.RealityServer(context.Background(), s, server); err == nil {
|
|
+ conn.Write([]byte("ok"))
|
|
+ }
|
|
+ s.Close()
|
|
+ }()
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
+ defer cancel()
|
|
+ conn, err := GetRealityConn(ctx, c, fp, "www.example.com", &RealityConfig{
|
|
+ PublicKey: key.PublicKey(), ShortID: shortID, ClientVersion: version,
|
|
+ })
|
|
+ if err != nil {
|
|
+ return err
|
|
+ }
|
|
+ buf := make([]byte, 2)
|
|
+ _, err = conn.Read(buf)
|
|
+ return err
|
|
+ }
|
|
+
|
|
+ if err := try([3]byte{1, 8, 2}); err == nil {
|
|
+ t.Fatal("version 1.8.2 (upstream mihomo) passed a server with minClientVer 25.0.0")
|
|
+ }
|
|
+ if err := try([3]byte{}); err != nil {
|
|
+ t.Fatalf("the default version: %v", err)
|
|
+ }
|
|
+ if err := try([3]byte{25, 3, 6}); err != nil {
|
|
+ t.Fatalf("client-version 25.3.6: %v", err)
|
|
+ }
|
|
+}
|