mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-15 01:09:20 +02:00
Add H2 path support for fallback
This commit is contained in:
parent
e13f9f59da
commit
4bec9ab845
|
@ -1,7 +1,10 @@
|
|||
package trojan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/hpack"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -413,6 +416,11 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||
break
|
||||
}
|
||||
}
|
||||
} else if firstLen >= 18 && first.Byte(4) == '*' { // process h2c
|
||||
h2path := extractPathFromH2Request(connection)
|
||||
if h2path != "" {
|
||||
path = h2path
|
||||
}
|
||||
}
|
||||
}
|
||||
fb := pfb[path]
|
||||
|
@ -520,3 +528,37 @@ func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.S
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package inbound
|
|||
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
gotls "crypto/tls"
|
||||
|
@ -13,6 +14,9 @@ import (
|
|||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/hpack"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
|
@ -305,6 +309,11 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||
break
|
||||
}
|
||||
}
|
||||
} else if firstLen >= 18 && first.Byte(4) == '*' { // process h2c
|
||||
h2path := extractPathFromH2Request(connection)
|
||||
if h2path != "" {
|
||||
path = h2path
|
||||
}
|
||||
}
|
||||
}
|
||||
fb := pfb[path]
|
||||
|
@ -583,3 +592,37 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue