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

View file

@ -6,11 +6,17 @@ import (
"encoding/hex"
"fmt"
"net/http"
"sync"
"time"
"github.com/trazfr/freebox-exporter/log"
)
type sessionInfo struct {
sessionToken string
challenge string
}
// FreeboxSession represents all the variables used in a session
type FreeboxSession struct {
client *FreeboxHttpClient
@ -18,8 +24,11 @@ type FreeboxSession struct {
getChallengeURL 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) {
@ -53,7 +62,7 @@ func GetAppToken(client *FreeboxHttpClient, apiVersion *FreeboxAPIVersion) (stri
switch status.Status {
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)
case "granted":
return postResponse.AppToken, nil
@ -88,16 +97,24 @@ func NewFreeboxSession(appToken string, client *FreeboxHttpClient, apiVersion *F
}
func (f *FreeboxSession) IsValid() bool {
return f.sessionToken != "" && f.challenge != ""
return f.sessionInfo != nil
}
func (f *FreeboxSession) AddHeader(req *http.Request) {
if f != nil && f.sessionToken != "" {
req.Header.Set("X-Fbx-App-Auth", f.sessionToken)
if f != nil && f.sessionInfo != nil {
req.Header.Set("X-Fbx-App-Auth", f.sessionInfo.sessionToken)
}
}
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()
if err != nil {
return err
@ -106,8 +123,12 @@ func (f *FreeboxSession) Refresh() error {
if err != nil {
return err
}
f.challenge = challenge
f.sessionToken = sessionToken
f.sessionTokenLastUpdate = time.Now()
f.oldSessionInfo = f.sessionInfo
f.sessionInfo = &sessionInfo{
challenge: challenge,
sessionToken: sessionToken,
}
return nil
}

View file

@ -34,6 +34,12 @@ func InitDebug() {
func Init() {
Debug = log.New(ioutil.Discard, "", 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)
}