bit better handling of session token

This commit is contained in:
Alexandre Blazart 2021-02-28 22:15:44 +01:00
parent 3673f3f3ad
commit 3be1da246c
3 changed files with 50 additions and 23 deletions

View file

@ -330,8 +330,8 @@ func (f *FreeboxConnection) GetMetricsConnection() (*MetricsFreeboxConnectionAll
// GetMetricsSwitch http://mafreebox.freebox.fr/api/v5/switch/status/ // GetMetricsSwitch http://mafreebox.freebox.fr/api/v5/switch/status/
func (f *FreeboxConnection) GetMetricsSwitch() (*MetricsFreeboxSwitch, error) { func (f *FreeboxConnection) GetMetricsSwitch() (*MetricsFreeboxSwitch, error) {
res := new(MetricsFreeboxSwitch) res := new(MetricsFreeboxSwitch)
err := f.get("switch/status/", &res.Ports)
if err != nil { if err := f.get("switch/status/", &res.Ports); err != nil {
return nil, err return nil, err
} }
@ -342,10 +342,10 @@ func (f *FreeboxConnection) GetMetricsSwitch() (*MetricsFreeboxSwitch, error) {
go func(port *MetricsFreeboxSwitchStatus) { go func(port *MetricsFreeboxSwitchStatus) {
defer wg.Done() defer wg.Done()
stats := new(MetricsFreeboxSwitchPortStats) stats := new(MetricsFreeboxSwitchPortStats)
// http://mafreebox.freebox.fr/api/v5/switch/port/1/stats // http://mafreebox.freebox.fr/api/v5/switch/port/1/stats
err := f.get(fmt.Sprintf("switch/port/%d/stats/", port.ID), stats) if err := f.get(fmt.Sprintf("switch/port/%d/stats/", port.ID), stats); err != nil {
if err != nil { log.Error.Println("Could not get status of port", port.ID, err)
log.Warning.Println("Could not get status of port", port.ID, err)
return return
} }
port.Stats = stats port.Stats = stats
@ -353,7 +353,7 @@ func (f *FreeboxConnection) GetMetricsSwitch() (*MetricsFreeboxSwitch, error) {
} }
wg.Wait() wg.Wait()
return res, err return res, nil
} }
// GetMetricsWifi https://dev.freebox.fr/sdk/os/wifi/ // GetMetricsWifi https://dev.freebox.fr/sdk/os/wifi/
@ -367,7 +367,7 @@ func (f *FreeboxConnection) GetMetricsWifi() (*MetricsFreeboxWifi, error) {
defer wg.Done() defer wg.Done()
if err := f.get("wifi/bss/", &res.Bss); err != nil { if err := f.get("wifi/bss/", &res.Bss); err != nil {
log.Warning.Println("Could not get the BSS", err) log.Error.Println("Could not get the BSS", err)
} }
}() }()
@ -375,7 +375,7 @@ func (f *FreeboxConnection) GetMetricsWifi() (*MetricsFreeboxWifi, error) {
defer wg.Done() defer wg.Done()
if err := f.get("wifi/ap/", &res.Ap); err != nil { if err := f.get("wifi/ap/", &res.Ap); err != nil {
log.Warning.Println("Could not get the AP", err) log.Error.Println("Could not get the AP", err)
return return
} }
@ -385,9 +385,9 @@ func (f *FreeboxConnection) GetMetricsWifi() (*MetricsFreeboxWifi, error) {
for _, ap := range res.Ap { for _, ap := range res.Ap {
go func(ap *MetricsFreeboxWifiAp) { go func(ap *MetricsFreeboxWifiAp) {
defer wgAp.Done() defer wgAp.Done()
err := f.get(fmt.Sprintf("wifi/ap/%d/stations/", ap.ID), &ap.Stations)
if err != nil { if err := f.get(fmt.Sprintf("wifi/ap/%d/stations/", ap.ID), &ap.Stations); err != nil {
log.Warning.Println("Could not get stations of AP", ap.ID, err) log.Error.Println("Could not get stations of AP", ap.ID, err)
} }
}(ap) }(ap)
} }
@ -444,7 +444,7 @@ func (f *FreeboxConnection) GetMetricsLan() (*MetricsFreeboxLan, error) {
for range interfaces { for range interfaces {
result := <-details result := <-details
if result.err != nil { if result.err != nil {
log.Warning.Println("Could not get the hosts on interface", result.name, result.err) log.Error.Println("Could not get the hosts on interface", result.name, result.err)
} else { } else {
res.Hosts[result.name] = result.hosts res.Hosts[result.name] = result.hosts
} }

View file

@ -6,20 +6,29 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/trazfr/freebox-exporter/log" "github.com/trazfr/freebox-exporter/log"
) )
type sessionInfo struct {
sessionToken string
challenge string
}
// FreeboxSession represents all the variables used in a session // FreeboxSession represents all the variables used in a session
type FreeboxSession struct { type FreeboxSession struct {
client *FreeboxHttpClient client *FreeboxHttpClient
getSessionTokenURL string getSessionTokenURL string
getChallengeURL string getChallengeURL string
appToken string appToken string
sessionToken string
challenge string sessionTokenLastUpdate time.Time
sessionTokenLock sync.Mutex
sessionInfo *sessionInfo
oldSessionInfo *sessionInfo // avoid deleting the sessionInfo too quickly
} }
func GetAppToken(client *FreeboxHttpClient, apiVersion *FreeboxAPIVersion) (string, error) { func GetAppToken(client *FreeboxHttpClient, apiVersion *FreeboxAPIVersion) (string, error) {
@ -53,7 +62,7 @@ func GetAppToken(client *FreeboxHttpClient, apiVersion *FreeboxAPIVersion) (stri
switch status.Status { switch status.Status {
case "pending": case "pending":
log.Warning.Println(counter, "Please accept the login on the Freebox Server") log.Info.Println(counter, "Please accept the login on the Freebox Server")
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
case "granted": case "granted":
return postResponse.AppToken, nil return postResponse.AppToken, nil
@ -88,16 +97,24 @@ func NewFreeboxSession(appToken string, client *FreeboxHttpClient, apiVersion *F
} }
func (f *FreeboxSession) IsValid() bool { func (f *FreeboxSession) IsValid() bool {
return f.sessionToken != "" && f.challenge != "" return f.sessionInfo != nil
} }
func (f *FreeboxSession) AddHeader(req *http.Request) { func (f *FreeboxSession) AddHeader(req *http.Request) {
if f != nil && f.sessionToken != "" { if f != nil && f.sessionInfo != nil {
req.Header.Set("X-Fbx-App-Auth", f.sessionToken) req.Header.Set("X-Fbx-App-Auth", f.sessionInfo.sessionToken)
} }
} }
func (f *FreeboxSession) Refresh() error { func (f *FreeboxSession) Refresh() error {
f.sessionTokenLock.Lock()
defer f.sessionTokenLock.Unlock()
if sinceLastUpdate := time.Now().Sub(f.sessionTokenLastUpdate); sinceLastUpdate < 5*time.Second {
log.Debug.Printf("Updated %v ago. Skipping", sinceLastUpdate)
return nil
}
challenge, err := f.getChallenge() challenge, err := f.getChallenge()
if err != nil { if err != nil {
return err return err
@ -106,8 +123,12 @@ func (f *FreeboxSession) Refresh() error {
if err != nil { if err != nil {
return err return err
} }
f.challenge = challenge f.sessionTokenLastUpdate = time.Now()
f.sessionToken = sessionToken f.oldSessionInfo = f.sessionInfo
f.sessionInfo = &sessionInfo{
challenge: challenge,
sessionToken: sessionToken,
}
return nil return nil
} }

View file

@ -34,6 +34,12 @@ func InitDebug() {
func Init() { func Init() {
Debug = log.New(ioutil.Discard, "", 0) Debug = log.New(ioutil.Discard, "", 0)
Info = log.New(os.Stdout, "", 0) Info = log.New(os.Stdout, "", 0)
Warning = log.New(os.Stdout, "", 0)
Error = log.New(os.Stderr, "", 0) Warning = log.New(os.Stdout,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
Error = log.New(os.Stderr,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
} }