2020-12-29 17:52:15 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-05 23:15:25 +00:00
|
|
|
|
"bytes"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
"encoding/json"
|
2020-12-31 11:20:58 +00:00
|
|
|
|
"errors"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
"flag"
|
2020-12-31 11:20:58 +00:00
|
|
|
|
"fmt"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2021-02-05 23:15:25 +00:00
|
|
|
|
"net/http/cookiejar"
|
2021-02-06 21:32:24 +00:00
|
|
|
|
"os"
|
2021-02-05 23:15:25 +00:00
|
|
|
|
"strconv"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2021-02-05 23:57:51 +00:00
|
|
|
|
|
|
|
|
|
_ "git.nils.zone/nils/prettify"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2021-02-06 21:32:24 +00:00
|
|
|
|
iso8601 = "2006-01-02T15:04:05-0700"
|
2020-12-29 17:52:15 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// flags
|
2021-02-06 21:32:24 +00:00
|
|
|
|
var configPath = flag.String("configPath", "config.json", "Path to config.json")
|
2021-01-02 11:32:38 +00:00
|
|
|
|
var version = "development"
|
2021-02-06 21:32:24 +00:00
|
|
|
|
var delay time.Duration = 5 * time.Second
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
|
|
|
|
func main() {
|
2021-01-02 21:16:07 +00:00
|
|
|
|
log.Printf("starting version %s...\n", version)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
// parse all flags
|
|
|
|
|
flag.Parse()
|
2021-02-06 21:32:24 +00:00
|
|
|
|
c := loadConfig(*configPath)
|
2021-02-06 10:46:08 +00:00
|
|
|
|
|
|
|
|
|
// check if flags are set
|
2021-02-06 21:32:24 +00:00
|
|
|
|
if *configPath == "" {
|
|
|
|
|
log.Fatalln("Please specify path to config.json flag '-configPath'")
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
2021-01-02 21:16:07 +00:00
|
|
|
|
// start API processing (runs in a loop)
|
2021-02-06 21:32:24 +00:00
|
|
|
|
go processAPIs(c)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
2021-02-06 10:46:08 +00:00
|
|
|
|
// start webserver on Port 3000
|
2021-01-02 21:16:07 +00:00
|
|
|
|
serveJSON()
|
|
|
|
|
}
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func loadConfig(file string) Config {
|
|
|
|
|
var config Config
|
|
|
|
|
configFile, err := os.Open(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
2021-02-06 21:32:24 +00:00
|
|
|
|
jsonParse := json.NewDecoder(configFile)
|
|
|
|
|
jsonParse.Decode(&config)
|
|
|
|
|
return config
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//int to bool converter
|
2021-02-05 23:15:25 +00:00
|
|
|
|
func itob(i int) bool {
|
|
|
|
|
if i == 1 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//Unifi Controller API processing
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func processUcAPIs(c Config) ([]node, []link) {
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//get list of Unifi devices to display
|
2021-02-05 23:15:25 +00:00
|
|
|
|
var nodes []node
|
2021-02-06 10:46:08 +00:00
|
|
|
|
var links []link
|
2021-02-06 21:32:24 +00:00
|
|
|
|
d, err := getDevices(c, "ucDevices.json")
|
2021-02-05 23:15:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//call Unifi Controller
|
2021-02-06 21:32:24 +00:00
|
|
|
|
ucAPI := newAPI(c.Unifi.User, c.Unifi.Password, c.Unifi.APIURL)
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//login
|
2021-02-05 23:15:25 +00:00
|
|
|
|
ucAPI.ucLogin()
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//get all Sites from Controller
|
2021-02-05 23:15:25 +00:00
|
|
|
|
sites, err := ucAPI.ucGetSites()
|
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
log.Fatalln(err)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//get all devices in all sites
|
2021-02-05 23:15:25 +00:00
|
|
|
|
devices, err := ucAPI.ucGetDevices(sites)
|
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
log.Fatalln(err)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
|
|
|
|
|
//build nodes struct
|
2021-02-05 23:15:25 +00:00
|
|
|
|
for _, jsonDevice := range d.Devices {
|
|
|
|
|
var currentDevice ucDevice
|
|
|
|
|
var currentJSONDevice device
|
|
|
|
|
for _, device := range devices {
|
|
|
|
|
if strings.ToUpper(device.Mac) == strings.ToUpper(jsonDevice.MAC) {
|
|
|
|
|
currentDevice = device
|
|
|
|
|
currentJSONDevice = jsonDevice
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
if isRemoteMACpublished(jsonDevice.MAC, d.Devices) == true {
|
|
|
|
|
links = ucAddLink(jsonDevice, links)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 23:15:25 +00:00
|
|
|
|
load, err := strconv.ParseFloat(currentDevice.Sysstats.CPU, 64)
|
2021-01-02 21:16:07 +00:00
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
log.Fatalln(err)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-05 23:15:25 +00:00
|
|
|
|
mem, err := strconv.ParseFloat(currentDevice.Sysstats.Memory, 64)
|
2021-01-02 21:16:07 +00:00
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
log.Fatalln(err)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-01-02 21:16:07 +00:00
|
|
|
|
|
2021-02-05 23:15:25 +00:00
|
|
|
|
nodes = append(nodes, node{
|
|
|
|
|
Firstseen: "0",
|
|
|
|
|
Lastseen: time.Unix(int64(currentDevice.LastSeen), 0).Format(iso8601),
|
|
|
|
|
IsOnline: itob(currentDevice.State),
|
|
|
|
|
IsGateway: false,
|
|
|
|
|
Clients: 0,
|
|
|
|
|
ClientsWifi24: 0,
|
|
|
|
|
ClientsWifi5: 0,
|
|
|
|
|
ClientsOther: 0,
|
|
|
|
|
RootFSUsage: 0,
|
|
|
|
|
LoadAVG: load / 100,
|
|
|
|
|
MemoryUsage: mem / 100,
|
|
|
|
|
Uptime: time.Now().Add(-1 * time.Second * time.Duration(currentDevice.Uptime)).Format(iso8601),
|
|
|
|
|
GatewayNexthop: currentJSONDevice.GatewayNexthop,
|
|
|
|
|
Gateway: currentJSONDevice.Gateway,
|
|
|
|
|
Location: currentJSONDevice.Location,
|
|
|
|
|
NodeID: strings.ReplaceAll(currentDevice.Mac, ":", ""),
|
|
|
|
|
MAC: currentDevice.Mac,
|
|
|
|
|
Adresses: []string{currentDevice.IP},
|
|
|
|
|
Domain: currentJSONDevice.Domain,
|
|
|
|
|
Hostname: "[Unifi] " + currentDevice.Name,
|
|
|
|
|
Owner: "Freifunk Rhein-Sieg",
|
|
|
|
|
Firmware: firmware{
|
|
|
|
|
Base: "Ubiquiti - Stock",
|
|
|
|
|
Release: currentDevice.Version,
|
|
|
|
|
},
|
|
|
|
|
Autoupdater: autoupdater{
|
|
|
|
|
Enabled: false,
|
|
|
|
|
Branch: "stable",
|
|
|
|
|
},
|
|
|
|
|
NProc: 1,
|
|
|
|
|
Model: lookupModels(currentDevice.Model),
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
return nodes, links
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 10:46:08 +00:00
|
|
|
|
//UNMS API processing (Richtfunk)
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func processUNMSAPI(c Config) ([]node, []link) {
|
2021-02-05 23:15:25 +00:00
|
|
|
|
// Variables for runtime
|
|
|
|
|
var links []link
|
|
|
|
|
var nodes []node
|
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
d, err := getDevices(c, "devices.json")
|
2021-02-05 23:15:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// API CALL 1
|
|
|
|
|
log.Println("calling API 1")
|
|
|
|
|
var u []unifiAPIResponse
|
2021-02-06 21:32:24 +00:00
|
|
|
|
err = callUnifiAPI(c, "/devices", &u)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
2021-01-02 21:16:07 +00:00
|
|
|
|
|
2021-02-05 23:15:25 +00:00
|
|
|
|
for i := range d.Devices {
|
|
|
|
|
var dev unifiAPIResponse
|
|
|
|
|
var currentDevice device
|
|
|
|
|
for j := range u {
|
|
|
|
|
if strings.ToUpper(u[j].Identification.MAC) == strings.ToUpper(d.Devices[i].MAC) {
|
|
|
|
|
dev = u[j]
|
|
|
|
|
currentDevice = d.Devices[i]
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-05 23:15:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isOnline bool = false
|
|
|
|
|
if dev.Overview.Status == "active" {
|
|
|
|
|
isOnline = true
|
|
|
|
|
}
|
|
|
|
|
// END OF API CALL 1
|
|
|
|
|
|
|
|
|
|
// API CALL 2
|
|
|
|
|
log.Println("calling API 2 for device", d.Devices[i].Name)
|
|
|
|
|
var details unifiAPIDetails
|
2021-02-06 21:32:24 +00:00
|
|
|
|
callUnifiAPI(c, "/devices/erouters/"+dev.Identification.ID, &details)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
// END OF API CALL 2
|
|
|
|
|
|
|
|
|
|
// API CALL 3
|
|
|
|
|
log.Println("calling API 3 for device", d.Devices[i].Name)
|
|
|
|
|
var airmaxes []unifiAPIAirmax
|
2021-02-06 21:32:24 +00:00
|
|
|
|
callUnifiAPI(c, "/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
// check if remote mac address is part of our published network
|
|
|
|
|
for i := range airmaxes {
|
|
|
|
|
if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) == true {
|
|
|
|
|
links = addLink(dev, airmaxes[i], links)
|
2021-01-02 21:16:07 +00:00
|
|
|
|
}
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-05 23:15:25 +00:00
|
|
|
|
// END OF API CALL 3
|
|
|
|
|
|
|
|
|
|
// Get info from json file (static)
|
|
|
|
|
nodes = append(nodes, node{
|
|
|
|
|
Firstseen: dev.Overview.CreatedAt.Format(iso8601),
|
|
|
|
|
Lastseen: dev.Overview.LastSeen.Format(iso8601),
|
|
|
|
|
IsOnline: isOnline,
|
|
|
|
|
IsGateway: false,
|
|
|
|
|
Clients: 0,
|
|
|
|
|
ClientsWifi24: 0,
|
|
|
|
|
ClientsWifi5: 0,
|
|
|
|
|
ClientsOther: 0,
|
|
|
|
|
RootFSUsage: 0,
|
|
|
|
|
LoadAVG: details.Overview.CPU / 100,
|
|
|
|
|
MemoryUsage: details.Overview.RAM / 100,
|
|
|
|
|
Uptime: dev.Identification.Started.Format(iso8601),
|
|
|
|
|
GatewayNexthop: currentDevice.GatewayNexthop,
|
|
|
|
|
Gateway: currentDevice.Gateway,
|
|
|
|
|
Location: currentDevice.Location,
|
|
|
|
|
NodeID: strings.ReplaceAll(dev.Identification.MAC, ":", ""),
|
|
|
|
|
MAC: dev.Identification.MAC,
|
|
|
|
|
Adresses: getAddresses(details.IPAddress),
|
|
|
|
|
Domain: currentDevice.Domain,
|
|
|
|
|
Hostname: "[RiFu] " + details.Identification.Name,
|
|
|
|
|
Owner: "Freifunk Rhein-Sieg",
|
|
|
|
|
Firmware: firmware{
|
|
|
|
|
Base: "Ubiquiti - Stock",
|
|
|
|
|
Release: details.Firmware.Current,
|
|
|
|
|
},
|
|
|
|
|
Autoupdater: autoupdater{
|
|
|
|
|
Enabled: false,
|
|
|
|
|
Branch: "stable",
|
|
|
|
|
},
|
|
|
|
|
NProc: 1,
|
|
|
|
|
Model: details.Identification.Model,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return nodes, links
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func processAPIs(c Config) {
|
2021-02-05 23:15:25 +00:00
|
|
|
|
tick := time.Tick(delay)
|
|
|
|
|
for range tick {
|
|
|
|
|
var nodes []node
|
|
|
|
|
var links []link
|
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
unmsNodes, unmsLinks := processUNMSAPI(c)
|
|
|
|
|
ucNodes, ucLinks := processUcAPIs(c)
|
2021-02-05 23:15:25 +00:00
|
|
|
|
nodes = append(nodes, unmsNodes...)
|
|
|
|
|
nodes = append(nodes, ucNodes...)
|
2021-02-06 10:46:08 +00:00
|
|
|
|
links = append(links, unmsLinks...)
|
|
|
|
|
links = append(links, ucLinks...)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
2021-01-02 21:16:07 +00:00
|
|
|
|
// assemble final struct
|
|
|
|
|
o := output{
|
|
|
|
|
Timestamp: time.Now().Format(iso8601),
|
|
|
|
|
Nodes: nodes,
|
|
|
|
|
Links: links,
|
|
|
|
|
}
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
2021-01-02 21:16:07 +00:00
|
|
|
|
// create file output
|
|
|
|
|
log.Println("writing json file")
|
2020-12-31 11:20:58 +00:00
|
|
|
|
|
2021-01-02 21:16:07 +00:00
|
|
|
|
if err := o.writeToFile(); err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we're done here
|
|
|
|
|
log.Println("...done")
|
|
|
|
|
}
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-05 23:57:51 +00:00
|
|
|
|
func getFile(url string) []byte {
|
|
|
|
|
resp, err := http.Get(url)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
if err != nil {
|
2021-02-05 23:57:51 +00:00
|
|
|
|
fmt.Printf("error")
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-05 23:57:51 +00:00
|
|
|
|
data := resp.Body
|
|
|
|
|
byteValue, _ := ioutil.ReadAll(data)
|
|
|
|
|
return byteValue
|
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func getDevices(c Config, file string) (devices, error) {
|
2021-02-05 23:57:51 +00:00
|
|
|
|
// get devices from JSON file
|
2021-02-06 21:32:24 +00:00
|
|
|
|
jsonFile := getFile(c.Unms.DevicesURL + file)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
|
|
|
|
|
// read file to bytes
|
|
|
|
|
// variable for d
|
|
|
|
|
var d devices
|
|
|
|
|
// unmarshal to struct
|
2021-02-06 21:32:24 +00:00
|
|
|
|
err := json.Unmarshal(jsonFile, &d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("can´t get devices file from " + c.Unms.DevicesURL)
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2020-12-31 11:20:58 +00:00
|
|
|
|
return d, nil
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 21:32:24 +00:00
|
|
|
|
func callUnifiAPI(c Config, url string, i interface{}) error {
|
|
|
|
|
request, err := http.NewRequest(http.MethodGet, c.Unms.UnmsAPIURL+url, nil)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url))
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
2021-02-06 21:32:24 +00:00
|
|
|
|
request.Header.Set("x-auth-token", c.Unms.APItoken)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
client := &http.Client{}
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
|
if err != nil {
|
2021-02-06 21:32:24 +00:00
|
|
|
|
return fmt.Errorf("can't get request %s with x-auth-token %s", c.Unms.UnmsAPIURL+url, c.Unms.APItoken)
|
|
|
|
|
}
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
log.Fatalln("Can´t call UNMS API, check token and URL. HTTP Status: ", response.StatusCode)
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
|
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
2020-12-31 11:20:58 +00:00
|
|
|
|
defer response.Body.Close()
|
2020-12-29 17:52:15 +00:00
|
|
|
|
if err != nil {
|
2020-12-31 11:20:58 +00:00
|
|
|
|
return fmt.Errorf("can't read response body: %+v", response.Body)
|
|
|
|
|
}
|
|
|
|
|
// no error occurred, unmarshal to struct
|
2020-12-29 17:52:15 +00:00
|
|
|
|
json.Unmarshal(data, &i)
|
2020-12-31 11:20:58 +00:00
|
|
|
|
return nil
|
2020-12-29 17:52:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isRemoteMACpublished(mac string, devices []device) bool {
|
|
|
|
|
for i := range devices {
|
|
|
|
|
if devices[i].MAC == mac {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addLink(dev unifiAPIResponse, airmaxes unifiAPIAirmax, links []link) []link {
|
|
|
|
|
for i := range links {
|
|
|
|
|
if links[i].SourceAddr == airmaxes.DeviceIdentification.MAC {
|
|
|
|
|
// link already exists
|
|
|
|
|
return links
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
links = append(links, link{
|
|
|
|
|
Type: "wifi",
|
|
|
|
|
Source: strings.ReplaceAll(dev.Identification.MAC, ":", ""),
|
|
|
|
|
Target: strings.ReplaceAll(airmaxes.DeviceIdentification.MAC, ":", ""),
|
|
|
|
|
SourceTQ: airmaxes.Statistics.LinkScore,
|
|
|
|
|
TargetTQ: airmaxes.Statistics.LinkScore,
|
|
|
|
|
SourceAddr: dev.Identification.MAC,
|
|
|
|
|
TargetAddr: airmaxes.DeviceIdentification.MAC,
|
|
|
|
|
})
|
|
|
|
|
return links
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getAddresses(ip string) []string {
|
|
|
|
|
var adresses []string
|
|
|
|
|
adresses = append(adresses, strings.Split(ip, "/")[0])
|
|
|
|
|
return adresses
|
|
|
|
|
}
|
2021-01-02 21:16:07 +00:00
|
|
|
|
|
|
|
|
|
func serveJSON() {
|
|
|
|
|
fs := http.FileServer(http.Dir("./output"))
|
|
|
|
|
http.Handle("/", fs)
|
|
|
|
|
|
|
|
|
|
log.Println("Listening on :3000...")
|
|
|
|
|
err := http.ListenAndServe(":3000", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-05 23:15:25 +00:00
|
|
|
|
|
|
|
|
|
func httpClient() *http.Client {
|
|
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
client := &http.Client{Jar: jar}
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newAPI(user string, pass string, baseURL string) ucAPIData {
|
|
|
|
|
return ucAPIData{
|
|
|
|
|
user: user,
|
|
|
|
|
pass: pass,
|
|
|
|
|
baseURL: baseURL,
|
|
|
|
|
client: httpClient(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *ucAPIData) ucCallAPI(url string, method string, body *bytes.Buffer, output interface{}) error {
|
|
|
|
|
req, err := http.NewRequest(method, u.baseURL+url, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("can't set request %s", u.baseURL+url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
response, err := u.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("can't login %s", u.baseURL+url)
|
|
|
|
|
}
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
return fmt.Errorf("Login failed %s", u.baseURL+url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &output)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *ucAPIData) ucLogin() error {
|
|
|
|
|
var loginData = []byte(`{"username":"` + u.user + `","password":"` + u.pass + `"}`)
|
|
|
|
|
|
|
|
|
|
url := "/api/login"
|
|
|
|
|
err := u.ucCallAPI(url, http.MethodPost, bytes.NewBuffer(loginData), nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *ucAPIData) ucGetSites() ([]ucSite, error) {
|
|
|
|
|
var d struct {
|
|
|
|
|
Data []ucSite `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url := "/api/self/sites"
|
|
|
|
|
err := u.ucCallAPI(url, http.MethodGet, bytes.NewBuffer([]byte{}), &d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []ucSite{}, err
|
|
|
|
|
}
|
|
|
|
|
return d.Data, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *ucAPIData) ucGetDevices(sites []ucSite) ([]ucDevice, error) {
|
|
|
|
|
var d struct {
|
|
|
|
|
Data []ucDevice `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
var s []ucDevice
|
|
|
|
|
|
|
|
|
|
for _, site := range sites {
|
|
|
|
|
url := "/api/s/" + site.ID + "/stat/device"
|
|
|
|
|
u.ucCallAPI(url, http.MethodGet, bytes.NewBuffer([]byte{}), &d)
|
|
|
|
|
s = append(s, d.Data...)
|
|
|
|
|
}
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
2021-02-06 10:46:08 +00:00
|
|
|
|
|
|
|
|
|
func ucAddLink(dev device, links []link) []link {
|
|
|
|
|
for i := range links {
|
|
|
|
|
if links[i].SourceAddr == dev.MAC {
|
|
|
|
|
// link already exists
|
|
|
|
|
return links
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if dev.LinkedTo == "" {
|
|
|
|
|
//no LinkedTo in ucDevices.json
|
|
|
|
|
return links
|
|
|
|
|
}
|
|
|
|
|
links = append(links, link{
|
|
|
|
|
Type: "cable",
|
|
|
|
|
Source: strings.ReplaceAll(dev.MAC, ":", ""),
|
|
|
|
|
Target: strings.ReplaceAll(dev.GatewayNexthop, ":", ""),
|
2021-02-06 10:53:43 +00:00
|
|
|
|
SourceTQ: 1,
|
|
|
|
|
TargetTQ: 1,
|
2021-02-06 10:46:08 +00:00
|
|
|
|
SourceAddr: dev.MAC,
|
|
|
|
|
TargetAddr: dev.LinkedTo,
|
|
|
|
|
})
|
|
|
|
|
return links
|
|
|
|
|
}
|