mirror of
https://github.com/XTLS/REALITY.git
synced 2025-08-22 22:48:36 +00:00
Refactor post-handshake records detection & imitation
https://github.com/XTLS/Xray-core/issues/4778#issuecomment-2948185205
This commit is contained in:
parent
967adadcc7
commit
21af070492
110
record_detect.go
110
record_detect.go
@ -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)
|
|
||||||
if err != nil {
|
var PostHandshakeRecordsLen map[*Config]map[string][]int
|
||||||
return nil, err
|
|
||||||
|
func DetectPostHandshakeRecords(config *Config) {
|
||||||
|
lock.Lock()
|
||||||
|
if PostHandshakeRecordsLen == nil {
|
||||||
|
PostHandshakeRecordsLen = make(map[*Config]map[string][]int)
|
||||||
}
|
}
|
||||||
conn := &detectConn{
|
if PostHandshakeRecordsLen[config] == nil {
|
||||||
Conn: NetConn,
|
PostHandshakeRecordsLen[config] = make(map[string][]int)
|
||||||
resultChan: make(chan []int, 1),
|
for sni := range config.ServerNames {
|
||||||
}
|
target, err := net.Dial("tcp", config.Dest)
|
||||||
host, _, err := net.SplitHostPort(target)
|
if err != nil {
|
||||||
if err != nil {
|
return
|
||||||
return nil, err
|
}
|
||||||
}
|
if config.Xver == 1 || config.Xver == 2 {
|
||||||
tlsConfig := &tls.Config{
|
if _, err = proxyproto.HeaderProxyFromAddrs(config.Xver, target.LocalAddr(), target.RemoteAddr()).WriteTo(target); err != nil {
|
||||||
ServerName: host,
|
return
|
||||||
}
|
}
|
||||||
tlsConn := tls.Client(conn, tlsConfig)
|
}
|
||||||
err = tlsConn.Handshake()
|
detectConn := &DetectConn{
|
||||||
if err != nil {
|
Conn: target,
|
||||||
return nil, err
|
config: config,
|
||||||
}
|
sni: sni,
|
||||||
go func() {
|
}
|
||||||
io.Copy(io.Discard, tlsConn)
|
tlsConn := tls.Client(detectConn, &tls.Config{
|
||||||
}()
|
ServerName: sni,
|
||||||
select {
|
})
|
||||||
case result := <-conn.resultChan:
|
if err = tlsConn.Handshake(); err != nil {
|
||||||
return result, nil
|
return
|
||||||
case <-time.After(2 * time.Second):
|
}
|
||||||
return nil, nil
|
io.Copy(io.Discard, tlsConn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
lock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
type detectConn struct {
|
type DetectConn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
ccsSent bool
|
config *Config
|
||||||
done bool
|
sni string
|
||||||
resultChan chan ([]int)
|
ccsSent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
c.Conn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||||
var result []int
|
data, _ := io.ReadAll(c.Conn)
|
||||||
for {
|
for {
|
||||||
if len(data) > 3 && bytes.Equal(data[:3], []byte{23, 3, 3}) {
|
if len(data) >= 5 && bytes.Equal(data[:3], []byte{23, 3, 3}) {
|
||||||
length := int(binary.BigEndian.Uint16(data[3:5]))
|
length := int(binary.BigEndian.Uint16(data[3:5])) + 5
|
||||||
if len(data) > length+5 {
|
PostHandshakeRecordsLen[c.config][c.sni] = append(PostHandshakeRecordsLen[c.config][c.sni], length)
|
||||||
result = append(result, int(length))
|
data = data[length:]
|
||||||
data = data[length+5:]
|
} else {
|
||||||
}
|
break
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(result) != 1 {
|
|
||||||
c.done = true
|
|
||||||
c.resultChan <- result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return n, err
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
15
tls.go
15
tls.go
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user