-
Notifications
You must be signed in to change notification settings - Fork 1
/
dracDetect.go
177 lines (151 loc) · 4.53 KB
/
dracDetect.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
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/net/publicsuffix"
)
type DracType int
const (
DRAC4 = 4
DRAC5 = 5
DRAC6 = 6
DRAC7 = 7
DRAC8 = 8
UNKNOWN = -1
)
func getDracType(host string, user string, password string) (sessionUser string, sessionPassword string, dracType DracType, ctrlPort int, videoPort int, err error) {
timeout := time.Duration(30 * time.Second)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
//buggy drac4 won't work if we negotiate higher max version
//MaxVersion: tls.VersionTLS10,
},
}
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&options)
if err != nil {
log.Fatal(err)
}
client := http.Client{
Timeout: timeout,
Transport: tr,
Jar: jar,
}
req, err := http.NewRequest("GET", "https://"+host+"/", nil)
if err != nil {
return "", "", -1, -1, -1, err
}
// some dracs have this bug where it will hold the connection open
// and send a bogus content length, which causes this client to hang
req.Header.Add("Connection", "Close")
resp, err := client.Do(req)
if err != nil {
return "", "", -1, -1, -1, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
body := string(bytes[:])
if strings.Contains(body, "iDrac8") {
dracType = DRAC8
} else if strings.Contains(body, "top.document.location.href = \"/index.html?console") {
dracType = DRAC7
} else if strings.Contains(body, "top.document.location.href = \"/sclogin") {
dracType = DRAC6
} else if strings.Contains(body, "top.document.location.replace(\"/cgi-bin/webcgi/index\");") {
dracType = DRAC5
} else if strings.Contains(body, "var s_oemProductName = \"DRAC 4\";") {
dracType = DRAC4
} else {
dracType = UNKNOWN
}
switch {
case dracType >= DRAC6:
return user, password, dracType, 5900, 5900, nil
case dracType == DRAC5:
resp, err := client.PostForm("https://"+host+"/cgi-bin/webcgi/login",
url.Values{"user": {user}, "password": {password}})
if err != nil {
return "", "", -1, -1, -1, err
}
defer client.PostForm("https://"+host+"/cgi-bin/webcgi/logout", url.Values{})
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
body := string(bytes[:])
log.Printf(body)
resp, err = client.Get("https://" + host + "/cgi-bin/webcgi/vkvm?state=1")
if err != nil {
return "", "", -1, -1, -1, err
}
defer resp.Body.Close()
bytes, err = ioutil.ReadAll(resp.Body)
body = string(bytes[:])
log.Printf(body)
sessionId := getValue(body, "<property name=\"vKvmSessionId\" type=\"string\"><value>")
//encryptionEnabled := getValue(body, "<property name=\"EncryptionEnabled\" type=\"boolean\"><value>")
ctrlPortStr := getValue(body, "<property name=\"KMPortNumber\" type=\"text\"><value>")
videoPortStr := getValue(body, "<property name=\"VideoPortNumber\" type=\"text\"><value>")
ctrlPort, _ := strconv.Atoi(ctrlPortStr)
videoPort, _ := strconv.Atoi(videoPortStr)
log.Printf("sessionId=%s ctrlPort=%d videoPort=%d", sessionId, ctrlPort, videoPort)
return sessionId, "", dracType, ctrlPort, videoPort, nil
case dracType == DRAC4:
resp, err := client.PostForm("https://"+host+"/cgi/login",
url.Values{"user": {user}, "hash": {password}})
if err != nil {
return "", "", -1, -1, -1, err
}
defer client.PostForm("https://"+host+"/cgi/logout", url.Values{})
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
body := string(bytes[:])
log.Printf(body)
resp, err = client.Get("https://" + host + "/cgi/vkvm")
if err != nil {
return "", "", -1, -1, -1, err
}
defer resp.Body.Close()
bytes, err = ioutil.ReadAll(resp.Body)
body = string(bytes[:])
log.Printf(body)
sessionNum := getValue(body, "var nSessionNum = ")
sessionId := getValue(body, "var sSessionId = \"")
ctrlPortStr := getValue(body, "var nPort = ")
ctrlPort, _ := strconv.Atoi(ctrlPortStr)
log.Printf("sessionNum=%s sessionId=%s ctrlPort=%d", sessionNum, sessionId, ctrlPort)
return sessionNum, sessionId, dracType, ctrlPort, -1, nil
default:
panic("unimplemented")
}
}
func getValue(str string, prefix string) string {
i := strings.Index(str, prefix)
if i < 0 {
return ""
}
i += len(prefix)
j := i + 1
for ; j < len(str); j++ {
if str[j] == '<' || str[j] == ';' || str[j] == '"' {
break
}
}
return str[i:j]
}
func dracTypeString(dracType DracType) string {
if dracType == UNKNOWN {
return "Unknown"
} else {
return fmt.Sprintf("DRAC%d", dracType)
}
}