-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAction.go
258 lines (225 loc) · 6.32 KB
/
createAction.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
package main
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"time"
)
const (
WGClientQuickConf = `[Interface]
Address = $address
PrivateKey = $PrivateKey
DNS = 10.0.0.121,1.1.1.1
[Peer]
PublicKey = $PublicKey
PresharedKey = $PresharedKey
AllowedIPs = 0.0.0.0/0
Endpoint = $endpoint
PersistentKeepalive = 15
`
WGPeerQuickConf = `
[Peer]
PublicKey = $PublicKey
PresharedKey = $PresharedKey
AllowedIPs = $address/32
`
)
/**
* User Creation
*/
func createUsers(usersToAdd []string) {
var currentUsers = getUsersList()
usersToAdd = checkUserList(currentUsers, usersToAdd)
if len(usersToAdd) > 0 {
setUpUsersIntoWireGuard(usersToAdd, currentUsers)
}
}
func setUpUsersIntoWireGuard(usersToAdd []string, users []User) {
for _, username := range usersToAdd {
newUser := User{}
newUser.name = username
newUser, errGCK := generateClientKeys(newUser)
if errGCK != nil {
fmt.Printf("There was an error creating the key for %s \n", username)
os.Exit(-1)
}
ip, errGMNIPA := giveMeNextIPAvailable(users)
if errGMNIPA != nil || ip == nil {
fmt.Printf("There was an error retrieveing the IP for %s \n", username)
os.Exit(-1)
}
newUser.ip = ip.String()
var command = "wg set wg0 peer '" + newUser.publicKey + "' preshared-key /etc/wg-users/" + newUser.name + "/" + newUser.name + ".psk allowed-ips " + ip.String()
cmd := exec.Command("bash", "-c", command)
_, errO := cmd.Output()
if errO != nil {
fmt.Printf("There was an error setting up %s user - error :\n %v \n", username, errO)
os.Exit(-1)
}
err := registerUserIntoCSV(newUser)
if err == nil {
users = append(users, newUser)
} else {
fmt.Printf("error in the csv %v", err)
}
err = registerUserIntoTheInterface(newUser)
if err == nil {
users = append(users, newUser)
} else {
fmt.Printf("error in the csv %v", err)
}
err = createWGQuickConfig(newUser)
if err == nil {
fmt.Printf("User %s added correctly\n", newUser.name)
} else {
fmt.Printf("we had some problems creating %s user\n", newUser.name)
}
}
}
func createWGQuickConfig(user User) error {
userConfig := WGClientQuickConf
userConfig = strings.Replace(userConfig, "$address", user.ip, 1)
userConfig = strings.Replace(userConfig, "$PrivateKey", user.privateKey, 1)
userConfig = strings.Replace(userConfig, "$PublicKey", wg0["ServerPublicKey"], 1)
userConfig = strings.Replace(userConfig, "$PresharedKey", user.presharedKey, 1)
userConfig = strings.Replace(userConfig, "$endpoint", getEndPoint()+":"+wg0["ListenPort"], 1)
f, err := os.OpenFile("/etc/wg-users/"+user.name+"/"+user.name+"-wg0.conf", os.O_CREATE|os.O_RDWR, 0600)
if _, err = f.WriteString(userConfig); err != nil {
return err
}
c := exec.Command("bash", "-c", "qrencode -o /etc/wg-users/"+user.name+"/"+user.name+".png < /etc/wg-users/"+user.name+"/"+user.name+"-wg0.conf")
_, err = c.Output()
if err != nil {
return err
}
return nil
}
func getEndPoint() string {
c := exec.Command("cat", DIRS[2]+"/endpoint.conf")
outputC, err := c.Output()
if err != nil {
return ""
}
return string(outputC[:len(outputC)-1])
}
func registerUserIntoCSV(user User) error {
f, err := os.OpenFile(FILES[0], os.O_APPEND|os.O_RDWR, 0600)
var lineToWrite = user.name + "\t" + user.ip + "\t" + user.creation.Format(time.RFC822) + "\t" + user.publicKey + "\t" + user.privateKey + "\t" + user.presharedKey + "\n"
if _, err = f.WriteString(lineToWrite); err != nil {
return err
}
f.Close()
return nil
}
func registerUserIntoTheInterface(user User) error {
f, err := os.OpenFile(FILES[1], os.O_APPEND|os.O_RDWR, 0600)
userConfig := WGPeerQuickConf
userConfig = strings.Replace(userConfig, "$address", user.ip, 1)
userConfig = strings.Replace(userConfig, "$PublicKey", user.publicKey, 1)
userConfig = strings.Replace(userConfig, "$PresharedKey", user.presharedKey, 1)
if _, err = f.WriteString(userConfig); err != nil {
return err
}
f.Close()
return nil
}
func giveMeNextIPAvailable(userList []User) (net.IP, error) {
ip, ipNet, err := net.ParseCIDR(wg0["Address"])
if err != nil {
return nil, err
}
flag := false
for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); inc(ip) {
if ip.String() != "10.51.0.0" && ip.String() != "10.51.0.1" {
if len(userList) == 0 {
return ip, nil
}
for _, user := range userList {
if user.ip == ip.String() {
flag = true
break
}
}
if flag == false {
return ip, nil
} else {
flag = false
}
}
}
return nil, nil
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
/*
* we are going to generate the Private and the Public Key for the User and we will retrieve the public
*/
func generateClientKeys(user User) (User, error) {
userFolder := "/etc/wg-users/" + user.name + "/"
_, err := os.Stat(userFolder)
if os.IsNotExist(err) {
errDir := os.MkdirAll(userFolder, 0700)
if errDir != nil {
return user, errDir
}
}
var command = "wg genkey | tee " + userFolder + user.name + " | wg pubkey > " + userFolder + user.name + ".pub && wg genpsk > " + userFolder + user.name + ".psk"
creationUserCmd := exec.Command("bash", "-c", command)
user.creation = time.Now()
_, err = creationUserCmd.Output()
if err != nil {
return user, err
}
cmd := exec.Command("cat", userFolder+user.name+".pub")
output, err := cmd.Output()
if err != nil {
return user, err
}
user.publicKey = string(output[:len(output)-1])
md := exec.Command("cat", userFolder+user.name)
outputP, err := md.Output()
if err != nil {
return user, err
}
user.privateKey = string(outputP[:len(outputP)-1])
c := exec.Command("cat", userFolder+user.name+".psk")
outputC, err := c.Output()
if err != nil {
return user, err
}
user.presharedKey = string(outputC[:len(outputC)-1])
return user, nil
}
/*
* This function is going to check if any user that is in the list is already in the WireGuard config
* and it will remove it from the list and it will print that user is already in the server
*/
func checkUserList(currentUsers []User, users []string) []string {
if len(currentUsers) < 1 {
return users
}
var cleanUserList []string
userFlag := false
for _, newUser := range users {
for _, user := range currentUsers {
if user.name == newUser {
userFlag = true
break
}
}
if !userFlag {
cleanUserList = append(cleanUserList, newUser)
} else {
userFlag = false
}
}
return cleanUserList
}