-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvpn_profile.go
141 lines (130 loc) · 3.18 KB
/
vpn_profile.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
package main
import (
"encoding/json"
"fmt"
"github.com/olekukonko/tablewriter"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strconv"
)
var (
vpnProfileFields = []string{"ID #", "Name", "Username"}
vpnProfileFilePath = path.Join(resourcePath, "vpn_profiles.json")
noSuchFileErrRegexp = regexp.MustCompile(`no such file or directory`)
)
type vpnProfile struct {
Name string `json:"name"`
Psk string `json:"psk"`
UserName string `json:"username"`
PassWord string `json:"password"`
}
func loadProfileFile() []vpnProfile {
file, e := ioutil.ReadFile(vpnProfileFilePath)
if e != nil {
if noSuchFileErrRegexp.MatchString(e.Error()) {
return []vpnProfile{}
}
fmt.Printf("Could not: %v\n", e.Error())
os.Exit(1)
}
var profiles []vpnProfile
err := json.Unmarshal(file, &profiles)
if err != nil {
log.Fatal("Could not load vpn profiles")
}
return profiles
}
func writeProfileFile(profileList []vpnProfile) {
profileJSON, err := json.Marshal(profileList)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Writing profile file to %s\n", vpnProfileFilePath)
writeError := ioutil.WriteFile(vpnProfileFilePath, profileJSON, 0755)
if writeError != nil {
fmt.Print("Could not write profile file\n")
log.Fatal(writeError)
}
fmt.Println("New profile saved!\n")
}
func printVPNProfileList() {
vpnProfiles := loadProfileFile()
consoleTable := tablewriter.NewWriter(os.Stdout)
consoleTable.SetHeader(vpnProfileFields)
for index, vpnProfile := range vpnProfiles {
row := []string{
strconv.Itoa(index),
vpnProfile.Name,
vpnProfile.UserName,
}
consoleTable.Append(row)
}
consoleTable.Render()
}
func selectVPNProfileDetails(profileName string) vpnProfile {
vpnProfiles := loadProfileFile()
var selectedProfile vpnProfile
for _, profile := range vpnProfiles {
if profile.Name == profileName {
selectedProfile = profile
}
}
if selectedProfile.Name == "" {
log.Fatalf("VPN Profile: %s not found in %s", profileName, vpnProfileFilePath)
}
return selectedProfile
}
func detectDuplicateName(providedName string) {
vpnProfiles := loadProfileFile()
for _, profile := range vpnProfiles {
if profile.Name == providedName {
log.Fatalf("profile name %s: Already present, please select another name", providedName)
}
}
}
func detailCapture(attr string) string {
var response string
fmt.Printf("%s ", attr)
_, err := fmt.Scanln(&response)
if err != nil {
if err.Error() == "unexpected newline" {
return ""
}
log.Fatal(err)
}
return response
}
func confirm() bool {
var returnVar bool
confirmation := detailCapture("Save Profile? [y/n]:")
switch confirmation {
case "y":
returnVar = true
case "n":
main()
default:
confirm()
}
return returnVar
}
func addProfile(profileName string) {
vpnProfiles := loadProfileFile()
detectDuplicateName(profileName)
fmt.Printf("Please enter the following values to configure VPN profile %s\n", profileName)
username := detailCapture("USERNAME:")
password := detailCapture("PASSWORD:")
psk := detailCapture("PSK:")
if confirm() {
vpnProfiles = append(vpnProfiles,
vpnProfile{Name: profileName,
UserName: username,
PassWord: password,
Psk: psk,
})
writeProfileFile(vpnProfiles)
}
}