Skip to content

Commit

Permalink
xally: reuse JWT token from electron, version: bump to 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hayzamjs committed May 11, 2024
1 parent 0ae3043 commit 7d93c68
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 96 deletions.
2 changes: 1 addition & 1 deletion services/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

const version = "1.0.9"
const version = "1.1.0"

type Config struct {
Node string `json:"node"`
Expand Down
154 changes: 59 additions & 95 deletions services/xally/xally.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xally

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -13,12 +12,10 @@ import (
)

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

var (
apiKey string
authToken string
retryCount int
lock sync.Mutex
lock sync.Mutex
)

type ApiResponse struct {
Expand All @@ -39,25 +36,6 @@ type NodeInfo struct {

var nodeData []NodeInfo

func GetXallyAPIKey() string {
file, err := os.Open("/root/.config/xally_client/Local Storage/leveldb/000003.log")
if err != nil {
fmt.Println("Error opening file:", err)
return ""
}
defer file.Close()

regex := regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if matches := regex.FindString(line); matches != "" {
return matches
}
}
return ""
}

func FetchNodeData() ([]NodeInfo, error) {
lock.Lock()
defer lock.Unlock()
Expand All @@ -68,102 +46,88 @@ func FetchNodeData() ([]NodeInfo, error) {
}
}

var nodes []NodeInfo
backoff := 1 * time.Second
const maxBackoff = 10 * time.Minute
retryCount = 0

for {
if retryCount > 5 {
fmt.Println("Max retries exceeded")
return nil, fmt.Errorf("max retries exceeded")
}
jwtToken, err := getJwtToken()
if err != nil {
return nil, err
}

req, err := http.NewRequest("GET", baseURL+"/nodes/info", nil)
if err != nil {
return nil, err
}

req.Header.Add("Authorization", "Bearer "+jwtToken)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

req, err := http.NewRequest("GET", baseURL+"/nodes/info", nil)
if resp.StatusCode == 401 {
jwtToken, err = getJwtToken()
if err != nil {
return nil, err
}

req.Header.Add("Authorization", "Bearer "+authToken)
resp, err := http.DefaultClient.Do(req)
req.Header.Set("Authorization", "Bearer "+jwtToken)
resp, err = http.DefaultClient.Do(req)
if err != nil {
time.Sleep(backoff)
backoff = min(2*backoff, maxBackoff)
retryCount++
continue
return nil, err
}
defer resp.Body.Close()
}

if resp.StatusCode == 401 {
newToken, err := getAuthKey(apiKey)
if err != nil {
return nil, err
}
authToken = newToken
continue
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}

var apiResp ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
time.Sleep(backoff)
backoff = min(2*backoff, maxBackoff)
retryCount++
continue
}

if err := json.Unmarshal(apiResp.Data, &nodes); err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}

lastCheckTs := time.Now().Unix()
var apiResp ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, err
}

for i := range nodes {
nodes[i].LastCheckTS = lastCheckTs
}
var nodes []NodeInfo
if err := json.Unmarshal(apiResp.Data, &nodes); err != nil {
return nil, err
}

nodeData = nodes
break
lastCheckTs := time.Now().Unix()
for i := range nodes {
nodes[i].LastCheckTS = lastCheckTs
}

nodeData = nodes

return nodes, nil
}

func getAuthKey(apiKey string) (string, error) {
if apiKey == "" {
apiKey = GetXallyAPIKey()
}

payload := fmt.Sprintf(`{"api_key":"%s"}`, apiKey)
req, err := http.NewRequest("POST", baseURL+"/auth/api-key", bytes.NewBufferString(payload))
func getJwtToken() (string, error) {
file, err := os.Open(levelPath)
if err != nil {
return "", err
return "", fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()

req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var lastToken string
tokenRegex := regexp.MustCompile(`[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+`)
scanner := bufio.NewScanner(file)

var apiResp ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return "", err
for scanner.Scan() {
line := scanner.Text()
tokens := tokenRegex.FindAllString(line, -1)
if len(tokens) > 0 {
lastToken = tokens[len(tokens)-1] // Get the last token in the line
}
}

if apiResp.Code != 2000 {
return "", fmt.Errorf("failed to refresh auth key: %s", apiResp.Message)
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to scan the file: %v", err)
}

key := make(map[string]interface{})

if err := json.Unmarshal(apiResp.Data, &key); err != nil {
return "", err
if lastToken == "" {
return "", fmt.Errorf("no JWT token found in the file")
}

return key["access_token"].(string), nil
return lastToken, nil
}

0 comments on commit 7d93c68

Please sign in to comment.