Skip to content

Commit cb4fea0

Browse files
committed
Refactor wireguard & add tun support
1 parent 8e7957d commit cb4fea0

20 files changed

+791
-424
lines changed

docs/configuration/outbound/wireguard.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"server": "127.0.0.1",
99
"server_port": 1080,
1010
"local_address": [
11-
"10.0.0.1",
1211
"10.0.0.2/32"
1312
],
1413
"private_key": "YNXtAzepDqRv9H52osJVDQnznT5AM11eCK3ESpwSt04=",
@@ -43,7 +42,7 @@ The server port.
4342

4443
==Required==
4544

46-
List of IP (v4 or v6) addresses (optionally with CIDR masks) to be assigned to the interface.
45+
List of IP (v4 or v6) address prefixes to be assigned to the interface.
4746

4847
#### private_key
4948

docs/configuration/outbound/wireguard.zh.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"server": "127.0.0.1",
99
"server_port": 1080,
1010
"local_address": [
11-
"10.0.0.1",
1211
"10.0.0.2/32"
1312
],
1413
"private_key": "YNXtAzepDqRv9H52osJVDQnznT5AM11eCK3ESpwSt04=",
@@ -45,7 +44,7 @@
4544

4645
接口的 IPv4/IPv6 地址或地址段的列表您。
4746

48-
要分配给接口的 IP(v4 或 v6)地址列表(可以选择带有 CIDR 掩码)
47+
要分配给接口的 IP(v4 或 v6)地址段列表
4948

5049
#### private_key
5150

inbound/tun.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Tun struct {
4040
func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TunInboundOptions) (*Tun, error) {
4141
tunName := options.InterfaceName
4242
if tunName == "" {
43-
tunName = tun.DefaultInterfaceName()
43+
tunName = tun.CalculateInterfaceName("")
4444
}
4545
tunMTU := options.MTU
4646
if tunMTU == 0 {
@@ -77,8 +77,8 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger
7777
tunOptions: tun.Options{
7878
Name: tunName,
7979
MTU: tunMTU,
80-
Inet4Address: options.Inet4Address.Build(),
81-
Inet6Address: options.Inet6Address.Build(),
80+
Inet4Address: common.Map(options.Inet4Address, option.ListenPrefix.Build),
81+
Inet6Address: common.Map(options.Inet6Address, option.ListenPrefix.Build),
8282
AutoRoute: options.AutoRoute,
8383
StrictRoute: options.StrictRoute,
8484
IncludeUID: includeUID,
@@ -87,6 +87,7 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger
8787
IncludePackage: options.IncludePackage,
8888
ExcludePackage: options.ExcludePackage,
8989
InterfaceMonitor: router.InterfaceMonitor(),
90+
TableIndex: 2022,
9091
},
9192
endpointIndependentNat: options.EndpointIndependentNat,
9293
udpTimeout: udpTimeout,

option/outbound.go

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type DialerOptions struct {
108108
Detour string `json:"detour,omitempty"`
109109
BindInterface string `json:"bind_interface,omitempty"`
110110
BindAddress *ListenAddress `json:"bind_address,omitempty"`
111+
BindAddress6 *ListenAddress `json:"bind_address6,omitempty"`
111112
ProtectPath string `json:"protect_path,omitempty"`
112113
RoutingMark int `json:"routing_mark,omitempty"`
113114
ReuseAddr bool `json:"reuse_addr,omitempty"`

option/tun.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package option
22

33
type TunInboundOptions struct {
4-
InterfaceName string `json:"interface_name,omitempty"`
5-
MTU uint32 `json:"mtu,omitempty"`
6-
Inet4Address *ListenPrefix `json:"inet4_address,omitempty"`
7-
Inet6Address *ListenPrefix `json:"inet6_address,omitempty"`
8-
AutoRoute bool `json:"auto_route,omitempty"`
9-
StrictRoute bool `json:"strict_route,omitempty"`
10-
IncludeUID Listable[uint32] `json:"include_uid,omitempty"`
11-
IncludeUIDRange Listable[string] `json:"include_uid_range,omitempty"`
12-
ExcludeUID Listable[uint32] `json:"exclude_uid,omitempty"`
13-
ExcludeUIDRange Listable[string] `json:"exclude_uid_range,omitempty"`
14-
IncludeAndroidUser Listable[int] `json:"include_android_user,omitempty"`
15-
IncludePackage Listable[string] `json:"include_package,omitempty"`
16-
ExcludePackage Listable[string] `json:"exclude_package,omitempty"`
17-
EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
18-
UDPTimeout int64 `json:"udp_timeout,omitempty"`
19-
Stack string `json:"stack,omitempty"`
4+
InterfaceName string `json:"interface_name,omitempty"`
5+
MTU uint32 `json:"mtu,omitempty"`
6+
Inet4Address Listable[ListenPrefix] `json:"inet4_address,omitempty"`
7+
Inet6Address Listable[ListenPrefix] `json:"inet6_address,omitempty"`
8+
AutoRoute bool `json:"auto_route,omitempty"`
9+
StrictRoute bool `json:"strict_route,omitempty"`
10+
IncludeUID Listable[uint32] `json:"include_uid,omitempty"`
11+
IncludeUIDRange Listable[string] `json:"include_uid_range,omitempty"`
12+
ExcludeUID Listable[uint32] `json:"exclude_uid,omitempty"`
13+
ExcludeUIDRange Listable[string] `json:"exclude_uid_range,omitempty"`
14+
IncludeAndroidUser Listable[int] `json:"include_android_user,omitempty"`
15+
IncludePackage Listable[string] `json:"include_package,omitempty"`
16+
ExcludePackage Listable[string] `json:"exclude_package,omitempty"`
17+
EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
18+
UDPTimeout int64 `json:"udp_timeout,omitempty"`
19+
Stack string `json:"stack,omitempty"`
2020
InboundOptions
2121
}

option/types.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error {
184184
return nil
185185
}
186186

187-
func (p *ListenPrefix) Build() netip.Prefix {
188-
if p == nil {
189-
return netip.Prefix{}
190-
}
191-
return netip.Prefix(*p)
187+
func (p ListenPrefix) Build() netip.Prefix {
188+
return netip.Prefix(p)
192189
}

option/wireguard.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package option
33
type WireGuardOutboundOptions struct {
44
DialerOptions
55
ServerOptions
6-
LocalAddress Listable[string] `json:"local_address"`
7-
PrivateKey string `json:"private_key"`
8-
PeerPublicKey string `json:"peer_public_key"`
9-
PreSharedKey string `json:"pre_shared_key,omitempty"`
10-
MTU uint32 `json:"mtu,omitempty"`
11-
Network NetworkList `json:"network,omitempty"`
6+
SystemInterface bool `json:"system_interface,omitempty"`
7+
InterfaceName string `json:"interface_name,omitempty"`
8+
LocalAddress Listable[ListenPrefix] `json:"local_address"`
9+
PrivateKey string `json:"private_key"`
10+
PeerPublicKey string `json:"peer_public_key"`
11+
PreSharedKey string `json:"pre_shared_key,omitempty"`
12+
MTU uint32 `json:"mtu,omitempty"`
13+
Network NetworkList `json:"network,omitempty"`
1214
}

0 commit comments

Comments
 (0)