-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
122 lines (109 loc) · 3.31 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
package main
//references
//http://apple.stackexchange.com/questions/128297/how-to-create-a-vpn-connection-via-terminal
//https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man8/scutil.8.html
//https://github.com/halo/macosvpn
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"log"
"os"
"os/user"
"path"
"regexp"
)
var (
//Connection Commands
connect = kingpin.Command("connect", "Connect to a VPN")
profile = connect.Flag("profile", "profile name.").Required().Short('p').Envar("VPN_PROFILE").String()
vpn = connect.Arg("vpn", "Identifier for VPN to be connected").Required().String()
//Disconnect Commands
_ = kingpin.Command("disconnect", "Disconnect current VPN connection")
//Host Commands
hosts = kingpin.Command("host", "Commands related to vpn hosts")
_ = hosts.Command("list", "List vpn hosts")
_ = hosts.Command("refresh", "Refreshes resources")
//Profile Commands
profiles = kingpin.Command("profile", "Commands related to VPN connection profiles")
_ = profiles.Command("list", "List vpn connection profiles")
addProfilecmd = profiles.Command("add", "Add new profile to existing set")
newProfile = addProfilecmd.Arg("profile", "Name of profile to add").Required().String()
//Command Regex Section
connectRegex = regexp.MustCompile(`^connect`)
hostCommadRegex = regexp.MustCompile(`^host`)
profileCommandRegex = regexp.MustCompile(`^profile`)
disconnectCommandRegex = regexp.MustCompile(`^disconnect`)
//Global Vars
cliVersion = "1.0.1"
resourcePath = path.Join(os.Getenv("HOME"), ".vpn_host_manager")
DEBUG = false
)
func permissionCheck() {
cu, err := user.Current()
if err != nil {
log.Fatalln("Could not retrieve user information:", err.Error())
}
if cu.Uid != "0" {
log.Fatal("Please rerun as root or with sudo")
}
}
func listVpnHosts() {
printVPNHostList()
}
func hostFunctions(hostMethod string) {
switch hostMethod {
case "host list":
listVpnHosts()
case "host refresh":
refreshHosts()
}
}
func profileFunctions(profileMethod string) {
switch profileMethod {
case "profile list":
printVPNProfileList()
case "profile add":
addProfile(*newProfile)
default:
log.Fatalf("not sure what to do with command: %s", profileMethod)
}
}
func connectVPN(profileName string, vpnIdentifier string) {
startConnection(vpnIdentifier, profileName)
}
func disconnectVPN() {
fmt.Println("😭 BYE!! 😭")
disconnectConnection()
}
func setupDirectories() {
if _, err := os.Stat(resourcePath); os.IsNotExist(err) {
error := os.Mkdir(resourcePath, 0700)
if error != nil {
log.Fatalf("encountered error during setup, %s", error)
}
}
}
func setup() {
permissionCheck()
setupDirectories()
}
func main() {
kingpin.Version(cliVersion)
setup()
parsedArg := kingpin.Parse()
switch {
case hostCommadRegex.MatchString(parsedArg):
hostFunctions(parsedArg)
case profileCommandRegex.MatchString(parsedArg):
profileFunctions(parsedArg)
case connectRegex.MatchString(parsedArg):
connectVPN(*profile, *vpn)
case disconnectCommandRegex.MatchString(parsedArg):
disconnectVPN()
default:
//if we are in this error block it is because we have established
//a command for the provided text, but have not specified a regex
//for handling it.
log.Fatalf("Command signature not recognized: %s", parsedArg)
}
}