-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathstack.go
More file actions
87 lines (78 loc) · 1.95 KB
/
Copy pathstack.go
File metadata and controls
87 lines (78 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package tun
import (
"context"
"encoding/binary"
"net"
"net/netip"
"time"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
)
type Stack interface {
Start() error
ResetNetwork()
Close() error
}
type MemoryPressure uint8
const (
MemoryPressureNone MemoryPressure = iota
MemoryPressureWarning
MemoryPressureCritical
)
type StackOptions struct {
Context context.Context
Tun Tun
TunOptions Options
UDPTimeout time.Duration
ICMPTimeout time.Duration
UDPMapping NATMapping
UDPFiltering NATFiltering
UDPNATMax uint32
Handler Handler
Logger logger.Logger
ForwarderBindInterface bool
IncludeAllNetworks bool
InterfaceFinder control.InterfaceFinder
MemoryPressure func() MemoryPressure
TCPCongestionControl string
}
func NewStack(
stack string,
options StackOptions,
) (Stack, error) {
switch stack {
case "", "go":
return NewGo(options)
case "gvisor":
return NewGVisor(options)
case "mixed":
if options.IncludeAllNetworks {
return nil, ErrIncludeAllNetworks
}
return NewMixed(options)
case "system":
if options.IncludeAllNetworks {
return nil, ErrIncludeAllNetworks
}
return NewSystem(options)
default:
return nil, E.New("unknown stack: ", stack)
}
}
func HasNextAddress(prefix netip.Prefix, count int) bool {
checkAddr := prefix.Addr()
for range count {
checkAddr = checkAddr.Next()
}
return prefix.Contains(checkAddr)
}
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)
}