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

113 lines
3.1 KiB
Go
Raw Normal View History

2023-05-28 02:39:16 -04:00
package tun
import (
"gvisor.dev/gvisor/pkg/tcpip"
2023-05-28 05:28:10 -04:00
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
2023-05-28 02:39:16 -04:00
"gvisor.dev/gvisor/pkg/tcpip/stack"
2023-05-31 01:13:34 -04:00
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
2023-10-22 20:44:36 -04:00
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
2023-05-28 02:39:16 -04:00
)
func CreateNIC(id tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption {
2023-05-28 02:39:16 -04:00
return func(s *stack.Stack) error {
if err := s.CreateNICWithOptions(id, linkEndpoint,
2023-05-28 02:39:16 -04:00
stack.NICOptions{
Disabled: false,
QDisc: nil,
}); err != nil {
return newError("failed to create NIC:", err)
}
return nil
}
}
2023-05-28 05:28:10 -04:00
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
}
}
2023-05-28 05:28:10 -04:00
func AddProtocolAddress(id tcpip.NICID, ips []*routercommon.CIDR) StackOption {
return func(s *stack.Stack) error {
for _, ip := range ips {
2023-05-28 10:43:08 -04:00
tcpIPAddr := tcpip.AddrFrom4Slice(ip.Ip)
2023-05-28 05:28:10 -04:00
protocolAddress := tcpip.ProtocolAddress{
AddressWithPrefix: tcpip.AddressWithPrefix{
2023-05-28 10:43:08 -04:00
Address: tcpIPAddr,
2023-05-28 05:28:10 -04:00
PrefixLen: int(ip.Prefix),
},
}
2023-05-28 10:43:08 -04:00
switch tcpIPAddr.Len() {
2023-05-28 05:28:10 -04:00
case 4:
protocolAddress.Protocol = ipv4.ProtocolNumber
case 16:
protocolAddress.Protocol = ipv6.ProtocolNumber
default:
2023-05-28 10:43:08 -04:00
return newError("invalid IP address length:", tcpIPAddr.Len())
2023-05-28 05:28:10 -04:00
}
if err := s.AddProtocolAddress(id, protocolAddress, stack.AddressProperties{}); err != nil {
return newError("failed to add protocol address:", err)
}
}
return nil
}
}
2023-05-28 06:00:23 -04:00
func SetRouteTable(id tcpip.NICID, routes []*routercommon.CIDR) StackOption {
return func(s *stack.Stack) error {
s.SetRouteTable(func() (table []tcpip.Route) {
for _, cidrs := range routes {
subnet := tcpip.AddressWithPrefix{
Address: tcpip.AddrFrom4Slice(cidrs.Ip),
PrefixLen: int(cidrs.Prefix),
}.Subnet()
route := tcpip.Route{
Destination: subnet,
NIC: id,
}
table = append(table, route)
}
return
}())
return nil
}
}
2023-05-31 01:13:34 -04:00
func SetTCPSendBufferSize(size int) StackOption {
return func(s *stack.Stack) error {
sendBufferSizeRangeOption := tcpip.TCPSendBufferSizeRangeOption{Min: tcp.MinBufferSize, Default: size, Max: tcp.MaxBufferSize}
if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &sendBufferSizeRangeOption); err != nil {
return newError("failed to set tcp send buffer size:", err)
}
return nil
}
}
func SetTCPReceiveBufferSize(size int) StackOption {
return func(s *stack.Stack) error {
receiveBufferSizeRangeOption := tcpip.TCPReceiveBufferSizeRangeOption{Min: tcp.MinBufferSize, Default: size, Max: tcp.MaxBufferSize}
if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &receiveBufferSizeRangeOption); err != nil {
return newError("failed to set tcp receive buffer size:", err)
}
return nil
}
}