mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-12-01 06:08:37 +00:00
Direct/Freedom outbound: Rename finalRules to firewall; Add reply to firewall; Merge Block/Blackhole outbound; Move proxyProtocol to "proxy-protocol" TCP finalmask (#7281)
https://github.com/XTLS/Xray-core/pull/7238#issuecomment-6467092152 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6484050347 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6484439860 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6621021705 https://github.com/XTLS/Xray-core/pull/7329#issuecomment-6623818990 https://github.com/XTLS/Xray-core/pull/7329#issuecomment-6624149740 https://github.com/XTLS/Xray-core/pull/7329#issuecomment-6625219097 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6627156942 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6850640864 https://github.com/XTLS/Xray-core/pull/7281#issuecomment-6973501579 --------- Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package proxyprotocol
|
||||
|
||||
import (
|
||||
"github.com/pires/go-proxyproto"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/transport/internet/finalmask"
|
||||
)
|
||||
|
||||
func (c *Config) WrapConnClient(conn net.Conn, dest *net.Destination, dialer *finalmask.Dialer) (net.Conn, error) {
|
||||
version := c.Version
|
||||
if version == 0 {
|
||||
version = 2
|
||||
}
|
||||
if version != 1 && version != 2 {
|
||||
return nil, errors.New("PROXY protocol version must be 1 or 2")
|
||||
}
|
||||
source := conn.LocalAddr()
|
||||
if dialer != nil && dialer.Source != nil {
|
||||
source = dialer.Source
|
||||
}
|
||||
header := proxyproto.HeaderProxyFromAddrs(byte(version), source, conn.RemoteAddr())
|
||||
if _, err := header.WriteTo(conn); err != nil {
|
||||
return nil, errors.New("failed to write PROXY protocol header").Base(err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (c *Config) WrapConnServer(conn net.Conn) (net.Conn, error) {
|
||||
return proxyproto.NewConn(conn, proxyproto.WithPolicy(proxyproto.REQUIRE)), nil
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.12
|
||||
// protoc (unknown)
|
||||
// source: transport/internet/finalmask/proxyprotocol/config.proto
|
||||
|
||||
package proxyprotocol
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
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 `protogen:"open.v1"`
|
||||
Version uint32 `protobuf:"varint,1,opt,name=version,proto3" json:"version,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *Config) Reset() {
|
||||
*x = Config{}
|
||||
mi := &file_transport_internet_finalmask_proxyprotocol_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_finalmask_proxyprotocol_config_proto_msgTypes[0]
|
||||
if 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_finalmask_proxyprotocol_config_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Config) GetVersion() uint32 {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_transport_internet_finalmask_proxyprotocol_config_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_transport_internet_finalmask_proxyprotocol_config_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"7transport/internet/finalmask/proxyprotocol/config.proto\x12/xray.transport.internet.finalmask.proxyprotocol\"\"\n" +
|
||||
"\x06Config\x12\x18\n" +
|
||||
"\aversion\x18\x01 \x01(\rR\aversionB\xaf\x01\n" +
|
||||
"3com.xray.transport.internet.finalmask.proxyprotocolP\x01ZDgithub.com/xtls/xray-core/transport/internet/finalmask/proxyprotocol\xaa\x02/Xray.Transport.Internet.Finalmask.ProxyProtocolb\x06proto3"
|
||||
|
||||
var (
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescOnce sync.Once
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescGZIP() []byte {
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescOnce.Do(func() {
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_transport_internet_finalmask_proxyprotocol_config_proto_rawDesc), len(file_transport_internet_finalmask_proxyprotocol_config_proto_rawDesc)))
|
||||
})
|
||||
return file_transport_internet_finalmask_proxyprotocol_config_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_transport_internet_finalmask_proxyprotocol_config_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
|
||||
var file_transport_internet_finalmask_proxyprotocol_config_proto_goTypes = []any{
|
||||
(*Config)(nil), // 0: xray.transport.internet.finalmask.proxyprotocol.Config
|
||||
}
|
||||
var file_transport_internet_finalmask_proxyprotocol_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_finalmask_proxyprotocol_config_proto_init() }
|
||||
func file_transport_internet_finalmask_proxyprotocol_config_proto_init() {
|
||||
if File_transport_internet_finalmask_proxyprotocol_config_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_transport_internet_finalmask_proxyprotocol_config_proto_rawDesc), len(file_transport_internet_finalmask_proxyprotocol_config_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 1,
|
||||
NumExtensions: 0,
|
||||
NumServices: 0,
|
||||
},
|
||||
GoTypes: file_transport_internet_finalmask_proxyprotocol_config_proto_goTypes,
|
||||
DependencyIndexes: file_transport_internet_finalmask_proxyprotocol_config_proto_depIdxs,
|
||||
MessageInfos: file_transport_internet_finalmask_proxyprotocol_config_proto_msgTypes,
|
||||
}.Build()
|
||||
File_transport_internet_finalmask_proxyprotocol_config_proto = out.File
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_goTypes = nil
|
||||
file_transport_internet_finalmask_proxyprotocol_config_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package xray.transport.internet.finalmask.proxyprotocol;
|
||||
option csharp_namespace = "Xray.Transport.Internet.Finalmask.ProxyProtocol";
|
||||
option go_package = "github.com/xtls/xray-core/transport/internet/finalmask/proxyprotocol";
|
||||
option java_package = "com.xray.transport.internet.finalmask.proxyprotocol";
|
||||
option java_multiple_files = true;
|
||||
|
||||
message Config {
|
||||
uint32 version = 1;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package proxyprotocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pires/go-proxyproto"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/session"
|
||||
"github.com/xtls/xray-core/transport/internet/finalmask"
|
||||
)
|
||||
|
||||
func TestClientHeader(t *testing.T) {
|
||||
for _, version := range []uint32{0, 1, 2} {
|
||||
for _, sourceIP := range []string{"", "198.51.100.7", "2001:db8::7"} {
|
||||
t.Run(fmt.Sprintf("version=%d/source=%s", version, sourceIP), func(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
ctx := context.Background()
|
||||
if sourceIP != "" {
|
||||
ctx = session.ContextWithInbound(ctx, &session.Inbound{
|
||||
Source: net.TCPDestination(net.ParseAddress(sourceIP), 12345),
|
||||
})
|
||||
}
|
||||
fm := finalmask.NewFinalMask([]finalmask.TCPMask{&Config{Version: version}}, nil,
|
||||
func(ctx context.Context, dest net.Destination) (net.Conn, error) {
|
||||
return net.Dial("tcp", dest.NetAddr())
|
||||
}, func(ctx context.Context, addr net.Addr) (net.Listener, error) {
|
||||
return listener, nil
|
||||
}, nil, nil)
|
||||
maskedListener, err := fm.Listen(context.Background(), listener.Addr())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client, err := fm.DialTCP(ctx, net.DestinationFromAddr(listener.Addr()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
server, err := maskedListener.Accept()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
if err := client.SetDeadline(deadline); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := server.SetDeadline(deadline); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
proxyConn, ok := server.(*proxyproto.Conn)
|
||||
if !ok {
|
||||
t.Fatalf("server connection type = %T, want *proxyproto.Conn", server)
|
||||
}
|
||||
header := proxyConn.ProxyHeader()
|
||||
wantVersion := byte(version)
|
||||
if wantVersion == 0 {
|
||||
wantVersion = 2
|
||||
}
|
||||
if header == nil || header.Version != wantVersion || header.Command != proxyproto.PROXY {
|
||||
t.Fatalf("unexpected PROXY header: %v", header)
|
||||
}
|
||||
wantSource := client.LocalAddr()
|
||||
if sourceIP != "" {
|
||||
wantSource = &net.TCPAddr{IP: net.ParseIP(sourceIP), Port: 12345}
|
||||
}
|
||||
if server.RemoteAddr().String() != wantSource.String() || server.LocalAddr().String() != listener.Addr().String() {
|
||||
t.Fatalf("addresses = %v -> %v, want %v -> %v", server.RemoteAddr(), server.LocalAddr(), wantSource, listener.Addr())
|
||||
}
|
||||
for _, pair := range [][2]net.Conn{{client, server}, {client, server}, {server, client}} {
|
||||
const payload = "application payload"
|
||||
if _, err := io.WriteString(pair[0], payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := make([]byte, len(payload))
|
||||
if _, err := io.ReadFull(pair[1], data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != payload {
|
||||
t.Fatalf("payload = %q", data)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user