58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
//go:build windows
|
|
|
|
package platformtun
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"socksrevivepc/internal/wintunloader"
|
|
)
|
|
|
|
type Logger interface {
|
|
Add(level, format string, args ...any)
|
|
}
|
|
|
|
// Prepare validates and loads Wintun for the current process, then normalizes
|
|
// the device string used by tun2socks. It intentionally does not pre-open a
|
|
// WireGuard TUN adapter here: tun2socks must own the live adapter/session.
|
|
// Pre-opening and closing the adapter before tun2socks can make some Windows
|
|
// builds close/crash with no useful GUI error.
|
|
func Prepare(device, interfaceName string, mtu int, logger Logger) (string, string, func(), error) {
|
|
if mtu <= 0 {
|
|
mtu = 1500
|
|
}
|
|
|
|
if err := wintunloader.Prepare(logger); err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
|
|
device = normalizeWindowsDevice(device)
|
|
interfaceName = normalizeWindowsInterface(interfaceName)
|
|
if logger != nil {
|
|
logger.Add("info", "Windows Wintun is ready: adapter=%s device=%s mtu=%d", interfaceName, device, mtu)
|
|
}
|
|
return device, interfaceName, nil, nil
|
|
}
|
|
|
|
func normalizeWindowsDevice(device string) string {
|
|
device = strings.TrimSpace(device)
|
|
// tun2socks v2 uses the Windows device model name "wintun". Values
|
|
// like "wintun://SocksRevive" are Linux-style URL device strings and
|
|
// can make the engine search for a non-existent network interface.
|
|
if device == "" || strings.EqualFold(device, "tun") || strings.EqualFold(device, "wintun") {
|
|
return "wintun"
|
|
}
|
|
if strings.HasPrefix(strings.ToLower(device), "wintun://") || strings.EqualFold(device, "tun://wintun") {
|
|
return "wintun"
|
|
}
|
|
return "wintun"
|
|
}
|
|
|
|
func normalizeWindowsInterface(interfaceName string) string {
|
|
interfaceName = strings.TrimSpace(interfaceName)
|
|
if interfaceName == "" || strings.EqualFold(interfaceName, "SocksRevive") {
|
|
return "wintun"
|
|
}
|
|
return interfaceName
|
|
}
|