github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/tun/option.go (about)

     1  package tun
     2  
     3  import (
     4  	"gvisor.dev/gvisor/pkg/tcpip"
     5  	"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
     6  	"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
     7  	"gvisor.dev/gvisor/pkg/tcpip/stack"
     8  	"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
     9  
    10  	"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
    11  )
    12  
    13  func CreateNIC(id tcpip.NICID, linkEndpoint stack.LinkEndpoint) StackOption {
    14  	return func(s *stack.Stack) error {
    15  		if err := s.CreateNICWithOptions(id, linkEndpoint,
    16  			stack.NICOptions{
    17  				Disabled: false,
    18  				QDisc:    nil,
    19  			}); err != nil {
    20  			return newError("failed to create NIC:", err)
    21  		}
    22  		return nil
    23  	}
    24  }
    25  
    26  func SetPromiscuousMode(id tcpip.NICID, enable bool) StackOption {
    27  	return func(s *stack.Stack) error {
    28  		if err := s.SetPromiscuousMode(id, enable); err != nil {
    29  			return newError("failed to set promiscuous mode:", err)
    30  		}
    31  		return nil
    32  	}
    33  }
    34  
    35  func SetSpoofing(id tcpip.NICID, enable bool) StackOption {
    36  	return func(s *stack.Stack) error {
    37  		if err := s.SetSpoofing(id, enable); err != nil {
    38  			return newError("failed to set spoofing:", err)
    39  		}
    40  		return nil
    41  	}
    42  }
    43  
    44  func AddProtocolAddress(id tcpip.NICID, ips []*routercommon.CIDR) StackOption {
    45  	return func(s *stack.Stack) error {
    46  		for _, ip := range ips {
    47  			tcpIPAddr := tcpip.AddrFrom4Slice(ip.Ip)
    48  			protocolAddress := tcpip.ProtocolAddress{
    49  				AddressWithPrefix: tcpip.AddressWithPrefix{
    50  					Address:   tcpIPAddr,
    51  					PrefixLen: int(ip.Prefix),
    52  				},
    53  			}
    54  
    55  			switch tcpIPAddr.Len() {
    56  			case 4:
    57  				protocolAddress.Protocol = ipv4.ProtocolNumber
    58  			case 16:
    59  				protocolAddress.Protocol = ipv6.ProtocolNumber
    60  			default:
    61  				return newError("invalid IP address length:", tcpIPAddr.Len())
    62  			}
    63  
    64  			if err := s.AddProtocolAddress(id, protocolAddress, stack.AddressProperties{}); err != nil {
    65  				return newError("failed to add protocol address:", err)
    66  			}
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func SetRouteTable(id tcpip.NICID, routes []*routercommon.CIDR) StackOption {
    74  	return func(s *stack.Stack) error {
    75  		s.SetRouteTable(func() (table []tcpip.Route) {
    76  			for _, cidrs := range routes {
    77  				subnet := tcpip.AddressWithPrefix{
    78  					Address:   tcpip.AddrFrom4Slice(cidrs.Ip),
    79  					PrefixLen: int(cidrs.Prefix),
    80  				}.Subnet()
    81  				route := tcpip.Route{
    82  					Destination: subnet,
    83  					NIC:         id,
    84  				}
    85  				table = append(table, route)
    86  			}
    87  			return
    88  		}())
    89  
    90  		return nil
    91  	}
    92  }
    93  
    94  func SetTCPSendBufferSize(size int) StackOption {
    95  	return func(s *stack.Stack) error {
    96  		sendBufferSizeRangeOption := tcpip.TCPSendBufferSizeRangeOption{Min: tcp.MinBufferSize, Default: size, Max: tcp.MaxBufferSize}
    97  		if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &sendBufferSizeRangeOption); err != nil {
    98  			return newError("failed to set tcp send buffer size:", err)
    99  		}
   100  		return nil
   101  	}
   102  }
   103  
   104  func SetTCPReceiveBufferSize(size int) StackOption {
   105  	return func(s *stack.Stack) error {
   106  		receiveBufferSizeRangeOption := tcpip.TCPReceiveBufferSizeRangeOption{Min: tcp.MinBufferSize, Default: size, Max: tcp.MaxBufferSize}
   107  		if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &receiveBufferSizeRangeOption); err != nil {
   108  			return newError("failed to set tcp receive buffer size:", err)
   109  		}
   110  		return nil
   111  	}
   112  }