Skip to content

Commit

Permalink
WireGuard config: Replace kernelMode with noKernelTun
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Oct 18, 2024
1 parent b0272c1 commit 0c9c81c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 49 deletions.
26 changes: 2 additions & 24 deletions infra/conf/wireguard.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package conf

import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"

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

KernelTun *bool `json:"kernelTun"`
KernelMode *bool `json:"kernelMode"`
NoKernelTun bool `json:"noKernelTun"`
SecretKey string `json:"secretKey"`
Address []string `json:"address"`
Peers []*WireGuardPeerConfig `json:"peers"`
Expand Down Expand Up @@ -121,26 +118,7 @@ func (c *WireGuardConfig) Build() (proto.Message, error) {
}

config.IsClient = c.IsClient
kernelTunSupported, err := wireguard.KernelTunSupported()
if err != nil {
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))
config.KernelMode = false
return config, nil
}
if c.KernelMode == nil {
c.KernelMode = c.KernelTun
}
if c.KernelMode != nil {
config.KernelMode = *c.KernelMode
if config.KernelMode && !kernelTunSupported {
errors.LogWarning(context.Background(), "kernel TUN is not supported on your OS or permission is insufficient")
}
} else {
config.KernelMode = kernelTunSupported
if config.KernelMode {
errors.LogDebug(context.Background(), "kernel TUN is enabled as it's supported and permission is sufficient")
}
}
config.NoKernelTun = c.NoKernelTun

return config, nil
}
Expand Down
4 changes: 2 additions & 2 deletions infra/conf/wireguard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestWireGuardConfig(t *testing.T) {
"mtu": 1300,
"workers": 2,
"domainStrategy": "ForceIPv6v4",
"kernelMode": false
"noKernelTun": false
}`,
Parser: loadJSON(creator),
Output: &wireguard.DeviceConfig{
Expand All @@ -45,7 +45,7 @@ func TestWireGuardConfig(t *testing.T) {
Mtu: 1300,
NumWorkers: 2,
DomainStrategy: wireguard.DeviceConfig_FORCE_IP64,
KernelMode: false,
NoKernelTun: false,
},
},
})
Expand Down
21 changes: 18 additions & 3 deletions proxy/wireguard/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package wireguard

import (
"context"

"github.com/xtls/xray-core/common/errors"
)

func (c *DeviceConfig) preferIP4() bool {
return c.DomainStrategy == DeviceConfig_FORCE_IP ||
c.DomainStrategy == DeviceConfig_FORCE_IP4 ||
Expand All @@ -25,8 +31,17 @@ func (c *DeviceConfig) fallbackIP6() bool {
}

func (c *DeviceConfig) createTun() tunCreator {
if c.KernelMode {
return createKernelTun
if c.NoKernelTun {
return createGVisorTun
}
kernelTunSupported, err := KernelTunSupported()
if err != nil {
errors.LogWarning(context.Background(), "Using gVisor TUN. Failed to check kernel TUN support:", err)
return createGVisorTun
}
if !kernelTunSupported {
errors.LogWarning(context.Background(), "Using gVisor TUN. Kernel TUN is not supported on your OS, or your permission is insufficient.)")
return createGVisorTun
}
return createGVisorTun
return createKernelTun
}
38 changes: 19 additions & 19 deletions proxy/wireguard/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proxy/wireguard/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ message DeviceConfig {
bytes reserved = 6;
DomainStrategy domain_strategy = 7;
bool is_client = 8;
bool kernel_mode = 9;
bool no_kernel_tun = 9;
}

0 comments on commit 0c9c81c

Please sign in to comment.