-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
104 lines (86 loc) · 2.83 KB
/
client.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
package aoscxgo
import (
"crypto/tls"
"errors"
"fmt"
"log"
"net/http"
)
type Client struct {
// Connection properties.
Hostname string `json:"hostname"`
Username string `json:"username"`
Password string `json:"password"`
Version string `json:"version"`
// Generated after Connect
Cookie *http.Cookie `json:"cookie"`
Csrf string `json:"Csrf"`
// HTTP transport options. Note that the VerifyCertificate setting is
// only used if you do not specify a HTTP transport yourself.
VerifyCertificate bool `json:"verify_certificate"`
Transport *http.Transport `json:"-"`
}
// Connect creates connection to given Client object.
func Connect(c *Client) (*Client, error) {
var err error
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !c.VerifyCertificate},
}
if c.Version == "" || (c.Version != "v10.09" && c.Version == "v10.10") {
c.Version = "v10.09"
}
if c.Transport == nil {
c.Transport = tr
}
cookie, csrf, err := login(c.Transport, c.Hostname, c.Version, c.Username, c.Password)
if err != nil {
return nil, err
}
c.Cookie = cookie
c.Csrf = csrf
return c, err
}
// Logout calls the logout endpoint to clear the session.
func (c *Client) Logout() error {
if c == nil {
return errors.New("nil value to Logout")
}
url := fmt.Sprintf("https://%s/rest/%s/logout", c.Hostname, c.Version)
resp := logout(c.Transport, c.Cookie, c.Csrf, url)
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
return nil
}
// login performs POST to create a cookie for authentication to the given IP with the provided credentials.
func login(http_transport *http.Transport, ip string, rest_version string, username string, password string) (*http.Cookie, string, error) {
url := fmt.Sprintf("https://%s/rest/%s/login?username=%s&password=%s", ip, rest_version, username, password)
req, _ := http.NewRequest("POST", url, nil)
req.Header.Set("accept", "*/*")
req.Header.Set("x-use-csrf-token", "true")
req.Close = false
res, err := http_transport.RoundTrip(req)
if res.Status != "200 OK" {
log.Fatalf("Got error while connecting to switch %s Error %s", res.Status, err)
return nil, "", err
}
fmt.Println("Login Successful")
csrf := res.Header["X-Csrf-Token"][0]
cookie := res.Cookies()[0]
return cookie, csrf, err
}
// logout performs POST to logout using a cookie from the given URL.
func logout(http_transport *http.Transport, cookie *http.Cookie, csrf string, url string) *http.Response {
req, _ := http.NewRequest("POST", url, nil)
req.Header.Set("accept", "*/*")
req.Header.Set("x-csrf-token", csrf)
req.Close = false
req.AddCookie(cookie)
res, err := http_transport.RoundTrip(req)
//Handle Error
if res.Status != "200 OK" {
log.Fatalf("Got error while logging out of switch %s Error %s", res.Status, err)
}
fmt.Println("Logout Successful")
return res
}