Skip to content

Commit 7d93c68

Browse files
committed
xally: reuse JWT token from electron, version: bump to 1.1.0
1 parent 0ae3043 commit 7d93c68

File tree

2 files changed

+60
-96
lines changed

2 files changed

+60
-96
lines changed

services/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313
)
1414

15-
const version = "1.0.9"
15+
const version = "1.1.0"
1616

1717
type Config struct {
1818
Node string `json:"node"`

services/xally/xally.go

Lines changed: 59 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package xally
22

33
import (
44
"bufio"
5-
"bytes"
65
"encoding/json"
76
"fmt"
87
"net/http"
@@ -13,12 +12,10 @@ import (
1312
)
1413

1514
const baseURL = "https://api-node.xally.ai"
15+
const levelPath = "/root/.config/xally_client/Local Storage/leveldb/000003.log"
1616

1717
var (
18-
apiKey string
19-
authToken string
20-
retryCount int
21-
lock sync.Mutex
18+
lock sync.Mutex
2219
)
2320

2421
type ApiResponse struct {
@@ -39,25 +36,6 @@ type NodeInfo struct {
3936

4037
var nodeData []NodeInfo
4138

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-
6139
func FetchNodeData() ([]NodeInfo, error) {
6240
lock.Lock()
6341
defer lock.Unlock()
@@ -68,102 +46,88 @@ func FetchNodeData() ([]NodeInfo, error) {
6846
}
6947
}
7048

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()
8166

82-
req, err := http.NewRequest("GET", baseURL+"/nodes/info", nil)
67+
if resp.StatusCode == 401 {
68+
jwtToken, err = getJwtToken()
8369
if err != nil {
8470
return nil, err
8571
}
8672

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)
8975
if err != nil {
90-
time.Sleep(backoff)
91-
backoff = min(2*backoff, maxBackoff)
92-
retryCount++
93-
continue
76+
return nil, err
9477
}
9578
defer resp.Body.Close()
79+
}
9680

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+
}
12184

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+
}
12389

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+
}
12794

128-
nodeData = nodes
129-
break
95+
lastCheckTs := time.Now().Unix()
96+
for i := range nodes {
97+
nodes[i].LastCheckTS = lastCheckTs
13098
}
13199

100+
nodeData = nodes
101+
132102
return nodes, nil
133103
}
134104

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)
142107
if err != nil {
143-
return "", err
108+
return "", fmt.Errorf("failed to open file: %v", err)
144109
}
110+
defer file.Close()
145111

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)
152115

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+
}
156122
}
157123

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)
160126
}
161127

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")
166130
}
167131

168-
return key["access_token"].(string), nil
132+
return lastToken, nil
169133
}

0 commit comments

Comments
 (0)