-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (99 loc) · 2.52 KB
/
main.go
File metadata and controls
117 lines (99 loc) · 2.52 KB
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
//go:generate go install -v github.com/josephspurrier/goversioninfo/cmd/goversioninfo
//go:generate goversioninfo -icon=res/papp.ico -manifest=res/papp.manifest
package main
import (
"os"
"path"
"runtime"
"github.com/portapps/portapps/v3"
"github.com/portapps/portapps/v3/pkg/log"
"github.com/portapps/portapps/v3/pkg/registry"
"github.com/portapps/portapps/v3/pkg/utl"
)
type config struct {
Cleanup bool `yaml:"cleanup" mapstructure:"cleanup"`
}
var (
app *portapps.App
cfg *config
)
func init() {
var err error
// Default config
cfg = &config{
Cleanup: false,
}
// Init app
if app, err = portapps.NewWithCfg("openvpn-portable", "OpenVPN", cfg); err != nil {
log.Fatal().Err(err).Msg("Cannot initialize application. See log file for more info.")
}
}
func main() {
utl.CreateFolder(app.DataPath)
appPath := utl.PathJoin(app.AppPath, "win10")
if app.WinVersion.Major < 10 {
appPath = utl.PathJoin(app.AppPath, "win7")
}
app.Process = utl.PathJoin(appPath, "bin", "openvpn-gui.exe")
app.WorkingDir = appPath
configPath := utl.CreateFolder(app.DataPath, "config")
logPath := utl.CreateFolder(app.DataPath, "log")
app.Args = []string{
"--exe_path",
utl.PathJoin(appPath, "bin", "openvpn.exe"),
"--config_dir",
configPath,
"--ext_string",
"ovpn",
"--log_dir",
logPath,
"--priority_string",
"NORMAL_PRIORITY_CLASS",
"--append_string",
"0",
}
// Cleanup on exit
if cfg.Cleanup {
defer func() {
utl.Cleanup([]string{
path.Join(os.Getenv("USERPROFILE"), "OpenVPN"),
})
}()
}
// Add OpenVPN reg key otherwise a dialog popup
regArch := "32"
if runtime.GOARCH == "amd64" {
regArch = "64"
}
mainRegKey := registry.Key{
Key: `HKLM\SOFTWARE\OpenVPN`,
Arch: regArch,
Default: appPath,
}
if err := mainRegKey.Add(true); err != nil {
log.Error().Err(err).Msg("Cannot add registry key")
}
regFile := utl.PathJoin(utl.CreateFolder(app.RootPath, "reg"), "OpenVPN-GUI.reg")
regKey := registry.Key{
Key: `HKCU\Software\OpenVPN-GUI`,
Arch: "32",
}
if err := regKey.Import(regFile); err != nil {
log.Error().Err(err).Msg("Cannot import registry key")
}
defer func() {
if err := regKey.Export(regFile); err != nil {
log.Error().Err(err).Msg("Cannot export registry key")
}
if cfg.Cleanup {
if err := mainRegKey.Delete(true); err != nil {
log.Error().Err(err).Msg("Cannot remove registry key")
}
if err := regKey.Delete(true); err != nil {
log.Error().Err(err).Msg("Cannot remove registry key")
}
}
}()
defer app.Close()
app.Launch(os.Args[1:])
}