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

46 lines
1.0 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{
SetTCPHandler(t.ctx, t.dispatcher, t.policyManager, t.config),
2023-05-28 03:16:37 -04:00
SetUDPHandler(t.ctx, t.dispatcher, t.policyManager, t.config),
2023-05-28 02:39:16 -04:00
CreateNIC(nicID, linkedEndpoint),
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
}