prototype for marking connection

This commit is contained in:
Darien Raymond 2018-09-06 10:06:57 +02:00
parent cfe7e7843b
commit af3a08a848
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 171 additions and 34 deletions

View File

@ -48,7 +48,7 @@ func (x TransportProtocol) String() string {
return proto.EnumName(TransportProtocol_name, int32(x))
}
func (TransportProtocol) EnumDescriptor() ([]byte, []int) {
return fileDescriptor_config_59931ebeb80dc13e, []int{0}
return fileDescriptor_config_807c5df32db81c88, []int{0}
}
type TransportConfig struct {
@ -68,7 +68,7 @@ func (m *TransportConfig) Reset() { *m = TransportConfig{} }
func (m *TransportConfig) String() string { return proto.CompactTextString(m) }
func (*TransportConfig) ProtoMessage() {}
func (*TransportConfig) Descriptor() ([]byte, []int) {
return fileDescriptor_config_59931ebeb80dc13e, []int{0}
return fileDescriptor_config_807c5df32db81c88, []int{0}
}
func (m *TransportConfig) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_TransportConfig.Unmarshal(m, b)
@ -119,6 +119,7 @@ type StreamConfig struct {
SecurityType string `protobuf:"bytes,3,opt,name=security_type,json=securityType,proto3" json:"security_type,omitempty"`
// Settings for transport security. For now the only choice is TLS.
SecuritySettings []*serial.TypedMessage `protobuf:"bytes,4,rep,name=security_settings,json=securitySettings,proto3" json:"security_settings,omitempty"`
SocketSettings *SocketConfig `protobuf:"bytes,6,opt,name=socket_settings,json=socketSettings,proto3" json:"socket_settings,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -128,7 +129,7 @@ func (m *StreamConfig) Reset() { *m = StreamConfig{} }
func (m *StreamConfig) String() string { return proto.CompactTextString(m) }
func (*StreamConfig) ProtoMessage() {}
func (*StreamConfig) Descriptor() ([]byte, []int) {
return fileDescriptor_config_59931ebeb80dc13e, []int{1}
return fileDescriptor_config_807c5df32db81c88, []int{1}
}
func (m *StreamConfig) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_StreamConfig.Unmarshal(m, b)
@ -184,6 +185,13 @@ func (m *StreamConfig) GetSecuritySettings() []*serial.TypedMessage {
return nil
}
func (m *StreamConfig) GetSocketSettings() *SocketConfig {
if m != nil {
return m.SocketSettings
}
return nil
}
type ProxyConfig struct {
Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
@ -195,7 +203,7 @@ func (m *ProxyConfig) Reset() { *m = ProxyConfig{} }
func (m *ProxyConfig) String() string { return proto.CompactTextString(m) }
func (*ProxyConfig) ProtoMessage() {}
func (*ProxyConfig) Descriptor() ([]byte, []int) {
return fileDescriptor_config_59931ebeb80dc13e, []int{2}
return fileDescriptor_config_807c5df32db81c88, []int{2}
}
func (m *ProxyConfig) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ProxyConfig.Unmarshal(m, b)
@ -222,44 +230,85 @@ func (m *ProxyConfig) GetTag() string {
return ""
}
type SocketConfig struct {
Mark int32 `protobuf:"varint,1,opt,name=mark,proto3" json:"mark,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *SocketConfig) Reset() { *m = SocketConfig{} }
func (m *SocketConfig) String() string { return proto.CompactTextString(m) }
func (*SocketConfig) ProtoMessage() {}
func (*SocketConfig) Descriptor() ([]byte, []int) {
return fileDescriptor_config_807c5df32db81c88, []int{3}
}
func (m *SocketConfig) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_SocketConfig.Unmarshal(m, b)
}
func (m *SocketConfig) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_SocketConfig.Marshal(b, m, deterministic)
}
func (dst *SocketConfig) XXX_Merge(src proto.Message) {
xxx_messageInfo_SocketConfig.Merge(dst, src)
}
func (m *SocketConfig) XXX_Size() int {
return xxx_messageInfo_SocketConfig.Size(m)
}
func (m *SocketConfig) XXX_DiscardUnknown() {
xxx_messageInfo_SocketConfig.DiscardUnknown(m)
}
var xxx_messageInfo_SocketConfig proto.InternalMessageInfo
func (m *SocketConfig) GetMark() int32 {
if m != nil {
return m.Mark
}
return 0
}
func init() {
proto.RegisterType((*TransportConfig)(nil), "v2ray.core.transport.internet.TransportConfig")
proto.RegisterType((*StreamConfig)(nil), "v2ray.core.transport.internet.StreamConfig")
proto.RegisterType((*ProxyConfig)(nil), "v2ray.core.transport.internet.ProxyConfig")
proto.RegisterType((*SocketConfig)(nil), "v2ray.core.transport.internet.SocketConfig")
proto.RegisterEnum("v2ray.core.transport.internet.TransportProtocol", TransportProtocol_name, TransportProtocol_value)
}
func init() {
proto.RegisterFile("v2ray.com/core/transport/internet/config.proto", fileDescriptor_config_59931ebeb80dc13e)
proto.RegisterFile("v2ray.com/core/transport/internet/config.proto", fileDescriptor_config_807c5df32db81c88)
}
var fileDescriptor_config_59931ebeb80dc13e = []byte{
// 419 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x91, 0xd1, 0x8a, 0x13, 0x31,
0x14, 0x86, 0xcd, 0x4c, 0x57, 0xdb, 0xd3, 0xae, 0xa6, 0xb9, 0x2a, 0xc2, 0x62, 0xad, 0x20, 0xc5,
0x8b, 0xcc, 0x32, 0xbe, 0x41, 0xbb, 0x17, 0x8a, 0xae, 0x86, 0xe9, 0xa8, 0xb0, 0x20, 0x25, 0x1b,
0x63, 0x19, 0xdc, 0x24, 0x25, 0x13, 0xc5, 0x79, 0x25, 0x9f, 0xc4, 0xa7, 0xf0, 0x59, 0x24, 0x33,
0x93, 0xb0, 0xa8, 0x94, 0x82, 0x77, 0x87, 0x9c, 0x3f, 0xff, 0x39, 0xdf, 0x7f, 0x80, 0x7e, 0xcb,
0x2d, 0x6f, 0xa8, 0x30, 0x2a, 0x13, 0xc6, 0xca, 0xcc, 0x59, 0xae, 0xeb, 0xbd, 0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d7, 0x6c, 0xfd, 0x45, 0x43, 0x9e, 0xe1, 0xd1, 0xa7, 0x43, 0x36, 0x30, 0x8d,
0xa2, 0xb8, 0xc2, 0xa0, 0x5d, 0xe1, 0xd8, 0x60, 0x71, 0x30, 0x08, 0x93, 0x17, 0x8f, 0x60, 0xcc,
0xac, 0xf9, 0xde, 0xf4, 0xf1, 0x62, 0x48, 0x1d, 0xdf, 0xb5, 0xc9, 0x8e, 0x0a, 0x5f, 0x3e, 0xbb,
0x82, 0xe9, 0x5f, 0x09, 0x92, 0x7b, 0x90, 0x96, 0x6b, 0x86, 0xef, 0xf8, 0xe2, 0xdd, 0x05, 0xc3,
0x88, 0x0c, 0x61, 0x70, 0xf9, 0x6a, 0xcd, 0x70, 0x42, 0x4e, 0x61, 0xf4, 0x41, 0x5e, 0x6f, 0x8c,
0xf8, 0x22, 0x1d, 0x4e, 0x7d, 0xe3, 0x45, 0x59, 0x32, 0x3c, 0x20, 0x18, 0x26, 0x17, 0x46, 0xf1,
0x4a, 0xf7, 0xbd, 0x93, 0xd5, 0x5b, 0x78, 0x2c, 0x8c, 0x3a, 0x1c, 0x1f, 0x43, 0x57, 0xc3, 0x50,
0xff, 0x48, 0xce, 0xde, 0xe7, 0x05, 0x6f, 0xe8, 0xda, 0x6b, 0xe3, 0x5a, 0xf4, 0x65, 0xdf, 0xbf,
0xbe, 0xdb, 0x1e, 0xed, 0xf9, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x1c, 0x47, 0x39, 0x56, 0x83,
0x03, 0x00, 0x00,
var fileDescriptor_config_807c5df32db81c88 = []byte{
// 456 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x92, 0xe1, 0x8a, 0x13, 0x31,
0x10, 0xc7, 0xdd, 0x6e, 0x7b, 0xb6, 0xd3, 0xde, 0xdd, 0x36, 0x9f, 0x8a, 0x70, 0x58, 0x57, 0x90,
0xa2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,
0x09, 0x4c, 0x2e, 0x24, 0x27, 0xb5, 0xe8, 0x72, 0x83, 0xe5, 0x5b, 0x78, 0x44, 0x25, 0x3f, 0x8c,
0x58, 0x44, 0x57, 0x43, 0x1f, 0x7f, 0xef, 0x9d, 0xbd, 0xcf, 0x4b, 0xd2, 0xe0, 0x95, 0xd5, 0x86,
0xb1, 0xf0, 0xcb, 0x2e, 0x7f, 0x7d, 0xe4, 0xbe, 0xcb, 0xf3, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff,
0xba, 0x0d, 0x33, 0xbb, 0xfd, 0x03, 0x00, 0x00,
}

View File

@ -43,8 +43,16 @@ message StreamConfig {
// Settings for transport security. For now the only choice is TLS.
repeated v2ray.core.common.serial.TypedMessage security_settings = 4;
SocketConfig socket_settings = 6;
}
message ProxyConfig {
string tag = 1;
}
// SocketConfig is options to be applied on network sockets.
message SocketConfig {
// Mark of the connection. If non-zero, the value will be set to SO_MARK.
int32 mark = 1;
}

View File

@ -0,0 +1,12 @@
package internet
import "syscall"
func applySocketOptions(fd uintptr, config *SocketConfig) error {
if config.Mark != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, config.Mark); err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1,46 @@
package internet_test
import (
"context"
"syscall"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/testing/servers/tcp"
. "v2ray.com/core/transport/internet"
)
func TestSockOptMark(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: func(b []byte) []byte {
return b
},
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
const mark = 1
ctx := context.Background()
ctx = ContextWithStreamSettings(ctx, &StreamConfig{
SocketSettings: &SocketConfig{
Mark: mark,
},
})
dialer := DefaultSystemDialer{}
conn, err := dialer.Dial(ctx, nil, dest)
common.Must(err)
defer conn.Close()
rawConn, err := conn.(*net.TCPConn).SyscallConn()
common.Must(err)
err = rawConn.Control(func(fd uintptr) {
m, err := syscall.GetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK)
common.Must(err)
if mark != m {
t.Fatal("unexpected conneciton mark", m, " want ", mark)
}
})
common.Must(err)
}

View File

@ -0,0 +1,7 @@
// +build !linux
package internet
func applySocketOptions(fd uintptr, config *SocketConfig) error {
return nil
}

View File

@ -2,9 +2,11 @@ package internet
import (
"context"
"syscall"
"time"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
)
var (
@ -23,6 +25,19 @@ func (DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.D
Timeout: time.Second * 60,
DualStack: true,
}
streamSettings := StreamSettingsFromContext(ctx)
if streamSettings != nil && streamSettings.SocketSettings != nil {
config := streamSettings.SocketSettings
dialer.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if err := applySocketOptions(fd, config); err != nil {
newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
})
}
}
if src != nil && src != net.AnyIP {
var addr net.Addr
if dest.Network == net.Network_TCP {