diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/boot.go | 47 | ||||
-rw-r--r-- | services/errors.go | 30 | ||||
-rw-r--r-- | services/names.go | 26 |
3 files changed, 63 insertions, 40 deletions
diff --git a/services/boot.go b/services/boot.go new file mode 100644 index 00000000..ae7aadda --- /dev/null +++ b/services/boot.go @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. + */ + +package services + +import ( + "errors" + "log" + "sync" + "time" + + "golang.org/x/sys/windows" + "golang.org/x/sys/windows/svc" + "golang.zx2c4.com/wireguard/windows/version" +) + +var ( + startedAtBoot bool + startedAtBootOnce sync.Once +) + +func StartedAtBoot() bool { + startedAtBootOnce.Do(func() { + if isService, err := svc.IsWindowsService(); err == nil && !isService { + return + } + if reason, err := svc.DynamicStartReason(); err == nil { + startedAtBoot = (reason&svc.StartReasonAuto) != 0 || (reason&svc.StartReasonDelayedAuto) != 0 + } else if errors.Is(err, windows.ERROR_PROC_NOT_FOUND) { + // TODO: Below this line is Windows 7 compatibility code, which hopefully we can delete at some point. + startedAtBoot = windows.DurationSinceBoot() < time.Minute*10 + } else { + log.Printf("Unable to determine service start reason: %v", err) + } + }) + return startedAtBoot +} + +func PrintStarting() { + boot := "" + if StartedAtBoot() { + boot = " at boot" + } + log.Printf("Starting%s %s", boot, version.UserAgent()) +} diff --git a/services/errors.go b/services/errors.go index 7a441778..c0588494 100644 --- a/services/errors.go +++ b/services/errors.go @@ -1,13 +1,12 @@ /* SPDX-License-Identifier: MIT * - * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved. + * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. */ package services import ( "fmt" - "syscall" "golang.org/x/sys/windows" ) @@ -18,18 +17,19 @@ const ( ErrorSuccess Error = iota ErrorRingloggerOpen ErrorLoadConfiguration - ErrorCreateWintun - ErrorUAPIListen + ErrorCreateNetworkAdapter ErrorDNSLookup ErrorFirewall ErrorDeviceSetConfig + ErrorDeviceBringUp ErrorBindSocketsToDefaultRoutes + ErrorMonitorMTUChanges ErrorSetNetConfig ErrorDetermineExecutablePath - ErrorOpenNULFile ErrorTrackTunnels ErrorEnumerateSessions ErrorDropPrivileges + ErrorRunScript ErrorWin32 ) @@ -43,28 +43,30 @@ func (e Error) Error() string { return "Unable to determine path of running executable" case ErrorLoadConfiguration: return "Unable to load configuration from path" - case ErrorCreateWintun: - return "Unable to create Wintun interface" - case ErrorUAPIListen: - return "Unable to listen on named pipe" + case ErrorCreateNetworkAdapter: + return "Unable to create network adapter" case ErrorDNSLookup: return "Unable to resolve one or more DNS hostname endpoints" case ErrorFirewall: return "Unable to enable firewall rules" case ErrorDeviceSetConfig: return "Unable to set device configuration" + case ErrorDeviceBringUp: + return "Unable to bring up adapter" case ErrorBindSocketsToDefaultRoutes: return "Unable to bind sockets to default route" + case ErrorMonitorMTUChanges: + return "Unable to monitor default route MTU for changes" case ErrorSetNetConfig: - return "Unable to set interface addresses, routes, dns, and/or interface settings" - case ErrorOpenNULFile: - return "Unable to open NUL file" + return "Unable to configure adapter network settings" case ErrorTrackTunnels: return "Unable to track existing tunnels" case ErrorEnumerateSessions: return "Unable to enumerate current sessions" case ErrorDropPrivileges: return "Unable to drop privileges" + case ErrorRunScript: + return "An error occurred while running a configuration script command" case ErrorWin32: return "An internal Windows error has occurred" default: @@ -73,7 +75,7 @@ func (e Error) Error() string { } func DetermineErrorCode(err error, serviceError Error) (bool, uint32) { - if syserr, ok := err.(syscall.Errno); ok { + if syserr, ok := err.(windows.Errno); ok { return false, uint32(syserr) } else if serviceError != ErrorSuccess { return true, uint32(serviceError) @@ -85,7 +87,7 @@ func DetermineErrorCode(err error, serviceError Error) (bool, uint32) { func CombineErrors(err error, serviceError Error) error { if serviceError != ErrorSuccess { if err != nil { - return fmt.Errorf("%v: %v", serviceError, err) + return fmt.Errorf("%v: %w", serviceError, err) } return serviceError } diff --git a/services/names.go b/services/names.go deleted file mode 100644 index 74445482..00000000 --- a/services/names.go +++ /dev/null @@ -1,26 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. - */ - -package services - -import ( - "errors" - - "golang.zx2c4.com/wireguard/windows/conf" -) - -func ServiceNameOfTunnel(tunnelName string) (string, error) { - if !conf.TunnelNameIsValid(tunnelName) { - return "", errors.New("Tunnel name is not valid") - } - return "WireGuardTunnel$" + tunnelName, nil -} - -func PipePathOfTunnel(tunnelName string) (string, error) { - if !conf.TunnelNameIsValid(tunnelName) { - return "", errors.New("Tunnel name is not valid") - } - return `\\.\pipe\ProtectedPrefix\Administrators\WireGuard\` + tunnelName, nil -} |