@@ -2,7 +2,6 @@ package xally
2
2
3
3
import (
4
4
"bufio"
5
- "bytes"
6
5
"encoding/json"
7
6
"fmt"
8
7
"net/http"
@@ -13,12 +12,10 @@ import (
13
12
)
14
13
15
14
const baseURL = "https://api-node.xally.ai"
15
+ const levelPath = "/root/.config/xally_client/Local Storage/leveldb/000003.log"
16
16
17
17
var (
18
- apiKey string
19
- authToken string
20
- retryCount int
21
- lock sync.Mutex
18
+ lock sync.Mutex
22
19
)
23
20
24
21
type ApiResponse struct {
@@ -39,25 +36,6 @@ type NodeInfo struct {
39
36
40
37
var nodeData []NodeInfo
41
38
42
- func GetXallyAPIKey () string {
43
- file , err := os .Open ("/root/.config/xally_client/Local Storage/leveldb/000003.log" )
44
- if err != nil {
45
- fmt .Println ("Error opening file:" , err )
46
- return ""
47
- }
48
- defer file .Close ()
49
-
50
- regex := regexp .MustCompile (`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}` )
51
- scanner := bufio .NewScanner (file )
52
- for scanner .Scan () {
53
- line := scanner .Text ()
54
- if matches := regex .FindString (line ); matches != "" {
55
- return matches
56
- }
57
- }
58
- return ""
59
- }
60
-
61
39
func FetchNodeData () ([]NodeInfo , error ) {
62
40
lock .Lock ()
63
41
defer lock .Unlock ()
@@ -68,102 +46,88 @@ func FetchNodeData() ([]NodeInfo, error) {
68
46
}
69
47
}
70
48
71
- var nodes []NodeInfo
72
- backoff := 1 * time .Second
73
- const maxBackoff = 10 * time .Minute
74
- retryCount = 0
75
-
76
- for {
77
- if retryCount > 5 {
78
- fmt .Println ("Max retries exceeded" )
79
- return nil , fmt .Errorf ("max retries exceeded" )
80
- }
49
+ jwtToken , err := getJwtToken ()
50
+ if err != nil {
51
+ return nil , err
52
+ }
53
+
54
+ req , err := http .NewRequest ("GET" , baseURL + "/nodes/info" , nil )
55
+ if err != nil {
56
+ return nil , err
57
+ }
58
+
59
+ req .Header .Add ("Authorization" , "Bearer " + jwtToken )
60
+
61
+ resp , err := http .DefaultClient .Do (req )
62
+ if err != nil {
63
+ return nil , err
64
+ }
65
+ defer resp .Body .Close ()
81
66
82
- req , err := http .NewRequest ("GET" , baseURL + "/nodes/info" , nil )
67
+ if resp .StatusCode == 401 {
68
+ jwtToken , err = getJwtToken ()
83
69
if err != nil {
84
70
return nil , err
85
71
}
86
72
87
- req .Header .Add ("Authorization" , "Bearer " + authToken )
88
- resp , err : = http .DefaultClient .Do (req )
73
+ req .Header .Set ("Authorization" , "Bearer " + jwtToken )
74
+ resp , err = http .DefaultClient .Do (req )
89
75
if err != nil {
90
- time .Sleep (backoff )
91
- backoff = min (2 * backoff , maxBackoff )
92
- retryCount ++
93
- continue
76
+ return nil , err
94
77
}
95
78
defer resp .Body .Close ()
79
+ }
96
80
97
- if resp .StatusCode == 401 {
98
- newToken , err := getAuthKey (apiKey )
99
- if err != nil {
100
- return nil , err
101
- }
102
- authToken = newToken
103
- continue
104
- }
105
-
106
- if resp .StatusCode != 200 {
107
- return nil , fmt .Errorf ("bad status code: %d" , resp .StatusCode )
108
- }
109
-
110
- var apiResp ApiResponse
111
- if err := json .NewDecoder (resp .Body ).Decode (& apiResp ); err != nil {
112
- time .Sleep (backoff )
113
- backoff = min (2 * backoff , maxBackoff )
114
- retryCount ++
115
- continue
116
- }
117
-
118
- if err := json .Unmarshal (apiResp .Data , & nodes ); err != nil {
119
- return nil , err
120
- }
81
+ if resp .StatusCode != 200 {
82
+ return nil , fmt .Errorf ("bad status code: %d" , resp .StatusCode )
83
+ }
121
84
122
- lastCheckTs := time .Now ().Unix ()
85
+ var apiResp ApiResponse
86
+ if err := json .NewDecoder (resp .Body ).Decode (& apiResp ); err != nil {
87
+ return nil , err
88
+ }
123
89
124
- for i := range nodes {
125
- nodes [i ].LastCheckTS = lastCheckTs
126
- }
90
+ var nodes []NodeInfo
91
+ if err := json .Unmarshal (apiResp .Data , & nodes ); err != nil {
92
+ return nil , err
93
+ }
127
94
128
- nodeData = nodes
129
- break
95
+ lastCheckTs := time .Now ().Unix ()
96
+ for i := range nodes {
97
+ nodes [i ].LastCheckTS = lastCheckTs
130
98
}
131
99
100
+ nodeData = nodes
101
+
132
102
return nodes , nil
133
103
}
134
104
135
- func getAuthKey (apiKey string ) (string , error ) {
136
- if apiKey == "" {
137
- apiKey = GetXallyAPIKey ()
138
- }
139
-
140
- payload := fmt .Sprintf (`{"api_key":"%s"}` , apiKey )
141
- req , err := http .NewRequest ("POST" , baseURL + "/auth/api-key" , bytes .NewBufferString (payload ))
105
+ func getJwtToken () (string , error ) {
106
+ file , err := os .Open (levelPath )
142
107
if err != nil {
143
- return "" , err
108
+ return "" , fmt . Errorf ( "failed to open file: %v" , err )
144
109
}
110
+ defer file .Close ()
145
111
146
- req .Header .Add ("Content-Type" , "application/json" )
147
- resp , err := http .DefaultClient .Do (req )
148
- if err != nil {
149
- return "" , err
150
- }
151
- defer resp .Body .Close ()
112
+ var lastToken string
113
+ tokenRegex := regexp .MustCompile (`[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+` )
114
+ scanner := bufio .NewScanner (file )
152
115
153
- var apiResp ApiResponse
154
- if err := json .NewDecoder (resp .Body ).Decode (& apiResp ); err != nil {
155
- return "" , err
116
+ for scanner .Scan () {
117
+ line := scanner .Text ()
118
+ tokens := tokenRegex .FindAllString (line , - 1 )
119
+ if len (tokens ) > 0 {
120
+ lastToken = tokens [len (tokens )- 1 ] // Get the last token in the line
121
+ }
156
122
}
157
123
158
- if apiResp . Code != 2000 {
159
- return "" , fmt .Errorf ("failed to refresh auth key : %s " , apiResp . Message )
124
+ if err := scanner . Err (); err != nil {
125
+ return "" , fmt .Errorf ("failed to scan the file : %v " , err )
160
126
}
161
127
162
- key := make (map [string ]interface {})
163
-
164
- if err := json .Unmarshal (apiResp .Data , & key ); err != nil {
165
- return "" , err
128
+ if lastToken == "" {
129
+ return "" , fmt .Errorf ("no JWT token found in the file" )
166
130
}
167
131
168
- return key [ "access_token" ].( string ) , nil
132
+ return lastToken , nil
169
133
}
0 commit comments