Added Config file #10
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -1,3 +1,5 @@
 | 
			
		||||
module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api
 | 
			
		||||
 | 
			
		||||
go 1.16
 | 
			
		||||
 | 
			
		||||
require git.nils.zone/nils/prettify v0.0.4
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								go.sum
									
									
									
									
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ=
 | 
			
		||||
git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c=
 | 
			
		||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
 | 
			
		||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
 | 
			
		||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
 | 
			
		||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
 | 
			
		||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
 | 
			
		||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
 | 
			
		||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
							
								
								
									
										46
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								main.go
									
									
									
									
									
								
							@ -14,27 +14,31 @@ import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	_ "git.nils.zone/nils/prettify"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const iso8601 = "2006-01-02T15:04:05-0700"
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	log.Printf("starting version %s...\n", version)
 | 
			
		||||
	// parse all flags
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
	c := loadconfig(*configPath)
 | 
			
		||||
 | 
			
		||||
	// 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 c.processAPIs()
 | 
			
		||||
	go processAPIs()
 | 
			
		||||
 | 
			
		||||
	// start webserver on Port 3000
 | 
			
		||||
	serveJSON()
 | 
			
		||||
@ -60,16 +64,16 @@ func itob(i int) bool {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//Unifi Controller API processing
 | 
			
		||||
func (c config) processUcAPIs() ([]node, []link) {
 | 
			
		||||
func processUcAPIs() ([]node, []link) {
 | 
			
		||||
	//get list of Unifi devices to display
 | 
			
		||||
	var nodes []node
 | 
			
		||||
	var links []link
 | 
			
		||||
	d, err := getDevices(c.Unifi.UCDevicesURL)
 | 
			
		||||
	d, err := getDevices(conf.Unifi.UCDevicesURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
	//call Unifi Controller
 | 
			
		||||
	ucAPI := newAPI(c.Unifi.User, c.Unifi.Password, c.Unifi.APIURL)
 | 
			
		||||
	ucAPI := newAPI(conf.Unifi.User, conf.Unifi.Password, conf.Unifi.APIURL)
 | 
			
		||||
	//login
 | 
			
		||||
	ucAPI.ucLogin()
 | 
			
		||||
	//get all Sites from Controller
 | 
			
		||||
@ -144,12 +148,12 @@ func (c config) processUcAPIs() ([]node, []link) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//UNMS API processing (Richtfunk)
 | 
			
		||||
func (c config) processUNMSAPI() ([]node, []link) {
 | 
			
		||||
func processUNMSAPI() ([]node, []link) {
 | 
			
		||||
	// Variables for runtime
 | 
			
		||||
	var links []link
 | 
			
		||||
	var nodes []node
 | 
			
		||||
 | 
			
		||||
	d, err := getDevices(c.Unms.DevicesURL)
 | 
			
		||||
	d, err := getDevices(conf.Unms.DevicesURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
@ -157,7 +161,7 @@ func (c config) processUNMSAPI() ([]node, []link) {
 | 
			
		||||
	// API CALL 1
 | 
			
		||||
	log.Println("calling API 1")
 | 
			
		||||
	var u []unifiAPIResponse
 | 
			
		||||
	err = c.callUnifiAPI("/devices", &u)
 | 
			
		||||
	err = callUnifiAPI("/devices", &u)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
@ -181,13 +185,13 @@ func (c config) processUNMSAPI() ([]node, []link) {
 | 
			
		||||
		// API CALL 2
 | 
			
		||||
		log.Println("calling API 2 for device", d.Devices[i].Name)
 | 
			
		||||
		var details unifiAPIDetails
 | 
			
		||||
		c.callUnifiAPI("/devices/erouters/"+dev.Identification.ID, &details)
 | 
			
		||||
		callUnifiAPI("/devices/erouters/"+dev.Identification.ID, &details)
 | 
			
		||||
		// END OF API CALL 2
 | 
			
		||||
 | 
			
		||||
		// API CALL 3
 | 
			
		||||
		log.Println("calling API 3 for device", d.Devices[i].Name)
 | 
			
		||||
		var airmaxes []unifiAPIAirmax
 | 
			
		||||
		c.callUnifiAPI("/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes)
 | 
			
		||||
		callUnifiAPI("/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes)
 | 
			
		||||
		// check if remote mac address is part of our published network
 | 
			
		||||
		for i := range airmaxes {
 | 
			
		||||
			if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) == true {
 | 
			
		||||
@ -234,19 +238,19 @@ func (c config) processUNMSAPI() ([]node, []link) {
 | 
			
		||||
	return nodes, links
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c config) processAPIs() {
 | 
			
		||||
func processAPIs() {
 | 
			
		||||
	tick := time.Tick(delay)
 | 
			
		||||
	for range tick {
 | 
			
		||||
		var nodes []node
 | 
			
		||||
		var links []link
 | 
			
		||||
 | 
			
		||||
		if c.Unms.Enabled == true {
 | 
			
		||||
			unmsNodes, unmsLinks := c.processUNMSAPI()
 | 
			
		||||
		if conf.Unms.Enabled == true {
 | 
			
		||||
			unmsNodes, unmsLinks := processUNMSAPI()
 | 
			
		||||
			nodes = append(nodes, unmsNodes...)
 | 
			
		||||
			links = append(links, unmsLinks...)
 | 
			
		||||
		}
 | 
			
		||||
		if c.Unifi.Enabled == true {
 | 
			
		||||
			ucNodes, ucLinks := c.processUcAPIs()
 | 
			
		||||
		if conf.Unifi.Enabled == true {
 | 
			
		||||
			ucNodes, ucLinks := processUcAPIs()
 | 
			
		||||
			nodes = append(nodes, ucNodes...)
 | 
			
		||||
			links = append(links, ucLinks...)
 | 
			
		||||
		}
 | 
			
		||||
@ -295,16 +299,16 @@ func getDevices(url string) (devices, error) {
 | 
			
		||||
	return d, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c config) callUnifiAPI(url string, i interface{}) error {
 | 
			
		||||
	request, err := http.NewRequest(http.MethodGet, c.Unms.UnmsAPIURL+url, nil)
 | 
			
		||||
func callUnifiAPI(url string, i interface{}) error {
 | 
			
		||||
	request, err := http.NewRequest(http.MethodGet, conf.Unms.UnmsAPIURL+url, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url))
 | 
			
		||||
		return errors.New(fmt.Sprint("can't set request", conf.Unms.UnmsAPIURL+url))
 | 
			
		||||
	}
 | 
			
		||||
	request.Header.Set("x-auth-token", c.Unms.APItoken)
 | 
			
		||||
	request.Header.Set("x-auth-token", conf.Unms.APItoken)
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	response, err := client.Do(request)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("can't get request %s with x-auth-token %s", c.Unms.UnmsAPIURL+url, c.Unms.APItoken)
 | 
			
		||||
		return fmt.Errorf("can't get request %s with x-auth-token %s", conf.Unms.UnmsAPIURL+url, conf.Unms.APItoken)
 | 
			
		||||
	}
 | 
			
		||||
	if response.StatusCode != 200 {
 | 
			
		||||
		log.Fatalln("Can´t call UNMS API, check token and URL. HTTP Status: ", response.StatusCode)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user