ubnt-freifunk-map-api/main.go
2023-04-28 15:20:42 +02:00

169 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
"time"
_ "git.nils.zone/nils/prettify"
)
const (
iso8601 = "2006-01-02T15:04:05-0700"
)
// flags
var configPath = flag.String("configPath", "config.json", "Path to config.json")
var version = "development"
var delay time.Duration = 60 * time.Second
var conf = loadconfig(*configPath)
var ucDev = getDevices(conf.Unifi.UCDevicesURL)
func main() {
log.Printf("starting version %s...\n", version)
// parse all flags
flag.Parse()
// check if flags are set
if *configPath == "" {
log.Fatalln("Please specify path to config.json flag '-configPath'")
}
// start API processing (runs in a loop)
go processAPIs()
//processUNMSAPIRouter()
//createMetrics(influxDBClient())
// start webserver on Port 3000
serveJSON()
}
func loadconfig(file string) config {
var config config
configFile, err := os.Open(file)
if err != nil {
log.Fatalln(err)
}
jsonParse := json.NewDecoder(configFile)
jsonParse.Decode(&config)
return config
}
// int to bool converter
func itob(i int) bool {
if i == 1 {
return true
}
return false
}
func processAPIs() {
tick := time.Tick(delay)
for range tick {
var nodes []node
var links []link
if conf.Unms.Enabled {
log.Println("Processing UNMS")
unmsNodes, unmsLinks := processUNMSAPI()
unmsRouters := processUNMSAPIRouter()
nodes = append(nodes, unmsNodes...)
nodes = append(nodes, unmsRouters...)
links = append(links, unmsLinks...)
}
if conf.Unifi.Enabled {
log.Println("Processing Unifi")
ucNodes, _ := processUcAPIs()
nodes = append(nodes, ucNodes...)
}
if conf.Meshviewer.Enabled {
log.Println("Processing Meshviewer")
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,
}
// 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")
}
}
// function to get file from meshviewer
func getFile(url string) []byte {
resp, err := http.Get(url)
if err != nil {
log.Println("Error getting file from:", url)
}
data := resp.Body
byteValue, _ := ioutil.ReadAll(data)
return byteValue
}
// get devices from devices file on webserver (config)
func getDevices(url string) devices {
// get devices from JSON file
jsonFile := getFile(url)
// read file to bytes
// variable for d
var d devices
// unmarshal to struct
err := json.Unmarshal(jsonFile, &d)
if err != nil {
fmt.Println("can´t get devices file from " + url)
log.Fatal(err)
}
return d
}
// check for MAC Adress in current Devices
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)
}
}
func httpClient() *http.Client {
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
}
client := &http.Client{Jar: jar}
return client
}