-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_windows.go
137 lines (108 loc) · 2.7 KB
/
main_windows.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
// +build windows
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
godnschange "github.com/inverse-inc/go-dnschange"
"github.com/inverse-inc/wireguard-go/binutils"
"github.com/inverse-inc/wireguard-go/device"
"github.com/inverse-inc/wireguard-go/ipc"
"github.com/inverse-inc/wireguard-go/outputlog"
"github.com/inverse-inc/wireguard-go/tun"
"github.com/inverse-inc/wireguard-go/util"
"github.com/inverse-inc/wireguard-go/ztn"
"github.com/joho/godotenv"
)
const (
ExitSetupSuccess = 0
ExitSetupFailed = 1
)
var logger *device.Logger
var DNSChange *godnschange.DNSStruct
func main() {
defer binutils.CapturePanic()
outputlog.RedirectOutputToRotatedLog("C:\\Program Files\\PacketFence-Zero-Trust-Client\\wireguard.log")
godotenv.Load(os.Args[1])
if len(os.Args) > 2 && os.Args[2] == "--master" {
logger = device.NewLogger(
device.LogLevelInfo,
fmt.Sprintf("(%s) ", "Master"),
)
os.Setenv("LOG_LEVEL", "info")
setMasterProcess()
go func() {
defer binutils.CapturePanic()
DNSChange = StartDNS()
}()
go checkParentIsAlive()
for {
binutils.RunTunnelFG(os.Args[1])
}
} else {
interfaceName := "wg0"
logger = device.NewLogger(
device.LogLevelInfo,
fmt.Sprintf("(%s) ", interfaceName),
)
os.Setenv("LOG_LEVEL", "info")
logger.Info.Println("Starting wireguard-go version", device.WireGuardGoVersion)
logger.Debug.Println("Debug log enabled")
tun, err := tun.CreateTUN(interfaceName, 0)
if err == nil {
realInterfaceName, err2 := tun.Name()
if err2 == nil {
interfaceName = realInterfaceName
}
} else {
logger.Error.Println("Failed to create TUN device:", err)
os.Exit(ExitSetupFailed)
}
device := device.NewDevice(tun, logger)
device.Up()
logger.Info.Println("Device started")
uapi, err := ipc.UAPIListen(interfaceName)
if err != nil {
logger.Error.Println("Failed to listen on uapi socket:", err)
os.Exit(ExitSetupFailed)
}
errs := make(chan error)
term := make(chan os.Signal, 1)
go func() {
for {
conn, err := uapi.Accept()
if err != nil {
errs <- err
return
}
go device.IpcHandle(conn)
}
}()
logger.Info.Println("UAPI listener started")
startInverse(interfaceName, device)
// wait for program to terminate
signal.Notify(term, os.Interrupt)
signal.Notify(term, os.Kill)
signal.Notify(term, syscall.SIGTERM)
select {
case <-term:
case <-errs:
case <-device.Wait():
}
// clean up
uapi.Close()
device.Close()
logger.Info.Println("Shutting down")
quit()
}
}
func checkParentIsAlive() {
if !ztn.RunningInCLI() {
util.CheckGUIIsAliveWindows(quit)
}
}