From 04104ad13db5d601e2332b5a3e924674a329ea64 Mon Sep 17 00:00:00 2001 From: AkinoKaede Date: Sun, 28 May 2023 17:42:22 +0800 Subject: [PATCH] feat: add promiscuous mode and spoofing config --- app/tun/config.pb.go | 52 +++++++++++++++++++++++++++++++------------- app/tun/config.proto | 2 ++ app/tun/nic.go | 22 +++++++++++++++++-- app/tun/stack.go | 2 ++ 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/app/tun/config.pb.go b/app/tun/config.pb.go index 52e0a0e73..974ee304e 100644 --- a/app/tun/config.pb.go +++ b/app/tun/config.pb.go @@ -28,12 +28,14 @@ type Config struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - Mtu uint32 `protobuf:"varint,2,opt,name=mtu,proto3" json:"mtu,omitempty"` - UserLevel uint32 `protobuf:"varint,3,opt,name=user_level,json=userLevel,proto3" json:"user_level,omitempty"` - PacketEncoding packetaddr.PacketAddrType `protobuf:"varint,4,opt,name=packet_encoding,json=packetEncoding,proto3,enum=v2ray.core.net.packetaddr.PacketAddrType" json:"packet_encoding,omitempty"` - Tag string `protobuf:"bytes,6,opt,name=tag,proto3" json:"tag,omitempty"` - Ips []*routercommon.CIDR `protobuf:"bytes,7,rep,name=ips,proto3" json:"ips,omitempty"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Mtu uint32 `protobuf:"varint,2,opt,name=mtu,proto3" json:"mtu,omitempty"` + UserLevel uint32 `protobuf:"varint,3,opt,name=user_level,json=userLevel,proto3" json:"user_level,omitempty"` + PacketEncoding packetaddr.PacketAddrType `protobuf:"varint,4,opt,name=packet_encoding,json=packetEncoding,proto3,enum=v2ray.core.net.packetaddr.PacketAddrType" json:"packet_encoding,omitempty"` + Tag string `protobuf:"bytes,6,opt,name=tag,proto3" json:"tag,omitempty"` + Ips []*routercommon.CIDR `protobuf:"bytes,7,rep,name=ips,proto3" json:"ips,omitempty"` + EnablePromiscuousMode bool `protobuf:"varint,8,opt,name=enable_promiscuous_mode,json=enablePromiscuousMode,proto3" json:"enable_promiscuous_mode,omitempty"` + EnableSpoofing bool `protobuf:"varint,9,opt,name=enable_spoofing,json=enableSpoofing,proto3" json:"enable_spoofing,omitempty"` } func (x *Config) Reset() { @@ -110,6 +112,20 @@ func (x *Config) GetIps() []*routercommon.CIDR { return nil } +func (x *Config) GetEnablePromiscuousMode() bool { + if x != nil { + return x.EnablePromiscuousMode + } + return false +} + +func (x *Config) GetEnableSpoofing() bool { + if x != nil { + return x.EnableSpoofing + } + return false +} + var File_app_tun_config_proto protoreflect.FileDescriptor var file_app_tun_config_proto_rawDesc = []byte{ @@ -122,7 +138,7 @@ var file_app_tun_config_proto_rawDesc = []byte{ 0x6b, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 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, 0x83, 0x02, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xe4, 0x02, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x74, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x6d, 0x74, 0x75, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x5f, @@ -137,14 +153,20 @@ var file_app_tun_config_proto_rawDesc = []byte{ 0x69, 0x70, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x2e, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x43, - 0x49, 0x44, 0x52, 0x52, 0x03, 0x69, 0x70, 0x73, 0x3a, 0x12, 0x82, 0xb5, 0x18, 0x0e, 0x0a, 0x07, - 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x03, 0x74, 0x75, 0x6e, 0x42, 0x57, 0x0a, 0x16, - 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x61, - 0x70, 0x70, 0x2e, 0x74, 0x75, 0x6e, 0x50, 0x01, 0x5a, 0x26, 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, 0x61, 0x70, 0x70, 0x2f, 0x74, 0x75, 0x6e, - 0xaa, 0x02, 0x12, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x41, 0x70, - 0x70, 0x2e, 0x54, 0x75, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x49, 0x44, 0x52, 0x52, 0x03, 0x69, 0x70, 0x73, 0x12, 0x36, 0x0a, 0x17, 0x65, 0x6e, 0x61, 0x62, + 0x6c, 0x65, 0x5f, 0x70, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x63, 0x75, 0x6f, 0x75, 0x73, 0x5f, 0x6d, + 0x6f, 0x64, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x15, 0x65, 0x6e, 0x61, 0x62, 0x6c, + 0x65, 0x50, 0x72, 0x6f, 0x6d, 0x69, 0x73, 0x63, 0x75, 0x6f, 0x75, 0x73, 0x4d, 0x6f, 0x64, 0x65, + 0x12, 0x27, 0x0a, 0x0f, 0x65, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x73, 0x70, 0x6f, 0x6f, 0x66, + 0x69, 0x6e, 0x67, 0x18, 0x09, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0e, 0x65, 0x6e, 0x61, 0x62, 0x6c, + 0x65, 0x53, 0x70, 0x6f, 0x6f, 0x66, 0x69, 0x6e, 0x67, 0x3a, 0x12, 0x82, 0xb5, 0x18, 0x0e, 0x0a, + 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x03, 0x74, 0x75, 0x6e, 0x42, 0x57, 0x0a, + 0x16, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, + 0x61, 0x70, 0x70, 0x2e, 0x74, 0x75, 0x6e, 0x50, 0x01, 0x5a, 0x26, 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, 0x61, 0x70, 0x70, 0x2f, 0x74, 0x75, + 0x6e, 0xaa, 0x02, 0x12, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x41, + 0x70, 0x70, 0x2e, 0x54, 0x75, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/app/tun/config.proto b/app/tun/config.proto index 050837e6f..cdad42620 100644 --- a/app/tun/config.proto +++ b/app/tun/config.proto @@ -22,4 +22,6 @@ message Config { v2ray.core.net.packetaddr.PacketAddrType packet_encoding = 4; string tag = 6; repeated v2ray.core.app.router.routercommon.CIDR ips = 7; + bool enable_promiscuous_mode = 8; + bool enable_spoofing = 9; } \ No newline at end of file diff --git a/app/tun/nic.go b/app/tun/nic.go index ff2a75a2b..c1b4233bc 100644 --- a/app/tun/nic.go +++ b/app/tun/nic.go @@ -8,9 +8,9 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/stack" ) -func CreateNIC(nicID tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption { +func CreateNIC(id tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption { return func(s *stack.Stack) error { - if err := s.CreateNICWithOptions(nicID, linkEndpoint, + if err := s.CreateNICWithOptions(id, linkEndpoint, stack.NICOptions{ Disabled: false, QDisc: nil, @@ -21,6 +21,24 @@ func CreateNIC(nicID tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption { } } +func SetPromiscuousMode(id tcpip.NICID, enable bool) StackOption { + return func(s *stack.Stack) error { + if err := s.SetPromiscuousMode(id, enable); err != nil { + return newError("failed to set promiscuous mode:", err) + } + return nil + } +} + +func SetSpoofing(id tcpip.NICID, enable bool) StackOption { + return func(s *stack.Stack) error { + if err := s.SetSpoofing(id, enable); err != nil { + return newError("failed to set spoofing:", err) + } + return nil + } +} + func AddProtocolAddress(id tcpip.NICID, ips []*routercommon.CIDR) StackOption { return func(s *stack.Stack) error { for _, ip := range ips { diff --git a/app/tun/stack.go b/app/tun/stack.go index 3097e3b6e..38d347149 100644 --- a/app/tun/stack.go +++ b/app/tun/stack.go @@ -34,6 +34,8 @@ func (t *TUN) CreateStack(linkedEndpoint stack.LinkEndpoint) (*stack.Stack, erro CreateNIC(nicID, linkedEndpoint), AddProtocolAddress(nicID, t.config.Ips), + SetPromiscuousMode(nicID, t.config.EnablePromiscuousMode), + SetSpoofing(nicID, t.config.EnableSpoofing), } for _, opt := range opts {