0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-23 06:58:39 +00:00

Refactor post-handshake records detection & imitation

https://github.com/XTLS/Xray-core/issues/4778#issuecomment-2948185205
This commit is contained in:
RPRX 2025-06-06 06:46:02 +00:00 committed by GitHub
parent 967adadcc7
commit 21af070492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 54 deletions

View File

@ -6,76 +6,78 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
"net" "net"
"sync"
"time" "time"
"github.com/pires/go-proxyproto"
) )
func DetectRecordFingerprint(target string) ([]int, error) { var lock sync.Mutex
NetConn, err := net.Dial("tcp", target)
var PostHandshakeRecordsLen map[*Config]map[string][]int
func DetectPostHandshakeRecords(config *Config) {
lock.Lock()
if PostHandshakeRecordsLen == nil {
PostHandshakeRecordsLen = make(map[*Config]map[string][]int)
}
if PostHandshakeRecordsLen[config] == nil {
PostHandshakeRecordsLen[config] = make(map[string][]int)
for sni := range config.ServerNames {
target, err := net.Dial("tcp", config.Dest)
if err != nil { if err != nil {
return nil, err return
} }
conn := &detectConn{ if config.Xver == 1 || config.Xver == 2 {
Conn: NetConn, if _, err = proxyproto.HeaderProxyFromAddrs(config.Xver, target.LocalAddr(), target.RemoteAddr()).WriteTo(target); err != nil {
resultChan: make(chan []int, 1), return
} }
host, _, err := net.SplitHostPort(target)
if err != nil {
return nil, err
} }
tlsConfig := &tls.Config{ detectConn := &DetectConn{
ServerName: host, Conn: target,
config: config,
sni: sni,
} }
tlsConn := tls.Client(conn, tlsConfig) tlsConn := tls.Client(detectConn, &tls.Config{
err = tlsConn.Handshake() ServerName: sni,
if err != nil { })
return nil, err if err = tlsConn.Handshake(); err != nil {
return
} }
go func() {
io.Copy(io.Discard, tlsConn) io.Copy(io.Discard, tlsConn)
}()
select {
case result := <-conn.resultChan:
return result, nil
case <-time.After(2 * time.Second):
return nil, nil
} }
} }
lock.Unlock()
}
type detectConn struct { type DetectConn struct {
net.Conn net.Conn
config *Config
sni string
ccsSent bool ccsSent bool
done bool
resultChan chan ([]int)
} }
func (c *detectConn) Write(b []byte) (n int, err error) { func (c *DetectConn) Write(b []byte) (n int, err error) {
if len(b) >= 3 && bytes.Equal(b[:3], []byte{20, 3, 3}) { if len(b) >= 3 && bytes.Equal(b[:3], []byte{20, 3, 3}) {
c.ccsSent = true c.ccsSent = true
} }
return c.Conn.Write(b) return c.Conn.Write(b)
} }
func (c *detectConn) Read(b []byte) (n int, err error) { func (c *DetectConn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b) if !c.ccsSent {
if c.ccsSent && !c.done { return c.Conn.Read(b)
data := make([]byte, len(b))
copy(data, b)
var result []int
for {
if len(data) > 3 && bytes.Equal(data[:3], []byte{23, 3, 3}) {
length := int(binary.BigEndian.Uint16(data[3:5]))
if len(data) > length+5 {
result = append(result, int(length))
data = data[length+5:]
} }
c.Conn.SetReadDeadline(time.Now().Add(5 * time.Second))
data, _ := io.ReadAll(c.Conn)
for {
if len(data) >= 5 && bytes.Equal(data[:3], []byte{23, 3, 3}) {
length := int(binary.BigEndian.Uint16(data[3:5])) + 5
PostHandshakeRecordsLen[c.config][c.sni] = append(PostHandshakeRecordsLen[c.config][c.sni], length)
data = data[length:]
} else { } else {
break break
} }
} }
if len(result) != 1 { return 0, io.EOF
c.done = true
c.resultChan <- result
}
}
return n, err
} }

15
tls.go
View File

@ -126,6 +126,8 @@ func Value(vals ...byte) (value int) {
// The configuration config must be non-nil and must include // The configuration config must be non-nil and must include
// at least one certificate or else set GetCertificate. // at least one certificate or else set GetCertificate.
func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) { func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
DetectPostHandshakeRecords(config)
remoteAddr := conn.RemoteAddr().String() remoteAddr := conn.RemoteAddr().String()
if config.Show { if config.Show {
fmt.Printf("REALITY remoteAddr: %v\n", remoteAddr) fmt.Printf("REALITY remoteAddr: %v\n", remoteAddr)
@ -336,6 +338,19 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
if err != nil { if err != nil {
break break
} }
for _, length := range PostHandshakeRecordsLen[config][hs.clientHello.serverName] {
plainText := make([]byte, length-16)
plainText[0] = 23
plainText[1] = 3
plainText[2] = 3
plainText[3] = byte((length - 5) >> 8)
plainText[4] = byte((length - 5))
plainText[5] = 23
postHandshakeRecord := hs.c.out.cipher.(aead).Seal(plainText[:5], hs.c.out.seq[:], plainText[5:], plainText[:5])
hs.c.out.incSeq()
hs.c.write(postHandshakeRecord)
fmt.Printf("REALITY remoteAddr: %v\tlen(postHandshakeRecord): %v\n", remoteAddr, len(postHandshakeRecord))
}
hs.c.isHandshakeComplete.Store(true) hs.c.isHandshakeComplete.Store(true)
break break
} }