-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinstall_windows.go
171 lines (146 loc) · 6.11 KB
/
install_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
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
// +build windows
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"golang.org/x/sys/windows/registry"
)
func install(chromeExtID string, firefoxExtID string) bool {
currentUser, error := user.Current()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get current user\n"))
return false
}
_ = os.Mkdir(filepath.Join(currentUser.HomeDir, "Socketify"), os.ModePerm)
binaryPath, error := os.Executable()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get binary path\n"))
return false
}
binaryStat, error := os.Stat(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get binary stat\n"))
return false
}
if binaryStat.Mode().IsRegular() != true {
os.Stdout.Write([]byte("install: binary is not a regular file\n"))
return false
}
binaryBytes, error := ioutil.ReadFile(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot read binary\n"))
return false
}
userBinaryPath := filepath.Join(currentUser.HomeDir, "Socketify", binaryStat.Name())
error = ioutil.WriteFile(userBinaryPath, binaryBytes, os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot write binary\n"))
return false
}
chromeManifestString := chromeManifest
chromeManifestString = strings.Replace(chromeManifestString, "BIN_PATH", filepath.ToSlash(userBinaryPath), 1)
chromeManifestString = strings.Replace(chromeManifestString, "EXT_ID", chromeExtID, 1)
firefoxManifestString := firefoxManifest
firefoxManifestString = strings.Replace(firefoxManifestString, "BIN_PATH", filepath.ToSlash(userBinaryPath), 1)
firefoxManifestString = strings.Replace(firefoxManifestString, "EXT_ID", firefoxExtID, 1)
chromeManifestPath := filepath.Join(currentUser.HomeDir, "Socketify", "net.socketify.messenger_chrome.json")
error = ioutil.WriteFile(chromeManifestPath, []byte(chromeManifestString), os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot write chrome manifest\n"))
return false
}
firefoxManifestPath := filepath.Join(currentUser.HomeDir, "Socketify", "net.socketify.messenger_firefox.json")
error = ioutil.WriteFile(firefoxManifestPath, []byte(firefoxManifestString), os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte([]byte("install: cannot write firefox manifest\n")))
return false
}
key, _, error := registry.CreateKey(registry.CURRENT_USER, `SOFTWARE\Google\Chrome\NativeMessagingHosts\net.socketify.messenger`, registry.WRITE)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot create chrome registry key\n"))
return false
}
error = key.SetStringValue("", chromeManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot set chrome registry key\n"))
return false
}
key, _, error = registry.CreateKey(registry.CURRENT_USER, `SOFTWARE\Mozilla\NativeMessagingHosts\net.socketify.messenger`, registry.WRITE)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot create firefox registry key\n"))
return false
}
error = key.SetStringValue("", firefoxManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot set firefox registry key\n"))
return false
}
return true
}
func uninstall() {
currentUser, error := user.Current()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get current user\n"))
return
}
binaryPath, error := os.Executable()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get binary path\n"))
return
}
binaryStat, error := os.Stat(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get binary stat\n"))
return
}
if binaryStat.Mode().IsRegular() != true {
os.Stdout.Write([]byte("uninstall: binary is not a regular file\n"))
return
}
userBinaryPath := filepath.Join(currentUser.HomeDir, "Socketify", binaryStat.Name())
error = os.Remove(userBinaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove binary\n"))
}
chromeManifestPath := filepath.Join(currentUser.HomeDir, "Socketify", "net.socketify.messenger_chrome.json")
error = os.Remove(chromeManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove chrome manifest\n"))
}
firefoxManifestPath := filepath.Join(currentUser.HomeDir, "Socketify", "net.socketify.messenger_firefox.json")
error = os.Remove(firefoxManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove firefox manifest\n"))
}
error = registry.DeleteKey(registry.CURRENT_USER, `SOFTWARE\Google\Chrome\NativeMessagingHosts\net.socketify.messenger`)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot delete chrome registry key\n"))
}
error = registry.DeleteKey(registry.CURRENT_USER, `SOFTWARE\Mozilla\NativeMessagingHosts\net.socketify.messenger`)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot delete firefox registry key\n"))
}
}