1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

add httpupgrade transport

This commit is contained in:
Shelikhoo 2023-10-27 02:57:23 +01:00
parent cb84b28de4
commit 1eefcd31f7
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
7 changed files with 359 additions and 0 deletions

View File

@ -74,6 +74,8 @@ import (
_ "github.com/v2fly/v2ray-core/v5/transport/internet/request/stereotype/meek"
_ "github.com/v2fly/v2ray-core/v5/transport/internet/httpupgrade"
// Transport headers
_ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/http"
_ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/noop"

View File

@ -0,0 +1,163 @@
package httpupgrade
import (
_ "github.com/v2fly/v2ray-core/v5/common/protoext"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Config struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Path string `protobuf:"bytes,1,opt,name=path,proto3" json:"path,omitempty"`
Host string `protobuf:"bytes,2,opt,name=host,proto3" json:"host,omitempty"`
}
func (x *Config) Reset() {
*x = Config{}
if protoimpl.UnsafeEnabled {
mi := &file_transport_internet_httpupgrade_config_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Config) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Config) ProtoMessage() {}
func (x *Config) ProtoReflect() protoreflect.Message {
mi := &file_transport_internet_httpupgrade_config_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Config.ProtoReflect.Descriptor instead.
func (*Config) Descriptor() ([]byte, []int) {
return file_transport_internet_httpupgrade_config_proto_rawDescGZIP(), []int{0}
}
func (x *Config) GetPath() string {
if x != nil {
return x.Path
}
return ""
}
func (x *Config) GetHost() string {
if x != nil {
return x.Host
}
return ""
}
var File_transport_internet_httpupgrade_config_proto protoreflect.FileDescriptor
var file_transport_internet_httpupgrade_config_proto_rawDesc = []byte{
0x0a, 0x2b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65,
0x72, 0x6e, 0x65, 0x74, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65,
0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x31, 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, 0x72, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65,
0x1a, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x65, 0x78,
0x74, 0x2f, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x22, 0x4e, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x0a, 0x04,
0x70, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68,
0x12, 0x12, 0x0a, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
0x68, 0x6f, 0x73, 0x74, 0x3a, 0x1c, 0x82, 0xb5, 0x18, 0x18, 0x0a, 0x09, 0x74, 0x72, 0x61, 0x6e,
0x73, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x0b, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67, 0x72, 0x61,
0x64, 0x65, 0x42, 0x9c, 0x01, 0x0a, 0x2d, 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, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70, 0x67,
0x72, 0x61, 0x64, 0x65, 0x50, 0x01, 0x5a, 0x3d, 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, 0x35, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74,
0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x75, 0x70,
0x67, 0x72, 0x61, 0x64, 0x65, 0xaa, 0x02, 0x29, 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, 0x48, 0x74, 0x74, 0x70, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64,
0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_transport_internet_httpupgrade_config_proto_rawDescOnce sync.Once
file_transport_internet_httpupgrade_config_proto_rawDescData = file_transport_internet_httpupgrade_config_proto_rawDesc
)
func file_transport_internet_httpupgrade_config_proto_rawDescGZIP() []byte {
file_transport_internet_httpupgrade_config_proto_rawDescOnce.Do(func() {
file_transport_internet_httpupgrade_config_proto_rawDescData = protoimpl.X.CompressGZIP(file_transport_internet_httpupgrade_config_proto_rawDescData)
})
return file_transport_internet_httpupgrade_config_proto_rawDescData
}
var file_transport_internet_httpupgrade_config_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_transport_internet_httpupgrade_config_proto_goTypes = []interface{}{
(*Config)(nil), // 0: v2ray.core.transport.internet.request.httpupgrade.Config
}
var file_transport_internet_httpupgrade_config_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_transport_internet_httpupgrade_config_proto_init() }
func file_transport_internet_httpupgrade_config_proto_init() {
if File_transport_internet_httpupgrade_config_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_transport_internet_httpupgrade_config_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Config); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_transport_internet_httpupgrade_config_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_transport_internet_httpupgrade_config_proto_goTypes,
DependencyIndexes: file_transport_internet_httpupgrade_config_proto_depIdxs,
MessageInfos: file_transport_internet_httpupgrade_config_proto_msgTypes,
}.Build()
File_transport_internet_httpupgrade_config_proto = out.File
file_transport_internet_httpupgrade_config_proto_rawDesc = nil
file_transport_internet_httpupgrade_config_proto_goTypes = nil
file_transport_internet_httpupgrade_config_proto_depIdxs = nil
}

