1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-10 07:45:54 -04:00

Merge branch 'dev-wsearlydatadiverse'

This commit is contained in:
Shelikhoo
2021-05-04 22:50:06 +01:00
10 changed files with 312 additions and 32 deletions

View File

@@ -86,6 +86,7 @@ type Config struct {
AcceptProxyProtocol bool `protobuf:"varint,4,opt,name=accept_proxy_protocol,json=acceptProxyProtocol,proto3" json:"accept_proxy_protocol,omitempty"`
MaxEarlyData int32 `protobuf:"varint,5,opt,name=max_early_data,json=maxEarlyData,proto3" json:"max_early_data,omitempty"`
UseBrowserForwarding bool `protobuf:"varint,6,opt,name=use_browser_forwarding,json=useBrowserForwarding,proto3" json:"use_browser_forwarding,omitempty"`
EarlyDataHeaderName string `protobuf:"bytes,7,opt,name=early_data_header_name,json=earlyDataHeaderName,proto3" json:"early_data_header_name,omitempty"`
}
func (x *Config) Reset() {
@@ -155,6 +156,13 @@ func (x *Config) GetUseBrowserForwarding() bool {
return false
}
func (x *Config) GetEarlyDataHeaderName() string {
if x != nil {
return x.EarlyDataHeaderName
}
return ""
}
var File_transport_internet_websocket_config_proto protoreflect.FileDescriptor
var file_transport_internet_websocket_config_proto_rawDesc = []byte{
@@ -166,7 +174,7 @@ var file_transport_internet_websocket_config_proto_rawDesc = []byte{
0x63, 0x6b, 0x65, 0x74, 0x22, 0x30, 0x0a, 0x06, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x10,
0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79,
0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xfb, 0x01, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69,
0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xb0, 0x02, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69,
0x67, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x47, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18,
0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2f, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f,
@@ -181,18 +189,21 @@ var file_transport_internet_websocket_config_proto_rawDesc = []byte{
0x61, 0x72, 0x6c, 0x79, 0x44, 0x61, 0x74, 0x61, 0x12, 0x34, 0x0a, 0x16, 0x75, 0x73, 0x65, 0x5f,
0x62, 0x72, 0x6f, 0x77, 0x73, 0x65, 0x72, 0x5f, 0x66, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x69,
0x6e, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x14, 0x75, 0x73, 0x65, 0x42, 0x72, 0x6f,
0x77, 0x73, 0x65, 0x72, 0x46, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x69, 0x6e, 0x67, 0x4a, 0x04,
0x08, 0x01, 0x10, 0x02, 0x42, 0x96, 0x01, 0x0a, 0x2b, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72,
0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72,
0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x77, 0x65, 0x62, 0x73, 0x6f,
0x63, 0x6b, 0x65, 0x74, 0x50, 0x01, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, 0x63,
0x6f, 0x72, 0x65, 0x2f, 0x76, 0x34, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74,
0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63,
0x6b, 0x65, 0x74, 0xaa, 0x02, 0x27, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65,
0x2e, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x49, 0x6e, 0x74, 0x65, 0x72,
0x6e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
0x77, 0x73, 0x65, 0x72, 0x46, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x69, 0x6e, 0x67, 0x12, 0x33,
0x0a, 0x16, 0x65, 0x61, 0x72, 0x6c, 0x79, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x68, 0x65, 0x61,
0x64, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x13,
0x65, 0x61, 0x72, 0x6c, 0x79, 0x44, 0x61, 0x74, 0x61, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x4e,
0x61, 0x6d, 0x65, 0x4a, 0x04, 0x08, 0x01, 0x10, 0x02, 0x42, 0x96, 0x01, 0x0a, 0x2b, 0x63, 0x6f,
0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x74, 0x72, 0x61,
0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e,
0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x50, 0x01, 0x5a, 0x3b, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32,
0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x34, 0x2f, 0x74, 0x72, 0x61, 0x6e,
0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x77,
0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0xaa, 0x02, 0x27, 0x56, 0x32, 0x52, 0x61, 0x79,
0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e,
0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b,
0x65, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@@ -24,4 +24,6 @@ message Config {
int32 max_early_data = 5;
bool use_browser_forwarding = 6;
string early_data_header_name = 7;
}

View File

@@ -7,15 +7,17 @@ import (
"context"
"encoding/base64"
"io"
"net/http"
"time"
"github.com/v2fly/v2ray-core/v4/features/extension"
"github.com/gorilla/websocket"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/features/extension"
"github.com/v2fly/v2ray-core/v4/transport/internet"
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
)
@@ -90,7 +92,7 @@ func dialWebsocket(ctx context.Context, dest net.Destination, streamSettings *in
}), nil
}
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader())
conn, resp, err := dialer.Dial(uri, wsSettings.GetRequestHeader()) // nolint: bodyclose
if err != nil {
var reason string
if resp != nil {
@@ -123,7 +125,20 @@ func (d dialerWithEarlyData) Dial(earlyData []byte) (*websocket.Conn, error) {
return nil, newError("websocket delayed dialer cannot encode early data tail").Base(errc)
}
conn, resp, err := d.dialer.Dial(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
dialFunction := func() (*websocket.Conn, *http.Response, error) {
return d.dialer.Dial(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
}
if d.config.EarlyDataHeaderName != "" {
dialFunction = func() (*websocket.Conn, *http.Response, error) {
earlyDataStr := earlyDataBuf.String()
currentHeader := d.config.GetRequestHeader()
currentHeader.Set(d.config.EarlyDataHeaderName, earlyDataStr)
return d.dialer.Dial(d.uriBase, currentHeader)
}
}
conn, resp, err := dialFunction() // nolint: bodyclose
if err != nil {
var reason string
if resp != nil {
@@ -160,7 +175,18 @@ func (d dialerWithEarlyDataRelayed) Dial(earlyData []byte) (io.ReadWriteCloser,
return nil, newError("websocket delayed dialer cannot encode early data tail").Base(errc)
}
conn, err := d.forwarder.DialWebsocket(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
dialFunction := func() (io.ReadWriteCloser, error) {
return d.forwarder.DialWebsocket(d.uriBase+earlyDataBuf.String(), d.config.GetRequestHeader())
}
if d.config.EarlyDataHeaderName != "" {
earlyDataStr := earlyDataBuf.String()
currentHeader := d.config.GetRequestHeader()
currentHeader.Set(d.config.EarlyDataHeaderName, earlyDataStr)
return d.forwarder.DialWebsocket(d.uriBase, currentHeader)
}
conn, err := dialFunction()
if err != nil {
var reason string
return nil, newError("failed to dial to (", d.uriBase, ") with early data: ", reason).Base(err)

View File

@@ -24,9 +24,10 @@ import (
)
type requestHandler struct {
path string
ln *Listener
earlyDataEnabled bool
path string
ln *Listener
earlyDataEnabled bool
earlyDataHeaderName string
}
var upgrader = &websocket.Upgrader{
@@ -40,11 +41,18 @@ var upgrader = &websocket.Upgrader{
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var earlyData io.Reader
if !h.earlyDataEnabled {
if !h.earlyDataEnabled { // nolint: gocritic
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
} else if h.earlyDataHeaderName != "" {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
earlyDataStr := request.Header.Get(h.earlyDataHeaderName)
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else {
if strings.HasPrefix(request.URL.RequestURI(), h.path) {
earlyDataStr := request.URL.RequestURI()[len(h.path):]
@@ -135,18 +143,21 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
l.listener = listener
var useEarlyData = false
var earlyDataHeaderName = ""
if wsSettings.MaxEarlyData != 0 {
useEarlyData = true
earlyDataHeaderName = wsSettings.EarlyDataHeaderName
}
l.server = http.Server{
Handler: &requestHandler{
path: wsSettings.GetNormalizedPath(),
ln: l,
earlyDataEnabled: useEarlyData,
path: wsSettings.GetNormalizedPath(),
ln: l,
earlyDataEnabled: useEarlyData,
earlyDataHeaderName: earlyDataHeaderName,
},
ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 2048,
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
}
go func() {