mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-08-22 17:46:48 +08:00
Compare commits
2 Commits
v25.3.6
...
fallback-h
Author | SHA1 | Date | |
---|---|---|---|
![]() |
600ee0ed1a | ||
![]() |
4bec9ab845 |
@@ -1,7 +1,10 @@
|
|||||||
package trojan
|
package trojan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
"golang.org/x/net/http2/hpack"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -346,14 +349,14 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||||||
cs := tlsConn.ConnectionState()
|
cs := tlsConn.ConnectionState()
|
||||||
name = cs.ServerName
|
name = cs.ServerName
|
||||||
alpn = cs.NegotiatedProtocol
|
alpn = cs.NegotiatedProtocol
|
||||||
errors.LogInfo(ctx, "realName = " + name)
|
errors.LogInfo(ctx, "realName = "+name)
|
||||||
errors.LogInfo(ctx, "realAlpn = " + alpn)
|
errors.LogInfo(ctx, "realAlpn = "+alpn)
|
||||||
} else if realityConn, ok := iConn.(*reality.Conn); ok {
|
} else if realityConn, ok := iConn.(*reality.Conn); ok {
|
||||||
cs := realityConn.ConnectionState()
|
cs := realityConn.ConnectionState()
|
||||||
name = cs.ServerName
|
name = cs.ServerName
|
||||||
alpn = cs.NegotiatedProtocol
|
alpn = cs.NegotiatedProtocol
|
||||||
errors.LogInfo(ctx, "realName = " + name)
|
errors.LogInfo(ctx, "realName = "+name)
|
||||||
errors.LogInfo(ctx, "realAlpn = " + alpn)
|
errors.LogInfo(ctx, "realAlpn = "+alpn)
|
||||||
}
|
}
|
||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
alpn = strings.ToLower(alpn)
|
alpn = strings.ToLower(alpn)
|
||||||
@@ -403,7 +406,7 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||||||
}
|
}
|
||||||
if k == '?' || k == ' ' {
|
if k == '?' || k == ' ' {
|
||||||
path = string(firstBytes[i:j])
|
path = string(firstBytes[i:j])
|
||||||
errors.LogInfo(ctx, "realPath = " + path)
|
errors.LogInfo(ctx, "realPath = "+path)
|
||||||
if pfb[path] == nil {
|
if pfb[path] == nil {
|
||||||
path = ""
|
path = ""
|
||||||
}
|
}
|
||||||
@@ -413,6 +416,11 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if firstLen >= 18 && first.Byte(4) == '*' { // process h2c
|
||||||
|
h2path := extractPathFromH2Request(connection)
|
||||||
|
if h2path != "" {
|
||||||
|
path = h2path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fb := pfb[path]
|
fb := pfb[path]
|
||||||
@@ -520,3 +528,37 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get path form http2
|
||||||
|
func extractPathFromH2Request(conn stat.Connection) string {
|
||||||
|
reader := bufio.NewReader(conn)
|
||||||
|
framer := http2.NewFramer(conn, reader)
|
||||||
|
|
||||||
|
for {
|
||||||
|
frame, err := framer.ReadFrame()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// find headers frame
|
||||||
|
if f, ok := frame.(*http2.HeadersFrame); ok {
|
||||||
|
decoder := hpack.NewDecoder(4096, func(hf hpack.HeaderField) {})
|
||||||
|
headerBlock := f.HeaderBlockFragment()
|
||||||
|
|
||||||
|
hf, err := decoder.DecodeFull(headerBlock)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
path := func(headers []hpack.HeaderField) string {
|
||||||
|
for _, header := range headers {
|
||||||
|
if header.Name == ":path" {
|
||||||
|
return header.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return path(hf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -13,6 +13,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
"golang.org/x/net/http2/hpack"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common"
|
"github.com/xtls/xray-core/common"
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
@@ -223,14 +226,14 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||||||
cs := tlsConn.ConnectionState()
|
cs := tlsConn.ConnectionState()
|
||||||
name = cs.ServerName
|
name = cs.ServerName
|
||||||
alpn = cs.NegotiatedProtocol
|
alpn = cs.NegotiatedProtocol
|
||||||
errors.LogInfo(ctx, "realName = " + name)
|
errors.LogInfo(ctx, "realName = "+name)
|
||||||
errors.LogInfo(ctx, "realAlpn = " + alpn)
|
errors.LogInfo(ctx, "realAlpn = "+alpn)
|
||||||
} else if realityConn, ok := iConn.(*reality.Conn); ok {
|
} else if realityConn, ok := iConn.(*reality.Conn); ok {
|
||||||
cs := realityConn.ConnectionState()
|
cs := realityConn.ConnectionState()
|
||||||
name = cs.ServerName
|
name = cs.ServerName
|
||||||
alpn = cs.NegotiatedProtocol
|
alpn = cs.NegotiatedProtocol
|
||||||
errors.LogInfo(ctx, "realName = " + name)
|
errors.LogInfo(ctx, "realName = "+name)
|
||||||
errors.LogInfo(ctx, "realAlpn = " + alpn)
|
errors.LogInfo(ctx, "realAlpn = "+alpn)
|
||||||
}
|
}
|
||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
alpn = strings.ToLower(alpn)
|
alpn = strings.ToLower(alpn)
|
||||||
@@ -295,7 +298,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||||||
}
|
}
|
||||||
if k == '?' || k == ' ' {
|
if k == '?' || k == ' ' {
|
||||||
path = string(firstBytes[i:j])
|
path = string(firstBytes[i:j])
|
||||||
errors.LogInfo(ctx, "realPath = " + path)
|
errors.LogInfo(ctx, "realPath = "+path)
|
||||||
if pfb[path] == nil {
|
if pfb[path] == nil {
|
||||||
path = ""
|
path = ""
|
||||||
}
|
}
|
||||||
@@ -305,9 +308,15 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if firstLen >= 18 && first.Byte(4) == '*' { // process h2c
|
||||||
|
h2path := extractPathFromH2Request(connection)
|
||||||
|
if h2path != "" {
|
||||||
|
path = h2path
|
||||||
|
errors.LogInfo(ctx, "realPath = "+path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fb := pfb[path]
|
fb := prefixMatch(pfb, path)
|
||||||
if fb == nil {
|
if fb == nil {
|
||||||
return errors.New(`failed to find the default "path" config`).AtWarning()
|
return errors.New(`failed to find the default "path" config`).AtWarning()
|
||||||
}
|
}
|
||||||
@@ -583,3 +592,54 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get path form http2
|
||||||
|
func extractPathFromH2Request(conn stat.Connection) string {
|
||||||
|
framer := http2.NewFramer(io.Discard, conn)
|
||||||
|
|
||||||
|
for {
|
||||||
|
frame, err := framer.ReadFrame()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// find headers frame
|
||||||
|
if f, ok := frame.(*http2.HeadersFrame); ok {
|
||||||
|
decoder := hpack.NewDecoder(4096, func(hf hpack.HeaderField) {})
|
||||||
|
headerBlock := f.HeaderBlockFragment()
|
||||||
|
|
||||||
|
hf, err := decoder.DecodeFull(headerBlock)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
path := func(headers []hpack.HeaderField) string {
|
||||||
|
for _, header := range headers {
|
||||||
|
if header.Name == ":path" {
|
||||||
|
return header.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return path(hf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prefixMatch(pfb map[string]*Fallback, path string) *Fallback {
|
||||||
|
for {
|
||||||
|
if val, exists := pfb[path]; exists {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
if path == "/" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
path = strings.TrimSuffix(path, "/")
|
||||||
|
if idx := strings.LastIndex(path, "/"); idx != -1 {
|
||||||
|
path = path[:idx]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user