-
Notifications
You must be signed in to change notification settings - Fork 365
/
main.go
329 lines (312 loc) · 8.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
*/
package main
import (
"debug/pe"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"time"
"golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/windows/conf"
"golang.zx2c4.com/wireguard/windows/driver"
"golang.zx2c4.com/wireguard/windows/elevate"
"golang.zx2c4.com/wireguard/windows/l18n"
"golang.zx2c4.com/wireguard/windows/manager"
"golang.zx2c4.com/wireguard/windows/ringlogger"
"golang.zx2c4.com/wireguard/windows/tunnel"
"golang.zx2c4.com/wireguard/windows/ui"
"golang.zx2c4.com/wireguard/windows/updater"
)
func setLogFile() {
logHandle, err := windows.GetStdHandle(windows.STD_ERROR_HANDLE)
if logHandle == 0 || err != nil {
logHandle, err = windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
}
if logHandle == 0 || err != nil {
log.SetOutput(io.Discard)
} else {
log.SetOutput(os.NewFile(uintptr(logHandle), "stderr"))
}
}
func fatal(v ...any) {
if log.Writer() == io.Discard {
windows.MessageBox(0, windows.StringToUTF16Ptr(fmt.Sprint(v...)), windows.StringToUTF16Ptr(l18n.Sprintf("Error")), windows.MB_ICONERROR)
os.Exit(1)
} else {
log.Fatal(append([]any{l18n.Sprintf("Error: ")}, v...))
}
}
func fatalf(format string, v ...any) {
fatal(l18n.Sprintf(format, v...))
}
func info(title, format string, v ...any) {
if log.Writer() == io.Discard {
windows.MessageBox(0, windows.StringToUTF16Ptr(l18n.Sprintf(format, v...)), windows.StringToUTF16Ptr(title), windows.MB_ICONINFORMATION)
} else {
log.Printf(title+":\n"+format, v...)
}
}
func usage() {
flags := [...]string{
l18n.Sprintf("(no argument): elevate and install manager service"),
"/installmanagerservice",
"/installtunnelservice CONFIG_PATH",
"/uninstallmanagerservice",
"/uninstalltunnelservice TUNNEL_NAME",
"/managerservice",
"/tunnelservice CONFIG_PATH",
"/ui CMD_READ_HANDLE CMD_WRITE_HANDLE CMD_EVENT_HANDLE LOG_MAPPING_HANDLE",
"/dumplog [/tail]",
"/update",
"/removedriver",
}
builder := strings.Builder{}
for _, flag := range flags {
builder.WriteString(fmt.Sprintf(" %s\n", flag))
}
info(l18n.Sprintf("Command Line Options"), "Usage: %s [\n%s]", os.Args[0], builder.String())
os.Exit(1)
}
func checkForWow64() {
b, err := func() (bool, error) {
var processMachine, nativeMachine uint16
err := windows.IsWow64Process2(windows.CurrentProcess(), &processMachine, &nativeMachine)
if err == nil {
return processMachine != pe.IMAGE_FILE_MACHINE_UNKNOWN, nil
}
if !errors.Is(err, windows.ERROR_PROC_NOT_FOUND) {
return false, err
}
var b bool
err = windows.IsWow64Process(windows.CurrentProcess(), &b)
if err != nil {
return false, err
}
return b, nil
}()
if err != nil {
fatalf("Unable to determine whether the process is running under WOW64: %v", err)
}
if b {
fatalf("You must use the native version of WireGuard on this computer.")
}
}
func checkForAdminGroup() {
// This is not a security check, but rather a user-confusion one.
var processToken windows.Token
err := windows.OpenProcessToken(windows.CurrentProcess(), windows.TOKEN_QUERY|windows.TOKEN_DUPLICATE, &processToken)
if err != nil {
fatalf("Unable to open current process token: %v", err)
}
defer processToken.Close()
if !elevate.TokenIsElevatedOrElevatable(processToken) {
fatalf("WireGuard may only be used by users who are a member of the Builtin %s group.", elevate.AdminGroupName())
}
}
func checkForAdminDesktop() {
adminDesktop, err := elevate.IsAdminDesktop()
if !adminDesktop && err == nil {
fatalf("WireGuard is running, but the UI is only accessible from desktops of the Builtin %s group.", elevate.AdminGroupName())
}
}
func execElevatedManagerServiceInstaller() error {
path, err := os.Executable()
if err != nil {
return err
}
err = elevate.ShellExecute(path, "/installmanagerservice", "", windows.SW_SHOW)
if err != nil && err != windows.ERROR_CANCELLED {
return err
}
os.Exit(0)
return windows.ERROR_UNHANDLED_EXCEPTION // Not reached
}
func pipeFromHandleArgument(handleStr string) (*os.File, error) {
handleInt, err := strconv.ParseUint(handleStr, 10, 64)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(handleInt), "pipe"), nil
}
func main() {
if windows.SetDllDirectory("") != nil || windows.SetDefaultDllDirectories(windows.LOAD_LIBRARY_SEARCH_SYSTEM32) != nil {
panic("failed to restrict dll search path")
}
setLogFile()
checkForWow64()
if len(os.Args) <= 1 {
if ui.RaiseUI() {
return
}
checkForAdminGroup()
err := execElevatedManagerServiceInstaller()
if err != nil {
fatal(err)
}
return
}
switch os.Args[1] {
case "/installmanagerservice":
if len(os.Args) != 2 {
usage()
}
go ui.WaitForRaiseUIThenQuit()
err := manager.InstallManager()
if err != nil {
if err == manager.ErrManagerAlreadyRunning {
checkForAdminDesktop()
}
fatal(err)
}
checkForAdminDesktop()
time.Sleep(30 * time.Second)
fatalf("WireGuard system tray icon did not appear after 30 seconds.")
return
case "/uninstallmanagerservice":
if len(os.Args) != 2 {
usage()
}
err := manager.UninstallManager()
if err != nil {
fatal(err)
}
return
case "/managerservice":
if len(os.Args) != 2 {
usage()
}
err := manager.Run()
if err != nil {
fatal(err)
}
return
case "/installtunnelservice":
if len(os.Args) != 3 {
usage()
}
err := manager.InstallTunnel(os.Args[2])
if err != nil {
fatal(err)
}
return
case "/uninstalltunnelservice":
if len(os.Args) != 3 {
usage()
}
err := manager.UninstallTunnel(os.Args[2])
if err != nil {
fatal(err)
}
return
case "/tunnelservice":
if len(os.Args) != 3 {
usage()
}
err := tunnel.Run(os.Args[2])
if err != nil {
fatal(err)
}
return
case "/ui":
if len(os.Args) != 6 {
usage()
}
var processToken windows.Token
isAdmin := false
err := windows.OpenProcessToken(windows.CurrentProcess(), windows.TOKEN_QUERY|windows.TOKEN_DUPLICATE, &processToken)
if err == nil {
isAdmin = elevate.TokenIsElevatedOrElevatable(processToken)
processToken.Close()
}
if isAdmin {
err := elevate.DropAllPrivileges(false)
if err != nil {
fatal(err)
}
}
readPipe, err := pipeFromHandleArgument(os.Args[2])
if err != nil {
fatal(err)
}
writePipe, err := pipeFromHandleArgument(os.Args[3])
if err != nil {
fatal(err)
}
eventPipe, err := pipeFromHandleArgument(os.Args[4])
if err != nil {
fatal(err)
}
ringlogger.Global, err = ringlogger.NewRingloggerFromInheritedMappingHandle(os.Args[5], "GUI")
if err != nil {
fatal(err)
}
manager.InitializeIPCClient(readPipe, writePipe, eventPipe)
ui.IsAdmin = isAdmin
ui.RunUI()
return
case "/dumplog":
if len(os.Args) != 2 && len(os.Args) != 3 {
usage()
}
outputHandle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil {
fatal(err)
}
if outputHandle == 0 {
fatal("Stdout must be set")
}
file := os.NewFile(uintptr(outputHandle), "stdout")
defer file.Close()
logPath, err := conf.LogFile(false)
if err != nil {
fatal(err)
}
err = ringlogger.DumpTo(logPath, file, len(os.Args) == 3 && os.Args[2] == "/tail")
if err != nil {
fatal(err)
}
return
case "/update":
if len(os.Args) != 2 {
usage()
}
for progress := range updater.DownloadVerifyAndExecute(0) {
if len(progress.Activity) > 0 {
if progress.BytesTotal > 0 || progress.BytesDownloaded > 0 {
var percent float64
if progress.BytesTotal > 0 {
percent = float64(progress.BytesDownloaded) / float64(progress.BytesTotal) * 100.0
}
log.Printf("%s: %d/%d (%.2f%%)\n", progress.Activity, progress.BytesDownloaded, progress.BytesTotal, percent)
} else {
log.Println(progress.Activity)
}
}
if progress.Error != nil {
log.Printf("Error: %v\n", progress.Error)
}
if progress.Complete || progress.Error != nil {
return
}
}
return
case "/removedriver":
if len(os.Args) != 2 {
usage()
}
_ = driver.UninstallLegacyWintun() // Best effort
err := driver.Uninstall()
if err != nil {
fatal(err)
}
return
}
usage()
}