1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/app/tun/stack.go

58 lines
1.5 KiB
Go
Raw Normal View History

2023-05-28 00:18:58 -04:00
package tun
import (
2023-05-28 02:39:16 -04:00
"gvisor.dev/gvisor/pkg/tcpip"
2023-05-28 00:18:58 -04:00
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
)
2023-05-28 02:17:40 -04:00
type StackOption func(*stack.Stack) error
2023-05-28 02:39:16 -04:00
func (t *TUN) CreateStack(linkedEndpoint stack.LinkEndpoint) (*stack.Stack, error) {
2023-05-28 00:18:58 -04:00
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{
ipv4.NewProtocol,
ipv6.NewProtocol,
},
TransportProtocols: []stack.TransportProtocolFactory{
tcp.NewProtocol,
udp.NewProtocol,
icmp.NewProtocol4,
icmp.NewProtocol6,
},
})
2023-05-28 02:39:16 -04:00
nicID := tcpip.NICID(s.UniqueID())
2023-05-28 02:32:48 -04:00
opts := []StackOption{
2023-05-30 01:28:22 -04:00
SetTCPHandler(t.ctx, t.dispatcher, t.policyManager, t.config),
SetUDPHandler(t.ctx, t.dispatcher, t.policyManager, t.config),
2023-05-28 02:39:16 -04:00
CreateNIC(nicID, linkedEndpoint),
2023-05-28 05:28:10 -04:00
AddProtocolAddress(nicID, t.config.Ips),
2023-05-28 06:00:23 -04:00
SetRouteTable(nicID, t.config.Routes),
SetPromiscuousMode(nicID, t.config.EnablePromiscuousMode),
SetSpoofing(nicID, t.config.EnableSpoofing),
2023-05-28 02:32:48 -04:00
}
2023-05-31 01:13:34 -04:00
if size := t.config.SocketSettings.TxBufSize; size != 0 {
opts = append(opts, SetTCPSendBufferSize(int(size)))
}
if size := t.config.SocketSettings.RxBufSize; size != 0 {
opts = append(opts, SetTCPReceiveBufferSize(int(size)))
}
2023-05-28 02:32:48 -04:00
for _, opt := range opts {
if err := opt(s); err != nil {
return nil, err
}
}
2023-05-28 00:18:58 -04:00
return s, nil
}