1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 08:16:00 -04:00
v2fly/app/tun/tun.go

78 lines
1.7 KiB
Go
Raw Normal View History

2023-05-28 00:18:58 -04:00
//go:build !confonly
// +build !confonly
package tun
import (
"context"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/tun/device"
2023-05-28 20:34:33 -04:00
"github.com/v2fly/v2ray-core/v5/app/tun/device/gvisor"
2023-05-28 00:18:58 -04:00
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/features/routing"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
type TUN struct {
ctx context.Context
dispatcher routing.Dispatcher
policyManager policy.Manager
config *Config
stack *stack.Stack
}
func (t *TUN) Type() interface{} {
return (*TUN)(nil)
}
func (t *TUN) Start() error {
2023-05-28 20:34:33 -04:00
DeviceConstructor := gvisor.New
2023-05-28 03:20:06 -04:00
device, err := DeviceConstructor(device.Options{
2023-05-28 00:18:58 -04:00
Name: t.config.Name,
MTU: t.config.Mtu,
})
if err != nil {
return newError("failed to create device").Base(err).AtError()
}
2023-05-28 02:32:48 -04:00
stack, err := t.CreateStack(device)
2023-05-28 00:18:58 -04:00
if err != nil {
return newError("failed to create stack").Base(err).AtError()
}
t.stack = stack
return nil
}
func (t *TUN) Close() error {
if t.stack != nil {
t.stack.Close()
t.stack.Wait()
}
return nil
}
2023-05-28 08:21:46 -04:00
func (t *TUN) Init(ctx context.Context, config *Config, dispatcher routing.Dispatcher, policyManager policy.Manager) error {
t.ctx = ctx
t.config = config
t.dispatcher = dispatcher
t.policyManager = policyManager
return nil
2023-05-28 00:18:58 -04:00
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
2023-05-28 08:21:46 -04:00
tun := new(TUN)
err := core.RequireFeatures(ctx, func(d routing.Dispatcher, p policy.Manager) error {
return tun.Init(ctx, config.(*Config), d, p)
2023-05-28 00:18:58 -04:00
})
2023-05-28 08:21:46 -04:00
return tun, err
2023-05-28 00:18:58 -04:00
}))
}