aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-09-21 21:47:06 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-09-23 15:29:18 +0200
commitd37a6b53214e0d55070ea7b5f4aad9df1d53e2ab (patch)
tree19532ff36d00dfa8d7af0518507aa2923c85ede3
parentmanager: switch to vanilla gob from rpc to remove reflection bloat (diff)
downloadwireguard-windows-d37a6b53214e0d55070ea7b5f4aad9df1d53e2ab.tar.xz
wireguard-windows-d37a6b53214e0d55070ea7b5f4aad9df1d53e2ab.zip
embeddable-dll-service: add basic outline for embedding wireguard
This allows people to embed WireGuard inside other apps as a service. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--embeddable-dll-service/README.md40
-rw-r--r--embeddable-dll-service/build.bat41
-rw-r--r--embeddable-dll-service/main.go27
3 files changed, 108 insertions, 0 deletions
diff --git a/embeddable-dll-service/README.md b/embeddable-dll-service/README.md
new file mode 100644
index 00000000..6b42787f
--- /dev/null
+++ b/embeddable-dll-service/README.md
@@ -0,0 +1,40 @@
+## Embeddable WireGuard Tunnel Library
+
+This allows embedding WireGuard as a service inside of another application. Build `tunnel.dll` by running `./build.bat` in this folder. The first time you run it, it will invoke `..\build.bat` simply for downloading dependencies. After, you should have `amd64/tunnel.dll` and `x86/tunnel.dll`.
+
+The basic setup to use `tunnel.dll` is:
+
+##### 1. Install a service with these parameters:
+
+ Service Name: "SomeServiceName"
+ Display Name: "Some Service Name"
+ Service Type: SERVICE_WIN32_OWN_PROCESS
+ Start Type: StartAutomatic
+ Error Control: ErrorNormal,
+ Dependencies: [ "Nsi" ]
+ Sid Type: SERVICE_SID_TYPE_UNRESTRICTED
+ Executable: "C:\path\to\example\vpnclient.exe /service configfile.conf"
+
+Some of these may have to be changed with `ChangeServiceConfig2` after the
+initial call to `CreateService` The `SERVICE_SID_TYPE_UNRESTRICTED` parameter
+is absolutely essential; do not forget it.
+
+##### 2. Have your program's main function handle the `/service` switch:
+
+ if (!strcmp(argv[1], "/service") && argc == 3) {
+ HMODULE tunnel_lib = LoadLibrary("tunnel.dll");
+ if (!tunnel_lib)
+ abort();
+ tunnel_proc_t tunnel_proc = (tunnel_proc_t)GetProcAddress(tunnel_lib, "WireGuardTunnelService");
+ if (!tunnel_proc)
+ abort();
+ struct go_string conf_file = {
+ .str = argv[2],
+ .n = strlen(argv[2])
+ };
+ return tunnel_proc(conf_file);
+ }
+
+##### 3. Scoop up logs by implementing a ringlogger format reader.
+
+##### 4. Talk to the service over its named pipe.
diff --git a/embeddable-dll-service/build.bat b/embeddable-dll-service/build.bat
new file mode 100644
index 00000000..14486696
--- /dev/null
+++ b/embeddable-dll-service/build.bat
@@ -0,0 +1,41 @@
+@echo off
+rem SPDX-License-Identifier: MIT
+rem Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+
+setlocal
+set BUILDDIR=%~dp0
+set PATH=%BUILDDIR%..\.deps\go\bin;%BUILDDIR%..\.deps;%PATH%
+set PATHEXT=.exe
+cd /d %BUILDDIR% || exit /b 1
+
+if exist ..\.deps\prepared goto :build
+:installdeps
+ call ..\build.bat || goto :error
+
+:build
+ set GOOS=windows
+ set GOPATH=%BUILDDIR%..\.deps\gopath
+ set GOROOT=%BUILDDIR%..\.deps\go
+ set CGO_ENABLED=1
+ set CGO_CFLAGS=-O3 -Wall -Wno-unused-function -Wno-switch -std=gnu11 -DWINVER=0x0601
+ set CGO_LDFLAGS=-Wl,--major-os-version=6 -Wl,--minor-os-version=1 -Wl,--major-subsystem-version=6 -Wl,--minor-subsystem-version=1
+ call :build_plat x86 i686 386 || goto :error
+ call :build_plat amd64 x86_64 amd64 || goto :error
+
+:success
+ echo [+] Success
+ exit /b 0
+
+:build_plat
+ set PATH=%BUILDDIR%..\.deps\%~2-w64-mingw32-native\bin;%PATH%
+ set CC=%~2-w64-mingw32-gcc
+ set GOARCH=%~3
+ mkdir %1 >NUL 2>&1
+ echo [+] Building library %1
+ go build -buildmode c-shared -ldflags="-w -s" -trimpath -v -o "%~1/tunnel.dll" || exit /b 1
+ del "%~1\tunnel.h"
+ goto :eof
+
+:error
+ echo [-] Failed with error #%errorlevel%.
+ cmd /c exit %errorlevel%
diff --git a/embeddable-dll-service/main.go b/embeddable-dll-service/main.go
new file mode 100644
index 00000000..a8ce7c10
--- /dev/null
+++ b/embeddable-dll-service/main.go
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package main
+
+import (
+ "C"
+ "golang.zx2c4.com/wireguard/windows/conf"
+ "golang.zx2c4.com/wireguard/windows/tunnel"
+ "log"
+ "path/filepath"
+)
+
+//export WireGuardTunnelService
+func WireGuardTunnelService(confFile string) bool {
+ conf.PresetRootDirectory(filepath.Dir(confFile))
+ tunnel.UseFixedGUIDInsteadOfDeterministic = true
+ err := tunnel.Run(confFile)
+ if err != nil {
+ log.Printf("Service run error: %v", err)
+ }
+ return err == nil
+}
+
+func main() {}