-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (146 loc) · 3.9 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
// DIRS This Variable due to is that static and they are used in other sections
var DIRS = []string{
"/etc/wireguard/",
"/etc/wg-users/",
"/etc/wg-users/config",
}
// FILES This Variable due to is that static and they are used in other sections
var FILES = []string{
"/etc/wg-users/users.tsv",
"/etc/wireguard/wg0.conf",
"/etc/wireguard/serverkey",
"/etc/wireguard/serverkey.pub",
}
var wg0 map[string]string
func main() {
if result, _ := isRunningInRoot(); result == false {
fmt.Printf("This program must be run as root!\n")
os.Exit(1)
}
if err := getWGServer(); err != nil {
fmt.Printf("There was a problem reading the config.")
os.Exit(1)
}
if result, err := checkingRequiredFolder(); result == false {
fmt.Printf("%s \nThere was an error creating the files", err)
os.Exit(1)
}
if result, err := checkingRequiredFiles(); result == false {
fmt.Printf("%s \nThere was an error creating the files", err)
os.Exit(1)
}
var endpoint string
createCommand := flag.NewFlagSet("create", flag.ExitOnError)
deleteCommand := flag.NewFlagSet("delete", flag.ExitOnError)
updateCommand := flag.NewFlagSet("update", flag.ExitOnError)
listCommand := flag.NewFlagSet("list", flag.ExitOnError)
configCommand := flag.NewFlagSet("config", flag.ExitOnError)
configCommand.StringVar(&endpoint, "e", "", "This is the external IP of the server.")
if len(os.Args) <= 1 {
printHelp(createCommand, deleteCommand, updateCommand, listCommand, configCommand)
os.Exit(-2)
}
var actions = os.Args[1]
switch strings.ToLower(actions) {
case "create":
if err := createCommand.Parse(os.Args); err != nil {
createCommand.PrintDefaults()
return
}
createUsers(os.Args[2:])
case "update":
if err := updateCommand.Parse(os.Args); err != nil {
updateCommand.PrintDefaults()
return
}
updateUsers(os.Args[2:])
case "delete":
if err := deleteCommand.Parse(os.Args); err != nil {
deleteCommand.PrintDefaults()
return
}
deleteUsers(os.Args[2:])
case "list":
if err := listCommand.Parse(os.Args); err != nil {
listCommand.PrintDefaults()
return
}
listUsers()
case "config":
if err := configCommand.Parse(os.Args); err != nil {
configCommand.PrintDefaults()
return
}
configEndPoint(endpoint)
default:
printHelp(createCommand, deleteCommand, updateCommand, listCommand, configCommand)
}
}
func printHelp(createCommand *flag.FlagSet, deleteCommand *flag.FlagSet, updateCommand *flag.FlagSet, listCommand *flag.FlagSet,
configCommand *flag.FlagSet) {
fmt.Printf(HeadHelp)
fmt.Printf(CreateHelp)
createCommand.PrintDefaults()
fmt.Printf(DeleteHelp)
deleteCommand.PrintDefaults()
fmt.Printf(UpdateHelp)
updateCommand.PrintDefaults()
fmt.Printf(ListHelp)
listCommand.PrintDefaults()
fmt.Printf(ConfigHelp)
configCommand.PrintDefaults()
}
func checkingRequiredFolder() (bool, error) {
for _, dirPath := range DIRS {
_, err := os.Stat(dirPath)
if os.IsNotExist(err) {
errDir := os.MkdirAll(dirPath, 0700)
if errDir != nil {
return false, errDir
}
}
}
return true, nil
}
func isRunningInRoot() (bool, error) {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
return false, err
}
i, uErr := strconv.Atoi(string(output[:len(output)-1]))
if uErr != nil {
return false, uErr
}
return i == 0, nil
}
func checkingRequiredFiles() (bool, error) {
for _, filePath := range FILES {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
theFile, fErr := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if fErr != nil {
return false, fErr
}
if strings.Contains(filePath, "users.tsv") {
_, err := theFile.WriteString("UserName\tIP\tCreation\tPublic Key\tPrivate Key\tPresharedKey\n")
if err != nil {
return false, err
}
}
tErr := theFile.Close()
if tErr != nil {
return false, tErr
}
}
}
return true, nil
}