-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnetgear_model.go
76 lines (68 loc) · 1.99 KB
/
netgear_model.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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"strings"
)
type NetgearModel string
const (
GS30xEPx NetgearModel = "GS30xEPx"
GS305EP NetgearModel = "GS305EP"
GS305EPP NetgearModel = "GS305EPP"
GS308EP NetgearModel = "GS308EP"
GS308EPP NetgearModel = "GS308EPP"
GS316EP NetgearModel = "GS316EP"
GS316EPP NetgearModel = "GS316EPP"
)
func isModel30x(nm NetgearModel) bool {
return nm == GS305EP || nm == GS305EPP || nm == GS308EP || nm == GS308EPP || nm == GS30xEPx
}
func isModel316(nm NetgearModel) bool {
return nm == GS316EP || nm == GS316EPP
}
func isSupportedModel(modelName string) bool {
return isModel30x(NetgearModel(modelName)) || isModel316(NetgearModel(modelName))
}
func detectNetgearModel(args *GlobalOptions, host string) (NetgearModel, error) {
url := fmt.Sprintf("http://%s/", host)
if args.Verbose {
fmt.Println("detecting Netgear switch model: " + url)
}
resp, err := http.Get(url)
if err != nil {
return "", err
}
if args.Verbose {
fmt.Println(fmt.Sprintf("HTTP response code %d", resp.StatusCode))
}
if resp.StatusCode != 200 {
fmt.Println(fmt.Sprintf("Warning: response code was not 200; unusual, but will attempt detection anyway"))
}
responseBody, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return "", err
}
model := detectNetgearModelFromResponse(string(responseBody))
if model == "" {
return "", errors.New("Can't auto-detect Netgear model from response. You may try using --model parameter ")
}
if args.Verbose {
fmt.Println(fmt.Sprintf("Detected model %s", model))
}
return model, nil
}
func detectNetgearModelFromResponse(body string) NetgearModel {
if strings.Contains(strings.ToLower(body), "<title>") && strings.Contains(body, "GS316EPP") {
return GS316EPP
}
if strings.Contains(strings.ToLower(body), "<title>") && strings.Contains(body, "GS316EP") {
return GS316EP
}
if strings.Contains(strings.ToLower(body), "<title>") && strings.Contains(body, "Redirect to Login") {
return GS30xEPx
}
return ""
}