Skip to content

Commit 45e3991

Browse files
committed
kickoff cleaner using struct to hold details
1 parent bc28ca3 commit 45e3991

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

main.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,52 @@ func main() {
2727
sourceConfigFile := flag.String("config", "~/.awsvpn.conf", "Source aws vpn config file")
2828
flag.Parse()
2929
configFilename, serverURL, serverPort, err := createTempConfigFile(*sourceConfigFile)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
defer os.Remove(configFilename)
3034
ips, err := net.LookupIP("dns." + serverURL) // have to use "random" subdomain
3135
if err != nil || len(ips) == 0 {
3236
fmt.Fprintf(os.Stderr, "Could not get IPs for VPN server : %v\n", err)
3337
os.Exit(1)
3438
}
35-
3639
serverURL = ips[0].String()
37-
if err != nil {
38-
log.Fatal(err)
39-
}
40-
defer os.Remove(configFilename)
4140
fmt.Printf("Starting vpn to %s:%s\n", serverURL, serverPort)
4241
//Connect once to find the saml auth url to use
43-
samlAuthpage, sid, err := initalcontactFindSAMLURL(configFilename, serverURL, serverPort)
44-
if err != nil {
45-
log.Fatal(err)
46-
}
47-
fmt.Println("Opening webpage to auth now", samlAuthpage)
48-
openbrowser(samlAuthpage)
49-
a := newSAMLAuth(sid, serverURL, serverPort, configFilename)
42+
43+
a := newawsSAMLAuthWrapper(serverURL, serverPort, configFilename)
5044
a.runHTTPServer()
5145
}
5246

53-
type SAMLAuth struct {
47+
type awsSAMLAuthWrapper struct {
48+
reauthrequest chan bool
5449
samlResponseChan chan string
5550
sidID string
5651
server string
5752
port string
5853
confpath string
5954
}
6055

61-
func newSAMLAuth(sid, server, port, confpath string) *SAMLAuth {
62-
s := &SAMLAuth{samlResponseChan: make(chan string, 2), sidID: sid, server: server, port: port, confpath: confpath}
56+
func newawsSAMLAuthWrapper(server, port, confpath string) *awsSAMLAuthWrapper {
57+
s := &awsSAMLAuthWrapper{
58+
samlResponseChan: make(chan string, 2),
59+
sidID: "",
60+
server: server,
61+
port: port,
62+
confpath: confpath,
63+
reauthrequest: make(chan bool, 2),
64+
}
6365
return s
6466
}
65-
func (s *SAMLAuth) runHTTPServer() {
67+
func (s *awsSAMLAuthWrapper) runHTTPServer() {
6668
go s.worker()
69+
s.reauthrequest <- true // Kick it all off
6770
http.HandleFunc("/", s.handleSAMLServer)
6871
log.Printf("Starting HTTP server at 127.0.0.1:35001")
6972
http.ListenAndServe("127.0.0.1:35001", nil)
7073
}
7174

72-
func (s *SAMLAuth) worker() {
75+
func (s *awsSAMLAuthWrapper) worker() {
7376
//Listens for events from saml http server and spawns openvpn as appropriate
7477
for {
7578
select {
@@ -80,11 +83,22 @@ func (s *SAMLAuth) worker() {
8083
//we have authentication, lets spawn the correct openvpn
8184
fmt.Println("Starting the actual openvpn ")
8285
runOpenVPNAuthenticated(auth, s.sidID, s.server, s.port, s.confpath)
83-
86+
case <-s.reauthrequest:
87+
//Startup the first stage to get our authentication going
88+
s.stageOne()
8489
}
8590
}
8691
}
87-
func (s *SAMLAuth) handleSAMLServer(w http.ResponseWriter, r *http.Request) {
92+
func (s *awsSAMLAuthWrapper) stageOne() {
93+
samlAuthpage, sid, err := initalcontactFindSAMLURL(s.confpath, s.server, s.port)
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
s.sidID = sid
98+
fmt.Println("Opening webpage to auth now", samlAuthpage)
99+
openbrowser(samlAuthpage)
100+
}
101+
func (s *awsSAMLAuthWrapper) handleSAMLServer(w http.ResponseWriter, r *http.Request) {
88102
switch r.Method {
89103
case "POST":
90104
if err := r.ParseForm(); err != nil {

0 commit comments

Comments
 (0)