Skip to content

Commit 6c3e8ca

Browse files
committed
replace slice with array
1 parent fa4bf24 commit 6c3e8ca

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const keysPerAccount = 10
44

55
type APIAccount struct {
66
Credentials *APIAccountCredentials
7-
Keys []*APIKey
7+
Keys [10]*APIKey
88
}
99

1010
type APIAccountCredentials struct {

client.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goclash
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net/http"
78
"slices"
89
"strconv"
@@ -32,7 +33,6 @@ func newClient(creds Credentials) (*Client, error) {
3233
accounts := make([]*APIAccount, 0, len(creds))
3334
for email, password := range creds {
3435
accounts = append(accounts, &APIAccount{
35-
Keys: make([]*APIKey, keysPerAccount),
3636
Credentials: &APIAccountCredentials{
3737
Email: email,
3838
Password: password,
@@ -162,7 +162,9 @@ func (h *Client) getAccountKeys(account *APIAccount) error {
162162
}
163163

164164
h.mu.Lock()
165-
account.Keys = body.Keys
165+
for i := range body.Keys {
166+
account.Keys[i] = body.Keys[i]
167+
}
166168
h.mu.Unlock()
167169
return nil
168170
}
@@ -175,12 +177,12 @@ func (h *Client) updateAccountKeys(account *APIAccount) error {
175177
errChan := make(chan error, keysPerAccount)
176178
var freeKeyIndexes []int
177179
var wg sync.WaitGroup
178-
for i, key := range account.Keys {
179-
if key == nil {
180+
for i := 0; i < keysPerAccount; i++ {
181+
if account.Keys[i] == nil {
180182
freeKeyIndexes = append(freeKeyIndexes, i)
181183
continue
182184
}
183-
if !slices.Contains(key.CidrRanges, h.ipAddr) {
185+
if !slices.Contains(account.Keys[i].CidrRanges, h.ipAddr) {
184186
wg.Add(1)
185187
go func(key *APIKey, i int) {
186188
defer wg.Done()
@@ -192,7 +194,7 @@ func (h *Client) updateAccountKeys(account *APIAccount) error {
192194
errChan <- err
193195
return
194196
}
195-
}(key, i)
197+
}(account.Keys[i], i)
196198
}
197199
}
198200
wg.Wait()
@@ -201,7 +203,7 @@ func (h *Client) updateAccountKeys(account *APIAccount) error {
201203
wg.Add(1)
202204
go func(i int) {
203205
defer wg.Done()
204-
if err := h.createAccountKey(account, keysPerAccount-i); err != nil {
206+
if err := h.createAccountKey(account, i); err != nil {
205207
errChan <- err
206208
}
207209
}(i)
@@ -216,12 +218,15 @@ func (h *Client) updateAccountKeys(account *APIAccount) error {
216218

217219
func (h *Client) createAccountKey(account *APIAccount, index int) error {
218220
desc := fmt.Sprintf("Created at %s by goclash", time.Now().UTC().Round(time.Minute).String())
219-
res, err := h.newDefaultRequest().SetBody(&APIKey{
221+
key := &APIKey{
220222
Name: "goclash",
221223
Description: desc,
222224
CidrRanges: []string{h.ipAddr},
223225
Scopes: []string{"clash"},
224-
}).Post(DevKeyCreateEndpoint.URL())
226+
}
227+
log.Print("key: ", key)
228+
229+
res, err := h.newDefaultRequest().SetBody(key).Post(DevKeyCreateEndpoint.URL())
225230
if err != nil {
226231
return err
227232
}

0 commit comments

Comments
 (0)