2022-07-09 19:18:37 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-12-20 19:55:14 +08:00
|
|
|
"time"
|
2022-07-09 19:18:37 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-12-04 11:47:25 +08:00
|
|
|
"github.com/sagernet/sing-box/common/taskmonitor"
|
2022-07-09 19:18:37 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-10-25 12:55:00 +08:00
|
|
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
2022-07-09 19:18:37 +08:00
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-11 18:44:59 +08:00
|
|
|
"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"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-08-15 00:25:49 +08:00
|
|
|
"github.com/sagernet/sing/common/ranges"
|
2022-07-09 19:18:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Inbound = (*Tun)(nil)
|
|
|
|
|
|
|
|
type Tun struct {
|
2022-08-07 17:21:49 +08:00
|
|
|
tag string
|
2022-07-26 19:21:56 +08:00
|
|
|
ctx context.Context
|
|
|
|
router adapter.Router
|
|
|
|
logger log.ContextLogger
|
|
|
|
inboundOptions option.InboundOptions
|
2022-08-15 00:25:49 +08:00
|
|
|
tunOptions tun.Options
|
2022-07-26 19:21:56 +08:00
|
|
|
endpointIndependentNat bool
|
|
|
|
udpTimeout int64
|
2022-08-07 17:21:49 +08:00
|
|
|
stack string
|
|
|
|
tunIf tun.Tun
|
|
|
|
tunStack tun.Stack
|
2022-10-25 12:55:00 +08:00
|
|
|
platformInterface platform.Interface
|
2023-02-28 19:02:27 +08:00
|
|
|
platformOptions option.TunPlatformOptions
|
2022-07-09 19:18:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:55:00 +08:00
|
|
|
func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TunInboundOptions, platformInterface platform.Interface) (*Tun, error) {
|
2022-07-10 09:15:01 +08:00
|
|
|
tunMTU := options.MTU
|
|
|
|
if tunMTU == 0 {
|
2022-09-06 22:52:37 +08:00
|
|
|
tunMTU = 9000
|
2022-07-10 09:15:01 +08:00
|
|
|
}
|
2023-12-20 19:55:14 +08:00
|
|
|
var udpTimeout time.Duration
|
2022-07-26 19:21:56 +08:00
|
|
|
if options.UDPTimeout != 0 {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
2022-07-26 19:21:56 +08:00
|
|
|
} else {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = C.UDPTimeout
|
2022-07-26 19:21:56 +08:00
|
|
|
}
|
2022-08-15 00:25:49 +08:00
|
|
|
includeUID := uidToRange(options.IncludeUID)
|
|
|
|
if len(options.IncludeUIDRange) > 0 {
|
|
|
|
var err error
|
|
|
|
includeUID, err = parseRange(includeUID, options.IncludeUIDRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse include_uid_range")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
excludeUID := uidToRange(options.ExcludeUID)
|
|
|
|
if len(options.ExcludeUIDRange) > 0 {
|
|
|
|
var err error
|
|
|
|
excludeUID, err = parseRange(excludeUID, options.ExcludeUIDRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse exclude_uid_range")
|
|
|
|
}
|
|
|
|
}
|
2022-07-09 19:18:37 +08:00
|
|
|
return &Tun{
|
2022-08-15 00:25:49 +08:00
|
|
|
tag: tag,
|
|
|
|
ctx: ctx,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
inboundOptions: options.InboundOptions,
|
|
|
|
tunOptions: tun.Options{
|
2023-10-30 12:00:00 +08:00
|
|
|
Name: options.InterfaceName,
|
|
|
|
MTU: tunMTU,
|
2023-12-20 20:00:00 +08:00
|
|
|
GSO: options.GSO,
|
2023-10-30 12:00:00 +08:00
|
|
|
Inet4Address: options.Inet4Address,
|
|
|
|
Inet6Address: options.Inet6Address,
|
|
|
|
AutoRoute: options.AutoRoute,
|
|
|
|
StrictRoute: options.StrictRoute,
|
|
|
|
IncludeInterface: options.IncludeInterface,
|
|
|
|
ExcludeInterface: options.ExcludeInterface,
|
|
|
|
Inet4RouteAddress: options.Inet4RouteAddress,
|
|
|
|
Inet6RouteAddress: options.Inet6RouteAddress,
|
|
|
|
Inet4RouteExcludeAddress: options.Inet4RouteExcludeAddress,
|
|
|
|
Inet6RouteExcludeAddress: options.Inet6RouteExcludeAddress,
|
|
|
|
IncludeUID: includeUID,
|
|
|
|
ExcludeUID: excludeUID,
|
|
|
|
IncludeAndroidUser: options.IncludeAndroidUser,
|
|
|
|
IncludePackage: options.IncludePackage,
|
|
|
|
ExcludePackage: options.ExcludePackage,
|
|
|
|
InterfaceMonitor: router.InterfaceMonitor(),
|
|
|
|
TableIndex: 2022,
|
2022-08-15 00:25:49 +08:00
|
|
|
},
|
2022-07-26 19:21:56 +08:00
|
|
|
endpointIndependentNat: options.EndpointIndependentNat,
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout: int64(udpTimeout.Seconds()),
|
2022-08-07 17:21:49 +08:00
|
|
|
stack: options.Stack,
|
2022-10-25 12:55:00 +08:00
|
|
|
platformInterface: platformInterface,
|
2023-02-28 19:02:27 +08:00
|
|
|
platformOptions: common.PtrValueOrDefault(options.Platform),
|
2022-07-09 19:18:37 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-15 00:25:49 +08:00
|
|
|
func uidToRange(uidList option.Listable[uint32]) []ranges.Range[uint32] {
|
|
|
|
return common.Map(uidList, func(uid uint32) ranges.Range[uint32] {
|
|
|
|
return ranges.NewSingle(uid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.Range[uint32], error) {
|
|
|
|
for _, uidRange := range rangeList {
|
|
|
|
if !strings.Contains(uidRange, ":") {
|
|
|
|
return nil, E.New("missing ':' in range: ", uidRange)
|
|
|
|
}
|
|
|
|
subIndex := strings.Index(uidRange, ":")
|
|
|
|
if subIndex == 0 {
|
|
|
|
return nil, E.New("missing range start: ", uidRange)
|
|
|
|
} else if subIndex == len(uidRange)-1 {
|
|
|
|
return nil, E.New("missing range end: ", uidRange)
|
|
|
|
}
|
|
|
|
var start, end uint64
|
|
|
|
var err error
|
|
|
|
start, err = strconv.ParseUint(uidRange[:subIndex], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse range start")
|
|
|
|
}
|
|
|
|
end, err = strconv.ParseUint(uidRange[subIndex+1:], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse range end")
|
|
|
|
}
|
|
|
|
uidRanges = append(uidRanges, ranges.New(uint32(start), uint32(end)))
|
|
|
|
}
|
|
|
|
return uidRanges, nil
|
|
|
|
}
|
|
|
|
|
2022-07-09 19:18:37 +08:00
|
|
|
func (t *Tun) Type() string {
|
|
|
|
return C.TypeTun
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tun) Tag() string {
|
|
|
|
return t.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tun) Start() error {
|
2022-10-25 12:55:00 +08:00
|
|
|
if C.IsAndroid && t.platformInterface == nil {
|
2022-08-15 11:40:49 +08:00
|
|
|
t.tunOptions.BuildAndroidRules(t.router.PackageManager(), t)
|
|
|
|
}
|
2023-06-07 20:28:21 +08:00
|
|
|
if t.tunOptions.Name == "" {
|
|
|
|
t.tunOptions.Name = tun.CalculateInterfaceName("")
|
|
|
|
}
|
2022-10-25 12:55:00 +08:00
|
|
|
var (
|
|
|
|
tunInterface tun.Tun
|
|
|
|
err error
|
|
|
|
)
|
2024-04-02 23:07:26 +08:00
|
|
|
monitor := taskmonitor.New(t.logger, C.StartTimeout)
|
2023-12-04 11:47:25 +08:00
|
|
|
monitor.Start("open tun interface")
|
2022-10-25 12:55:00 +08:00
|
|
|
if t.platformInterface != nil {
|
2023-04-17 19:05:02 +08:00
|
|
|
tunInterface, err = t.platformInterface.OpenTun(&t.tunOptions, t.platformOptions)
|
2022-10-25 12:55:00 +08:00
|
|
|
} else {
|
2023-02-26 19:16:28 +08:00
|
|
|
tunInterface, err = tun.New(t.tunOptions)
|
2022-10-25 12:55:00 +08:00
|
|
|
}
|
2023-12-04 11:47:25 +08:00
|
|
|
monitor.Finish()
|
2022-07-09 19:18:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "configure tun interface")
|
|
|
|
}
|
2023-06-07 20:28:21 +08:00
|
|
|
t.logger.Trace("creating stack")
|
2022-10-25 12:55:00 +08:00
|
|
|
t.tunIf = tunInterface
|
2024-05-07 20:34:24 +08:00
|
|
|
var (
|
|
|
|
forwarderBindInterface bool
|
|
|
|
includeAllNetworks bool
|
|
|
|
)
|
|
|
|
if t.platformInterface != nil {
|
|
|
|
forwarderBindInterface = true
|
|
|
|
includeAllNetworks = t.platformInterface.IncludeAllNetworks()
|
|
|
|
}
|
2024-06-25 13:10:25 +08:00
|
|
|
tunStack, err := tun.NewStack(t.stack, tun.StackOptions{
|
2022-09-06 19:30:12 +08:00
|
|
|
Context: t.ctx,
|
2022-10-25 12:55:00 +08:00
|
|
|
Tun: tunInterface,
|
2023-12-20 20:00:00 +08:00
|
|
|
TunOptions: t.tunOptions,
|
2022-09-06 19:30:12 +08:00
|
|
|
EndpointIndependentNat: t.endpointIndependentNat,
|
|
|
|
UDPTimeout: t.udpTimeout,
|
|
|
|
Handler: t,
|
2022-09-07 22:54:04 +08:00
|
|
|
Logger: t.logger,
|
2024-05-07 20:34:24 +08:00
|
|
|
ForwarderBindInterface: forwarderBindInterface,
|
2023-04-20 09:11:06 +08:00
|
|
|
InterfaceFinder: t.router.InterfaceFinder(),
|
2024-05-07 20:34:24 +08:00
|
|
|
IncludeAllNetworks: includeAllNetworks,
|
2022-09-06 19:30:12 +08:00
|
|
|
})
|
2022-08-07 17:21:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-04 11:47:25 +08:00
|
|
|
monitor.Start("initiating tun stack")
|
2024-06-25 13:10:25 +08:00
|
|
|
err = tunStack.Start()
|
2023-12-04 11:47:25 +08:00
|
|
|
monitor.Finish()
|
2024-06-25 13:10:25 +08:00
|
|
|
t.tunStack = tunStack
|
2022-07-09 19:18:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-15 00:25:49 +08:00
|
|
|
t.logger.Info("started at ", t.tunOptions.Name)
|
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.Source = upstreamMetadata.Source
|
|
|
|
metadata.Destination = upstreamMetadata.Destination
|
2022-10-07 20:30:27 +08:00
|
|
|
metadata.InboundOptions = t.inboundOptions
|
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)
|
|
|
|
}
|
2022-09-16 15:32:50 +08:00
|
|
|
return nil
|
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.Source = upstreamMetadata.Source
|
|
|
|
metadata.Destination = upstreamMetadata.Destination
|
2022-10-07 20:30:27 +08:00
|
|
|
metadata.InboundOptions = t.inboundOptions
|
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)
|
|
|
|
}
|
2022-09-16 15:32:50 +08:00
|
|
|
return nil
|
2022-07-09 19:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tun) NewError(ctx context.Context, err error) {
|
|
|
|
NewError(t.logger, ctx, err)
|
|
|
|
}
|