View File

@ -0,0 +1,17 @@
syntax = "proto3";
package v2ray.core.transport.internet.request.httpupgrade;
option csharp_namespace = "V2Ray.Core.Transport.Internet.HttpUpgrade";
option go_package = "github.com/v2fly/v2ray-core/v5/transport/internet/httpupgrade";
option java_package = "com.v2ray.core.transport.internet.httpupgrade";
option java_multiple_files = true;
import "common/protoext/extensions.proto";
message Config {
option (v2ray.core.common.protoext.message_opt).type = "transport";
option (v2ray.core.common.protoext.message_opt).short_name = "httpupgrade";
string path = 1;
string host = 2;
}

View File

@ -0,0 +1,63 @@
package httpupgrade
import (
"bufio"
"context"
"net/http"
"strings"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/session"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/transportcommon"
)
func dialhttpUpgrade(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (net.Conn, error) {
transportConfiguration := streamSettings.ProtocolSettings.(*Config)
conn, err := transportcommon.DialWithSecuritySettings(ctx, dest, streamSettings)
if err != nil {
return nil, newError("failed to dial request to ", dest).Base(err)
}
req, err := http.NewRequest("GET", "/"+transportConfiguration.Path, nil)
if err != nil {
return nil, err
}
req.Header.Set("Connection", "upgrade")
req.Header.Set("Upgrade", "websocket")
req.Host = transportConfiguration.Host
err = req.Write(conn)
if err != nil {
return nil, err
}
// TODO The bufio usage here is unreliable
resp, err := http.ReadResponse(bufio.NewReader(conn), req) // nolint:bodyclose
if err != nil {
return nil, err
}
if resp.Status == "101 Switching Protocols" &&
strings.ToLower(resp.Header.Get("Upgrade")) == "websocket" &&
strings.ToLower(resp.Header.Get("Connection")) == "upgrade" {
return conn, nil
}
return nil, newError("unrecognized reply")
}
func dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := dialhttpUpgrade(ctx, dest, streamSettings)
if err != nil {
return nil, newError("failed to dial request to ", dest).Base(err)
}
return internet.Connection(conn), nil
}
func init() {
common.Must(internet.RegisterTransportDialer(protocolName, dial))
}

View File

@ -0,0 +1,9 @@
package httpupgrade
import "github.com/v2fly/v2ray-core/v5/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -0,0 +1,17 @@
package httpupgrade
import (
"context"
"github.com/v2fly/v2ray-core/v5/common"
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
const protocolName = "httpupgrade"
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return nil, newError("httpupgrade is a transport protocol.")
}))
}

View File

@ -0,0 +1,88 @@
package httpupgrade
import (
"bufio"
"context"
"net/http"
"strings"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/transportcommon"
)
type server struct {
addConn internet.ConnHandler
innnerListener net.Listener
}
func (s *server) Close() error {
return s.innnerListener.Close()
}
func (s *server) Addr() net.Addr {
return nil
}
func (s *server) Handle(conn net.Conn) (internet.Connection, error) {
connReader := bufio.NewReader(conn)
req, err := http.ReadRequest(connReader)
if err != nil {
return nil, err
}
connection := strings.ToLower(req.Header.Get("Connection"))
upgrade := strings.ToLower(req.Header.Get("Upgrade"))
if connection != "upgrade" || upgrade != "websocket" {
return nil, newError("unrecognized request")
}
resp := &http.Response{
Status: "101 Switching Protocols",
StatusCode: 101,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
}
resp.Header.Set("Connection", "upgrade")
resp.Header.Set("Upgrade", "websocket")
err = resp.Write(conn)
if err != nil {
return nil, err
}
return internet.Connection(conn), nil
}
func (s *server) keepAccepting() {
for {
conn, err := s.innnerListener.Accept()
if err != nil {
return
}
handledConn, err := s.Handle(conn)
if err != nil {
newError("failed to handle request").Base(err).WriteToLog()
common.Must(handledConn.Close())
continue
}
s.addConn(handledConn)
}
}
func listenHTTPUpgrade(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
transportConfiguration := streamSettings.ProtocolSettings.(*Config)
_ = transportConfiguration
serverInstance := &server{addConn: addConn}
listener, err := transportcommon.ListenWithSecuritySettings(ctx, address, port, streamSettings)
if err != nil {
return nil, newError("failed to listen on ", address, ":", port).Base(err)
}
serverInstance.innnerListener = listener
go serverInstance.keepAccepting()
return serverInstance, nil
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, listenHTTPUpgrade))
}