ubnt-freifunk-map-api/main.go

169 lines
3.4 KiB
Go
Raw Normal View History

2020-12-29 17:52:15 +00:00
package main
import (
"encoding/json"
"flag"
"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"
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)
2021-02-10 18:31:31 +00:00
go processAPIs()
2023-03-20 14:38:48 +00:00
//processUNMSAPIRouter()
//createMetrics(influxDBClient())
2021-02-06 10:46:08 +00:00
// start webserver on Port 3000
serveJSON()
}
2020-12-29 17:52:15 +00:00
func loadconfig(file string) config {
var config config
2021-02-06 21:32:24 +00:00
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
}
2023-03-20 14:38:48 +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-10 18:31:31 +00:00
func processAPIs() {
2021-02-05 23:15:25 +00:00
tick := time.Tick(delay)
for range tick {
var nodes []node
var links []link
2021-05-18 19:17:09 +00:00
if conf.Unms.Enabled {
2021-04-05 10:52:35 +00:00
log.Println("Processing UNMS")
2021-02-10 18:31:31 +00:00
unmsNodes, unmsLinks := processUNMSAPI()
2023-03-20 14:38:48 +00:00
unmsRouters := processUNMSAPIRouter()
nodes = append(nodes, unmsNodes...)
2023-03-20 14:38:48 +00:00
nodes = append(nodes, unmsRouters...)
links = append(links, unmsLinks...)
}
2021-05-18 19:17:09 +00:00
if conf.Unifi.Enabled {
2021-04-05 10:52:35 +00:00
log.Println("Processing Unifi")
2021-08-04 18:08:26 +00:00
ucNodes, _ := processUcAPIs()
nodes = append(nodes, ucNodes...)
}
2021-05-18 19:17:09 +00:00
if conf.Meshviewer.Enabled {
2021-04-05 10:52:35 +00:00
log.Println("Processing Meshviewer")
2021-02-13 15:10:41 +00:00
mvNodes, mvLinks := getMeshviewer()
nodes = append(nodes, mvNodes...)
links = append(links, mvLinks...)
}
// assemble final struct
o := output{
Timestamp: time.Now().Format(iso8601),
Nodes: nodes,
Links: links,
}
2020-12-29 17:52:15 +00:00
// create file output
log.Println("writing json file")
if err := o.writeToFile(); err != nil {
log.Fatalln(err)
}
2021-04-05 10:52:35 +00:00
// 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")
}
2020-12-29 17:52:15 +00:00
}
2021-05-18 19:17:09 +00:00
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, _ := ioutil.ReadAll(data)
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
}