2022-08-07 15:40:46 +08:00
|
|
|
package tun
|
|
|
|
|
|
2022-08-07 17:15:29 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
2023-11-06 21:33:35 +08:00
|
|
|
"encoding/binary"
|
|
|
|
|
"net"
|
2022-09-06 19:24:47 +08:00
|
|
|
"net/netip"
|
2024-10-21 21:55:07 +08:00
|
|
|
"time"
|
2022-08-07 17:15:29 +08:00
|
|
|
|
2023-04-20 09:09:11 +08:00
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-08-07 17:15:29 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-09-09 11:31:08 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-08-07 17:15:29 +08:00
|
|
|
)
|
2022-08-07 15:40:46 +08:00
|
|
|
|
2025-08-27 00:12:17 +08:00
|
|
|
var (
|
2025-12-26 05:56:29 +08:00
|
|
|
ErrDrop = E.New("drop by rule")
|
|
|
|
|
ErrReset = E.New("reset by rule")
|
|
|
|
|
ErrBypass = E.New("bypass by rule")
|
2025-08-27 00:12:17 +08:00
|
|
|
)
|
2024-10-20 13:42:53 +08:00
|
|
|
|
2022-08-07 15:40:46 +08:00
|
|
|
type Stack interface {
|
2022-08-07 17:15:29 +08:00
|
|
|
Start() error
|
2022-08-07 15:40:46 +08:00
|
|
|
Close() error
|
|
|
|
|
}
|
2022-08-07 17:15:29 +08:00
|
|
|
|
2022-09-06 19:24:47 +08:00
|
|
|
type StackOptions struct {
|
2023-05-20 12:11:00 +08:00
|
|
|
Context context.Context
|
|
|
|
|
Tun Tun
|
2023-12-10 00:00:14 +08:00
|
|
|
TunOptions Options
|
2024-10-21 21:55:07 +08:00
|
|
|
UDPTimeout time.Duration
|
2023-05-20 12:11:00 +08:00
|
|
|
Handler Handler
|
|
|
|
|
Logger logger.Logger
|
|
|
|
|
ForwarderBindInterface bool
|
2024-05-07 20:20:44 +08:00
|
|
|
IncludeAllNetworks bool
|
2023-05-20 12:11:00 +08:00
|
|
|
InterfaceFinder control.InterfaceFinder
|
2022-09-06 19:24:47 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-07 17:15:29 +08:00
|
|
|
func NewStack(
|
|
|
|
|
stack string,
|
2022-09-06 19:24:47 +08:00
|
|
|
options StackOptions,
|
2022-08-07 17:15:29 +08:00
|
|
|
) (Stack, error) {
|
|
|
|
|
switch stack {
|
2022-09-15 11:19:31 +08:00
|
|
|
case "":
|
2024-05-07 20:20:44 +08:00
|
|
|
if options.IncludeAllNetworks {
|
|
|
|
|
return NewGVisor(options)
|
|
|
|
|
} else if WithGVisor && !options.TunOptions.GSO {
|
2023-07-23 14:19:51 +08:00
|
|
|
return NewMixed(options)
|
|
|
|
|
} else {
|
|
|
|
|
return NewSystem(options)
|
|
|
|
|
}
|
2022-09-15 11:19:31 +08:00
|
|
|
case "gvisor":
|
2022-09-06 19:24:47 +08:00
|
|
|
return NewGVisor(options)
|
2023-07-23 14:19:51 +08:00
|
|
|
case "mixed":
|
2024-05-07 20:20:44 +08:00
|
|
|
if options.IncludeAllNetworks {
|
|
|
|
|
return nil, ErrIncludeAllNetworks
|
|
|
|
|
}
|
2023-07-23 14:19:51 +08:00
|
|
|
return NewMixed(options)
|
2022-09-06 19:24:47 +08:00
|
|
|
case "system":
|
2024-05-07 20:20:44 +08:00
|
|
|
if options.IncludeAllNetworks {
|
|
|
|
|
return nil, ErrIncludeAllNetworks
|
|
|
|
|
}
|
2022-09-06 19:24:47 +08:00
|
|
|
return NewSystem(options)
|
2022-08-07 17:15:29 +08:00
|
|
|
default:
|
|
|
|
|
return nil, E.New("unknown stack: ", stack)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-06 21:33:35 +08:00
|
|
|
|
2024-10-26 14:54:08 +08:00
|
|
|
func HasNextAddress(prefix netip.Prefix, count int) bool {
|
|
|
|
|
checkAddr := prefix.Addr()
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
checkAddr = checkAddr.Next()
|
|
|
|
|
}
|
|
|
|
|
return prefix.Contains(checkAddr)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 21:33:35 +08:00
|
|
|
func BroadcastAddr(inet4Address []netip.Prefix) netip.Addr {
|
|
|
|
|
if len(inet4Address) == 0 {
|
|
|
|
|
return netip.Addr{}
|
|
|
|
|
}
|
|
|
|
|
prefix := inet4Address[0]
|
|
|
|
|
var broadcastAddr [4]byte
|
|
|
|
|
binary.BigEndian.PutUint32(broadcastAddr[:], binary.BigEndian.Uint32(prefix.Masked().Addr().AsSlice())|^binary.BigEndian.Uint32(net.CIDRMask(prefix.Bits(), 32)))
|
|
|
|
|
return netip.AddrFrom4(broadcastAddr)
|
|
|
|
|
}
|