Compare commits
3 Commits
94222942e6
...
9c1057d1ce
Author | SHA1 | Date | |
---|---|---|---|
9c1057d1ce | |||
fa50a2d618 | |||
78b274c160 |
34
.drone.yml
34
.drone.yml
@ -1,35 +1,45 @@
|
|||||||
|
global-variables:
|
||||||
|
env: &env
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- go build -ldflags "-X main.version=${DRONE_TAG##v}" -o release/$GOOS/$GOARCH/ubnt-freifunk-map-api .
|
||||||
|
- tar -cvzf release/ubnt-freifunk-map-api_$GOOS_$GOARCH.tar.gz -C release/$GOOS/$GOARCH ubnt-freifunk-map-api
|
||||||
|
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
type: docker
|
type: docker
|
||||||
name: build go
|
name: build go
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: build linux/amd64
|
- name: build linux/amd64
|
||||||
image: golang
|
<<: *env
|
||||||
commands:
|
|
||||||
- go build -ldflags "-X main.version=${DRONE_TAG}" -o release/linux/amd64/ubnt-freifunk-map-api .
|
|
||||||
- tar -cvzf release/ubnt-freifunk-map-api_linux_amd64.tar.gz -C release/linux/amd64 ubnt-freifunk-map-api
|
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
|
|
||||||
- name: build linux/arm64
|
- name: build linux/arm64
|
||||||
image: golang
|
<<: *env
|
||||||
commands:
|
|
||||||
- go build -ldflags "-X main.version=${DRONE_TAG}" -o release/linux/arm64/ubnt-freifunk-map-api .
|
|
||||||
- tar -cvzf release/ubnt-freifunk-map-api_linux_arm64.tar.gz -C release/linux/arm64 ubnt-freifunk-map-api
|
|
||||||
environment:
|
environment:
|
||||||
GOARCH: arm64
|
GOARCH: arm64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
|
|
||||||
- name: build windows
|
- name: build windows
|
||||||
image: golang
|
<<: *env
|
||||||
commands:
|
|
||||||
- go build -ldflags "-X main.version=${DRONE_TAG}" -o release/windows/amd64/ubnt-freifunk-map-api .
|
|
||||||
- tar -cvzf release/ubnt-freifunk-map-api_windows_amd64.tar.gz -C release/windows/amd64 ubnt-freifunk-map-api
|
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
|
|
||||||
|
- name: build macos/x64
|
||||||
|
<<: *env
|
||||||
|
environment:
|
||||||
|
GOARCH: amd64
|
||||||
|
GOOS: darwin
|
||||||
|
|
||||||
|
- name: build macos/arm64
|
||||||
|
<<: *env
|
||||||
|
environment:
|
||||||
|
GOARCH: arm64
|
||||||
|
GOOS: darwin
|
||||||
|
|
||||||
- name: docker
|
- name: docker
|
||||||
image: plugins/docker
|
image: plugins/docker
|
||||||
settings:
|
settings:
|
||||||
|
226
main.go
226
main.go
@ -23,118 +23,131 @@ const (
|
|||||||
// flags
|
// flags
|
||||||
var token = flag.String("token", "", "Defines the x-auth-token")
|
var token = flag.String("token", "", "Defines the x-auth-token")
|
||||||
var version = "development"
|
var version = "development"
|
||||||
|
var delay time.Duration = 60 * time.Second
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
log.Printf("starting version %s...\n", version)
|
||||||
log.Println("starting version", version, "...")
|
|
||||||
// parse all flags
|
// parse all flags
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// check if token is set
|
// check if token is set
|
||||||
if *token == "" {
|
if *token == "" {
|
||||||
log.Fatalln("Please specify an API token via the flag '-token'")
|
log.Fatalln("Please specify an API token via the flag '-token'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables for runtime
|
// start API processing (runs in a loop)
|
||||||
var links []link
|
go processAPIs()
|
||||||
var nodes []node
|
|
||||||
|
|
||||||
d, err := getDevices()
|
// start webserver
|
||||||
if err != nil {
|
serveJSON()
|
||||||
log.Fatalln(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// API CALL 1
|
func processAPIs() {
|
||||||
log.Println("calling API 1")
|
tick := time.Tick(delay)
|
||||||
var u []unifiAPIResponse
|
for range tick {
|
||||||
err = callUnifiAPI("/devices", &u)
|
// Variables for runtime
|
||||||
if err != nil {
|
var links []link
|
||||||
log.Fatalln(err)
|
var nodes []node
|
||||||
}
|
|
||||||
|
|
||||||
for i := range d.Devices {
|
d, err := getDevices()
|
||||||
var dev unifiAPIResponse
|
if err != nil {
|
||||||
var currentDevice device
|
log.Fatalln(err)
|
||||||
for j := range u {
|
}
|
||||||
if strings.ToUpper(u[j].Identification.MAC) == strings.ToUpper(d.Devices[i].MAC) {
|
|
||||||
dev = u[j]
|
// API CALL 1
|
||||||
currentDevice = d.Devices[i]
|
log.Println("calling API 1")
|
||||||
|
var u []unifiAPIResponse
|
||||||
|
err = callUnifiAPI("/devices", &u)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var isOnline bool = false
|
var isOnline bool = false
|
||||||
if dev.Overview.Status == "active" {
|
if dev.Overview.Status == "active" {
|
||||||
isOnline = true
|
isOnline = true
|
||||||
}
|
|
||||||
// END OF API CALL 1
|
|
||||||
|
|
||||||
// API CALL 2
|
|
||||||
log.Println("calling API 2 for device", d.Devices[i].Name)
|
|
||||||
var details unifiAPIDetails
|
|
||||||
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
|
|
||||||
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 {
|
|
||||||
links = addLink(dev, airmaxes[i], links)
|
|
||||||
}
|
}
|
||||||
|
// END OF API CALL 1
|
||||||
|
|
||||||
|
// API CALL 2
|
||||||
|
log.Println("calling API 2 for device", d.Devices[i].Name)
|
||||||
|
var details unifiAPIDetails
|
||||||
|
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
|
||||||
|
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 {
|
||||||
|
links = addLink(dev, airmaxes[i], links)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
// END OF API CALL 3
|
|
||||||
|
|
||||||
// Get info from json file (static)
|
// assemble final struct
|
||||||
nodes = append(nodes, node{
|
o := output{
|
||||||
Firstseen: dev.Overview.CreatedAt.Format(iso8601),
|
Timestamp: time.Now().Format(iso8601),
|
||||||
Lastseen: dev.Overview.LastSeen.Format(iso8601),
|
Nodes: nodes,
|
||||||
IsOnline: isOnline,
|
Links: links,
|
||||||
IsGateway: false,
|
}
|
||||||
Clients: 0,
|
|
||||||
ClientsWifi24: 0,
|
// create file output
|
||||||
ClientsWifi5: 0,
|
log.Println("writing json file")
|
||||||
ClientsOther: 0,
|
|
||||||
RootFSUsage: 0,
|
if err := o.writeToFile(); err != nil {
|
||||||
LoadAVG: details.Overview.CPU / 100,
|
log.Fatalln(err)
|
||||||
MemoryUsage: details.Overview.RAM / 100,
|
}
|
||||||
Uptime: dev.Identification.Started.Format(iso8601),
|
|
||||||
GatewayNexthop: currentDevice.GatewayNexthop,
|
// we're done here
|
||||||
Gateway: currentDevice.Gateway,
|
log.Println("...done")
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// assemble final struct
|
|
||||||
o := output{
|
|
||||||
Timestamp: time.Now().Format(iso8601),
|
|
||||||
Nodes: nodes,
|
|
||||||
Links: links,
|
|
||||||
}
|
|
||||||
|
|
||||||
// create file output
|
|
||||||
log.Println("writing json file")
|
|
||||||
writeJSONtoFile(o)
|
|
||||||
|
|
||||||
// we're done here
|
|
||||||
log.Println("...done")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDevices() (devices, error) {
|
func getDevices() (devices, error) {
|
||||||
@ -213,22 +226,19 @@ func addLink(dev unifiAPIResponse, airmaxes unifiAPIAirmax, links []link) []link
|
|||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeJSONtoFile(o output) error {
|
|
||||||
file, err := json.MarshalIndent(o, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("can't marshal to json: %+v", o)
|
|
||||||
}
|
|
||||||
|
|
||||||
// write to file
|
|
||||||
err = ioutil.WriteFile("example.json", file, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("can't write to json file example.json")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAddresses(ip string) []string {
|
func getAddresses(ip string) []string {
|
||||||
var adresses []string
|
var adresses []string
|
||||||
adresses = append(adresses, strings.Split(ip, "/")[0])
|
adresses = append(adresses, strings.Split(ip, "/")[0])
|
||||||
return adresses
|
return adresses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
28
types.go
28
types.go
@ -1,6 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type device struct {
|
type device struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -111,6 +117,26 @@ type output struct {
|
|||||||
Links []link `json:"links"`
|
Links []link `json:"links"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *output) writeToFile() error {
|
||||||
|
file, err := json.MarshalIndent(o, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't marshal to json: %+v", o)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write to file
|
||||||
|
if _, err := os.Stat("output"); os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir("output", 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't create output folder")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile("output/meshviewer.json", file, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't write to json file meshviewer.json")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type apiResponse struct {
|
type apiResponse struct {
|
||||||
StatusCode int `json:"statusCode"`
|
StatusCode int `json:"statusCode"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
|
Loading…
Reference in New Issue
Block a user