sing-box/inbound/tun.go

165 lines
4.3 KiB
Go
Raw Normal View History

2022-07-20 07:12:40 +08:00
//go:build (linux || windows) && !no_gvisor
2022-07-17 11:16:13 +08:00
2022-07-09 19:18:37 +08:00
package inbound
import (
"context"
"net"
"net/netip"
"runtime"
"strconv"
"strings"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-11 18:44:59 +08:00
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing-tun"
2022-07-13 19:01:20 +08:00
"github.com/sagernet/sing/common"
2022-07-09 19:18:37 +08:00
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.Inbound = (*Tun)(nil)
type Tun struct {
tag string
2022-07-10 09:15:01 +08:00
ctx context.Context
router adapter.Router
2022-07-12 15:17:29 +08:00
logger log.ContextLogger
2022-07-10 09:15:01 +08:00
inboundOptions option.InboundOptions
tunName string
tunMTU uint32
inet4Address netip.Prefix
inet6Address netip.Prefix
autoRoute bool
2022-07-13 19:01:20 +08:00
tunIf tun.Tun
tunStack *tun.GVisorTun
2022-07-09 19:18:37 +08:00
}
2022-07-12 15:17:29 +08:00
func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TunInboundOptions) (*Tun, error) {
2022-07-10 09:15:01 +08:00
tunName := options.InterfaceName
if tunName == "" {
tunName = mkInterfaceName()
}
tunMTU := options.MTU
if tunMTU == 0 {
tunMTU = 1500
}
2022-07-09 19:18:37 +08:00
return &Tun{
2022-07-10 09:15:01 +08:00
tag: tag,
ctx: ctx,
router: router,
logger: logger,
inboundOptions: options.InboundOptions,
tunName: tunName,
tunMTU: tunMTU,
2022-07-10 14:22:28 +08:00
inet4Address: options.Inet4Address.Build(),
inet6Address: options.Inet6Address.Build(),
2022-07-10 09:15:01 +08:00
autoRoute: options.AutoRoute,
2022-07-09 19:18:37 +08:00
}, nil
}
func (t *Tun) Type() string {
return C.TypeTun
}
func (t *Tun) Tag() string {
return t.tag
}
func (t *Tun) Start() error {
2022-07-13 19:01:20 +08:00
tunIf, err := tun.Open(t.tunName, t.inet4Address, t.inet6Address, t.tunMTU, t.autoRoute)
2022-07-09 19:18:37 +08:00
if err != nil {
return E.Cause(err, "configure tun interface")
}
2022-07-13 19:01:20 +08:00
t.tunIf = tunIf
t.tunStack = tun.NewGVisor(t.ctx, tunIf, t.tunMTU, t)
err = t.tunStack.Start()
2022-07-09 19:18:37 +08:00
if err != nil {
return err
}
2022-07-10 09:15:01 +08:00
t.logger.Info("started at ", t.tunName)
2022-07-09 19:18:37 +08:00
return nil
}
func (t *Tun) Close() error {
2022-07-13 19:01:20 +08:00
return common.Close(
t.tunStack,
t.tunIf,
2022-07-09 19:18:37 +08:00
)
}
func (t *Tun) NewConnection(ctx context.Context, conn net.Conn, upstreamMetadata M.Metadata) error {
2022-07-12 15:17:29 +08:00
ctx = log.ContextWithNewID(ctx)
2022-07-09 19:18:37 +08:00
var metadata adapter.InboundContext
metadata.Inbound = t.tag
2022-07-19 22:16:49 +08:00
metadata.InboundType = C.TypeTun
2022-07-09 19:18:37 +08:00
metadata.Network = C.NetworkTCP
metadata.Source = upstreamMetadata.Source
metadata.Destination = upstreamMetadata.Destination
2022-07-10 09:15:01 +08:00
metadata.SniffEnabled = t.inboundOptions.SniffEnabled
metadata.SniffOverrideDestination = t.inboundOptions.SniffOverrideDestination
2022-07-11 18:44:59 +08:00
metadata.DomainStrategy = dns.DomainStrategy(t.inboundOptions.DomainStrategy)
2022-07-12 15:17:29 +08:00
t.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
t.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
2022-07-20 09:41:44 +08:00
err := t.router.RouteConnection(ctx, conn, metadata)
if err != nil {
t.NewError(ctx, err)
}
return err
2022-07-09 19:18:37 +08:00
}
func (t *Tun) NewPacketConnection(ctx context.Context, conn N.PacketConn, upstreamMetadata M.Metadata) error {
2022-07-12 15:17:29 +08:00
ctx = log.ContextWithNewID(ctx)
2022-07-09 19:18:37 +08:00
var metadata adapter.InboundContext
metadata.Inbound = t.tag
2022-07-19 22:16:49 +08:00
metadata.InboundType = C.TypeTun
2022-07-09 19:18:37 +08:00
metadata.Network = C.NetworkUDP
metadata.Source = upstreamMetadata.Source
metadata.Destination = upstreamMetadata.Destination
2022-07-10 09:15:01 +08:00
metadata.SniffEnabled = t.inboundOptions.SniffEnabled
metadata.SniffOverrideDestination = t.inboundOptions.SniffOverrideDestination
2022-07-11 18:44:59 +08:00
metadata.DomainStrategy = dns.DomainStrategy(t.inboundOptions.DomainStrategy)
2022-07-12 15:17:29 +08:00
t.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
t.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
2022-07-20 09:41:44 +08:00
err := t.router.RoutePacketConnection(ctx, conn, metadata)
if err != nil {
t.NewError(ctx, err)
}
return err
2022-07-09 19:18:37 +08:00
}
func (t *Tun) NewError(ctx context.Context, err error) {
NewError(t.logger, ctx, err)
}
func mkInterfaceName() (tunName string) {
switch runtime.GOOS {
case "darwin":
tunName = "utun"
default:
tunName = "tun"
}
interfaces, err := net.Interfaces()
if err != nil {
return
}
var tunIndex int
for _, netInterface := range interfaces {
if strings.HasPrefix(netInterface.Name, tunName) {
index, parseErr := strconv.ParseInt(netInterface.Name[len(tunName):], 10, 16)
if parseErr == nil {
tunIndex = int(index) + 1
}
}
}
tunName = F.ToString(tunName, tunIndex)
return
}