Skip to content

Commit 3469956

Browse files
committed
refactor: add dummy WireGuard implementation for non-Linux platforms
1 parent b0d0b45 commit 3469956

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

core/lib/netutil/wg_dummy.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//go:build !linux
2+
// +build !linux
3+
4+
package netutil
5+
6+
import (
7+
"context"
8+
"errors"
9+
"net"
10+
"os"
11+
12+
"golang.zx2c4.com/wireguard/device"
13+
"golang.zx2c4.com/wireguard/tun"
14+
)
15+
16+
// LogLevel specifies the verbosity of logging
17+
type LogLevel int
18+
19+
// Log level constants
20+
const (
21+
LogLevelSilent LogLevel = 0
22+
LogLevelError LogLevel = 1
23+
LogLevelVerbose LogLevel = 2
24+
25+
// WgFileServerPort port for file server
26+
WgFileServerPort = 7000
27+
28+
// WgRelayedHTTPPort port for relayed HTTP server
29+
WgRelayedHTTPPort = 1025
30+
)
31+
32+
var (
33+
WgSubnet = "172.16.254.0/24" // WireGuard subnet
34+
WgServerIP = "172.16.254.1" // server's static WireGuard IP
35+
WgOperatorIP = "172.16.254.2" // operator's static WireGuard IP
36+
WgServer *WireGuardDevice // server's WireGuard device
37+
WgOperator *WireGuardDevice // operator's WireGuard device
38+
)
39+
40+
type WireGuardConfig struct {
41+
// Interface name (e.g. "wg0")
42+
InterfaceName string
43+
// IP address with CIDR (e.g. "192.168.2.1/24")
44+
IPAddress string
45+
// Private key (optional, will be generated if empty)
46+
PrivateKey string
47+
// UDP listen port for WireGuard
48+
ListenPort int
49+
// Log verbosity level
50+
LogLevel LogLevel
51+
// Peer configurations
52+
Peers []PeerConfig
53+
}
54+
55+
// WireGuardDevice represents a WireGuard virtual network interface
56+
type WireGuardDevice struct {
57+
// Interface name (e.g. "wg0")
58+
Name string
59+
// IP address with CIDR (e.g. "192.168.2.1/24")
60+
IPAddress string
61+
// WireGuard private key
62+
PrivateKey string
63+
// Generated public key (derived from private key)
64+
PublicKey string
65+
// UDP listen port for WireGuard
66+
ListenPort int
67+
// Log verbosity level
68+
LogLevel LogLevel
69+
// Context of the WireGuard device
70+
Context context.Context
71+
// Cancel function for the context
72+
Cancel context.CancelFunc
73+
74+
// Underlying device objects
75+
device *device.Device
76+
tun tun.Device
77+
uapi net.Listener
78+
uapiFile *os.File
79+
logger *device.Logger
80+
}
81+
82+
// PeerConfig represents WireGuard peer configuration
83+
type PeerConfig struct {
84+
// Public key of the peer
85+
PublicKey string
86+
// Comma-separated list of allowed IPs (e.g. "10.0.0.0/24,192.168.1.0/24")
87+
AllowedIPs string
88+
// Endpoint address of the peer (e.g. "example.com:51820")
89+
Endpoint string
90+
}
91+
92+
// Dummy implementations that return errors since WireGuard is not supported on non-Linux platforms
93+
94+
// GeneratePrivateKey creates a new random WireGuard private key
95+
func GeneratePrivateKey() (string, error) {
96+
return "", errors.New("WireGuard is not supported on this platform")
97+
}
98+
99+
// PublicKeyFromPrivate derives the public key from a private key
100+
func PublicKeyFromPrivate(privateKey string) (string, error) {
101+
return "", errors.New("WireGuard is not supported on this platform")
102+
}
103+
104+
// Close shuts down the WireGuard device
105+
func (w *WireGuardDevice) Close() {
106+
// No-op for dummy implementation
107+
}
108+
109+
// WaitShutdown waits for the device to be shut down
110+
func (w *WireGuardDevice) WaitShutdown() {
111+
// No-op for dummy implementation
112+
}
113+
114+
// ConfigureWireGuardDevice configures the WireGuard device with the given peers
115+
func (w *WireGuardDevice) ConfigureWireGuardDevice(peers []PeerConfig) error {
116+
return errors.New("WireGuard is not supported on this platform")
117+
}
118+
119+
// CreateWireGuardDevice creates and configures a new WireGuard interface
120+
func CreateWireGuardDevice(config WireGuardConfig) (*WireGuardDevice, error) {
121+
return nil, errors.New("WireGuard is not supported on this platform")
122+
}
123+
124+
// WireGuardDeviceInfo returns information about the WireGuard device
125+
func (w *WireGuardDevice) WireGuardDeviceInfo() string {
126+
return "WireGuard is not supported on this platform"
127+
}
128+
129+
// WireGuardMain provides the main entry point for using this library programmatically
130+
func WireGuardMain(config WireGuardConfig) (wg *WireGuardDevice, err error) {
131+
return nil, errors.New("WireGuard is not supported on this platform")
132+
}

0 commit comments

Comments
 (0)