-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasmotalist.go
256 lines (202 loc) · 4.36 KB
/
tasmotalist.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
func FindTasmotaDevices(hosts []Host) (tasmotalist []TasmotaInfo) {
for _, host := range hosts {
var tasmota TasmotaInfo
url := fmt.Sprintf("http://%s/cm?cmnd=status%s", host.Address.Addr, "%200")
res, err := http.Get(url)
if err != nil {
continue
}
if !(res.StatusCode == 401 || res.StatusCode == 200) {
continue
}
content, err := io.ReadAll(res.Body)
if err != nil {
continue
}
err = json.Unmarshal(content, &tasmota)
if err != nil {
continue
}
if tasmota.Warning != "" {
tasmota.StatusNET.IPAddress = host.Address.Addr
tasmota.Status.DeviceName = "Secured. (Requires password)"
tasmota.StatusFWR.Version = "?"
}
tasmotalist = append(tasmotalist, tasmota)
}
return tasmotalist
}
func FindPotentialHosts(hosts []Host) []Host {
return Filter[Host](hosts, func(host Host, _ int) bool {
if host.Status.State != "up" && strings.Contains(host.Status.Reason, "refused") {
return false
}
_, portFound := Find[Port](host.Ports.Port, func(port Port) bool {
return port.State.State == "open" && port.Portid == "80"
})
return portFound
})
}
func CheckNmap() (string, error) {
cmd := "which"
if runtime.GOOS == "windows" {
cmd = "where"
}
output, err := ExecuteCmd(cmd, "nmap")
if err != nil && output == "" {
return "", err
}
if strings.Contains(output, "not found") || strings.Contains(output, "Could not find") {
return "", errors.New("nmap not found")
}
output, err = ExecuteCmd("nmap", "-V")
return output, nil
}
func GetNmapOutput(filePath string) (output NmapOutput, err error) {
xmlFile, err := os.Open(filePath)
defer func(file *os.File) {
err = file.Close()
if err != nil {
panic(err)
}
}(xmlFile)
if err != nil {
return output, err
}
byteValue, err := ioutil.ReadAll(xmlFile)
if err != nil {
return output, err
}
err = xml.Unmarshal(byteValue, &output)
if err != nil {
return output, err
}
return output, nil
}
func ExecuteCmd(cmd string, args ...string) (output string, err error) {
proc := exec.Command(cmd, args...)
reader, err := proc.StdoutPipe()
errreader, err := proc.StderrPipe()
if err != nil {
return "", nil
}
scanner := bufio.NewScanner(reader)
errscanner := bufio.NewScanner(errreader)
go func() {
for errscanner.Scan() {
output += errscanner.Text() + "\n"
}
}()
go func() {
for scanner.Scan() {
output += scanner.Text() + "\n"
}
}()
err = proc.Start()
if err != nil {
return output, err
}
err = proc.Wait()
time.Sleep(time.Second * 1)
if err != nil {
return output, err
}
return output, nil
}
func GetActiveInterfaces() ([]net.Interface, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var validinterfaces []net.Interface
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
if !(iface.Flags&net.FlagUp > 0) {
continue
}
ipv4 := FindPrimaryAddress(addrs)
if ipv4 != nil {
validinterfaces = append(validinterfaces, iface)
}
}
return validinterfaces, nil
}
func FindPrimaryAddresses(ifaces []net.Interface) (addrs []net.IP) {
for _, i := range ifaces {
a, _ := i.Addrs()
addrs = append(addrs, FindPrimaryAddress(a))
}
return addrs
}
func FindPrimaryAddress(addrs []net.Addr) net.IP {
for _, addr := range addrs {
ipStr := addr.String()
i := strings.LastIndex(ipStr, "/")
if i < 0 {
continue
}
ipStr = ipStr[0:i]
ip := net.ParseIP(ipStr)
if ip == nil {
continue
}
ipv4 := ip.To4()
if ipv4 == nil {
continue
}
return ipv4
}
return nil
}
func Filter[V any](collection []V, predicate func(V, int) bool) []V {
result := []V{}
for i, item := range collection {
if predicate(item, i) {
result = append(result, item)
}
}
return result
}
func Find[T any](collection []T, predicate func(T) bool) (T, bool) {
for _, item := range collection {
if predicate(item) {
return item, true
}
}
var result T
return result, false
}
func GetOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return nil, err
}
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
panic(err)
}
}(conn)
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}