From fb3f2e5578630ce37eb00be301e577a767aa8f78 Mon Sep 17 00:00:00 2001 From: spsobole Date: Thu, 12 Aug 2021 12:46:14 -0600 Subject: [PATCH] Add support for alluser --- core/unifi/types.go | 4 +++ core/unifi/unifi.go | 7 +++++ core/unifi/users.go | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 core/unifi/users.go diff --git a/core/unifi/types.go b/core/unifi/types.go index d31aa239..bced62e2 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -28,6 +28,8 @@ const ( APIClientDPI string = "/api/s/%s/stat/stadpi" // APIClientPath is Unifi Clients API Path. APIClientPath string = "/api/s/%s/stat/sta" + // APIAllUserPath is Unifi Insight all previous Clients API Path. + APIAllUserPath string = "/api/s/%s/stat/alluser" // APINetworkPath is where we get data about Unifi networks. APINetworkPath string = "/api/s/%s/rest/networkconf" // APIDevicePath is where we get data about Unifi devices. @@ -36,6 +38,8 @@ const ( APILoginPath string = "/api/login" // APILoginPathNew is how we log into UDM 5.12.55+. APILoginPathNew string = "/api/auth/login" + // APILogoutPath is the how we logout from UDM + APILogoutPath string = "/api/auth/logout" // APIEventPathIDS returns Intrusion Detection/Prevention Systems Events. APIEventPathIDS string = "/api/s/%s/stat/ips/event" // APIEventPathAlarms contains the site alarms. diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 93962065..653f6895 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -142,6 +142,13 @@ func (u *Unifi) Login() error { return nil } +// Logout closes the current session +func (u *Unifi) Logout() error { + var response struct { + } + return u.GetData(APILogoutPath, &response) +} + // with the release of controller version 5.12.55 on UDM in Jan 2020 the api paths // changed and broke this library. This function runs when `NewUnifi()` is called to // check if this is a newer controller or not. If it is, we set new to true. diff --git a/core/unifi/users.go b/core/unifi/users.go new file mode 100644 index 00000000..56b034d1 --- /dev/null +++ b/core/unifi/users.go @@ -0,0 +1,69 @@ +package unifi + +import ( + "fmt" + "strings" +) + +// GetUsers returns a response full of clients that connected to the UDM within the provided amount of time +// it uses the insight historical connections data set +func (u *Unifi) GetUsers(sites []*Site, hours int) ([]*User, error) { + data := make([]*User, 0) + + for _, site := range sites { + var response struct { + Data []*User `json:"data"` + } + params := fmt.Sprintf(`{ "type": "all:", "conn": "all", "within":%d }`, hours) + + u.DebugLog("Polling Controller, retrieving UniFi Users, site %s ", site.SiteName) + + clientPath := fmt.Sprintf(APIAllUserPath, site.Name) + if err := u.GetData(clientPath, &response, params); err != nil { + return nil, err + } + + for i, d := range response.Data { + // Add special SourceName value. + response.Data[i].SourceName = u.URL + // Add the special "Site Name" to each client. This becomes a Grafana filter somewhere. + response.Data[i].SiteName = site.SiteName + // Fix name and hostname fields. Sometimes one or the other is blank. + response.Data[i].Hostname = strings.TrimSpace(pick(d.Hostname, d.Name, d.Mac)) + response.Data[i].Name = strings.TrimSpace(pick(d.Name, d.Hostname)) + } + + data = append(data, response.Data...) + } + + return data, nil +} + +// User defines the metadata available for previously connected clients +type User struct { + SourceName string `json:"-"` + SiteName string `json:"-"` + ID string `json:"_id"` + Mac string `json:"mac"` + SiteID string `json:"site_id"` + Oui string `json:"oui,omitempty"` + IsGuest bool `json:"is_guest"` + FirstSeen int64 `json:"first_seen,omitempty"` + LastSeen int64 `json:"last_seen,omitempty"` + IsWired bool `json:"is_wired,omitempty"` + Hostname string `json:"hostname,omitempty"` + Duration int64 `json:"duration,omitempty"` + TxBytes int64 `json:"tx_bytes,omitempty"` + TxPackets int64 `json:"tx_packets,omitempty"` + RxBytes int64 `json:"rx_bytes,omitempty"` + RxPackets int64 `json:"rx_packets,omitempty"` + WifiTxAttempts int64 `json:"wifi_tx_attempts,omitempty"` + TxRetries int64 `json:"tx_retries,omitempty"` + UsergroupID string `json:"usergroup_id,omitempty"` + Name string `json:"name,omitempty"` + Note string `json:"note,omitempty"` + Noted bool `json:"noted,omitempty"` + Blocked bool `json:"blocked,omitempty"` + DevIDOverride int64 `json:"dev_id_override,omitempty"` + FingerprintOverride bool `json:"fingerprint_override,omitempty"` +}