ubnt-freifunk-map-api/main.go

189 lines
3.8 KiB
Go
Raw Normal View History

2020-12-29 17:52:15 +00:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
2020-12-29 17:52:15 +00:00
"log"
"net/http"
2021-02-05 23:15:25 +00:00
"net/http/cookiejar"
2021-02-06 21:32:24 +00:00
"os"
2020-12-29 17:52:15 +00:00
"time"
2021-02-10 18:31:31 +00:00
_ "git.nils.zone/nils/prettify"
2020-12-29 17:52:15 +00:00
)
2021-02-10 18:31:31 +00:00
const (
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-05-06 12:49:44 +00:00
var delay time.Duration = 60 * time.Second
2021-02-10 18:31:31 +00:00
var conf = loadconfig(*configPath)
2021-02-13 15:10:41 +00:00
var ucDev = getDevices(conf.Unifi.UCDevicesURL)
2020-12-29 17:52:15 +00:00
func main() {
log.Printf("starting version %s...\n", version)
2020-12-29 17:52:15 +00:00
// parse all flags
flag.Parse()
2021-02-06 10:46:08 +00:00
// check if flags are set
2023-03-20 14:38:48 +00:00
if *configPath == "" {
log.Fatalln("Please specify path to config.json flag '-configPath'")
}
// start API processing (runs in a loop)
go func() {
if err := processAPIs(); err != nil {
2023-05-14 09:44:20 +00:00
log.Fatalln("API processing failed, error is: ", err)
}
tick := time.Tick(delay)
for range tick {
if err := processAPIs(); err != nil {
2023-05-14 09:44:20 +00:00
log.Fatalln("API processing failed, error is: ", err)
}
}
}()
2021-02-06 10:46:08 +00:00
// start webserver on Port 3000
serveJSON()
}
2020-12-29 17:52:15 +00:00
func processAPIs() error {
var nodes []node
var links []link
if conf.Unms.Enabled {
log.Println("Processing UNMS")
2023-05-14 09:44:20 +00:00
unmsNodes, unmsLinks, err := processUNMSAPI()
if err != nil {
return err
}
unmsRouters, err := processUNMSAPIRouter()
2021-04-05 10:52:35 +00:00
if err != nil {
return err
2021-04-05 10:52:35 +00:00
}
nodes = append(nodes, unmsNodes...)
nodes = append(nodes, unmsRouters...)
links = append(links, unmsLinks...)
}
if conf.Unifi.Enabled {
log.Println("Processing Unifi")
2023-05-14 09:44:20 +00:00
ucNodes, _, err := processUcAPIs()
if err != nil {
return err
}
nodes = append(nodes, ucNodes...)
}
if conf.Meshviewer.Enabled {
log.Println("Processing Meshviewer")
mvNodes, mvLinks := getMeshviewer()
nodes = append(nodes, mvNodes...)
links = append(links, mvLinks...)
}
2023-05-11 19:38:10 +00:00
if conf.Gateways.Enabled {
log.Println("Processing Gateways")
gwNodes := processGateways()
nodes = append(nodes, gwNodes...)
}
// assemble final struct
o := output{
Timestamp: time.Now().Format(iso8601),
Nodes: nodes,
Links: links,
}
// create file output
log.Println("writing json file")
if err := o.writeToFile(); err != nil {
log.Fatalln(err)
}
// get outages to serve as .csv
l := getUNMSLogs()
err := writeOutagesToCSV(l)
if err != nil {
log.Println("Error writing outages.csv")
}
// we're done here
log.Println("...done")
return nil
2020-12-29 17:52:15 +00:00
}
2021-05-18 19:17:09 +00:00
2023-05-14 09:44:20 +00:00
func loadconfig(file string) config {
var config config
configFile, err := os.Open(file)
if err != nil {
log.Fatalln("Failed loding Config file: ", err)
}
jsonParse := json.NewDecoder(configFile)
if err := jsonParse.Decode(&config); err != nil {
log.Fatalln(err)
}
return config
}
// int to bool converter
func itob(i int) bool {
return i == 1
}
2023-04-28 13:20:42 +00:00
// function to get file from meshviewer
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-04-05 10:52:35 +00:00
log.Println("Error getting file from:", url)
2020-12-29 17:52:15 +00:00
}
2021-02-05 23:57:51 +00:00
data := resp.Body
byteValue, _ := io.ReadAll(data)
2021-02-05 23:57:51 +00:00
return byteValue
}
2021-02-06 10:46:08 +00:00
2023-03-20 14:38:48 +00:00
// get devices from devices file on webserver (config)
2021-02-13 15:10:41 +00:00
func getDevices(url string) devices {
2021-02-05 23:57:51 +00:00
// get devices from JSON file
2021-02-06 21:44:05 +00:00
jsonFile := getFile(url)
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 {
2021-02-06 21:44:05 +00:00
fmt.Println("can´t get devices file from " + url)
2021-02-06 21:32:24 +00:00
log.Fatal(err)
}
2021-02-13 15:10:41 +00:00
return d
2020-12-29 17:52:15 +00:00
}
2023-03-20 14:38:48 +00:00
// check for MAC Adress in current Devices
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 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
}