Skip to content

Commit 9bdf72d

Browse files
authored
WireGuard config: Replace kernelMode with noKernelTun
#3871 (comment)
1 parent b0272c1 commit 9bdf72d

File tree

6 files changed

+58
-65
lines changed

6 files changed

+58
-65
lines changed

infra/conf/wireguard.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package conf
22

33
import (
4-
"context"
54
"encoding/base64"
65
"encoding/hex"
7-
"fmt"
86
"strings"
97

108
"github.com/xtls/xray-core/common/errors"
@@ -53,8 +51,7 @@ func (c *WireGuardPeerConfig) Build() (proto.Message, error) {
5351
type WireGuardConfig struct {
5452
IsClient bool `json:""`
5553

56-
KernelTun *bool `json:"kernelTun"`
57-
KernelMode *bool `json:"kernelMode"`
54+
NoKernelTun bool `json:"noKernelTun"`
5855
SecretKey string `json:"secretKey"`
5956
Address []string `json:"address"`
6057
Peers []*WireGuardPeerConfig `json:"peers"`
@@ -121,26 +118,7 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
121118
}
122119

123120
config.IsClient = c.IsClient
124-
kernelTunSupported, err := wireguard.KernelTunSupported()
125-
if err != nil {
126-
errors.LogWarning(context.Background(), fmt.Sprintf("Failed to check kernel TUN support: %v. This may indicate that your OS doesn't support kernel TUN or you lack the necessary permissions. Please ensure you have the required privileges.", err))
127-
config.KernelMode = false
128-
return config, nil
129-
}
130-
if c.KernelMode == nil {
131-
c.KernelMode = c.KernelTun
132-
}
133-
if c.KernelMode != nil {
134-
config.KernelMode = *c.KernelMode
135-
if config.KernelMode && !kernelTunSupported {
136-
errors.LogWarning(context.Background(), "kernel TUN is not supported on your OS or permission is insufficient")
137-
}
138-
} else {
139-
config.KernelMode = kernelTunSupported
140-
if config.KernelMode {
141-
errors.LogDebug(context.Background(), "kernel TUN is enabled as it's supported and permission is sufficient")
142-
}
143-
}
121+
config.NoKernelTun = c.NoKernelTun
144122

145123
return config, nil
146124
}

infra/conf/wireguard_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestWireGuardConfig(t *testing.T) {
2626
"mtu": 1300,
2727
"workers": 2,
2828
"domainStrategy": "ForceIPv6v4",
29-
"kernelMode": false
29+
"noKernelTun": false
3030
}`,
3131
Parser: loadJSON(creator),
3232
Output: &wireguard.DeviceConfig{
@@ -45,7 +45,7 @@ func TestWireGuardConfig(t *testing.T) {
4545
Mtu: 1300,
4646
NumWorkers: 2,
4747
DomainStrategy: wireguard.DeviceConfig_FORCE_IP64,
48-
KernelMode: false,
48+
NoKernelTun: false,
4949
},
5050
},
5151
})

proxy/wireguard/config.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package wireguard
22

3+
import (
4+
"context"
5+
6+
"github.com/xtls/xray-core/common/errors"
7+
)
8+
39
func (c *DeviceConfig) preferIP4() bool {
410
return c.DomainStrategy == DeviceConfig_FORCE_IP ||
511
c.DomainStrategy == DeviceConfig_FORCE_IP4 ||
@@ -25,8 +31,17 @@ func (c *DeviceConfig) fallbackIP6() bool {
2531
}
2632

2733
func (c *DeviceConfig) createTun() tunCreator {
28-
if c.KernelMode {
29-
return createKernelTun
34+
if c.NoKernelTun {
35+
return createGVisorTun
36+
}
37+
kernelTunSupported, err := KernelTunSupported()
38+
if err != nil {
39+
errors.LogWarning(context.Background(), "Using gVisor TUN. Failed to check kernel TUN support:", err)
40+
return createGVisorTun
41+
}
42+
if !kernelTunSupported {
43+
errors.LogWarning(context.Background(), "Using gVisor TUN. Kernel TUN is not supported on your OS, or your permission is insufficient.)")
44+
return createGVisorTun
3045
}
31-
return createGVisorTun
46+
return createKernelTun
3247
}

proxy/wireguard/config.pb.go

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/wireguard/config.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ message DeviceConfig {
3030
bytes reserved = 6;
3131
DomainStrategy domain_strategy = 7;
3232
bool is_client = 8;
33-
bool kernel_mode = 9;
33+
bool no_kernel_tun = 9;
3434
}

testing/scenarios/wireguard_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestWireguard(t *testing.T) {
4848
Listen: net.NewIPOrDomain(net.LocalHostIP),
4949
}),
5050
ProxySettings: serial.ToTypedMessage(&wireguard.DeviceConfig{
51-
IsClient: false,
52-
KernelMode: false,
53-
Endpoint: []string{"10.0.0.1"},
54-
Mtu: 1420,
55-
SecretKey: serverPrivate,
51+
IsClient: false,
52+
NoKernelTun: false,
53+
Endpoint: []string{"10.0.0.1"},
54+
Mtu: 1420,
55+
SecretKey: serverPrivate,
5656
Peers: []*wireguard.PeerConfig{{
57-
PublicKey: serverPublic,
57+
PublicKey: serverPublic,
5858
AllowedIps: []string{"0.0.0.0/0", "::0/0"},
5959
}},
6060
}),
@@ -82,23 +82,23 @@ func TestWireguard(t *testing.T) {
8282
Listen: net.NewIPOrDomain(net.LocalHostIP),
8383
}),
8484
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
85-
Address: net.NewIPOrDomain(dest.Address),
86-
Port: uint32(dest.Port),
85+
Address: net.NewIPOrDomain(dest.Address),
86+
Port: uint32(dest.Port),
8787
Networks: []net.Network{net.Network_TCP},
8888
}),
8989
},
9090
},
9191
Outbound: []*core.OutboundHandlerConfig{
9292
{
9393
ProxySettings: serial.ToTypedMessage(&wireguard.DeviceConfig{
94-
IsClient: true,
95-
KernelMode: false,
96-
Endpoint: []string{"10.0.0.2"},
97-
Mtu: 1420,
98-
SecretKey: clientPrivate,
94+
IsClient: true,
95+
NoKernelTun: false,
96+
Endpoint: []string{"10.0.0.2"},
97+
Mtu: 1420,
98+
SecretKey: clientPrivate,
9999
Peers: []*wireguard.PeerConfig{{
100-
Endpoint: "127.0.0.1:" + serverPort.String(),
101-
PublicKey: clientPublic,
100+
Endpoint: "127.0.0.1:" + serverPort.String(),
101+
PublicKey: clientPublic,
102102
AllowedIps: []string{"0.0.0.0/0", "::0/0"},
103103
}},
104104
}),
@@ -119,4 +119,4 @@ func TestWireguard(t *testing.T) {
119119
// if err := errg.Wait(); err != nil {
120120
// t.Error(err)
121121
// }
122-
}
122+
}

0 commit comments

Comments
 (0)